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