1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224: 225: 226: 227: 228: 229: 230: 231: 232: 233: 234: 235: 236: 237: 238: 239: 240: 241: 242: 243: 244: 245: 246: 247: 248: 249: 250: 251: 252: 253: 254: 255: 256: 257: 258: 259: 260: 261: 262: 263: 264: 265: 266: 267: 268: 269: 270: 271: 272: 273: 274: 275: 276: 277: 278: 279: 280: 281: 282: 283: 284: 285: 286: 287: 288: 289: 290: 291: 292: 293: 294: 295: 296: 297: 298: 299: 300: 301: 302: 303: 304: 305: 306: 307: 308: 309: 310: 311: 312: 313: 314: 315: 316: 317: 318: 319: 320: 321: 322: 323: 324: 325: 326: 327: 328: 329: 330: 331: 332: 333: 334: 335: 336: 337: 338: 339: 340: 341: 342: 343: 344: 345: 346: 347: 348: 349: 350: 351: 352: 353: 354: 355: 356: 357: 358: 359: 360: 361: 362: 363: 364: 365: 366: 367: 368: 369: 370: 371: 372: 373: 374: 375: 376: 377: 378: 379: 380: 381: 382: 383: 384: 385: 386: 387: 388: 389: 390: 391: 392: 393: 394: 395: 396: 397: 398: 399: 400: 401: 402: 403: 404: 405: 406: 407: 408: 409: 410: 411: 412: 413: 414: 415: 416: 417: 418: 419: 420: 421: 422: 423: 424: 425: 426: 427: 428: 429: 430: 431: 432: 433: 434: 435: 436: 437: 438: 439: 440: 441: 442: 443: 444: 445: 446: 447: 448: 449: 450: 451:
<?php
class icms_core_Filesystem {
static public function chmod($target, $mode = 0777) {
return @chmod($target, $mode);
}
static public function mkdir($target, $mode = 0777, $base = ICMS_ROOT_PATH, $metachars = array()) {
if (is_dir($target)) return TRUE;
if (!isset($metachars)) {
$metachars = array('[', '?', '"', '.', '<', '>', '|', ' ', ':');
}
$base = preg_replace ('/[\\|\/]/', DIRECTORY_SEPARATOR, $base);
$target = preg_replace ('/[\\|\/]/', DIRECTORY_SEPARATOR, $target);
if ($base !== '') {
$target = str_ireplace($base . DIRECTORY_SEPARATOR, '', $target);
$target = $base . DIRECTORY_SEPARATOR . str_replace($metachars , '_', $target);
} else {
$target = str_replace($metachars , '_', $target);
}
if (mkdir($target, $mode, TRUE)) {
if ($fh = @fopen($target . '/index.html', 'w')) {
fwrite($fh, '<script>history.go(-1);</script>');
@fclose($fh);
}
if (substr(decoct(fileperms($target)), 2) != $mode) {
chmod($target, $mode);
}
}
return is_dir($target);
}
static public function cleanFolders($dir, $remove_admin_cache = FALSE) {
global $icmsConfig;
foreach ($dir as $d) {
$dd = opendir($d);
while ($file = readdir($dd)) {
$files_array = $remove_admin_cache
? ($file != 'index.html' && $file != 'php.ini' && $file != '.htaccess'
&& $file != '.svn')
: ($file != 'index.html' && $file != 'php.ini' && $file != '.htaccess'
&& $file != '.svn' && $file != 'adminmenu_' . $icmsConfig['language'] . '.php');
if (is_file($d . $file) && $files_array) {
unlink($d . $file);
}
}
closedir($dd);
}
return true;
}
static public function cleanWriteFolders() {
return self::cleanFolders(
array(
'templates_c' => ICMS_COMPILE_PATH . '/',
'cache' => ICMS_CACHE_PATH . '/',
)
);
}
static public function copyRecursive($source, $dest) {
if (is_file($source)) {return copy($source, $dest);}
if (!is_dir($dest)) {
self::mkdir($dest, 0777, '');
}
$dir = dir($source);
while (false !== $entry = $dir->read()) {
if ($entry == '.' || $entry == '..') {continue;}
if (is_dir("$source/$entry") && ($dest !== "$source/$entry")) {
self::copyRecursive("$source/$entry", "$dest/$entry");
} else {
copy("$source/$entry", "$dest/$entry");
}
}
$dir->close();
return true;
}
static public function deleteFile($dirname) {
if (is_file($dirname)) {
return unlink($dirname);
}
}
static public function copyStream($src, $dest) {
$len = false;
if (@ini_get('allow_url_fopen')) {
$fsrc = fopen($src, 'r');
$fdest = fopen($dest, 'w+');
$len = stream_copy_to_stream($fsrc, $fdest);
fclose($fsrc);
fclose($fdest);
}
return $len;
}
static public function deleteRecursive($dir, $deleteRootToo=true) {
if (!$dh = @opendir($dir)) {
return;
}
while (false !== ($obj = readdir($dh))) {
if ($obj == '.' || $obj == '..') {
continue;
}
if (!@unlink($dir . '/' . $obj)) {
self::deleteRecursive($dir . '/' . $obj, true);
}
}
closedir($dh);
if ($deleteRootToo) {
@rmdir($dir);
}
return;
}
static public function writeIndexFile($path = '') {
if (empty($path)) {
return false;
}
$path = substr($path, -1) == "/" ? substr($path, 0, -1) : $path;
$filename = $path . '/index.html';
if (file_exists($filename)) {
return true;
}
if (! $file = fopen($filename, "w")) {
echo 'failed open file';
return false;
}
if (fwrite($file, "<script>history.go(-1);</script>") == FALSE) {
echo 'failed write file';
return false;
}
fclose($file);
return true;
}
static public function generateChecksum() {
$rootdir = preg_replace('#[\|/]#', DIRECTORY_SEPARATOR, ICMS_ROOT_PATH);
$dir = new RecursiveDirectoryIterator($rootdir);
$checkfile = preg_replace('#[\|/]#', DIRECTORY_SEPARATOR, ICMS_TRUST_PATH) . DIRECTORY_SEPARATOR . 'checkfile.sha1';
$file = new SplFileObject($checkfile, 'w');
$cache_dir = preg_replace('#[\|/]#', DIRECTORY_SEPARATOR, ICMS_CACHE_PATH);
$templates_dir = preg_replace('#[\|/]#', DIRECTORY_SEPARATOR, ICMS_COMPILE_PATH);
$newline = '';
foreach (new RecursiveIteratorIterator($dir) as $name=>$item) {
$itemPath = $item->getPath();
$itemFilename = $item->getBasename();
$itemPerms = $item->getPerms();
if ($itemPath != $cache_dir && $itemPath != $templates_dir) {
$fileHash = sha1_file($name);
echo _CORE_CHECKSUM_ADDING . ': ' . $name . _CORE_CHECKSUM_CHECKSUM . ' : <em>'. $fileHash .'</em>, ' ._CORE_CHECKSUM_PERMISSIONS .' : '. $itemPerms . '<br />';
$file->fwrite($newline . $name . ';' .$fileHash . ';' . $itemPerms);
}
$newline = "\n";
}
unset($file);
unset($item);
unset($dir);
}
static public function validateChecksum() {
$validationFile = new SplFileObject($checkfile);
if ($validationFile->isReadable()) {
$currentHash = $currentPerms = array();
$cache_dir = preg_replace('#[\|/]#', DIRECTORY_SEPARATOR, ICMS_CACHE_PATH);
$templates_dir = preg_replace('#[\|/]#', DIRECTORY_SEPARATOR, ICMS_COMPILE_PATH);
foreach (new RecursiveIteratorIterator($dir) as $name=>$item) {
$itemPath = $item->getPath();
$itemFilename = $item->getBasename();
$itemPerms = $item->getPerms();
if ($itemPath != $cache_dir && $itemPath != $templates_dir) {
$fileHash = sha1_file($name);
$currentHash[$name] = $fileHash;
$currentPerms[$name] = $itemPerms;
}
}
echo _CORE_CHECKSUM_CHECKFILE . $checkfile . '<br />';
$validHash = $validPerms = array();
while (! $validationFile->eof()) {
list($filename, $checksum, $filePermissions) = $validationFile->fgetcsv(';');
$validHash[$filename] = $checksum;
$validPerms[$filename] = $filePermissions;
}
$hashVariations = array_diff_assoc($validHash, $currentHash);
$addedFiles = array_diff_key($currentHash, $validHash);
$missingFiles = array_diff_key($validHash, $currentHash);
$permVariations = array_diff_assoc($validPerms, $currentPerms);
echo '<br /><strong>'. count($hashVariations) . _CORE_CHECKSUM_ALTERED_REMOVED . '</strong><br />';
foreach ($hashVariations as $file=>$check) {
echo $file . '<br />';
}
echo '<br /><strong>' . count($addedFiles) . _CORE_CHECKSUM_FILES_ADDED . '</strong><br />';
foreach ($addedFiles as $file=>$hash) {
echo $file . '<br />';
}
echo '<br /><strong>' . count($missingFiles) . _CORE_CHECKSUM_FILES_REMOVED . '</strong><br />';
foreach ($missingFiles as $file=>$hash) {
echo $file . '<br />';
}
echo '<br /><strong>' . count($permVariations) . _CORE_CHECKSUM_PERMISSIONS_ALTERED . '</strong><br />';
foreach ($permVariations as $file=>$perms) {
echo $file . '<br />';
}
} else {
echo _CORE_CHECKSUM_CHECKFILE_UNREADABLE;
}
unset($validationFile);
unset($item);
unset($dir);
}
static public function getDirList($dirname, array $ignore = array('cvs', '_darcs', '.svn'), $hideDot = TRUE) {
$dirList = array();
$iterator = new DirectoryIterator($dirname);
foreach ($iterator as $file) {
if ($file->isDir() && !$file->isDot()) {
$filename = $file->getFilename();
if (!$hideDot || substr($filename, 0, 1) != '.') {
$dirList[$filename] = $filename;
}
}
}
asort($dirList);
return array_diff($dirList, $ignore);
}
static public function getFileList($dirname, $prefix = '', array $extension = array(), $hideDot = FALSE) {
if (!is_dir($dirname)) return array();
$fileList = array();
if (empty($extension)) {
$extList = '';
} else {
$extList = implode('|\.', $extension);
}
$iterator = new DirectoryIterator($dirname);
foreach ($iterator as $file) {
if ($file->isFile() && !$file->isDot()) {
$filename = $file->getFilename();
if (!$hideDot || substr($filename, 0, 1) != '.') {
if ($extList == '') {
$file = $prefix . $filename;
$fileList[$file] = $file;
} elseif (preg_match("/(\." . $extList . ")$/i", $filename)) {
$file = $prefix . $filename;
$fileList[$file] = $file;
}
}
}
}
asort($fileList);
return $fileList;
}
static public function writeFile($contents, $filename, $extension = '', $location = ICMS_TRUST_PATH) {
if ($extension == '') $extension = 'php';
if (DIRECTORY_SEPARATOR !== "/") $location = str_replace(DIRECTORY_SEPARATOR, "/", $location);
$file = $location . '/' . $filename . '.' . $extension;
if ($fp = fopen($file, "wt")) {
if (fwrite($fp, $contents) == FALSE) {
echo 'failed write file';
return FALSE;
}
fclose($fp);
}
}
static public function getImgList($dirname, $prefix = '', $extension = array('gif', 'jpg', 'png')) {
return self::getFileList($dirname, $prefix, $extension);
}
static public function getFontList($dirname, $prefix = '', $extension = array('ttf')) {
return self::getFileList($dirname, $prefix, $extension);
}
static public function getPhpFiles($dirname, $prefix = '', $extension = array('php')) {
return self::getFileList($dirname, $prefix, $extension);
}
static public function getHtmlFiles($dirname, $prefix = '', $extension = array('htm', 'html', 'xhtml')) {
return self::getFileList($dirname, $prefix, $extension);
}
}