FilesystemHelper.php
7.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
<?php
/**
* Joomla! Content Management System
*
* @copyright Copyright (C) 2005 - 2019 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
namespace Joomla\CMS\Filesystem;
defined('JPATH_PLATFORM') or die;
/**
* File system helper
*
* Holds support functions for the filesystem, particularly the stream
*
* @since 1.7.0
*/
class FilesystemHelper
{
/**
* Remote file size function for streams that don't support it
*
* @param string $url TODO Add text
*
* @return mixed
*
* @link https://www.php.net/manual/en/function.filesize.php
* @since 1.7.0
*/
public static function remotefsize($url)
{
$sch = parse_url($url, PHP_URL_SCHEME);
if (($sch != 'http') && ($sch != 'https') && ($sch != 'ftp') && ($sch != 'ftps'))
{
return false;
}
if (($sch == 'http') || ($sch == 'https'))
{
$headers = get_headers($url, 1);
if ((!array_key_exists('Content-Length', $headers)))
{
return false;
}
return $headers['Content-Length'];
}
if (($sch == 'ftp') || ($sch == 'ftps'))
{
$server = parse_url($url, PHP_URL_HOST);
$port = parse_url($url, PHP_URL_PORT);
$path = parse_url($url, PHP_URL_PATH);
$user = parse_url($url, PHP_URL_USER);
$pass = parse_url($url, PHP_URL_PASS);
if ((!$server) || (!$path))
{
return false;
}
if (!$port)
{
$port = 21;
}
if (!$user)
{
$user = 'anonymous';
}
if (!$pass)
{
$pass = '';
}
switch ($sch)
{
case 'ftp':
$ftpid = ftp_connect($server, $port);
break;
case 'ftps':
$ftpid = ftp_ssl_connect($server, $port);
break;
}
if (!$ftpid)
{
return false;
}
$login = ftp_login($ftpid, $user, $pass);
if (!$login)
{
return false;
}
$ftpsize = ftp_size($ftpid, $path);
ftp_close($ftpid);
if ($ftpsize == -1)
{
return false;
}
return $ftpsize;
}
}
/**
* Quick FTP chmod
*
* @param string $url Link identifier
* @param integer $mode The new permissions, given as an octal value.
*
* @return mixed
*
* @link https://www.php.net/manual/en/function.ftp-chmod.php
* @since 1.7.0
*/
public static function ftpChmod($url, $mode)
{
$sch = parse_url($url, PHP_URL_SCHEME);
if (($sch != 'ftp') && ($sch != 'ftps'))
{
return false;
}
$server = parse_url($url, PHP_URL_HOST);
$port = parse_url($url, PHP_URL_PORT);
$path = parse_url($url, PHP_URL_PATH);
$user = parse_url($url, PHP_URL_USER);
$pass = parse_url($url, PHP_URL_PASS);
if ((!$server) || (!$path))
{
return false;
}
if (!$port)
{
$port = 21;
}
if (!$user)
{
$user = 'anonymous';
}
if (!$pass)
{
$pass = '';
}
switch ($sch)
{
case 'ftp':
$ftpid = ftp_connect($server, $port);
break;
case 'ftps':
$ftpid = ftp_ssl_connect($server, $port);
break;
}
if (!$ftpid)
{
return false;
}
$login = ftp_login($ftpid, $user, $pass);
if (!$login)
{
return false;
}
$res = ftp_chmod($ftpid, $mode, $path);
ftp_close($ftpid);
return $res;
}
/**
* Modes that require a write operation
*
* @return array
*
* @since 1.7.0
*/
public static function getWriteModes()
{
return array('w', 'w+', 'a', 'a+', 'r+', 'x', 'x+');
}
/**
* Stream and Filter Support Operations
*
* Returns the supported streams, in addition to direct file access
* Also includes Joomla! streams as well as PHP streams
*
* @return array Streams
*
* @since 1.7.0
*/
public static function getSupported()
{
// Really quite cool what php can do with arrays when you let it...
static $streams;
if (!$streams)
{
$streams = array_merge(stream_get_wrappers(), self::getJStreams());
}
return $streams;
}
/**
* Returns a list of transports
*
* @return array
*
* @since 1.7.0
*/
public static function getTransports()
{
// Is this overkill?
return stream_get_transports();
}
/**
* Returns a list of filters
*
* @return array
*
* @since 1.7.0
*/
public static function getFilters()
{
// Note: This will look like the getSupported() function with J! filters.
// TODO: add user space filter loading like user space stream loading
return stream_get_filters();
}
/**
* Returns a list of J! streams
*
* @return array
*
* @since 1.7.0
*/
public static function getJStreams()
{
static $streams = array();
if (!$streams)
{
$files = new \DirectoryIterator(__DIR__ . '/Streams');
/* @type $file DirectoryIterator */
foreach ($files as $file)
{
// Only load for php files.
if (!$file->isFile() || $file->getExtension() !== 'php')
{
continue;
}
$streams[] = str_replace('stream', '', strtolower($file->getBasename('.php')));
}
}
return $streams;
}
/**
* Determine if a stream is a Joomla stream.
*
* @param string $streamname The name of a stream
*
* @return boolean True for a Joomla Stream
*
* @since 1.7.0
*/
public static function isJoomlaStream($streamname)
{
return in_array($streamname, self::getJStreams());
}
/**
* Calculates the maximum upload file size and returns string with unit or the size in bytes
*
* Call it with JFilesystemHelper::fileUploadMaxSize();
*
* @param bool $unit_output This parameter determines whether the return value should be a string with a unit
*
* @return float|string The maximum upload size of files with the appropriate unit or in bytes
*
* @since 3.4
*/
public static function fileUploadMaxSize($unit_output = true)
{
static $max_size = false;
static $output_type = true;
if ($max_size === false || $output_type != $unit_output)
{
$max_size = self::parseSize(ini_get('post_max_size'));
$upload_max = self::parseSize(ini_get('upload_max_filesize'));
if ($upload_max > 0 && ($upload_max < $max_size || $max_size == 0))
{
$max_size = $upload_max;
}
if ($unit_output == true)
{
$max_size = self::parseSizeUnit($max_size);
}
$output_type = $unit_output;
}
return $max_size;
}
/**
* Returns the size in bytes without the unit for the comparison
*
* @param string $size The size which is received from the PHP settings
*
* @return float The size in bytes without the unit
*
* @since 3.4
*/
private static function parseSize($size)
{
$unit = preg_replace('/[^bkmgtpezy]/i', '', $size);
$size = preg_replace('/[^0-9\.]/', '', $size);
$return = round($size);
if ($unit)
{
$return = round($size * pow(1024, stripos('bkmgtpezy', $unit[0])));
}
return $return;
}
/**
* Creates the rounded size of the size with the appropriate unit
*
* @param float $max_size The maximum size which is allowed for the uploads
*
* @return string String with the size and the appropriate unit
*
* @since 3.4
*/
private static function parseSizeUnit($max_size)
{
$base = log($max_size) / log(1024);
$suffixes = array('', 'k', 'M', 'G', 'T');
return round(pow(1024, $base - floor($base)), 0) . $suffixes[floor($base)];
}
}