requireLogin(); // $auth->requireAccessClearance(ZONE_ADMIN_APPLOG); App::sslOn(); require_once 'codebase/lib/PageNumbers.inc.php'; require_once 'codebase/lib/SortOrder.inc.php'; require_once 'codebase/lib/TemplateGlue.inc.php'; require_once 'codebase/lib/Prefs.inc.php'; require_once 'codebase/lib/Upload.inc.php'; /****************************************************************************** * CODE CONFIG *****************************************************************************/ // Files with these extentions will be displayed at the top of the log list. $valid_file_extensions = array('', 'txt', 'log'); // Files that cannot be deleted (preg search expressions). $no_delete_files = '/^app_error_log$|^php_error_log$|^access_log$|^error_log$|^ssl_request_log$/'; // Files that cannot be cleared (preg search expressions). $no_clear_files = '/__|^access_log$|^error_log$|^ssl_request_log$/'; // Files that cannot be archived (preg search expressions). $no_archive_files = '/__|^access_log$|^error_log$|^ssl_request_log$/'; // Files that cannot be archived (preg search expressions). $no_download_files = '/^$/'; // Set the defaults and catch incoming settings. Prefs::setDefault('log_file', App::getParam('log_filename'), 'logs_module'); Prefs::setValue('log_file', getFormData('log'), 'logs_module'); // Titles and navigation header. $nav->addPage(sprintf(_("Viewing log: %s"), Prefs::getValue('log_file', 'logs_module')), '/admin/logs.php'); /****************************************************************************** * MAIN *****************************************************************************/ // Allow realtime file stats. clearstatcache(); // What action to take. switch (getFormData('op')) { case 'delete' : // $auth->requireAccessClearance(ZONE_ADMIN_APPLOG_FUNC_RESET); deleteLog(Prefs::getValue('log_file', 'logs_module')); Prefs::setValue('log_file', App::getParam('log_filename'), 'logs_module'); if (App::validBoomerangURL('app_log')) { // Display boomerang page. App::dieBoomerangURL('app_log'); } // Display default page. App::dieURL($_SERVER['PHP_SELF']); break; case 'clear' : // $auth->requireAccessClearance(ZONE_ADMIN_APPLOG_FUNC_RESET); clearLog(Prefs::getValue('log_file', 'logs_module')); if (App::validBoomerangURL('app_log')) { // Display boomerang page. App::dieBoomerangURL('app_log'); } // Display default page. App::dieURL($_SERVER['PHP_SELF']); break; case 'archive' : // $auth->requireAccessClearance(ZONE_ADMIN_APPLOG_FUNC_RESET); if (archiveLog(Prefs::getValue('log_file', 'logs_module'))) { // Now flush current log. App::dieURL($_SERVER['PHP_SELF'] . '?op=clear'); } if (App::validBoomerangURL('app_log')) { // Display boomerang page. App::dieBoomerangURL('app_log'); } // Display default page. App::dieURL($_SERVER['PHP_SELF']); break; // case 'ouput' : // $main_template = 'ouput'; // break; case 'download' : header('Content-Type: application/octet-stream'); header(sprintf('Content-Disposition: attachment; filename=%s.txt', Prefs::getValue('log_file', 'logs_module'))); printLog(Prefs::getValue('log_file', 'logs_module')); die; break; default : $list =& getLog(Prefs::getValue('log_file', 'logs_module'), getFormData('search_query')); $main_template = 'log_list.ihtml'; break; } /****************************************************************************** * TEMPLATE INITIALIZATION *****************************************************************************/ $logs = &getLogList(); // Instantiate page numbers. Total items are set and calculation is done in the getRecordList function. $page = new PageNumbers(); $page->setPerPage(getFormData('per_page'), 500); $page->setPageNumber(getFormData('page_number')); $page->setTotalItems(sizeof($list)); $page->per_page_options = array(100, 250, 500, 600, 800, 1000, 2000, 5000, 10000); $page->calculate(); include 'header.ihtml'; if ('output' == $main_template) { printLog(Prefs::getValue('log_file', 'logs_module')); } else { include 'codebase/services/templates/' . $main_template; } include 'footer.ihtml'; /****************************************************************************** * FUNCTIONS *****************************************************************************/ function deleteLog($log_file) { if (!file_exists(App::getParam('log_directory') . '/' . $log_file)) { App::raiseMsg(sprintf(_("Log file %s does not exist."), $log_file), MSG_NOTICE, __FILE__, __LINE__); App::logMsg(sprintf('Cannot delete nonexistent log file %s', App::getParam('log_directory') . '/' . $log_file), LOG_INFO, __FILE__, __LINE__); return false; } if (!is_writable(App::getParam('log_directory') . '/' . $log_file) && !is_writable(App::getParam('log_directory'))) { App::raiseMsg(sprintf(_("Log file %s could not be deleted."), $log_file), MSG_NOTICE, __FILE__, __LINE__); App::logMsg(sprintf('Cannot delete log file %s, not writable.', App::getParam('log_directory') . '/' . $log_file), LOG_INFO, __FILE__, __LINE__); return false; } if (unlink(App::getParam('log_directory') . '/' . $log_file)) { App::raiseMsg(sprintf(_("Log file %s has been deleted."), $log_file), MSG_NOTICE, __FILE__, __LINE__); App::logMsg(sprintf('Log file %s has been deleted', $log_file), LOG_INFO, __FILE__, __LINE__); return true; } else { App::raiseMsg(sprintf(_("Log file %s could not be deleted."), $log_file), MSG_WARNING, __FILE__, __LINE__); App::logMsg(sprintf('unlink failed on log file %s', App::getParam('log_directory') . '/' . $log_file), LOG_WARNING, __FILE__, __LINE__); return false; } } function clearLog($log_file) { if (!$fp = fopen(App::getParam('log_directory') . '/' . $log_file, 'r+')) { App::raiseMsg(sprintf(_("Log file %s could not be opened."), $log_file), MSG_NOTICE, __FILE__, __LINE__); App::logMsg(sprintf('fopen failed on log file %s', App::getParam('log_directory') . '/' . $log_file), LOG_INFO, __FILE__, __LINE__); return false; } flock($fp, LOCK_EX); $ftruncate_return = ftruncate($fp, 0); flock($fp, LOCK_UN); fclose($fp); if (!$ftruncate_return) { App::raiseMsg(sprintf(_("Log file %s could not be cleared."), $log_file), MSG_WARNING, __FILE__, __LINE__); App::logMsg(sprintf('ftruncate failed on log file %s', App::getParam('log_directory') . '/' . $log_file), LOG_WARNING, __FILE__, __LINE__); return false; } else { App::raiseMsg(sprintf(_("Log file %s has been cleared."), $log_file), MSG_NOTICE, __FILE__, __LINE__); App::logMsg(sprintf('Log file %s has been cleared', $log_file), LOG_INFO, __FILE__, __LINE__); return true; } } function archiveLog($log_file) { $old_file_name = $log_file; $new_file_name = $log_file . '__' . date('Y-m-d'); If (!is_writable(App::getParam('log_directory') . '')) { App::raiseMsg(sprintf('Cannot archive log, log directory not writable: %s', App::getParam('log_directory')), MSG_WARNING, __FILE__, __LINE__); App::logMsg(sprintf('Cannot archive log, log directory not writable: %s', App::getParam('log_directory')), LOG_WARNING, __FILE__, __LINE__); return false; } If (!copy(App::getParam('log_directory') . '/' . $old_file_name, App::getParam('log_directory') . '/' . $new_file_name)) { App::raiseMsg(sprintf(_("Cannot archive log, copying old log file failed."), null), MSG_WARNING, __FILE__, __LINE__); App::logMsg(sprintf('Cannot archive log, copying old log file failed.', null), LOG_WARNING, __FILE__, __LINE__); return false; } App::raiseMsg(sprintf(_("Log file %s has been archived to %s."), $old_file_name, $new_file_name), MSG_NOTICE, __FILE__, __LINE__); App::logMsg(sprintf('Log file %s has been archived to %s.', $old_file_name, $new_file_name), LOG_NOTICE, __FILE__, __LINE__); return true; } function printLog($log_file) { if (!is_file(App::getParam('log_directory') . '/' . $log_file)) { App::raiseMsg(sprintf(_("Log file %s not found."), $log_file), MSG_WARNING, __FILE__, __LINE__); App::logMsg(sprintf('Log file %s not found.', App::getParam('log_directory') . '/' . $log_file), LOG_WARNING, __FILE__, __LINE__); return false; } readfile(App::getParam('log_directory') . '/' . $log_file); } function &getLog($log_file, $search_query='') { if (!is_file(App::getParam('log_directory') . '/' . $log_file)) { App::raiseMsg(sprintf(_("Log file %s not found."), $log_file), MSG_WARNING, __FILE__, __LINE__); App::logMsg(sprintf('Log file %s not found.', App::getParam('log_directory') . '/' . $log_file), LOG_WARNING, __FILE__, __LINE__); return false; } $log = file(App::getParam('log_directory') . '/' . $log_file); if ('' != trim($search_query)) { if (getFormData('search_grep')) { $log = preg_grep('/' . str_replace('/', '\/', $search_query) . '/', $log); } else { $log = preg_grep('/' . preg_quote($search_query, '/') . '/i', $log); } } return array_values($log); } function &getLogList() { global $valid_file_extensions; // Get a list of all files in the log directory. $dir_handle = opendir(App::getParam('log_directory')); $list = array(); while ($dir_handle && ($file = readdir($dir_handle)) !== false) { if (!preg_match('/^\./', $file) && is_file(App::getParam('log_directory') . '/' . $file) && in_array(strtolower(Upload::getFilenameExtension($file)), $valid_file_extensions)) { $list[] = array( 'filename' => $file, 'filesize' => filesize(App::getParam('log_directory') . '/' . $file), 'modified' => filemtime(App::getParam('log_directory') . '/' . $file), ); } } if (is_array($list) && !empty($list)) { sort($list); return $list; } else { return false; } } ?>