/libraries/joomla/filesystem/helper.php

https://bitbucket.org/pastor399/newcastleunifc · PHP · 300 lines · 164 code · 46 blank · 90 comment · 44 complexity · 5a3096b08c2f56970f9e7e9e4970e702 MD5 · raw file

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