10 defined(
'JPATH_PLATFORM') or die;
12 jimport('joomla.filesystem.path');
37 public static function copy($src, $dest, $path =
'', $force =
false, $use_streams =
false)
39 @set_time_limit(ini_get(
'max_execution_time'));
50 $src = rtrim($src, DIRECTORY_SEPARATOR);
51 $dest = rtrim($dest, DIRECTORY_SEPARATOR);
53 if (!self::exists($src))
55 throw new RuntimeException(
'Source folder not found', -1);
57 if (self::exists($dest) && !$force)
59 throw new RuntimeException(
'Destination folder not found', -1);
63 if (!self::create($dest))
65 throw new RuntimeException(
'Cannot create destination folder', -1);
69 if ($FTPOptions[
'enabled'] == 1 && !$use_streams)
72 $ftp =
JClientFtp::getInstance($FTPOptions[
'host'], $FTPOptions[
'port'], array(), $FTPOptions[
'user'], $FTPOptions[
'pass']);
74 if (!($dh = @opendir($src)))
76 throw new RuntimeException(
'Cannot open source folder', -1);
79 while (($file = readdir($dh)) !==
false)
81 $sfid = $src .
'/' . $file;
82 $dfid = $dest .
'/' . $file;
84 switch (filetype($sfid))
87 if ($file !=
'.' && $file !=
'..')
89 $ret = self::copy($sfid, $dfid, null, $force);
100 $dfid =
JPath::clean(str_replace(JPATH_ROOT, $FTPOptions[
'root'], $dfid),
'/');
102 if (!$ftp->store($sfid, $dfid))
104 throw new RuntimeException(
'Copy file failed', -1);
112 if (!($dh = @opendir($src)))
114 throw new RuntimeException(
'Cannot open source folder', -1);
117 while (($file = readdir($dh)) !==
false)
119 $sfid = $src .
'/' . $file;
120 $dfid = $dest .
'/' . $file;
122 switch (filetype($sfid))
125 if ($file !=
'.' && $file !=
'..')
127 $ret = self::copy($sfid, $dfid, null, $force, $use_streams);
141 if (!$stream->copy($sfid, $dfid))
143 throw new RuntimeException(
'Cannot copy file: ' . $stream->getError(), -1);
148 if (!@copy($sfid, $dfid))
150 throw new RuntimeException(
'Copy file failed', -1);
170 public static function create($path =
'', $mode = 0755)
179 $parent = dirname($path);
181 if (!self::exists($parent))
186 if (($nested > 20) || ($parent == $path))
195 if (self::create($parent, $mode) !==
true)
208 if (self::exists($path))
214 if ($FTPOptions[
'enabled'] == 1)
217 $ftp =
JClientFtp::getInstance($FTPOptions[
'host'], $FTPOptions[
'port'], array(), $FTPOptions[
'user'], $FTPOptions[
'pass']);
220 $path =
JPath::clean(str_replace(JPATH_ROOT, $FTPOptions[
'root'], $path),
'/');
221 $ret = $ftp->mkdir($path);
222 $ftp->chmod($path, $mode);
227 $obd = ini_get(
'open_basedir');
242 $obdArray = explode($obdSeparator, $obd);
246 foreach ($obdArray as $test)
250 if (strpos($path, $test) === 0)
256 if ($inBaseDir ==
false)
266 $origmask = @umask(0);
269 if (!$ret = @mkdir($path, $mode))
273 __METHOD__ .
': ' .
JText::_(
'JLIB_FILESYSTEM_ERROR_COULD_NOT_CREATE_DIRECTORY') .
'Path: ' . $path,
JLog::WARNING,
'jerror'
295 public static function delete($path)
297 @set_time_limit(ini_get(
'max_execution_time'));
315 catch (UnexpectedValueException $e)
329 $files = self::files($path,
'.',
false,
true, array(), array());
333 jimport(
'joomla.filesystem.file');
343 $folders = self::folders($path,
'.',
false,
true, array(), array());
345 foreach ($folders as $folder)
347 if (is_link($folder))
350 jimport(
'joomla.filesystem.file');
358 elseif (self::delete($folder) !==
true)
365 if ($FTPOptions[
'enabled'] == 1)
368 $ftp =
JClientFtp::getInstance($FTPOptions[
'host'], $FTPOptions[
'port'], array(), $FTPOptions[
'user'], $FTPOptions[
'pass']);
377 elseif ($FTPOptions[
'enabled'] == 1)
380 $path =
JPath::clean(str_replace(JPATH_ROOT, $FTPOptions[
'root'], $path),
'/');
383 $ret = $ftp->delete($path);
406 public static function move($src, $dest, $path =
'', $use_streams =
false)
416 if (!self::exists($src))
418 return JText::_(
'JLIB_FILESYSTEM_ERROR_FIND_SOURCE_FOLDER');
421 if (self::exists($dest))
423 return JText::_(
'JLIB_FILESYSTEM_ERROR_FOLDER_EXISTS');
430 if (!$stream->move($src, $dest))
432 return JText::sprintf(
'JLIB_FILESYSTEM_ERROR_FOLDER_RENAME', $stream->getError());
439 if ($FTPOptions[
'enabled'] == 1)
442 $ftp =
JClientFtp::getInstance($FTPOptions[
'host'], $FTPOptions[
'port'], array(), $FTPOptions[
'user'], $FTPOptions[
'pass']);
445 $src =
JPath::clean(str_replace(JPATH_ROOT, $FTPOptions[
'root'], $src),
'/');
446 $dest =
JPath::clean(str_replace(JPATH_ROOT, $FTPOptions[
'root'], $dest),
'/');
449 if (!$ftp->rename($src, $dest))
458 if (!@rename($src, $dest))
479 public static function exists($path)
499 public static function files($path, $filter =
'.', $recurse =
false, $full =
false, $exclude = array(
'.svn',
'CVS',
'.DS_Store',
'__MACOSX'),
500 $excludefilter = array(
'^\..*',
'.*~'), $naturalSort =
false)
514 if (count($excludefilter))
516 $excludefilter_string =
'/(' . implode(
'|', $excludefilter) .
')/';
520 $excludefilter_string =
'';
524 $arr = self::_items($path, $filter, $recurse, $full, $exclude, $excludefilter_string,
true);
536 return array_values($arr);
553 public static function folders($path, $filter =
'.', $recurse =
false, $full =
false, $exclude = array(
'.svn',
'CVS',
'.DS_Store',
'__MACOSX'),
554 $excludefilter = array(
'^\..*'))
568 if (count($excludefilter))
570 $excludefilter_string =
'/(' . implode(
'|', $excludefilter) .
')/';
574 $excludefilter_string =
'';
578 $arr = self::_items($path, $filter, $recurse, $full, $exclude, $excludefilter_string,
false);
583 return array_values($arr);
601 protected static function _items($path, $filter, $recurse, $full, $exclude, $excludefilter_string, $findfiles)
603 @set_time_limit(ini_get(
'max_execution_time'));
608 if (!($handle = @opendir($path)))
613 while (($file = readdir($handle)) !==
false)
615 if ($file !=
'.' && $file !=
'..' && !in_array($file, $exclude)
616 && (empty($excludefilter_string) || !preg_match($excludefilter_string, $file)))
619 $fullpath = $path .
'/' . $file;
622 $isDir = is_dir($fullpath);
624 if (($isDir xor $findfiles) && preg_match(
"/$filter/", $file))
639 if ($isDir && $recurse)
642 if (is_int($recurse))
645 $arr = array_merge($arr, self::_items($fullpath, $filter, $recurse - 1, $full, $exclude, $excludefilter_string, $findfiles));
649 $arr = array_merge($arr, self::_items($fullpath, $filter, $recurse, $full, $exclude, $excludefilter_string, $findfiles));
673 public static function listFolderTree($path, $filter, $maxLevel = 3, $level = 0, $parent = 0)
679 $GLOBALS[
'_JFolder_folder_tree_index'] = 0;
682 if ($level < $maxLevel)
684 $folders = self::folders($path, $filter);
687 foreach ($folders as $name)
689 $id = ++
$GLOBALS[
'_JFolder_folder_tree_index'];
691 $dirs[] = array(
'id' => $id,
'parent' => $parent,
'name' => $name,
'fullname' => $fullName,
692 'relname' => str_replace(JPATH_ROOT,
'', $fullName));
693 $dirs2 = self::listFolderTree($fullName, $filter, $maxLevel, $level + 1, $id);
694 $dirs = array_merge($dirs, $dirs2);
710 public static function makeSafe($path)
712 $regex = array(
'#[^A-Za-z0-9_\\\/\(\)\[\]\{\}\#\$\^\+\.\'~`!@&=;,-]#');
714 return preg_replace($regex,
'', $path);