Support Joomla!

Joomla! 1.5 Documentation

Packages

Package: Joomla-Framework

Developer Network License

The Joomla! Developer Network content is © copyright 2006 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution- NonCommercial- ShareAlike 2.5
Source code for file /joomla/filesystem/file.php

Documentation is available at file.php

  1. <?php
  2. /**
  3.  * @version        $Id: file.php 6527 2007-02-08 05:22:52Z tcp $
  4.  * @package        Joomla.Framework
  5.  * @subpackage    FileSystem
  6.  * @copyright    Copyright (C) 2005 - 2007 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 <[email protected]>
  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 '#\.\.[^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.             // Connect the FTP client
  143.             jimport('joomla.client.ftp');
  144.             $ftp JFTP::getInstance($FTPOptions['host']$FTPOptions['port']null$FTPOptions['user']$FTPOptions['pass']);
  145.         }
  146.  
  147.         foreach ($files as $file{
  148.             $file JPath::clean($file);
  149.  
  150.             // In case of restricted permissions we zap it one way or the other
  151.             // as long as the owner is either the webserver or the ftp
  152.             if (@unlink($file)) {
  153.                 // Do nothing
  154.             elseif ($FTPOptions['enabled'== 1{
  155.                 $file JPath::clean(str_replace(JPATH_ROOT$FTPOptions['root']$file)'/');
  156.                 if (!$ftp->delete($file)) {
  157.                     // FTP connector throws an error
  158.                     return false;
  159.                 }
  160.             else {
  161.                 $filename    basename($file);
  162.                 JError::raiseWarning('SOME_ERROR_CODE'JText::_('Delete failed'": '$filename'");
  163.                 return false;
  164.             }
  165.         }
  166.  
  167.         return true;
  168.     }
  169.  
  170.     /**
  171.      * Moves a file
  172.      *
  173.      * @param string $src The path to the source file
  174.      * @param string $dest The path to the destination file
  175.      * @param string $path An optional base path to prefix to the file names
  176.      * @return boolean True on success
  177.      * @since 1.5
  178.      */
  179.     function move($src$dest$path '')
  180.     {
  181.         // Initialize variables
  182.         jimport('joomla.client.helper');
  183.         $FTPOptions JClientHelper::getCredentials('ftp');
  184.  
  185.         if ($path{
  186.             $src JPath::clean($path.DS.$src);
  187.             $dest JPath::clean($path.DS.$dest);
  188.         }
  189.  
  190.         //Check src path
  191.         if (!is_readable($src&& !is_writable($src)) {
  192.             return JText::_('Cannot find source file');
  193.         }
  194.  
  195.         if ($FTPOptions['enabled'== 1{
  196.             // Connect the FTP client
  197.             jimport('joomla.client.ftp');
  198.             $ftp JFTP::getInstance($FTPOptions['host']$FTPOptions['port']null$FTPOptions['user']$FTPOptions['pass']);
  199.  
  200.             //Translate path for the FTP account
  201.             $src    JPath::clean(str_replace(JPATH_ROOT$FTPOptions['root']$src)'/');
  202.             $dest    JPath::clean(str_replace(JPATH_ROOT$FTPOptions['root']$dest)'/');
  203.  
  204.             // Use FTP rename to simulate move
  205.             if (!$ftp->rename($src$dest)) {
  206.                 JError::raiseWarning(21JText::_('Rename failed'));
  207.                 return false;
  208.             }
  209.         else {
  210.             if (!rename($src$dest)) {
  211.                 JError::raiseWarning(21JText::_('Rename failed'));
  212.                 return false;
  213.             }
  214.         }
  215.         return true;
  216.     }
  217.  
  218.     /**
  219.      * Read the contents of a file
  220.      *
  221.      * @param string $filename The full file path
  222.      * @param boolean $incpath Use include path
  223.      * @return mixed Returns file contents or boolean False if failed
  224.      * @since 1.5
  225.      */
  226.     function read($filename$incpath false)
  227.     {
  228.         // Initialize variables
  229.         $data null;
  230.  
  231.         if (false === $fh fopen($filename'rb'$incpath)) {
  232.             JError::raiseWarning(21'JFile::read: '.JText::_('Unable to open file ').$filename);
  233.             return false;
  234.         }
  235.         clearstatcache();
  236.         if ($fsize filesize($filename)) {
  237.             $data fread($fh$fsize);
  238.         else {
  239.             $data '';
  240.             while (!feof($fh)) {
  241.                 $data .= fread($fh8192);
  242.             }
  243.         }
  244.         fclose($fh);
  245.  
  246.         return $data;
  247.     }
  248.  
  249.     /**
  250.      * Write contents to a file
  251.      *
  252.      * @param string $file The full file path
  253.      * @param string $buffer The buffer to write
  254.      * @return boolean True on success
  255.      * @since 1.5
  256.      */
  257.     function write($file$buffer)
  258.     {
  259.         // Initialize variables
  260.         jimport('joomla.client.helper');
  261.         $FTPOptions JClientHelper::getCredentials('ftp');
  262.  
  263.         // If the destination directory doesn't exist we need to create it
  264.         if (!file_exists(dirname($file))) {
  265.             jimport('joomla.filesystem.folder');
  266.             JFolder::create(dirname($file));
  267.         }
  268.  
  269.         if ($FTPOptions['enabled'== 1{
  270.             // Connect the FTP client
  271.             jimport('joomla.client.ftp');
  272.             $ftp JFTP::getInstance($FTPOptions['host']$FTPOptions['port']null$FTPOptions['user']$FTPOptions['pass']);
  273.  
  274.             // Translate path for the FTP account and use FTP write buffer to file
  275.             $file JPath::clean(str_replace(JPATH_ROOT$FTPOptions['root']$file)'/');
  276.             $ret $ftp->write($file$buffer);
  277.         else {
  278.             $file JPath::clean($file);
  279.             $ret file_put_contents($file$buffer);
  280.         }
  281.         return $ret;
  282.     }
  283.  
  284.     /**
  285.      * Moves an uploaded file to a destination folder
  286.      *
  287.      * @param string $src The name of the php (temporary) uploaded file
  288.      * @param string $dest The path (including filename) to move the uploaded file to
  289.      * @return boolean True on success
  290.      * @since 1.5
  291.      */
  292.     function upload($src$dest)
  293.     {
  294.         // Initialize variables
  295.         jimport('joomla.client.helper');
  296.         $FTPOptions JClientHelper::getCredentials('ftp');
  297.         $ret        false;
  298.  
  299.         // Ensure that the path is valid and clean
  300.         $dest JPath::clean($dest);
  301.  
  302.         // Create the destination directory if it does not exist
  303.         $baseDir dirname($dest);
  304.         if (!file_exists($baseDir)) {
  305.             jimport('joomla.filesystem.folder');
  306.             JFolder::create($baseDir);
  307.         }
  308.  
  309.         if ($FTPOptions['enabled'== 1{
  310.             // Connect the FTP client
  311.             jimport('joomla.client.ftp');
  312.             $ftp JFTP::getInstance($FTPOptions['host']$FTPOptions['port']null$FTPOptions['user']$FTPOptions['pass']);
  313.  
  314.             //Translate path for the FTP account
  315.             $dest JPath::clean(str_replace(JPATH_ROOT$FTPOptions['root']$dest)'/');
  316.  
  317.             // Copy the file to the destination directory
  318.             if ($ftp->store($src$dest)) {
  319.                 $ftp->chmod($dest0777);
  320.                 $ret true;
  321.             else {
  322.                 JError::raiseWarning(21JText::_('WARNFS_ERR02'));
  323.             }
  324.         else {
  325.             if (move_uploaded_file($src$dest)) {
  326.                 if (JPath::setPermissions($dest)) {
  327.                     $ret true;
  328.                 else {
  329.                     JError::raiseWarning(21JText::_('WARNFS_ERR01'));
  330.                 }
  331.             else {
  332.                 JError::raiseWarning(21JText::_('WARNFS_ERR02'));
  333.             }
  334.         }
  335.         return $ret;
  336.     }
  337.  
  338.     /**
  339.      * Wrapper for the standard file_exists function
  340.      *
  341.      * @param string $file File path
  342.      * @return boolean True if path is a file
  343.      * @since 1.5
  344.      */
  345.     function exists($file)
  346.     {
  347.         return is_file(JPath::clean($file));
  348.     }
  349. }
  350. ?>

Documentation generated on Mon, 05 Mar 2007 20:58:30 +0000 by phpDocumentor 1.3.1