/** * clears old files and truncates too big ones within folder and (if $recursive is set to TRUE) recursively below * * @param string $folderPath * @param boolean $recursive * @param boolean $allowDocumentRoot * for checking isPathSecure */ public static function clearAndTruncateFolder($folderPath, $recursive = FALSE, $allowDocumentRoot = FALSE) { $inst = new self(); if (substr($folderPath, -1) !== '/') { $folderPath .= '/'; } if (!$inst->isPathSecure($folderPath, $allowDocumentRoot)) { trigger_error('ERR: Path unsafe to be cleaned: ' . realpath($folderPath), E_USER_WARNING); return FALSE; } self::initStatic('logger_clean_truncate', self::CLEAN_TRUNCATE_LOGFILE); self::logStatic('logger_clean_truncate', 'Clear and truncate FOLDER (recursive: ' . ($recursive ? 'YES' : 'NO') . '): ' . $folderPath); $inst->clearAndTruncateLogFilesInFolder($folderPath); if ($recursive) { $dirhandle = opendir($folderPath); while ($dirname = readdir($dirhandle)) { if ($dirname == '.' || $dirname == '..') { continue; } $dirPath = $folderPath . $dirname; if (is_dir($dirPath) && !is_link($dirPath)) { self::clearAndTruncateFolder($dirPath, $recursive); } } } closedir($dirhandle); }