/php/idc_manager/cidc-read-only/library/Etao/File/Dir.php
PHP | 474 lines | 336 code | 34 blank | 104 comment | 88 complexity | 8e80fa6b42c526ef24017d380985af34 MD5 | raw file
Possible License(s): MIT, LGPL-2.1, MPL-2.0-no-copyleft-exception, GPL-3.0, AGPL-1.0
- <?php
-
- /**
- * ?????
- */
- class Etao_File_Dir
- {
-
- /**
- * ??????listDirRecursive
- * @var array
- */
- public static $file_lists = array ();
- /**
- * ?????
- */
- public static $txt_files = array (
- 'txt', 'php', 'js', 'html', 'htm', 'css', 'phtml', 'xml'
- );
-
- /**
- * ????????????
- *
- * @param string $path
- * @param string $appendPath
- * @return array
- */
- public static function getDirectories ($path, $appendPath = false)
- {
- if (is_dir ($path)) {
- $contents = scandir ($path);
- if (is_array ($contents)) {
- $returnDirs = false;
- foreach ($contents as $dir)
- {
- if (is_dir ($path . '/' . $dir) && $dir != '.' && $dir != '..' && $dir != '.svn') {
- $returnDirs[] = $appendPath . $dir;
- }
- }
-
- if ($returnDirs) {
- return $returnDirs;
- }
- }
- }
- }
-
- /**
- * ???????/?????????
- *
- * @param string $file
- * @return string
- */
- public static function del_postfix ($file)
- {
- if (!preg_match ('#^/#', $file)) {
- throw new Exception ('?????/??');
- }
- $file = preg_replace ('#/$#', '', trim ($file));
- return $file;
- }
-
- /**
- * ????
- *
- * @param unknown_type $path
- */
- public static function make ($path)
- {
- return mkdir ($path, 0755);
- }
-
- /**
- * ??????
- * eg: /my/own/path
- * will create
- * >my
- * >>own
- * >>>path
- *
- * @param string $base
- * @param string $path
- */
- public static function makeRecursive ($base, $path)
- {
- $pathArray = explode ('/', $path);
- if (is_array ($pathArray)) {
- $strPath = null;
- foreach ($pathArray as $path)
- {
- if (!empty ($path)) {
- $strPath .= '/' . $path;
- if (!is_dir ($base . $strPath)) {
- if (!self::make ($base . $strPath)) {
- return false;
- }
- }
- }
- }
- return true;
- }
- }
-
- /**
- * ????????
- *
- * @param string $source
- * @param string $newName
- */
- public static function rename ($source, $newName)
- {
- if (is_dir ($source)) {
- return rename ($source, $newName);
- }
- }
-
- /**
- * ?????
- * @param string $source
- * @param string $target
- */
- public static function copyRecursive ($source, $target)
- {
- if (is_dir ($source)) {
- @mkdir ($target);
-
- $d = dir ($source);
-
- while (false !== ($entry = $d->read ()))
- {
- if ($entry == '.' || $entry == '..') {
- continue;
- }
-
- $Entry = $source . '/' . $entry;
- if (is_dir ($Entry)) {
- Etao_File_Directory_Writer::copyRecursive ($Entry, $target . '/' . $entry);
- continue;
- }
- copy ($Entry, $target . '/' . $entry);
- }
-
- $d->close ();
- } else {
- copy ($source, $target);
- }
- }
-
- /**
- * ?????
- *
- * @param string $target
- * @param bool $verbose
- * @return bool
- */
- public static function deleteRecursive ($target, $verbose=false)
- {
- $exceptions = array ('.', '..');
- if (!$sourcedir = @opendir ($target)) {
- if ($verbose) {
- echo '<strong>Couldn’t open ' . $target . "</strong><br />\n";
- }
- return false;
- }
- while (false !== ($sibling = readdir ($sourcedir)))
- {
- if (!in_array ($sibling, $exceptions)) {
- $object = str_replace ('//', '/', $target . '/' . $sibling);
- if ($verbose)
- echo 'Processing: <strong>' . $object . "</strong><br />\n";
- if (is_dir ($object))
- Etao_File_Dir::deleteRecursive ($object);
- if (is_file ($object)) {
- $result = @unlink ($object);
- if ($verbose && $result)
- echo "File has been removed<br />\n";
- if ($verbose && (!$result))
- echo "<strong>Couldn’t remove file</strong>";
- }
- }
- }
- closedir ($sourcedir);
-
-
- if ($result = @rmdir ($target)) {
- if ($verbose) {
- echo "Target directory has been removed<br />\n";
- return true;
- }
- } else {
- if ($verbose) {
- echo "<strong>Couldn’t remove target directory</strong>";
- return false;
- }
- }
- }
-
- /**
- * ???????
- * @param string $file
- * @param bool $dot ????
- */
- public static function getSuffix ($file, $dot=false)
- {
- if ($file === null)
- return false;
- $suffix = strtolower (pathinfo ($file, PATHINFO_EXTENSION));
- if ($dot) {
- $suffix = '.' . $suffix;
- }
- return $suffix;
- }
-
- /**
- * ????????????????
- * @param $path
- * @param $pattern
- * @param $mode
- * @param $deep
- */
- public static function findFile ($path, $pattern, $mode = 'filename', $deep = 0)
- {
- if (!is_dir ($path))
- return false;
- $path = self::pathReplace ($path);
- $alldata = glob ($path . '*');
- if (!is_array ($alldata))
- return array ();
- if ($mode == 'filename') {
- $finds = preg_grep ($pattern, $alldata);
- } elseif ($mode == 'filedata') {
- foreach ($alldata as $file)
- {
- if (is_file ($file))
- $data[$file] = file_get_contents ($file);
- }
- $finds = array_keys (preg_grep ($pattern, $data));
- }
- if ($deep) {
- foreach ($alldata as $file)
- {
- if (is_dir ($file))
- $finds = array_merge ($finds, self::findFile ($file, $pattern, $mode, $deep));
- }
- }
- return $finds;
- }
-
- /**
- * ???????????
- * @param unknown_type $path
- * @param unknown_type $type
- * @param unknown_type $array
- */
- public static function listFile ($path, $type = null, & $array = array ())
- {
- if (!is_dir ($path))
- return false;
- $path = self::pathReplace ($path);
- $alldata = glob ($path . '*');
- if (!is_array ($alldata))
- return $array;
- switch ($type)
- {
- case 'file':
- foreach ($alldata as $data)
- {
- if (is_dir ($data)) {
- self::listFile ($data, $type, & $array);
- } else {
- $array[] = $data;
- }
- }
- break;
-
- case 'dir':
- foreach ($alldata as $data)
- {
- if (is_dir ($data)) {
- $array[] = $data;
- self::listFile ($data, $type, & $array);
- }
- }
- break;
-
- default:
- foreach ($alldata as $data)
- {
- if (is_dir ($data)) {
- $array['dir'][] = $data;
- self::listFile ($data, $type, & $array);
- } else {
- $array['file'][] = $data;
- }
- }
- break;
- }
-
- return $array;
- }
-
- /**
- * ??????
- * @param $path
- */
- public static function sizeFile ($path)
- {
- if (!is_dir ($path))
- return false;
- $size = 0;
- $path = self::pathReplace ($path);
- $alldata = glob ($path . '*');
- if (!is_array ($alldata))
- return $size;
- foreach ($alldata as $data)
- {
- if (is_dir ($data)) {
- $size += self::sizeFile ($data);
- } else {
- $size += filesize ($data);
- }
- }
- return $size;
- }
-
- public static function randMd5 ($value=null)
- {
- if ($value === null) {
- $value = md5 (time () . rand (2, 1048576));
- }
- $dir = substr ($value, 8, 2) . '/' . substr ($value, 23, 2) . '/';
- return $dir;
- }
-
- /**
- * ??????
- * @param unknown_type $filesize
- */
- public static function sizeUnit ($filesize)
- {
- if ($filesize >= 1073741824) {
- $filesize = round ($filesize / 1073741824 * 100) / 100 . ' GB';
- } elseif ($filesize >= 1048576) {
- $filesize = round ($filesize / 1048576 * 100) / 100 . ' MB';
- } elseif ($filesize >= 1024) {
- $filesize = round ($filesize / 1024 * 100) / 100 . ' KB';
- } else {
- $filesize = $filesize . ' Bytes';
- }
- return $filesize;
- }
-
- public static function pathReplace ($path)
- {
- return rtrim (preg_replace ("|[\/]+|", DS, $path), DS) . DS;
- }
-
- /**
- * ??????????????
- */
- public static function printFileInPath ($path)
- {
- $files = self::listFile ($path);
- $html = '';
- if (key_exists ('file', $files)) {
- foreach ($files['file'] as $filename)
- {
- if (in_array (self::getSuffix ($filename), self::$txt_files)) {
- $html .= self::printFile ($filename);
- } else {
- $html .= '<strong><a name="' . $filename . '">' . $filename . '</a> (' . self::getSuffix ($filename) . '????????)</strong><br />';
- }
- }
- return $html;
- }
- }
-
- /**
- * ???????
- */
- public static function printFile ($filename)
- {
- $html = '';
- if (file_exists ($filename) && is_readable ($filename)) {
- if (in_array (self::getSuffix ($filename), self::$txt_files)) {
- ob_start (); //?????
- $html .= '<strong><a name="' . $filename . '">' . $filename . '</a></strong><br />';
- $html .='<blockquote>';
- highlight_file ($filename);
- $html .= ob_get_contents (); //?????????????$info
- $html .='</blockquote>';
- ob_clean ();
- }
- }
-
- return $html;
- }
-
- /**
- * ?????????
- * @param string $dir
- */
- public static function isEmptyDir ($dir)
- {
- $result = TRUE;
- $d = dir ($dir);
- while (($entry = $d->read ()) !== false)
- {
- if ($entry !== '.' && $entry != '..') {
- $result = FALSE;
- break;
- }
- }
- $d->close ();
- return $result;
- }
-
- /* ?? listDirRecursive( $dirName = null )
- * * ?? ?????????????
- * * ?? $dirName ????
- * * ?? ?????? false???
- */
-
- public static function listDirRecursive ($dirName = null)
- {
- if (empty ($dirName)) {
- throw new Exception ("directory is empty.");
- } else {
- $dirName = self::del_postfix ($dirName);
- }
- if (is_dir ($dirName)) {
- if ($dh = opendir ($dirName)) {
- $tree = array ();
- while (($file = readdir ($dh)) !== false)
- {
- if ($file != "." && $file != "..") {
- $filePath = $dirName . "/" . $file;
- //self::$file_lists[] = $filePath;
- if (is_dir ($filePath)) {//???,??
- self::$file_lists[] = '[' . $filePath . ']';
- $tree[$file] = self::listDirRecursive ($filePath);
- } else {//???,???????
- self::$file_lists[] = $filePath;
- $tree[] = $file;
- }
- }
- }
- closedir ($dh);
- } else {
- throw new Exception ("can not open directory $dirName.");
- }
- return self::$file_lists;
- } else {
- throw new Exception ("$dirName is not a directory.");
- }
- }
-
- /**
- *??????
- * @param type $path
- * @param type $time
- */
- public static function deleteOldFile ($path, $time=60)
- {
- $iterator = new DirectoryIterator ($path);
- foreach ($iterator as $fileinfo)
- {
- if ((time () - $fileinfo->getCTime ()) > $time) {
- @unlink ($path . DIRECTORY_SEPARATOR . $fileinfo->getFilename ());
- }
- }
- }
-
- }