Support Joomla!

Joomla! 1.5 Documentation

Packages

Package: Joomla-Framework

License

Content on this site is copyright © 2005 - 2008 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution- NonCommercial- ShareAlike 2.5. Some parts of this website may be subject to other licenses.
Source code for file /joomla/filesystem/file.php

Documentation is available at file.php

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

Documentation generated on Tue, 29 Jan 2008 18:45:47 +0000 by phpDocumentor 1.3.1