Joomla Platform  13.1
Documentation des API du framework Joomla Platform
 Tout Classes Espaces de nommage Fichiers Fonctions Variables Pages
helper.php
Aller à la documentation de ce fichier.
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 
10 defined('JPATH_PLATFORM') or die;
11 
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  */
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  public static function remotefsize($url)
34  {
35  $sch = parse_url($url, PHP_URL_SCHEME);
36 
37  if (($sch != 'http') && ($sch != 'https') && ($sch != 'ftp') && ($sch != 'ftps'))
38  {
39  return false;
40  }
41 
42  if (($sch == 'http') || ($sch == 'https'))
43  {
44  $headers = get_headers($url, 1);
45 
46  if ((!array_key_exists('Content-Length', $headers)))
47  {
48  return false;
49  }
50 
51  return $headers['Content-Length'];
52  }
53 
54  if (($sch == 'ftp') || ($sch == 'ftps'))
55  {
56  $server = parse_url($url, PHP_URL_HOST);
57  $port = parse_url($url, PHP_URL_PORT);
58  $path = parse_url($url, PHP_URL_PATH);
59  $user = parse_url($url, PHP_URL_USER);
60  $pass = parse_url($url, PHP_URL_PASS);
61 
62  if ((!$server) || (!$path))
63  {
64  return false;
65  }
66 
67  if (!$port)
68  {
69  $port = 21;
70  }
71 
72  if (!$user)
73  {
74  $user = 'anonymous';
75  }
76 
77  if (!$pass)
78  {
79  $pass = '';
80  }
81 
82  switch ($sch)
83  {
84  case 'ftp':
85  $ftpid = ftp_connect($server, $port);
86  break;
87 
88  case 'ftps':
89  $ftpid = ftp_ssl_connect($server, $port);
90  break;
91  }
92 
93  if (!$ftpid)
94  {
95  return false;
96  }
97 
98  $login = ftp_login($ftpid, $user, $pass);
99 
100  if (!$login)
101  {
102  return false;
103  }
104 
105  $ftpsize = ftp_size($ftpid, $path);
106  ftp_close($ftpid);
107 
108  if ($ftpsize == -1)
109  {
110  return false;
111  }
112 
113  return $ftpsize;
114  }
115  }
116 
117  /**
118  * Quick FTP chmod
119  *
120  * @param string $url Link identifier
121  * @param integer $mode The new permissions, given as an octal value.
122  *
123  * @return mixed
124  *
125  * @see http://www.php.net/manual/en/function.ftp-chmod.php
126  * @since 11.1
127  */
128  public static function ftpChmod($url, $mode)
129  {
130  $sch = parse_url($url, PHP_URL_SCHEME);
131 
132  if (($sch != 'ftp') && ($sch != 'ftps'))
133  {
134  return false;
135  }
136 
137  $server = parse_url($url, PHP_URL_HOST);
138  $port = parse_url($url, PHP_URL_PORT);
139  $path = parse_url($url, PHP_URL_PATH);
140  $user = parse_url($url, PHP_URL_USER);
141  $pass = parse_url($url, PHP_URL_PASS);
142 
143  if ((!$server) || (!$path))
144  {
145  return false;
146  }
147 
148  if (!$port)
149  {
150  $port = 21;
151  }
152 
153  if (!$user)
154  {
155  $user = 'anonymous';
156  }
157 
158  if (!$pass)
159  {
160  $pass = '';
161  }
162 
163  switch ($sch)
164  {
165  case 'ftp':
166  $ftpid = ftp_connect($server, $port);
167  break;
168 
169  case 'ftps':
170  $ftpid = ftp_ssl_connect($server, $port);
171  break;
172  }
173 
174  if (!$ftpid)
175  {
176  return false;
177  }
178 
179  $login = ftp_login($ftpid, $user, $pass);
180 
181  if (!$login)
182  {
183  return false;
184  }
185 
186  $res = ftp_chmod($ftpid, $mode, $path);
187  ftp_close($ftpid);
188 
189  return $res;
190  }
191 
192  /**
193  * Modes that require a write operation
194  *
195  * @return array
196  *
197  * @since 11.1
198  */
199  public static function getWriteModes()
200  {
201  return array('w', 'w+', 'a', 'a+', 'r+', 'x', 'x+');
202  }
203 
204  /**
205  * Stream and Filter Support Operations
206  *
207  * Returns the supported streams, in addition to direct file access
208  * Also includes Joomla! streams as well as PHP streams
209  *
210  * @return array Streams
211  *
212  * @since 11.1
213  */
214  public static function getSupported()
215  {
216  // Really quite cool what php can do with arrays when you let it...
217  static $streams;
218 
219  if (!$streams)
220  {
221  $streams = array_merge(stream_get_wrappers(), self::getJStreams());
222  }
223 
224  return $streams;
225  }
226 
227  /**
228  * Returns a list of transports
229  *
230  * @return array
231  *
232  * @since 11.1
233  */
234  public static function getTransports()
235  {
236  // Is this overkill?
237  return stream_get_transports();
238  }
239 
240  /**
241  * Returns a list of filters
242  *
243  * @return array
244  *
245  * @since 11.1
246  */
247  public static function getFilters()
248  {
249  // Note: This will look like the getSupported() function with J! filters.
250  // TODO: add user space filter loading like user space stream loading
251  return stream_get_filters();
252  }
253 
254  /**
255  * Returns a list of J! streams
256  *
257  * @return array
258  *
259  * @since 11.1
260  */
261  public static function getJStreams()
262  {
263  static $streams = array();
264 
265  if (!$streams)
266  {
267  $files = new DirectoryIterator(__DIR__ . '/streams');
268 
269  foreach ($files as $file)
270  {
271  $filename = $file->getFilename();
272 
273  // Only load for php files.
274  // Note: DirectoryIterator::getExtension only available PHP >= 5.3.6
275  if (!$file->isFile() || substr($filename, strrpos($filename, '.') + 1) != 'php')
276  {
277  continue;
278  }
279 
280  $streams[] = $file->getBasename('.php');
281  }
282  }
283 
284  return $streams;
285  }
286 
287  /**
288  * Determine if a stream is a Joomla stream.
289  *
290  * @param string $streamname The name of a stream
291  *
292  * @return boolean True for a Joomla Stream
293  *
294  * @since 11.1
295  */
296  public static function isJoomlaStream($streamname)
297  {
298  return in_array($streamname, self::getJStreams());
299  }
300 }