Skip to content

Commit

Permalink
Use the new wrapper classes in the filesystem package
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Babker committed Jan 21, 2015
1 parent 01848e0 commit 7ef467b
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 52 deletions.
53 changes: 30 additions & 23 deletions libraries/joomla/filesystem/file.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@

defined('JPATH_PLATFORM') or die;

jimport('joomla.filesystem.path');

/**
* A File handling class
*
Expand Down Expand Up @@ -81,11 +79,13 @@ public static function makeSafe($file)
*/
public static function copy($src, $dest, $path = null, $use_streams = false)
{
$pathObject = new JFilesystemWrapperPath;

// Prepend a base path if it exists
if ($path)
{
$src = JPath::clean($path . '/' . $src);
$dest = JPath::clean($path . '/' . $dest);
$src = $pathObject->clean($path . '/' . $src);
$dest = $pathObject->clean($path . '/' . $dest);
}

// Check src path
Expand Down Expand Up @@ -121,12 +121,12 @@ public static function copy($src, $dest, $path = null, $use_streams = false)
// If the parent folder doesn't exist we must create it
if (!file_exists(dirname($dest)))
{
jimport('joomla.filesystem.folder');
JFolder::create(dirname($dest));
$folderObject = new JFilesystemWrapperFolder;
$folderObject->create(dirname($dest));
}

// Translate the destination path for the FTP account
$dest = JPath::clean(str_replace(JPATH_ROOT, $FTPOptions['root'], $dest), '/');
$dest = $pathObject->clean(str_replace(JPATH_ROOT, $FTPOptions['root'], $dest), '/');

if (!$ftp->store($src, $dest))
{
Expand Down Expand Up @@ -164,6 +164,7 @@ public static function copy($src, $dest, $path = null, $use_streams = false)
public static function delete($file)
{
$FTPOptions = JClientHelper::getCredentials('ftp');
$pathObject = new JFilesystemWrapperPath;

if (is_array($file))
{
Expand All @@ -183,7 +184,7 @@ public static function delete($file)

foreach ($files as $file)
{
$file = JPath::clean($file);
$file = $pathObject->clean($file);

if (!is_file($file))
{
Expand All @@ -202,7 +203,7 @@ public static function delete($file)
}
elseif ($FTPOptions['enabled'] == 1)
{
$file = JPath::clean(str_replace(JPATH_ROOT, $FTPOptions['root'], $file), '/');
$file = $pathObject->clean(str_replace(JPATH_ROOT, $FTPOptions['root'], $file), '/');

if (!$ftp->delete($file))
{
Expand Down Expand Up @@ -237,10 +238,12 @@ public static function delete($file)
*/
public static function move($src, $dest, $path = '', $use_streams = false)
{
$pathObject = new JFilesystemWrapperPath;

if ($path)
{
$src = JPath::clean($path . '/' . $src);
$dest = JPath::clean($path . '/' . $dest);
$src = $pathObject->clean($path . '/' . $src);
$dest = $pathObject->clean($path . '/' . $dest);
}

// Check src path
Expand Down Expand Up @@ -272,8 +275,8 @@ public static function move($src, $dest, $path = '', $use_streams = false)
$ftp = JClientFtp::getInstance($FTPOptions['host'], $FTPOptions['port'], array(), $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), '/');
$src = $pathObject->clean(str_replace(JPATH_ROOT, $FTPOptions['root'], $src), '/');
$dest = $pathObject->clean(str_replace(JPATH_ROOT, $FTPOptions['root'], $dest), '/');

// Use FTP rename to simulate move
if (!$ftp->rename($src, $dest))
Expand Down Expand Up @@ -386,9 +389,9 @@ public static function write($file, &$buffer, $use_streams = false)
// If the destination directory doesn't exist we need to create it
if (!file_exists(dirname($file)))
{
jimport('joomla.filesystem.folder');
$folderObject = new JFilesystemWrapperFolder;

if (JFolder::create(dirname($file)) == false)
if ($folderObject->create(dirname($file)) == false)
{
return false;
}
Expand All @@ -413,19 +416,20 @@ public static function write($file, &$buffer, $use_streams = false)
else
{
$FTPOptions = JClientHelper::getCredentials('ftp');
$pathObject = new JFilesystemWrapperPath;

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 and use FTP write buffer to file
$file = JPath::clean(str_replace(JPATH_ROOT, $FTPOptions['root'], $file), '/');
$file = $pathObject->clean(str_replace(JPATH_ROOT, $FTPOptions['root'], $file), '/');
$ret = $ftp->write($file, $buffer);
}
else
{
$file = JPath::clean($file);
$file = $pathObject->clean($file);
$ret = is_int(file_put_contents($file, $buffer)) ? true : false;
}

Expand All @@ -447,15 +451,16 @@ public static function write($file, &$buffer, $use_streams = false)
public static function upload($src, $dest, $use_streams = false)
{
// Ensure that the path is valid and clean
$dest = JPath::clean($dest);
$pathObject = new JFilesystemWrapperPath;
$dest = $pathObject->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);
$folderObject = new JFilesystemWrapperFolder;
$folderObject->create($baseDir);
}

if ($use_streams)
Expand All @@ -482,7 +487,7 @@ public static function upload($src, $dest, $use_streams = false)
$ftp = JClientFtp::getInstance($FTPOptions['host'], $FTPOptions['port'], array(), $FTPOptions['user'], $FTPOptions['pass']);

// Translate path for the FTP account
$dest = JPath::clean(str_replace(JPATH_ROOT, $FTPOptions['root'], $dest), '/');
$dest = $pathObject->clean(str_replace(JPATH_ROOT, $FTPOptions['root'], $dest), '/');

// Copy the file to the destination directory
if (is_uploaded_file($src) && $ftp->store($src, $dest))
Expand All @@ -500,7 +505,7 @@ public static function upload($src, $dest, $use_streams = false)
if (is_writeable($baseDir) && move_uploaded_file($src, $dest))
{
// Short circuit to prevent file permission errors
if (JPath::setPermissions($dest))
if ($pathObject->setPermissions($dest))
{
$ret = true;
}
Expand Down Expand Up @@ -530,7 +535,9 @@ public static function upload($src, $dest, $use_streams = false)
*/
public static function exists($file)
{
return is_file(JPath::clean($file));
$pathObject = new JFilesystemWrapperPath;

return is_file($pathObject->clean($file));
}

/**
Expand Down
53 changes: 30 additions & 23 deletions libraries/joomla/filesystem/folder.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@

defined('JPATH_PLATFORM') or die;

jimport('joomla.filesystem.path');

/**
* A Folder handling class
*
Expand All @@ -37,11 +35,12 @@ public static function copy($src, $dest, $path = '', $force = false, $use_stream
@set_time_limit(ini_get('max_execution_time'));

$FTPOptions = JClientHelper::getCredentials('ftp');
$pathObject = new JFilesystemWrapperPath;

if ($path)
{
$src = JPath::clean($path . '/' . $src);
$dest = JPath::clean($path . '/' . $dest);
$src = $pathObject->clean($path . '/' . $src);
$dest = $pathObject->clean($path . '/' . $dest);
}

// Eliminate trailing directory separators, if any
Expand Down Expand Up @@ -96,7 +95,7 @@ public static function copy($src, $dest, $path = '', $force = false, $use_stream

case 'file':
// Translate path for the FTP account
$dfid = JPath::clean(str_replace(JPATH_ROOT, $FTPOptions['root'], $dfid), '/');
$dfid = $pathObject->clean(str_replace(JPATH_ROOT, $FTPOptions['root'], $dfid), '/');

if (!$ftp->store($sfid, $dfid))
{
Expand Down Expand Up @@ -173,7 +172,8 @@ public static function create($path = '', $mode = 0755)
static $nested = 0;

// Check to make sure the path valid and clean
$path = JPath::clean($path);
$pathObject = new JFilesystemWrapperPath;
$path = $pathObject->clean($path);

// Check if parent dir exists
$parent = dirname($path);
Expand Down Expand Up @@ -217,7 +217,7 @@ public static function create($path = '', $mode = 0755)
$ftp = JClientFtp::getInstance($FTPOptions['host'], $FTPOptions['port'], array(), $FTPOptions['user'], $FTPOptions['pass']);

// Translate path to FTP path
$path = JPath::clean(str_replace(JPATH_ROOT, $FTPOptions['root'], $path), '/');
$path = $pathObject->clean(str_replace(JPATH_ROOT, $FTPOptions['root'], $path), '/');
$ret = $ftp->mkdir($path);
$ftp->chmod($path, $mode);
}
Expand Down Expand Up @@ -245,7 +245,7 @@ public static function create($path = '', $mode = 0755)
// Iterate through open_basedir paths looking for a match
foreach ($obdArray as $test)
{
$test = JPath::clean($test);
$test = $pathObject->clean($test);

if (strpos($path, $test) === 0)
{
Expand Down Expand Up @@ -297,6 +297,7 @@ public static function create($path = '', $mode = 0755)
public static function delete($path)
{
@set_time_limit(ini_get('max_execution_time'));
$pathObject = new JFilesystemWrapperPath;

// Sanity check
if (!$path)
Expand All @@ -312,7 +313,7 @@ public static function delete($path)
try
{
// Check to make sure the path valid and clean
$path = JPath::clean($path);
$path = $pathObject->clean($path);
}
catch (UnexpectedValueException $e)
{
Expand All @@ -332,9 +333,9 @@ public static function delete($path)

if (!empty($files))
{
jimport('joomla.filesystem.file');
$file = new JFilesystemWrapperFile;

if (JFile::delete($files) !== true)
if ($file->delete($files) !== true)
{
// JFile::delete throws an error
return false;
Expand All @@ -349,9 +350,9 @@ public static function delete($path)
if (is_link($folder))
{
// Don't descend into linked directories, just delete the link.
jimport('joomla.filesystem.file');
$file = new JFilesystemWrapperFile;

if (JFile::delete($folder) !== true)
if ($file->delete($folder) !== true)
{
// JFile::delete throws an error
return false;
Expand Down Expand Up @@ -379,7 +380,7 @@ public static function delete($path)
elseif ($FTPOptions['enabled'] == 1)
{
// Translate path and delete
$path = JPath::clean(str_replace(JPATH_ROOT, $FTPOptions['root'], $path), '/');
$path = $pathObject->clean(str_replace(JPATH_ROOT, $FTPOptions['root'], $path), '/');

// FTP connector throws an error
$ret = $ftp->delete($path);
Expand Down Expand Up @@ -408,11 +409,12 @@ public static function delete($path)
public static function move($src, $dest, $path = '', $use_streams = false)
{
$FTPOptions = JClientHelper::getCredentials('ftp');
$pathObject = new JFilesystemWrapperPath;

if ($path)
{
$src = JPath::clean($path . '/' . $src);
$dest = JPath::clean($path . '/' . $dest);
$src = $pathObject->clean($path . '/' . $src);
$dest = $pathObject->clean($path . '/' . $dest);
}

if (!self::exists($src))
Expand Down Expand Up @@ -444,8 +446,8 @@ public static function move($src, $dest, $path = '', $use_streams = false)
$ftp = JClientFtp::getInstance($FTPOptions['host'], $FTPOptions['port'], array(), $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), '/');
$src = $pathObject->clean(str_replace(JPATH_ROOT, $FTPOptions['root'], $src), '/');
$dest = $pathObject->clean(str_replace(JPATH_ROOT, $FTPOptions['root'], $dest), '/');

// Use FTP rename to simulate move
if (!$ftp->rename($src, $dest))
Expand Down Expand Up @@ -480,7 +482,9 @@ public static function move($src, $dest, $path = '', $use_streams = false)
*/
public static function exists($path)
{
return is_dir(JPath::clean($path));
$pathObject = new JFilesystemWrapperPath;

return is_dir($pathObject->clean($path));
}

/**
Expand All @@ -502,7 +506,8 @@ public static function files($path, $filter = '.', $recurse = false, $full = fal
$excludefilter = array('^\..*', '.*~'), $naturalSort = false)
{
// Check to make sure the path valid and clean
$path = JPath::clean($path);
$pathObject = new JFilesystemWrapperPath;
$path = $pathObject->clean($path);

// Is the path a folder?
if (!is_dir($path))
Expand Down Expand Up @@ -556,7 +561,8 @@ public static function folders($path, $filter = '.', $recurse = false, $full = f
$excludefilter = array('^\..*'))
{
// Check to make sure the path valid and clean
$path = JPath::clean($path);
$pathObject = new JFilesystemWrapperPath;
$path = $pathObject->clean($path);

// Is the path a folder?
if (!is_dir($path))
Expand Down Expand Up @@ -683,13 +689,14 @@ public static function listFolderTree($path, $filter, $maxLevel = 3, $level = 0,

if ($level < $maxLevel)
{
$folders = self::folders($path, $filter);
$folders = self::folders($path, $filter);
$pathObject = new JFilesystemWrapperPath;

// First path, index foldernames
foreach ($folders as $name)
{
$id = ++$GLOBALS['_JFolder_folder_tree_index'];
$fullName = JPath::clean($path . '/' . $name);
$fullName = $pathObject->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);
Expand Down
7 changes: 4 additions & 3 deletions libraries/joomla/filesystem/path.php
Original file line number Diff line number Diff line change
Expand Up @@ -242,17 +242,18 @@ public static function isOwner($path)

if ($dir)
{
$test = $dir . '/' . $tmp;
$fileObject = new JFilesystemWrapperFile;
$test = $dir . '/' . $tmp;

// Create the test file
$blank = '';
JFile::write($test, $blank, false);
$fileObject->write($test, $blank, false);

// Test ownership
$return = (fileowner($test) == fileowner($path));

// Delete the test file
JFile::delete($test);
$fileObject->delete($test);

return $return;
}
Expand Down
4 changes: 3 additions & 1 deletion libraries/joomla/filesystem/wrapper/file.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@

defined('JPATH_PLATFORM') or die;

jimport('joomla.filesystem.file');

/**
* Wrapper class for JFilesystemFile
* Wrapper class for JFile
*
* @package Joomla.Platform
* @subpackage Filesystem
Expand Down

0 comments on commit 7ef467b

Please sign in to comment.