AAAAfolder.php 0000666 00000044422 15137272116 0006547 0 ustar 00 store($sfid, $dfid)) { return JError::raiseError(-1, JText::_('JLIB_FILESYSTEM_ERROR_COPY_FAILED')); } break; } } } else { if (!($dh = @opendir($src))) { return JError::raiseError(-1, JText::_('JLIB_FILESYSTEM_ERROR_FOLDER_OPEN')); } // Walk through the directory copying files and recursing into folders. while (($file = readdir($dh)) !== false) { $sfid = $src . '/' . $file; $dfid = $dest . '/' . $file; switch (filetype($sfid)) { case 'dir': if ($file != '.' && $file != '..') { $ret = self::copy($sfid, $dfid, null, $force, $use_streams); if ($ret !== true) { return $ret; } } break; case 'file': if ($use_streams) { $stream = JFactory::getStream(); if (!$stream->copy($sfid, $dfid)) { return JError::raiseError(-1, JText::_('JLIB_FILESYSTEM_ERROR_COPY_FAILED') . ': ' . $stream->getError()); } } else { if (!@copy($sfid, $dfid)) { return JError::raiseError(-1, JText::_('JLIB_FILESYSTEM_ERROR_COPY_FAILED')); } } break; } } } return true; } /** * 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. * * @since 11.1 */ public static function create($path = '', $mode = 0755) { // Initialise variables. $FTPOptions = JClientHelper::getCredentials('ftp'); static $nested = 0; // 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! $nested++; if (($nested > 20) || ($parent == $path)) { JError::raiseWarning('SOME_ERROR_CODE', __METHOD__ . ': ' . JText::_('JLIB_FILESYSTEM_ERROR_FOLDER_LOOP')); $nested--; return false; } // Create the parent directory if (self::create($parent, $mode) !== true) { // JFolder::create throws an error $nested--; return false; } // OK, parent directory has been created $nested--; } // Check if dir already exists if (self::exists($path)) { return true; } // Check for safe mode if ($FTPOptions['enabled'] == 1) { // Connect the FTP client jimport('joomla.client.ftp'); $ftp = JFTP::getInstance($FTPOptions['host'], $FTPOptions['port'], null, $FTPOptions['user'], $FTPOptions['pass']); // Translate path to FTP path $path = JPath::clean(str_replace(JPATH_ROOT, $FTPOptions['root'], $path), '/'); $ret = $ftp->mkdir($path); $ftp->chmod($path, $mode); } else { // We need to get and explode the open_basedir paths $obd = ini_get('open_basedir'); // If open_basedir is set we need to get the open_basedir that the path is in if ($obd != null) { if (JPATH_ISWIN) { $obdSeparator = ";"; } else { $obdSeparator = ":"; } // Create the array of open_basedir paths $obdArray = explode($obdSeparator, $obd); $inBaseDir = false; // Iterate through open_basedir paths looking for a match foreach ($obdArray as $test) { $test = JPath::clean($test); if (strpos($path, $test) === 0) { $inBaseDir = true; break; } } if ($inBaseDir == false) { // Return false for JFolder::create because the path to be created is not in open_basedir JError::raiseWarning('SOME_ERROR_CODE', __METHOD__ . ': ' . JText::_('JLIB_FILESYSTEM_ERROR_FOLDER_PATH')); return false; } } // First set umask $origmask = @umask(0); // Create the path if (!$ret = @mkdir($path, $mode)) { @umask($origmask); JError::raiseWarning( 'SOME_ERROR_CODE', __METHOD__ . ': ' . JText::_('JLIB_FILESYSTEM_ERROR_COULD_NOT_CREATE_DIRECTORY'). ' Path: ' . $path ); return false; } // Reset umask @umask($origmask); } return $ret; } /** * Delete a folder. * * @param string $path The path to the folder to delete. * * @return boolean True on success. * * @since 11.1 */ public static function delete($path) { @set_time_limit(ini_get('max_execution_time')); // Sanity check if (!$path) { // Bad programmer! Bad Bad programmer! JError::raiseWarning(500, __METHOD__ . ': ' . JText::_('JLIB_FILESYSTEM_ERROR_DELETE_BASE_DIRECTORY')); return false; } // Initialise variables. $FTPOptions = JClientHelper::getCredentials('ftp'); try { // Check to make sure the path valid and clean $path = JPath::clean($path); } catch (UnexpectedValueException $e) { throw new UnexpectedValueException($e); } // Is this really a folder? if (!is_dir($path)) { JError::raiseWarning(21, JText::sprintf('JLIB_FILESYSTEM_ERROR_PATH_IS_NOT_A_FOLDER', $path)); return false; } // Remove all the files in folder if they exist; disable all filtering $files = self::files($path, '.', false, true, array(), array()); if (!empty($files)) { jimport('joomla.filesystem.file'); if (JFile::delete($files) !== true) { // JFile::delete throws an error return false; } } // Remove sub-folders of folder; disable all filtering $folders = self::folders($path, '.', false, true, array(), array()); foreach ($folders as $folder) { if (is_link($folder)) { // Don't descend into linked directories, just delete the link. jimport('joomla.filesystem.file'); if (JFile::delete($folder) !== true) { // JFile::delete throws an error return false; } } elseif (self::delete($folder) !== true) { // JFolder::delete throws an error return false; } } if ($FTPOptions['enabled'] == 1) { // Connect the FTP client jimport('joomla.client.ftp'); $ftp = JFTP::getInstance($FTPOptions['host'], $FTPOptions['port'], null, $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. if (@rmdir($path)) { $ret = true; } elseif ($FTPOptions['enabled'] == 1) { // Translate path and delete $path = JPath::clean(str_replace(JPATH_ROOT, $FTPOptions['root'], $path), '/'); // FTP connector throws an error $ret = $ftp->delete($path); } else { JError::raiseWarning('SOME_ERROR_CODE', JText::sprintf('JLIB_FILESYSTEM_ERROR_FOLDER_DELETE', $path)); $ret = false; } return $ret; } /** * Moves a folder. * * @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. * * @since 11.1 */ public static function move($src, $dest, $path = '', $use_streams = false) { // Initialise variables. $FTPOptions = JClientHelper::getCredentials('ftp'); if ($path) { $src = JPath::clean($path . '/' . $src); $dest = JPath::clean($path . '/' . $dest); } if (!self::exists($src)) { return JText::_('JLIB_FILESYSTEM_ERROR_FIND_SOURCE_FOLDER'); } if (self::exists($dest)) { return JText::_('JLIB_FILESYSTEM_ERROR_FOLDER_EXISTS'); } if ($use_streams) { $stream = JFactory::getStream(); if (!$stream->move($src, $dest)) { return JText::sprintf('JLIB_FILESYSTEM_ERROR_FOLDER_RENAME', $stream->getError()); } $ret = true; } else { if ($FTPOptions['enabled'] == 1) { // Connect the FTP client jimport('joomla.client.ftp'); $ftp = JFTP::getInstance($FTPOptions['host'], $FTPOptions['port'], null, $FTPOptions['user'], $FTPOptions['pass']); //Translate path for the FTP account $src = JPath::clean(str_replace(JPATH_ROOT, $FTPOptions['root'], $src), '/'); $dest = JPath::clean(str_replace(JPATH_ROOT, $FTPOptions['root'], $dest), '/'); // Use FTP rename to simulate move if (!$ftp->rename($src, $dest)) { return JText::_('Rename failed'); } $ret = true; } else { if (!@rename($src, $dest)) { return JText::_('Rename failed'); } $ret = true; } } return $ret; } /** * Wrapper for the standard file_exists function * * @param string $path Folder name relative to installation dir * * @return boolean True if path is a folder * * @since 11.1 */ public static function exists($path) { return is_dir(JPath::clean($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. * * @since 11.1 */ 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 $path = JPath::clean($path); // Is the path a folder? if (!is_dir($path)) { JError::raiseWarning(21, JText::sprintf('JLIB_FILESYSTEM_ERROR_PATH_IS_NOT_A_FOLDER_FILES', $path)); return false; } // Compute the excludefilter string if (count($excludefilter)) { $excludefilter_string = '/(' . implode('|', $excludefilter) . ')/'; } else { $excludefilter_string = ''; } // Get the files $arr = self::_items($path, $filter, $recurse, $full, $exclude, $excludefilter_string, true); // Sort the files based on either natural or alpha method if ($naturalSort) { natsort($arr); } else { asort($arr); } return array_values($arr); } /** * 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. * * @since 11.1 */ 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 $path = JPath::clean($path); // Is the path a folder? if (!is_dir($path)) { JError::raiseWarning(21, JText::sprintf('JLIB_FILESYSTEM_ERROR_PATH_IS_NOT_A_FOLDER_FOLDER', $path)); return false; } // Compute the excludefilter string if (count($excludefilter)) { $excludefilter_string = '/(' . implode('|', $excludefilter) . ')/'; } else { $excludefilter_string = ''; } // Get the folders $arr = self::_items($path, $filter, $recurse, $full, $exclude, $excludefilter_string, false); // Sort the folders asort($arr); return array_values($arr); } /** * 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 * * @return array Files. * * @since 11.1 */ protected static function _items($path, $filter, $recurse, $full, $exclude, $excludefilter_string, $findfiles) { @set_time_limit(ini_get('max_execution_time')); // Initialise variables. $arr = array(); // Read the source directory if (!($handle = @opendir($path))) { return $arr; } while (($file = readdir($handle)) !== false) { if ($file != '.' && $file != '..' && !in_array($file, $exclude) && (empty($excludefilter_string) || !preg_match($excludefilter_string, $file))) { // Compute the fullpath $fullpath = $path . DIRECTORY_SEPARATOR . $file; // Compute the isDir flag $isDir = is_dir($fullpath); 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 if ($full) { // Full path is requested $arr[] = $fullpath; } else { // Filename is requested $arr[] = $file; } } if ($isDir && $recurse) { // Search recursively if (is_integer($recurse)) { // Until depth 0 is reached $arr = array_merge($arr, self::_items($fullpath, $filter, $recurse - 1, $full, $exclude, $excludefilter_string, $findfiles)); } else { $arr = array_merge($arr, self::_items($fullpath, $filter, $recurse, $full, $exclude, $excludefilter_string, $findfiles)); } } } } closedir($handle); return $arr; } /** * 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. * * @since 11.1 */ public static function listFolderTree($path, $filter, $maxLevel = 3, $level = 0, $parent = 0) { $dirs = array(); if ($level == 0) { $GLOBALS['_JFolder_folder_tree_index'] = 0; } if ($level < $maxLevel) { $folders = self::folders($path, $filter); // First path, index foldernames foreach ($folders as $name) { $id = ++$GLOBALS['_JFolder_folder_tree_index']; $fullName = JPath::clean($path . '/' . $name); $dirs[] = array('id' => $id, 'parent' => $parent, 'name' => $name, 'fullname' => $fullName, 'relname' => str_replace(JPATH_ROOT, '', $fullName)); $dirs2 = self::listFolderTree($fullName, $filter, $maxLevel, $level + 1, $id); $dirs = array_merge($dirs, $dirs2); } } return $dirs; } /** * Makes path name safe to use. * * @param string $path The full path to sanitise. * * @return string The sanitised string. * * @since 11.1 */ public static function makeSafe($path) { $regex = array('#[^A-Za-z0-9:_\\\/-]#'); return preg_replace($regex, '', $path); } } file.php 0000666 00000030667 15137272116 0006221 0 ustar 00 copy($src, $dest)) { JError::raiseWarning(21, JText::sprintf('JLIB_FILESYSTEM_ERROR_JFILE_STREAMS', $src, $dest, $stream->getError())); return false; } return true; } else { // Initialise variables. $FTPOptions = JClientHelper::getCredentials('ftp'); if ($FTPOptions['enabled'] == 1) { // Connect the FTP client jimport('joomla.client.ftp'); $ftp = JFTP::getInstance($FTPOptions['host'], $FTPOptions['port'], null, $FTPOptions['user'], $FTPOptions['pass']); // If the parent folder doesn't exist we must create it if (!file_exists(dirname($dest))) { jimport('joomla.filesystem.folder'); JFolder::create(dirname($dest)); } // Translate the destination path for the FTP account $dest = JPath::clean(str_replace(JPATH_ROOT, $FTPOptions['root'], $dest), '/'); if (!$ftp->store($src, $dest)) { // FTP connector throws an error return false; } $ret = true; } else { if (!@ copy($src, $dest)) { JError::raiseWarning(21, JText::_('JLIB_FILESYSTEM_ERROR_COPY_FAILED')); return false; } $ret = true; } return $ret; } } /** * Delete a file or array of files * * @param mixed $file The file name or an array of file names * * @return boolean True on success * * @since 11.1 */ public static function delete($file) { // Initialise variables. jimport('joomla.client.helper'); $FTPOptions = JClientHelper::getCredentials('ftp'); if (is_array($file)) { $files = $file; } else { $files[] = $file; } // Do NOT use ftp if it is not enabled if ($FTPOptions['enabled'] == 1) { // Connect the FTP client jimport('joomla.client.ftp'); $ftp = JFTP::getInstance($FTPOptions['host'], $FTPOptions['port'], null, $FTPOptions['user'], $FTPOptions['pass']); } foreach ($files as $file) { $file = JPath::clean($file); if (!is_file($file)) { continue; } // Try making the file writable first. If it's read-only, it can't be deleted // on Windows, even if the parent folder is writable @chmod($file, 0777); // 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 if (@unlink($file)) { // Do nothing } elseif ($FTPOptions['enabled'] == 1) { $file = JPath::clean(str_replace(JPATH_ROOT, $FTPOptions['root'], $file), '/'); if (!$ftp->delete($file)) { // FTP connector throws an error return false; } } else { $filename = basename($file); JError::raiseWarning('SOME_ERROR_CODE', JText::sprintf('JLIB_FILESYSTEM_DELETE_FAILED', $filename)); return false; } } return true; } /** * Moves a file * * @param string $src The path to the source file * @param string $dest The path to the destination file * @param string $path An optional base path to prefix to the file names * @param boolean $use_streams True to use streams * * @return boolean True on success * * @since 11.1 */ public static function move($src, $dest, $path = '', $use_streams = false) { if ($path) { $src = JPath::clean($path . '/' . $src); $dest = JPath::clean($path . '/' . $dest); } // Check src path if (!is_readable($src)) { return JText::_('JLIB_FILESYSTEM_CANNOT_FIND_SOURCE_FILE'); } if ($use_streams) { $stream = JFactory::getStream(); if (!$stream->move($src, $dest)) { JError::raiseWarning(21, JText::sprintf('JLIB_FILESYSTEM_ERROR_JFILE_MOVE_STREAMS', $stream->getError())); return false; } return true; } else { // Initialise variables. jimport('joomla.client.helper'); $FTPOptions = JClientHelper::getCredentials('ftp'); if ($FTPOptions['enabled'] == 1) { // Connect the FTP client jimport('joomla.client.ftp'); $ftp = JFTP::getInstance($FTPOptions['host'], $FTPOptions['port'], null, $FTPOptions['user'], $FTPOptions['pass']); // Translate path for the FTP account $src = JPath::clean(str_replace(JPATH_ROOT, $FTPOptions['root'], $src), '/'); $dest = JPath::clean(str_replace(JPATH_ROOT, $FTPOptions['root'], $dest), '/'); // Use FTP rename to simulate move if (!$ftp->rename($src, $dest)) { JError::raiseWarning(21, JText::_('JLIB_FILESYSTEM_ERROR_RENAME_FILE')); return false; } } else { if (!@ rename($src, $dest)) { JError::raiseWarning(21, JText::_('JLIB_FILESYSTEM_ERROR_RENAME_FILE')); return false; } } return true; } } /** * Read the contents of a file * * @param string $filename The full file path * @param boolean $incpath Use include path * @param integer $amount Amount of file to read * @param integer $chunksize Size of chunks to read * @param integer $offset Offset of the file * * @return mixed Returns file contents or boolean False if failed * * @since 11.1 */ public static function read($filename, $incpath = false, $amount = 0, $chunksize = 8192, $offset = 0) { // Initialise variables. $data = null; if ($amount && $chunksize > $amount) { $chunksize = $amount; } if (false === $fh = fopen($filename, 'rb', $incpath)) { JError::raiseWarning(21, JText::sprintf('JLIB_FILESYSTEM_ERROR_READ_UNABLE_TO_OPEN_FILE', $filename)); return false; } clearstatcache(); if ($offset) { fseek($fh, $offset); } if ($fsize = @ filesize($filename)) { if ($amount && $fsize > $amount) { $data = fread($fh, $amount); } else { $data = fread($fh, $fsize); } } else { $data = ''; // While it's: // 1: Not the end of the file AND // 2a: No Max Amount set OR // 2b: The length of the data is less than the max amount we want while (!feof($fh) && (!$amount || strlen($data) < $amount)) { $data .= fread($fh, $chunksize); } } fclose($fh); return $data; } /** * Write contents to a file * * @param string $file The full file path * @param string &$buffer The buffer to write * @param boolean $use_streams Use streams * * @return boolean True on success * * @since 11.1 */ public static function write($file, &$buffer, $use_streams = false) { @set_time_limit(ini_get('max_execution_time')); // If the destination directory doesn't exist we need to create it if (!file_exists(dirname($file))) { jimport('joomla.filesystem.folder'); JFolder::create(dirname($file)); } if ($use_streams) { $stream = JFactory::getStream(); // Beef up the chunk size to a meg $stream->set('chunksize', (1024 * 1024 * 1024)); if (!$stream->writeFile($file, $buffer)) { JError::raiseWarning(21, JText::sprintf('JLIB_FILESYSTEM_ERROR_WRITE_STREAMS', $file, $stream->getError())); return false; } return true; } else { // Initialise variables. $FTPOptions = JClientHelper::getCredentials('ftp'); if ($FTPOptions['enabled'] == 1) { // Connect the FTP client jimport('joomla.client.ftp'); $ftp = JFTP::getInstance($FTPOptions['host'], $FTPOptions['port'], null, $FTPOptions['user'], $FTPOptions['pass']); // Translate path for the FTP account and use FTP write buffer to file $file = JPath::clean(str_replace(JPATH_ROOT, $FTPOptions['root'], $file), '/'); $ret = $ftp->write($file, $buffer); } else { $file = JPath::clean($file); $ret = is_int(file_put_contents($file, $buffer)) ? true : false; } return $ret; } } /** * Moves an uploaded file to a destination folder * * @param string $src The name of the php (temporary) uploaded file * @param string $dest The path (including filename) to move the uploaded file to * @param boolean $use_streams True to use streams * * @return boolean True on success * * @since 11.1 */ public static function upload($src, $dest, $use_streams = false) { // Ensure that the path is valid and clean $dest = JPath::clean($dest); // Create the destination directory if it does not exist $baseDir = dirname($dest); if (!file_exists($baseDir)) { jimport('joomla.filesystem.folder'); JFolder::create($baseDir); } if ($use_streams) { $stream = JFactory::getStream(); if (!$stream->upload($src, $dest)) { JError::raiseWarning(21, JText::sprintf('JLIB_FILESYSTEM_ERROR_UPLOAD', $stream->getError())); return false; } return true; } else { // Initialise variables. $FTPOptions = JClientHelper::getCredentials('ftp'); $ret = false; if ($FTPOptions['enabled'] == 1) { // Connect the FTP client jimport('joomla.client.ftp'); $ftp = JFTP::getInstance($FTPOptions['host'], $FTPOptions['port'], null, $FTPOptions['user'], $FTPOptions['pass']); // Translate path for the FTP account $dest = JPath::clean(str_replace(JPATH_ROOT, $FTPOptions['root'], $dest), '/'); // Copy the file to the destination directory if (is_uploaded_file($src) && $ftp->store($src, $dest)) { unlink($src); $ret = true; } else { JError::raiseWarning(21, JText::_('JLIB_FILESYSTEM_ERROR_WARNFS_ERR02')); } } else { if (is_writeable($baseDir) && move_uploaded_file($src, $dest)) { // Short circuit to prevent file permission errors if (JPath::setPermissions($dest)) { $ret = true; } else { JError::raiseWarning(21, JText::_('JLIB_FILESYSTEM_ERROR_WARNFS_ERR01')); } } else { JError::raiseWarning(21, JText::_('JLIB_FILESYSTEM_ERROR_WARNFS_ERR02')); } } return $ret; } } /** * Wrapper for the standard file_exists function * * @param string $file File path * * @return boolean True if path is a file * * @since 11.1 */ public static function exists($file) { return is_file(JPath::clean($file)); } /** * Returns the name, without any path. * * @param string $file File path * * @return string filename * * @since 11.1 */ public static function getName($file) { // Convert back slashes to forward slashes $file = str_replace('\\', '/', $file); $slash = strrpos($file, '/'); if ($slash !== false) { return substr($file, $slash + 1); } else { return $file; } } } index.html 0000666 00000000037 15137272116 0006552 0 ustar 00