Source for file folder.php
Documentation is available at folder.php
* @package Joomla.Platform
* @copyright Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE
* A Folder handling class
* @package Joomla.Platform
* @param string $src The path to the source folder.
* @param string $dest The path to the destination folder.
* @param string $path An optional base path to prefix to the file names.
* @param boolean $force Force copy.
* @param boolean $use_streams Optionally force folder/file overwrites.
* @return boolean True on success.
* @throws RuntimeException
public static function copy($src, $dest, $path =
'', $force =
false, $use_streams =
false)
// Eliminate trailing directory separators, if any
$src =
rtrim($src, DIRECTORY_SEPARATOR);
$dest =
rtrim($dest, DIRECTORY_SEPARATOR);
throw
new RuntimeException('Source folder not found', -
1);
if (self::exists($dest) &&
!$force)
throw
new RuntimeException('Destination folder not found', -
1);
// Make sure the destination exists
if (!self::create($dest))
throw
new RuntimeException('Cannot create destination folder', -
1);
// If we're using ftp and don't have streams enabled
if ($FTPOptions['enabled'] ==
1 &&
!$use_streams)
// Connect the FTP client
$ftp =
JClientFtp::getInstance($FTPOptions['host'], $FTPOptions['port'], array(), $FTPOptions['user'], $FTPOptions['pass']);
throw
new RuntimeException('Cannot open source folder', -
1);
// Walk through the directory copying files and recursing into folders.
while (($file =
readdir($dh)) !==
false)
$sfid =
$src .
'/' .
$file;
$dfid =
$dest .
'/' .
$file;
if ($file !=
'.' &&
$file !=
'..')
$ret =
self::copy($sfid, $dfid, null, $force);
// Translate path for the FTP account
if (!$ftp->store($sfid, $dfid))
throw
new RuntimeException('Copy file failed', -
1);
throw
new RuntimeException('Cannot open source folder', -
1);
// Walk through the directory copying files and recursing into folders.
while (($file =
readdir($dh)) !==
false)
$sfid =
$src .
'/' .
$file;
$dfid =
$dest .
'/' .
$file;
if ($file !=
'.' &&
$file !=
'..')
$ret =
self::copy($sfid, $dfid, null, $force, $use_streams);
if (!$stream->copy($sfid, $dfid))
throw
new RuntimeException('Cannot copy file: ' .
$stream->getError(), -
1);
if (!@copy($sfid, $dfid))
throw
new RuntimeException('Copy file failed', -
1);
* Create a folder -- and all necessary parent folders.
* @param string $path A path to create from the base path.
* @param integer $mode Directory permissions to set for folders created. 0755 by default.
* @return boolean True if successful.
public static function create($path =
'', $mode =
0755)
// Check to make sure the path valid and clean
$path =
JPath::clean($path);
// Check if parent dir exists
$parent =
dirname($path);
if (!self::exists($parent))
// Prevent infinite loops!
if (($nested >
20) ||
($parent ==
$path))
JLog::add(__METHOD__ .
': ' .
JText::_('JLIB_FILESYSTEM_ERROR_FOLDER_LOOP'), JLog::WARNING, 'jerror');
// Create the parent directory
if (self::create($parent, $mode) !==
true)
// JFolder::create throws an error
// OK, parent directory has been created
// Check if dir already exists
if ($FTPOptions['enabled'] ==
1)
// Connect the FTP client
$ftp =
JClientFtp::getInstance($FTPOptions['host'], $FTPOptions['port'], array(), $FTPOptions['user'], $FTPOptions['pass']);
// Translate path to FTP path
$ret =
$ftp->mkdir($path);
$ftp->chmod($path, $mode);
// We need to get and explode the open_basedir paths
// If open_basedir is set we need to get the open_basedir that the path is in
// Create the array of open_basedir paths
$obdArray =
explode($obdSeparator, $obd);
// Iterate through open_basedir paths looking for a match
foreach ($obdArray as $test)
if (strpos($path, $test) ===
0)
// Return false for JFolder::create because the path to be created is not in open_basedir
JLog::add(__METHOD__ .
': ' .
JText::_('JLIB_FILESYSTEM_ERROR_FOLDER_PATH'), JLog::WARNING, 'jerror');
if (!$ret =
@mkdir($path, $mode))
__METHOD__ .
': ' .
JText::_('JLIB_FILESYSTEM_ERROR_COULD_NOT_CREATE_DIRECTORY') .
'Path: ' .
$path, JLog::WARNING, 'jerror'
* @param string $path The path to the folder to delete.
* @return boolean True on success.
* @throws UnexpectedValueException
public static function delete($path)
// Bad programmer! Bad Bad programmer!
JLog::add(__METHOD__ .
': ' .
JText::_('JLIB_FILESYSTEM_ERROR_DELETE_BASE_DIRECTORY'), JLog::WARNING, 'jerror');
// Check to make sure the path valid and clean
catch
(UnexpectedValueException $e)
// Is this really a folder?
// Remove all the files in folder if they exist; disable all filtering
$files =
self::files($path, '.', false, true, array(), array());
// JFile::delete throws an error
// Remove sub-folders of folder; disable all filtering
$folders =
self::folders($path, '.', false, true, array(), array());
foreach ($folders as $folder)
// Don't descend into linked directories, just delete the link.
// JFile::delete throws an error
elseif (self::delete($folder) !==
true)
// JFolder::delete throws an error
if ($FTPOptions['enabled'] ==
1)
// Connect the FTP client
$ftp =
JClientFtp::getInstance($FTPOptions['host'], $FTPOptions['port'], array(), $FTPOptions['user'], $FTPOptions['pass']);
// In case of restricted permissions we zap it one way or the other
// as long as the owner is either the webserver or the ftp.
elseif ($FTPOptions['enabled'] ==
1)
// Translate path and delete
// FTP connector throws an error
* @param string $src The path to the source folder.
* @param string $dest The path to the destination folder.
* @param string $path An optional base path to prefix to the file names.
* @param boolean $use_streams Optionally use streams.
* @return mixed Error message on false or boolean true on success.
public static function move($src, $dest, $path =
'', $use_streams =
false)
return JText::_('JLIB_FILESYSTEM_ERROR_FIND_SOURCE_FOLDER');
return JText::_('JLIB_FILESYSTEM_ERROR_FOLDER_EXISTS');
if (!$stream->move($src, $dest))
return JText::sprintf('JLIB_FILESYSTEM_ERROR_FOLDER_RENAME', $stream->getError());
if ($FTPOptions['enabled'] ==
1)
// Connect the FTP client
$ftp =
JClientFtp::getInstance($FTPOptions['host'], $FTPOptions['port'], array(), $FTPOptions['user'], $FTPOptions['pass']);
// Translate path for the FTP account
// Use FTP rename to simulate move
if (!$ftp->rename($src, $dest))
return JText::_('Rename failed');
return JText::_('Rename failed');
* Wrapper for the standard file_exists function
* @param string $path Folder name relative to installation dir
* @return boolean True if path is a folder
public static function exists($path)
* Utility function to read the files in a folder.
* @param string $path The path of the folder to read.
* @param string $filter A filter for file names.
* @param mixed $recurse True to recursively search into sub-folders, or an integer to specify the maximum depth.
* @param boolean $full True to return the full path to the file.
* @param array $exclude Array with names of files which should not be shown in the result.
* @param array $excludefilter Array of filter to exclude
* @param boolean $naturalSort False for asort, true for natsort
* @return array Files in the given folder.
public static function files($path, $filter =
'.', $recurse =
false, $full =
false, $exclude =
array('.svn', 'CVS', '.DS_Store', '__MACOSX'),
$excludefilter =
array('^\..*', '.*~'), $naturalSort =
false)
// Check to make sure the path valid and clean
// Compute the excludefilter string
if (count($excludefilter))
$excludefilter_string =
'/(' .
implode('|', $excludefilter) .
')/';
$excludefilter_string =
'';
$arr =
self::_items($path, $filter, $recurse, $full, $exclude, $excludefilter_string, true);
// Sort the files based on either natural or alpha method
* Utility function to read the folders in a folder.
* @param string $path The path of the folder to read.
* @param string $filter A filter for folder names.
* @param mixed $recurse True to recursively search into sub-folders, or an integer to specify the maximum depth.
* @param boolean $full True to return the full path to the folders.
* @param array $exclude Array with names of folders which should not be shown in the result.
* @param array $excludefilter Array with regular expressions matching folders which should not be shown in the result.
* @return array Folders in the given folder.
public static function folders($path, $filter =
'.', $recurse =
false, $full =
false, $exclude =
array('.svn', 'CVS', '.DS_Store', '__MACOSX'),
$excludefilter =
array('^\..*'))
// Check to make sure the path valid and clean
// Compute the excludefilter string
if (count($excludefilter))
$excludefilter_string =
'/(' .
implode('|', $excludefilter) .
')/';
$excludefilter_string =
'';
$arr =
self::_items($path, $filter, $recurse, $full, $exclude, $excludefilter_string, false);
* Function to read the files/folders in a folder.
* @param string $path The path of the folder to read.
* @param string $filter A filter for file names.
* @param mixed $recurse True to recursively search into sub-folders, or an integer to specify the maximum depth.
* @param boolean $full True to return the full path to the file.
* @param array $exclude Array with names of files which should not be shown in the result.
* @param string $excludefilter_string Regexp of files to exclude
* @param boolean $findfiles True to read the files, false to read the folders
protected static function _items($path, $filter, $recurse, $full, $exclude, $excludefilter_string, $findfiles)
// Read the source directory
while (($file =
readdir($handle)) !==
false)
if ($file !=
'.' &&
$file !=
'..' &&
!in_array($file, $exclude)
&&
(empty($excludefilter_string) ||
!preg_match($excludefilter_string, $file)))
$fullpath =
$path .
'/' .
$file;
// Compute the isDir flag
if (($isDir xor $findfiles) &&
preg_match("/$filter/", $file))
// (fullpath is dir and folders are searched or fullpath is not dir and files are searched) and file matches the filter
// Full path is requested
// Until depth 0 is reached
$arr =
array_merge($arr, self::_items($fullpath, $filter, $recurse -
1, $full, $exclude, $excludefilter_string, $findfiles));
$arr =
array_merge($arr, self::_items($fullpath, $filter, $recurse, $full, $exclude, $excludefilter_string, $findfiles));
* Lists folder in format suitable for tree display.
* @param string $path The path of the folder to read.
* @param string $filter A filter for folder names.
* @param integer $maxLevel The maximum number of levels to recursively read, defaults to three.
* @param integer $level The current level, optional.
* @param integer $parent Unique identifier of the parent folder, if any.
* @return array Folders in the given folder.
public static function listFolderTree($path, $filter, $maxLevel =
3, $level =
0, $parent =
0)
$GLOBALS['_JFolder_folder_tree_index'] =
0;
$folders =
self::folders($path, $filter);
// First path, index foldernames
foreach ($folders as $name)
$id = ++
$GLOBALS['_JFolder_folder_tree_index'];
$dirs[] =
array('id' =>
$id, 'parent' =>
$parent, 'name' =>
$name, 'fullname' =>
$fullName,
$dirs2 =
self::listFolderTree($fullName, $filter, $maxLevel, $level +
1, $id);
* Makes path name safe to use.
* @param string $path The full path to sanitise.
* @return string The sanitised string.
$regex =
array('#[^A-Za-z0-9_\\\/\(\)\[\]\{\}\#\$\^\+\.\'~`!@&=;,-]#');
Documentation generated on Tue, 19 Nov 2013 15:03:26 +0100 by phpDocumentor 1.4.3