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/folder.php

Documentation is available at folder.php

  1. <?php
  2. /**
  3.  * @version        $Id: folder.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 Folder 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 JFolder
  30. {
  31.     /**
  32.      * Copies a folder
  33.      *
  34.      * @param    string    $src    The path to the source folder
  35.      * @param    string    $dest    The path to the destination folder
  36.      * @param    string    $path    An optional base path to prefix to the file names
  37.      * @param    boolean    $force    Optionally force folder/file overwrites
  38.      * @return    mixed    JError object on failure or boolean True on success
  39.      * @since    1.5
  40.      */
  41.     function copy($src$dest$path ''$force false)
  42.     {
  43.         // Initialize variables
  44.         jimport('joomla.client.helper');
  45.         $FTPOptions JClientHelper::getCredentials('ftp');
  46.  
  47.         if ($path{
  48.             $src JPath::clean($path.DS.$src);
  49.             $dest JPath::clean($path.DS.$dest);
  50.         }
  51.  
  52.         // Eliminate trailing directory separators, if any
  53.         $src rtrim($srcDS);
  54.         $dest rtrim($destDS);
  55.  
  56.         if (!JFolder::exists($src)) {
  57.             return JError::raiseError(-1JText::_('Cannot find source folder'));
  58.         }
  59.         if (JFolder::exists($dest&& !$force{
  60.             return JError::raiseError(-1JText::_('Folder already exists'));
  61.         }
  62.  
  63.         // Make sure the destination exists
  64.         if (JFolder::create($dest)) {
  65.             return JError::raiseError(-1JText::_('Unable to create target folder'));
  66.         }
  67.  
  68.         if ($FTPOptions['enabled'== 1)
  69.         {
  70.             // Connect the FTP client
  71.             jimport('joomla.client.ftp');
  72.             $ftp JFTP::getInstance($FTPOptions['host']$FTPOptions['port']null$FTPOptions['user']$FTPOptions['pass']);
  73.  
  74.             if(($dh @opendir($src))) {
  75.                 return JError::raiseError(-1JText::_('Unable to open source folder'));
  76.             }
  77.             // Walk through the directory copying files and recursing into folders.
  78.             while (($file readdir($dh)) !== false{
  79.                 $sfid $src DS $file;
  80.                 $dfid $dest DS $file;
  81.                 switch (filetype($sfid)) {
  82.                     case 'dir':
  83.                         if ($file != '.' && $file != '..'{
  84.                             $ret JFolder::copy($sfid$dfidnull$force);
  85.                             if ($ret !== true{
  86.                                 return $ret;
  87.                             }
  88.                         }
  89.                         break;
  90.  
  91.                     case 'file':
  92.                         //Translate path for the FTP account
  93.                         $dfid JPath::clean(str_replace(JPATH_ROOT$FTPOptions['root']$dfid)'/');
  94.                         if ($ftp->store($sfid$dfid)) {
  95.                             return JError::raiseError(-1JText::_('Copy failed'));
  96.                         }
  97.                         break;
  98.                 }
  99.             }
  100.         else {
  101.             if(($dh @opendir($src))) {
  102.                 return JError::raiseError(-1JText::_('Unable to open source folder'));
  103.             }
  104.             // Walk through the directory copying files and recursing into folders.
  105.             while (($file readdir($dh)) !== false{
  106.                 $sfid $src.DS.$file;
  107.                 $dfid $dest.DS.$file;
  108.                 switch (filetype($sfid)) {
  109.                     case 'dir':
  110.                         if ($file != '.' && $file != '..'{
  111.                             $ret JFolder::copy($sfid$dfidnull$force);
  112.                             if ($ret !== true{
  113.                                 return $ret;
  114.                             }
  115.                         }
  116.                         break;
  117.  
  118.                     case 'file':
  119.                         if (!copy($sfid$dfid)) {
  120.                             return JError::raiseError(-1JText::_('Copy failed'));
  121.                         }
  122.                         break;
  123.                 }
  124.             }
  125.         }
  126.         return true;
  127.     }
  128.  
  129.     /**
  130.      * Create a folder -- and all necessary parent folders
  131.      *
  132.      * @param string $path A path to create from the base path
  133.      * @param int $mode Directory permissions to set for folders created
  134.      * @return boolean True if successful
  135.      * @since 1.5
  136.      */
  137.     function create($path ''$mode 0755)
  138.     {
  139.         // Initialize variables
  140.         jimport('joomla.client.helper');
  141.         $FTPOptions JClientHelper::getCredentials('ftp');
  142.         static $nested 0;
  143.  
  144.         // Check to make sure the path valid and clean
  145.         
  146.         $path JPath::clean($path);
  147.  
  148.         // Check if parent dir exists
  149.         
  150.         $parent dirname($path);
  151.         if (!JFolder::exists($parent)) {
  152.             // Prevent infinite loops!
  153.             
  154.             $nested++;
  155.             if (($nested 20|| ($parent == $path)) {
  156.                 JError::raiseWarning('SOME_ERROR_CODE''JFolder::create: '.JText::_('Infinite loop detected'));
  157.                 $nested--;
  158.                 return false;
  159.             }
  160.  
  161.             // Create the parent directory
  162.             if (JFolder::create($parent$mode!== true{
  163.                 // JFolder::create throws an error
  164.                 $nested--;
  165.                 return false;
  166.             }
  167.  
  168.             // OK, parent directory has been created
  169.             $nested--;
  170.         }
  171.  
  172.         // Check if dir already exists
  173.         
  174.         if (JFolder::exists($path)) {
  175.             return true;
  176.         }
  177.  
  178.         // Check for safe mode
  179.         if ($FTPOptions['enabled'== 1{
  180.             // Connect the FTP client
  181.             jimport('joomla.client.ftp');
  182.             $ftp JFTP::getInstance($FTPOptions['host']$FTPOptions['port']null$FTPOptions['user']$FTPOptions['pass']);
  183.  
  184.             // Translate path to FTP path
  185.             $path JPath::clean(str_replace(JPATH_ROOT$FTPOptions['root']$path)'/');
  186.             $ret $ftp->mkdir($path);
  187.             $ftp->chmod($path$mode);
  188.         }
  189.         else
  190.         {
  191.             // We need to get and explode the open_basedir paths
  192.             $obd ini_get('open_basedir');
  193.  
  194.             // If open_basedir is set we need to get the open_basedir that the path is in
  195.             if ($obd != null)
  196.             {
  197.                 if (JPATH_ISWIN{
  198.                     $obdSeparator ";";
  199.                 else {
  200.                     $obdSeparator ":";
  201.                 }
  202.                 // Create the array of open_basedir paths
  203.                 $obdArray explode($obdSeparator$obd);
  204.                 $inOBD false;
  205.                 // Iterate through open_basedir paths looking for a match
  206.                 foreach ($obdArray as $test{
  207.                     $test JPath::clean($test);
  208.                     if (strpos($path$test=== 0{
  209.                         $obdpath $test;
  210.                         $inOBD true;
  211.                         break;
  212.                     }
  213.                 }
  214.                 if ($inOBD == false{
  215.                     // Return false for JFolder::create because the path to be created is not in open_basedir
  216.                     JError::raiseWarning('SOME_ERROR_CODE''JFolder::create: '.JText::_('Path not in open_basedir paths'));
  217.                     return false;
  218.                 }
  219.             }
  220.  
  221.             // First set umask
  222.             $origmask umask(0);
  223.  
  224.             // Create the path
  225.             if (!$ret @mkdir($path$mode)) {
  226.                 umask($origmask);
  227.                 JError::raiseWarning('SOME_ERROR_CODE''JFolder::create: '.JText::_('Could not create directory')'Path: '.$path);
  228.                 return false;
  229.             }
  230.  
  231.             // Reset umask
  232.             umask($origmask);
  233.         }
  234.         return $ret;
  235.     }
  236.  
  237.     /**
  238.      * Delete a folder
  239.      *
  240.      * @param string $path The path to the folder to delete
  241.      * @return boolean True on success
  242.      * @since 1.5
  243.      */
  244.     function delete($path)
  245.     {
  246.         // Sanity check
  247.         if $path {
  248.             // Bad programmer! Bad Bad programmer!
  249.             JError::raiseWarning(500'JFolder::delete: '.JText::_('Attempt to delete base directory') );
  250.             return false;
  251.         }
  252.  
  253.         // Initialize variables
  254.         jimport('joomla.client.helper');
  255.         $FTPOptions JClientHelper::getCredentials('ftp');
  256.  
  257.         // Check to make sure the path valid and clean
  258.         $path JPath::clean($path);
  259.  
  260.         // Is this really a folder?
  261.         if (!is_dir($path)) {
  262.             JError::raiseWarning(21'JFolder::delete: '.JText::_('Path is not a folder').' '.$path);
  263.             return false;
  264.         }
  265.  
  266.         // Remove all the files in folder if they exist
  267.         $files JFolder::files($path'.'falsetruearray());
  268.         if (count($files)) {
  269.             jimport('joomla.filesystem.file');
  270.             if (JFile::delete($files!== true{
  271.                 // JFile::delete throws an error
  272.                 return false;
  273.             }
  274.         }
  275.  
  276.         // Remove sub-folders of folder
  277.         $folders JFolder::folders($path'.'falsetruearray());
  278.         foreach ($folders as $folder{
  279.             if (JFolder::delete($folder!== true{
  280.                 // JFolder::delete throws an error
  281.                 return false;
  282.             }
  283.         }
  284.  
  285.         if ($FTPOptions['enabled'== 1{
  286.             // Connect the FTP client
  287.             jimport('joomla.client.ftp');
  288.             $ftp JFTP::getInstance($FTPOptions['host']$FTPOptions['port']null$FTPOptions['user']$FTPOptions['pass']);
  289.         }
  290.  
  291.         // In case of restricted permissions we zap it one way or the other
  292.         // as long as the owner is either the webserver or the ftp
  293.         if (@rmdir($path)) {
  294.             $ret true;
  295.         elseif ($FTPOptions['enabled'== 1{
  296.             // Translate path and delete
  297.             $path JPath::clean(str_replace(JPATH_ROOT$FTPOptions['root']$path)'/');
  298.             // FTP connector throws an error
  299.             $ret $ftp->delete($path);
  300.         else {
  301.             JError::raiseWarning('SOME_ERROR_CODE''JFolder::delete: '.JText::_('Could not delete folder').' '.$path);
  302.             $ret false;
  303.         }
  304.  
  305.         return $ret;
  306.     }
  307.  
  308.     /**
  309.      * Moves a folder
  310.      *
  311.      * @param string $src The path to the source folder
  312.      * @param string $dest The path to the destination folder
  313.      * @param string $path An optional base path to prefix to the file names
  314.      * @return mixed Error message on false or boolean True on success
  315.      * @since 1.5
  316.      */
  317.     function move($src$dest$path '')
  318.     {
  319.         // Initialize variables
  320.         jimport('joomla.client.helper');
  321.         $FTPOptions JClientHelper::getCredentials('ftp');
  322.  
  323.         if ($path{
  324.             $src JPath::clean($path.DS.$src);
  325.             $dest JPath::clean($path.DS.$dest);
  326.         }
  327.  
  328.         if (!JFolder::exists($src&& !is_writable($src)) {
  329.             return JText::_('Cannot find source folder');
  330.         }
  331.         if (JFolder::exists($dest)) {
  332.             return JText::_('Folder already exists');
  333.         }
  334.  
  335.         if ($FTPOptions['enabled'== 1{
  336.             // Connect the FTP client
  337.             jimport('joomla.client.ftp');
  338.             $ftp JFTP::getInstance($FTPOptions['host']$FTPOptions['port']null$FTPOptions['user']$FTPOptions['pass']);
  339.  
  340.             //Translate path for the FTP account
  341.             $src JPath::clean(str_replace(JPATH_ROOT$FTPOptions['root']$src)'/');
  342.             $dest JPath::clean(str_replace(JPATH_ROOT$FTPOptions['root']$dest)'/');
  343.  
  344.             // Use FTP rename to simulate move
  345.             if (!$ftp->rename($src$dest)) {
  346.                 return JText::_('Rename failed');
  347.             }
  348.             $ret true;
  349.         else {
  350.             if (!rename($src$dest)) {
  351.                 return JText::_('Rename failed');
  352.             }
  353.             $ret true;
  354.         }
  355.         return $ret;
  356.     }
  357.  
  358.     /**
  359.      * Wrapper for the standard file_exists function
  360.      *
  361.      * @param string $path Folder name relative to installation dir
  362.      * @return boolean True if path is a folder
  363.      * @since 1.5
  364.      */
  365.     function exists($path)
  366.     {
  367.         return is_dir(JPath::clean($path));
  368.     }
  369.  
  370.     /**
  371.      * Utility function to read the files in a folder
  372.      *
  373.      * @param    string    $path        The path of the folder to read
  374.      * @param    string    $filter        A filter for file names
  375.      * @param    mixed    $recurse    True to recursively search into sub-folders, or an integer to specify the maximum depth
  376.      * @param    boolean    $fullpath    True to return the full path to the file
  377.      * @param    array    $exclude    Array with names of files which should not be shown in the result
  378.      * @return    array    Files in the given folder
  379.      * @since 1.5
  380.      */
  381.     function files($path