/libraries/joomla/filesystem/helper.php

https://github.com/katalystsol/joomla-platform · PHP · 289 lines · 157 code · 44 blank · 88 comment · 41 complexity · 3add0fcf9e9619fe0796f17b19937492 MD5 · raw file

  1. <?php
  2. /**
  3. * @package Joomla.Platform
  4. * @subpackage FileSystem
  5. *
  6. * @copyright Copyright (C) 2005 - 2011 Open Source Matters, Inc. All rights reserved.
  7. * @license GNU General Public License version 2 or later; see LICENSE
  8. */
  9. defined('JPATH_PLATFORM') or die();
  10. jimport('joomla.filesystem.file');
  11. jimport('joomla.filesystem.folder');
  12. /**
  13. * File system helper
  14. *
  15. * Holds support functions for the filesystem, particularly the stream
  16. *
  17. * @package Joomla.Platform
  18. * @subpackage FileSystem
  19. * @since 11.1
  20. */
  21. class JFilesystemHelper
  22. {
  23. /**
  24. * Remote file size function for streams that don't support it
  25. *
  26. * @param string $url TODO Add text
  27. *
  28. * @return mixed
  29. *
  30. * @see http://www.php.net/manual/en/function.filesize.php#71098
  31. * @since 11.1
  32. */
  33. function remotefsize($url)
  34. {
  35. $sch = parse_url($url, PHP_URL_SCHEME);
  36. if (($sch != 'http') && ($sch != 'https') && ($sch != 'ftp') && ($sch != 'ftps'))
  37. {
  38. return false;
  39. }
  40. if (($sch == 'http') || ($sch == 'https'))
  41. {
  42. $headers = get_headers($url, 1);
  43. if ((!array_key_exists('Content-Length', $headers)))
  44. {
  45. return false;
  46. }
  47. return $headers['Content-Length'];
  48. }
  49. if (($sch == 'ftp') || ($sch == 'ftps'))
  50. {
  51. $server = parse_url($url, PHP_URL_HOST);
  52. $port = parse_url($url, PHP_URL_PORT);
  53. $path = parse_url($url, PHP_URL_PATH);
  54. $user = parse_url($url, PHP_URL_USER);
  55. $pass = parse_url($url, PHP_URL_PASS);
  56. if ((!$server) || (!$path))
  57. {
  58. return false;
  59. }
  60. if (!$port)
  61. {
  62. $port = 21;
  63. }
  64. if (!$user)
  65. {
  66. $user = 'anonymous';
  67. }
  68. if (!$pass)
  69. {
  70. $pass = '';
  71. }
  72. switch ($sch)
  73. {
  74. case 'ftp':
  75. $ftpid = ftp_connect($server, $port);
  76. break;
  77. case 'ftps':
  78. $ftpid = ftp_ssl_connect($server, $port);
  79. break;
  80. }
  81. if (!$ftpid)
  82. {
  83. return false;
  84. }
  85. $login = ftp_login($ftpid, $user, $pass);
  86. if (!$login)
  87. {
  88. return false;
  89. }
  90. $ftpsize = ftp_size($ftpid, $path);
  91. ftp_close($ftpid);
  92. if ($ftpsize == -1)
  93. {
  94. return false;
  95. }
  96. return $ftpsize;
  97. }
  98. }
  99. /**
  100. * Quick FTP chmod
  101. *
  102. * @param string $url Link identifier
  103. * @param integer $mode The new permissions, given as an octal value.
  104. *
  105. * @return mixed
  106. *
  107. * @see http://www.php.net/manual/en/function.ftp-chmod.php
  108. * @since 11.1
  109. */
  110. function ftpChmod($url, $mode)
  111. {
  112. $sch = parse_url($url, PHP_URL_SCHEME);
  113. if (($sch != 'ftp') && ($sch != 'ftps'))
  114. {
  115. return false;
  116. }
  117. $server = parse_url($url, PHP_URL_HOST);
  118. $port = parse_url($url, PHP_URL_PORT);
  119. $path = parse_url($url, PHP_URL_PATH);
  120. $user = parse_url($url, PHP_URL_USER);
  121. $pass = parse_url($url, PHP_URL_PASS);
  122. if ((!$server) || (!$path))
  123. {
  124. return false;
  125. }
  126. if (!$port)
  127. {
  128. $port = 21;
  129. }
  130. if (!$user)
  131. {
  132. $user = 'anonymous';
  133. }
  134. if (!$pass)
  135. {
  136. $pass = '';
  137. }
  138. switch ($sch)
  139. {
  140. case 'ftp':
  141. $ftpid = ftp_connect($server, $port);
  142. break;
  143. case 'ftps':
  144. $ftpid = ftp_ssl_connect($server, $port);
  145. break;
  146. }
  147. if (!$ftpid)
  148. {
  149. return false;
  150. }
  151. $login = ftp_login($ftpid, $user, $pass);
  152. if (!$login)
  153. {
  154. return false;
  155. }
  156. $res = ftp_chmod($ftpid, $mode, $path);
  157. ftp_close($ftpid);
  158. return $res;
  159. }
  160. /**
  161. * Modes that require a write operation
  162. *
  163. * @return array
  164. *
  165. * @since 11.1
  166. */
  167. static function getWriteModes()
  168. {
  169. return array('w', 'w+', 'a', 'a+', 'r+', 'x', 'x+');
  170. }
  171. /**
  172. * Stream and Filter Support Operations
  173. *
  174. * Returns the supported streams, in addition to direct file access
  175. * Also includes Joomla! streams as well as PHP streams
  176. *
  177. * @return array Streams
  178. *
  179. * @since 11.1
  180. */
  181. function getSupported()
  182. {
  183. // Really quite cool what php can do with arrays when you let it...
  184. static $streams;
  185. if (!$streams)
  186. {
  187. $streams = array_merge(stream_get_wrappers(), JFilesystemHelper::getJStreams());
  188. }
  189. return $streams;
  190. }
  191. /**
  192. * Returns a list of transports
  193. *
  194. * @return array
  195. *
  196. * @since 11.1
  197. */
  198. function getTransports()
  199. {
  200. // Is this overkill?
  201. return stream_get_transports();
  202. }
  203. /**
  204. * Returns a list of filters
  205. *
  206. * @return array
  207. *
  208. * @since 11.1
  209. */
  210. function getFilters()
  211. {
  212. // Note: This will look like the getSupported() function with J! filters.
  213. // TODO: add user space filter loading like user space stream loading
  214. return stream_get_filters();
  215. }
  216. /**
  217. * Returns a list of J! streams
  218. *
  219. * @return array
  220. *
  221. * @since 11.1
  222. */
  223. function getJStreams()
  224. {
  225. static $streams;
  226. if (!$streams)
  227. {
  228. $streams = array_map(array('JFile', 'stripExt'), JFolder::files(dirname(__FILE__) . '/streams', '.php'));
  229. }
  230. return $streams;
  231. }
  232. /**
  233. * Determine if a stream is a Joomla stream.
  234. *
  235. * @param string $streamname The name of a stream
  236. *
  237. * @return boolean True for a Joomla Stream
  238. *
  239. * @since 11.1
  240. */
  241. function isJoomlaStream($streamname)
  242. {
  243. return in_array($streamname, JFilesystemHelper::getJStreams());
  244. }
  245. }