[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/vendor/joomla/filesystem/src/ -> Helper.php (source)

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


Generated: Wed Sep 7 05:41:13 2022 Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer