[ Index ]

PHP Cross Reference of Joomla 1.5.26 DE

title

Body

[close]

/libraries/joomla/filesystem/ -> file.php (source)

   1  <?php
   2  /**
   3   * @version        $Id: file.php 19177 2010-10-21 03:08:56Z ian $
   4   * @package        Joomla.Framework
   5   * @subpackage    FileSystem
   6   * @copyright    Copyright (C) 2005 - 2008 Open Source Matters. All rights reserved.
   7   * @license        GNU/GPL, see LICENSE.php
   8   * Joomla! is free software. This version may have been modified pursuant
   9   * to the GNU General Public License, and as distributed it includes or
  10   * is derivative of works licensed under the GNU General Public License or
  11   * other free or open source software licenses.
  12   * See COPYRIGHT.php for copyright notices and details.
  13   */
  14  
  15  // Check to ensure this file is within the rest of the framework
  16  defined('JPATH_BASE') or die();
  17  
  18  jimport('joomla.filesystem.path');
  19  
  20  /**
  21   * A File handling class
  22   *
  23   * @static
  24   * @package     Joomla.Framework
  25   * @subpackage    FileSystem
  26   * @since        1.5
  27   */
  28  class JFile
  29  {
  30      /**
  31       * Gets the extension of a file name
  32       *
  33       * @param string $file The file name
  34       * @return string The file extension
  35       * @since 1.5
  36       */
  37  	function getExt($file) {
  38          $chunks = explode('.', $file);
  39          $chunksCount = count($chunks) - 1;
  40  
  41          if($chunksCount > 0) {
  42              return $chunks[$chunksCount];
  43          }
  44          
  45          return false;
  46      }
  47  
  48      /**
  49       * Strips the last extension off a file name
  50       *
  51       * @param string $file The file name
  52       * @return string The file name without the extension
  53       * @since 1.5
  54       */
  55  	function stripExt($file) {
  56          return preg_replace('#\.[^.]*$#', '', $file);
  57      }
  58  
  59      /**
  60       * Makes file name safe to use
  61       *
  62       * @param string $file The name of the file [not full path]
  63       * @return string The sanitised string
  64       * @since 1.5
  65       */
  66  	function makeSafe($file) {
  67          $regex = array('#(\.){2,}#', '#[^A-Za-z0-9\.\_\- ]#', '#^\.#');
  68          return preg_replace($regex, '', $file);
  69      }
  70  
  71      /**
  72       * Copies a file
  73       *
  74       * @param string $src The path to the source file
  75       * @param string $dest The path to the destination file
  76       * @param string $path An optional base path to prefix to the file names
  77       * @return boolean True on success
  78       * @since 1.5
  79       */
  80  	function copy($src, $dest, $path = null)
  81      {
  82          // Initialize variables
  83          jimport('joomla.client.helper');
  84          $FTPOptions = JClientHelper::getCredentials('ftp');
  85  
  86          // Prepend a base path if it exists
  87          if ($path) {
  88              $src = JPath::clean($path.DS.$src);
  89              $dest = JPath::clean($path.DS.$dest);
  90          }
  91  
  92          //Check src path
  93          if (!is_readable($src)) {
  94              JError::raiseWarning(21, 'JFile::copy: ' . JText::_('Cannot find or read file') . ": '$src'");
  95              return false;
  96          }
  97  
  98          if ($FTPOptions['enabled'] == 1) {
  99              // Connect the FTP client
 100              jimport('joomla.client.ftp');
 101              $ftp = & JFTP::getInstance($FTPOptions['host'], $FTPOptions['port'], null, $FTPOptions['user'], $FTPOptions['pass']);
 102  
 103              // If the parent folder doesn't exist we must create it
 104              if (!file_exists(dirname($dest))) {
 105                  jimport('joomla.filesystem.folder');
 106                  JFolder::create(dirname($dest));
 107              }
 108  
 109              //Translate the destination path for the FTP account
 110              $dest = JPath::clean(str_replace(JPATH_ROOT, $FTPOptions['root'], $dest), '/');
 111              if (!$ftp->store($src, $dest)) {
 112                  // FTP connector throws an error
 113                  return false;
 114              }
 115              $ret = true;
 116          } else {
 117              if (!@ copy($src, $dest)) {
 118                  JError::raiseWarning(21, JText::_('Copy failed'));
 119                  return false;
 120              }
 121              $ret = true;
 122          }
 123          return $ret;
 124      }
 125  
 126      /**
 127       * Delete a file or array of files
 128       *
 129       * @param mixed $file The file name or an array of file names
 130       * @return boolean  True on success
 131       * @since 1.5
 132       */
 133  	function delete($file)
 134      {
 135          // Initialize variables
 136          jimport('joomla.client.helper');
 137          $FTPOptions = JClientHelper::getCredentials('ftp');
 138  
 139          if (is_array($file)) {
 140              $files = $file;
 141          } else {
 142              $files[] = $file;
 143          }
 144  
 145          // Do NOT use ftp if it is not enabled
 146          if ($FTPOptions['enabled'] == 1)
 147          {
 148              // Connect the FTP client
 149              jimport('joomla.client.ftp');
 150              $ftp = & JFTP::getInstance($FTPOptions['host'], $FTPOptions['port'], null, $FTPOptions['user'], $FTPOptions['pass']);
 151          }
 152  
 153          foreach ($files as $file)
 154          {
 155              $file = JPath::clean($file);
 156  
 157              // Try making the file writeable first. If it's read-only, it can't be deleted
 158              // on Windows, even if the parent folder is writeable
 159              @chmod($file, 0777);
 160  
 161              // In case of restricted permissions we zap it one way or the other
 162              // as long as the owner is either the webserver or the ftp
 163              if (@unlink($file)) {
 164                  // Do nothing
 165              } elseif ($FTPOptions['enabled'] == 1) {
 166                  $file = JPath::clean(str_replace(JPATH_ROOT, $FTPOptions['root'], $file), '/');
 167                  if (!$ftp->delete($file)) {
 168                      // FTP connector throws an error
 169                      return false;
 170                  }
 171              } else {
 172                  $filename    = basename($file);
 173                  JError::raiseWarning('SOME_ERROR_CODE', JText::_('Delete failed') . ": '$filename'");
 174                  return false;
 175              }
 176          }
 177  
 178          return true;
 179      }
 180  
 181      /**
 182       * Moves a file
 183       *
 184       * @param string $src The path to the source file
 185       * @param string $dest The path to the destination file
 186       * @param string $path An optional base path to prefix to the file names
 187       * @return boolean True on success
 188       * @since 1.5
 189       */
 190  	function move($src, $dest, $path = '')
 191      {
 192          // Initialize variables
 193          jimport('joomla.client.helper');
 194          $FTPOptions = JClientHelper::getCredentials('ftp');
 195  
 196          if ($path) {
 197              $src = JPath::clean($path.DS.$src);
 198              $dest = JPath::clean($path.DS.$dest);
 199          }
 200  
 201          //Check src path
 202          if (!is_readable($src) && !is_writable($src)) {
 203              JError::raiseWarning(21, 'JFile::move: ' . JText::_('Cannot find, read or write file') . ": '$src'");
 204              return false;
 205          }
 206  
 207          if ($FTPOptions['enabled'] == 1) {
 208              // Connect the FTP client
 209              jimport('joomla.client.ftp');
 210              $ftp = & JFTP::getInstance($FTPOptions['host'], $FTPOptions['port'], null, $FTPOptions['user'], $FTPOptions['pass']);
 211  
 212              //Translate path for the FTP account
 213              $src    = JPath::clean(str_replace(JPATH_ROOT, $FTPOptions['root'], $src), '/');
 214              $dest    = JPath::clean(str_replace(JPATH_ROOT, $FTPOptions['root'], $dest), '/');
 215  
 216              // Use FTP rename to simulate move
 217              if (!$ftp->rename($src, $dest)) {
 218                  JError::raiseWarning(21, JText::_('Rename failed'));
 219                  return false;
 220              }
 221          } else {
 222              if (!@ rename($src, $dest)) {
 223                  JError::raiseWarning(21, JText::_('Rename failed'));
 224                  return false;
 225              }
 226          }
 227          return true;
 228      }
 229  
 230      /**
 231       * Read the contents of a file
 232       *
 233       * @param string $filename The full file path
 234       * @param boolean $incpath Use include path
 235       * @param int $amount Amount of file to read
 236       * @param int $chunksize Size of chunks to read
 237       * @param int $offset Offset of the file
 238       * @return mixed Returns file contents or boolean False if failed
 239       * @since 1.5
 240       */
 241  	function read($filename, $incpath = false, $amount = 0, $chunksize = 8192, $offset = 0)
 242      {
 243          // Initialize variables
 244          $data = null;
 245          if($amount && $chunksize > $amount) { $chunksize = $amount; }
 246          if (false === $fh = fopen($filename, 'rb', $incpath)) {
 247              JError::raiseWarning(21, 'JFile::read: '.JText::_('Unable to open file') . ": '$filename'");
 248              return false;
 249          }
 250          clearstatcache();
 251          if($offset) fseek($fh, $offset);
 252          if ($fsize = @ filesize($filename)) {
 253              if($amount && $fsize > $amount) {
 254                  $data = fread($fh, $amount);
 255              } else {
 256                  $data = fread($fh, $fsize);
 257              }
 258          } else {
 259              $data = '';
 260              $x = 0;
 261              // While its:
 262              // 1: Not the end of the file AND
 263              // 2a: No Max Amount set OR
 264              // 2b: The length of the data is less than the max amount we want
 265              while (!feof($fh) && (!$amount || strlen($data) < $amount)) {
 266                  $data .= fread($fh, $chunksize);
 267              }
 268          }
 269          fclose($fh);
 270  
 271          return $data;
 272      }
 273  
 274      /**
 275       * Write contents to a file
 276       *
 277       * @param string $file The full file path
 278       * @param string $buffer The buffer to write
 279       * @return boolean True on success
 280       * @since 1.5
 281       */
 282  	function write($file, $buffer)
 283      {
 284          // Initialize variables
 285          jimport('joomla.client.helper');
 286          $FTPOptions = JClientHelper::getCredentials('ftp');
 287  
 288          // If the destination directory doesn't exist we need to create it
 289          if (!file_exists(dirname($file))) {
 290              jimport('joomla.filesystem.folder');
 291              JFolder::create(dirname($file));
 292          }
 293  
 294          if ($FTPOptions['enabled'] == 1) {
 295              // Connect the FTP client
 296              jimport('joomla.client.ftp');
 297              $ftp = & JFTP::getInstance($FTPOptions['host'], $FTPOptions['port'], null, $FTPOptions['user'], $FTPOptions['pass']);
 298  
 299              // Translate path for the FTP account and use FTP write buffer to file
 300              $file = JPath::clean(str_replace(JPATH_ROOT, $FTPOptions['root'], $file), '/');
 301              $ret = $ftp->write($file, $buffer);
 302          } else {
 303              $file = JPath::clean($file);
 304              $ret = file_put_contents($file, $buffer);
 305          }
 306          return $ret;
 307      }
 308  
 309      /**
 310       * Moves an uploaded file to a destination folder
 311       *
 312       * @param string $src The name of the php (temporary) uploaded file
 313       * @param string $dest The path (including filename) to move the uploaded file to
 314       * @return boolean True on success
 315       * @since 1.5
 316       */
 317  	function upload($src, $dest)
 318      {
 319          // Initialize variables
 320          jimport('joomla.client.helper');
 321          $FTPOptions = JClientHelper::getCredentials('ftp');
 322          $ret        = false;
 323  
 324          // Ensure that the path is valid and clean
 325          $dest = JPath::clean($dest);
 326  
 327          // Create the destination directory if it does not exist
 328          $baseDir = dirname($dest);
 329          if (!file_exists($baseDir)) {
 330              jimport('joomla.filesystem.folder');
 331              JFolder::create($baseDir);
 332          }
 333  
 334          if ($FTPOptions['enabled'] == 1) {
 335              // Connect the FTP client
 336              jimport('joomla.client.ftp');
 337              $ftp = & JFTP::getInstance($FTPOptions['host'], $FTPOptions['port'], null, $FTPOptions['user'], $FTPOptions['pass']);
 338  
 339              //Translate path for the FTP account
 340              $dest = JPath::clean(str_replace(JPATH_ROOT, $FTPOptions['root'], $dest), '/');
 341  
 342              // Copy the file to the destination directory
 343              if (is_uploaded_file($src) && $ftp->store($src, $dest))
 344              {
 345                          $ret = true;
 346                          unlink($src);
 347              } else {
 348                  JError::raiseWarning(21, JText::_('WARNFS_ERR02'));
 349              }
 350          } else {
 351              if (is_writeable($baseDir) && move_uploaded_file($src, $dest)) { // Short circuit to prevent file permission errors
 352                  if (JPath::setPermissions($dest)) {
 353                      $ret = true;
 354                  } else {
 355                      JError::raiseWarning(21, JText::_('WARNFS_ERR01'));
 356                  }
 357              } else {
 358                  JError::raiseWarning(21, JText::_('WARNFS_ERR02'));
 359              }
 360          }
 361          return $ret;
 362      }
 363  
 364      /**
 365       * Wrapper for the standard file_exists function
 366       *
 367       * @param string $file File path
 368       * @return boolean True if path is a file
 369       * @since 1.5
 370       */
 371  	function exists($file)
 372      {
 373          return is_file(JPath::clean($file));
 374      }
 375  
 376      /**
 377       * Returns the name, sans any path
 378       *
 379       * param string $file File path
 380       * @return string filename
 381       * @since 1.5
 382       */
 383  	function getName($file) {
 384          $slash = strrpos($file, DS);
 385          if ($slash !== false) {
 386              return substr($file, $slash + 1);
 387          } else {
 388              return $file;
 389          }
 390      }
 391  }


Generated: Wed Mar 28 15:54:07 2012 Cross-referenced by PHPXref 0.7.1