[1] | 1 | <?php |
---|
| 2 | /** |
---|
[42] | 3 | * logs.php |
---|
[1] | 4 | * Code by Strangecode :: www.strangecode.com :: This document contains copyrighted information |
---|
| 5 | */ |
---|
| 6 | |
---|
| 7 | // require_once dirname(__FILE__) . '/_config.inc.php'; |
---|
| 8 | |
---|
| 9 | $auth->requireLogin(); |
---|
[22] | 10 | // $auth->requireAccessClearance(ZONE_ADMIN_APPLOG); |
---|
[129] | 11 | $app->sslOn(); |
---|
[1] | 12 | |
---|
| 13 | require_once 'codebase/lib/PageNumbers.inc.php'; |
---|
| 14 | require_once 'codebase/lib/SortOrder.inc.php'; |
---|
| 15 | require_once 'codebase/lib/TemplateGlue.inc.php'; |
---|
| 16 | require_once 'codebase/lib/Prefs.inc.php'; |
---|
| 17 | require_once 'codebase/lib/Upload.inc.php'; |
---|
| 18 | |
---|
| 19 | |
---|
| 20 | /****************************************************************************** |
---|
| 21 | * CODE CONFIG |
---|
| 22 | *****************************************************************************/ |
---|
| 23 | |
---|
| 24 | // Files with these extentions will be displayed at the top of the log list. |
---|
| 25 | $valid_file_extensions = array('', 'txt', 'log'); |
---|
| 26 | |
---|
| 27 | // Files that cannot be deleted (preg search expressions). |
---|
| 28 | $no_delete_files = '/^app_error_log$|^php_error_log$|^access_log$|^error_log$|^ssl_request_log$/'; |
---|
| 29 | |
---|
| 30 | // Files that cannot be cleared (preg search expressions). |
---|
| 31 | $no_clear_files = '/__|^access_log$|^error_log$|^ssl_request_log$/'; |
---|
| 32 | |
---|
| 33 | // Files that cannot be archived (preg search expressions). |
---|
| 34 | $no_archive_files = '/__|^access_log$|^error_log$|^ssl_request_log$/'; |
---|
| 35 | |
---|
| 36 | // Files that cannot be archived (preg search expressions). |
---|
| 37 | $no_download_files = '/^$/'; |
---|
| 38 | |
---|
| 39 | |
---|
| 40 | // Set the defaults and catch incoming settings. |
---|
[132] | 41 | $prefs =& Prefs::getInstance(); |
---|
| 42 | $prefs->setDefault('log_file', $app->getParam('log_filename'), 'logs_module'); |
---|
| 43 | $prefs->setValue('log_file', getFormData('log'), 'logs_module'); |
---|
[1] | 44 | |
---|
| 45 | // Titles and navigation header. |
---|
[132] | 46 | $nav->addPage(sprintf(_("Viewing log: <em>%s</em>"), $prefs->getValue('log_file', 'logs_module')), '/admin/logs.php'); |
---|
[1] | 47 | |
---|
| 48 | /****************************************************************************** |
---|
| 49 | * MAIN |
---|
| 50 | *****************************************************************************/ |
---|
[42] | 51 | |
---|
[1] | 52 | // Allow realtime file stats. |
---|
[42] | 53 | clearstatcache(); |
---|
[1] | 54 | |
---|
| 55 | // What action to take. |
---|
| 56 | switch (getFormData('op')) { |
---|
| 57 | case 'delete' : |
---|
[22] | 58 | // $auth->requireAccessClearance(ZONE_ADMIN_APPLOG_FUNC_RESET); |
---|
[132] | 59 | deleteLog($prefs->getValue('log_file', 'logs_module')); |
---|
| 60 | $prefs->setValue('log_file', $app->getParam('log_filename'), 'logs_module'); |
---|
[129] | 61 | if ($app->validBoomerangURL('app_log')) { |
---|
[1] | 62 | // Display boomerang page. |
---|
[129] | 63 | $app->dieBoomerangURL('app_log'); |
---|
[1] | 64 | } |
---|
| 65 | // Display default page. |
---|
[129] | 66 | $app->dieURL($_SERVER['PHP_SELF']); |
---|
[1] | 67 | break; |
---|
[42] | 68 | |
---|
[1] | 69 | case 'clear' : |
---|
[22] | 70 | // $auth->requireAccessClearance(ZONE_ADMIN_APPLOG_FUNC_RESET); |
---|
[132] | 71 | clearLog($prefs->getValue('log_file', 'logs_module')); |
---|
[129] | 72 | if ($app->validBoomerangURL('app_log')) { |
---|
[1] | 73 | // Display boomerang page. |
---|
[129] | 74 | $app->dieBoomerangURL('app_log'); |
---|
[1] | 75 | } |
---|
| 76 | // Display default page. |
---|
[129] | 77 | $app->dieURL($_SERVER['PHP_SELF']); |
---|
[1] | 78 | break; |
---|
[42] | 79 | |
---|
[1] | 80 | case 'archive' : |
---|
[22] | 81 | // $auth->requireAccessClearance(ZONE_ADMIN_APPLOG_FUNC_RESET); |
---|
[132] | 82 | if (archiveLog($prefs->getValue('log_file', 'logs_module'))) { |
---|
[1] | 83 | // Now flush current log. |
---|
[129] | 84 | $app->dieURL($_SERVER['PHP_SELF'] . '?op=clear'); |
---|
[1] | 85 | } |
---|
[129] | 86 | if ($app->validBoomerangURL('app_log')) { |
---|
[1] | 87 | // Display boomerang page. |
---|
[129] | 88 | $app->dieBoomerangURL('app_log'); |
---|
[1] | 89 | } |
---|
| 90 | // Display default page. |
---|
[129] | 91 | $app->dieURL($_SERVER['PHP_SELF']); |
---|
[1] | 92 | break; |
---|
[42] | 93 | |
---|
[1] | 94 | // case 'ouput' : |
---|
| 95 | // $main_template = 'ouput'; |
---|
| 96 | // break; |
---|
[42] | 97 | |
---|
[1] | 98 | case 'download' : |
---|
| 99 | header('Content-Type: application/octet-stream'); |
---|
[132] | 100 | header(sprintf('Content-Disposition: attachment; filename=%s.txt', $prefs->getValue('log_file', 'logs_module'))); |
---|
| 101 | printLog($prefs->getValue('log_file', 'logs_module')); |
---|
[1] | 102 | die; |
---|
| 103 | break; |
---|
[42] | 104 | |
---|
[1] | 105 | default : |
---|
[132] | 106 | $list =& getLog($prefs->getValue('log_file', 'logs_module'), getFormData('search_query')); |
---|
[1] | 107 | $main_template = 'log_list.ihtml'; |
---|
| 108 | break; |
---|
| 109 | } |
---|
| 110 | |
---|
| 111 | |
---|
| 112 | /****************************************************************************** |
---|
| 113 | * TEMPLATE INITIALIZATION |
---|
| 114 | *****************************************************************************/ |
---|
| 115 | |
---|
| 116 | $logs = &getLogList(); |
---|
| 117 | |
---|
| 118 | // Instantiate page numbers. Total items are set and calculation is done in the getRecordList function. |
---|
| 119 | $page = new PageNumbers(); |
---|
| 120 | $page->setPerPage(getFormData('per_page'), 500); |
---|
| 121 | $page->setPageNumber(getFormData('page_number')); |
---|
| 122 | $page->setTotalItems(sizeof($list)); |
---|
| 123 | $page->per_page_options = array(100, 250, 500, 600, 800, 1000, 2000, 5000, 10000); |
---|
| 124 | $page->calculate(); |
---|
| 125 | |
---|
| 126 | include 'header.ihtml'; |
---|
| 127 | if ('output' == $main_template) { |
---|
[132] | 128 | printLog($prefs->getValue('log_file', 'logs_module')); |
---|
[1] | 129 | } else { |
---|
| 130 | include 'codebase/services/templates/' . $main_template; |
---|
| 131 | } |
---|
| 132 | include 'footer.ihtml'; |
---|
| 133 | |
---|
| 134 | |
---|
| 135 | /****************************************************************************** |
---|
| 136 | * FUNCTIONS |
---|
| 137 | *****************************************************************************/ |
---|
| 138 | |
---|
| 139 | function deleteLog($log_file) |
---|
[42] | 140 | { |
---|
[129] | 141 | $app =& App::getInstance(); |
---|
| 142 | |
---|
| 143 | if (!file_exists($app->getParam('log_directory') . '/' . $log_file)) { |
---|
| 144 | $app->raiseMsg(sprintf(_("Log file %s does not exist."), $log_file), MSG_NOTICE, __FILE__, __LINE__); |
---|
| 145 | $app->logMsg(sprintf('Cannot delete nonexistent log file %s', $app->getParam('log_directory') . '/' . $log_file), LOG_INFO, __FILE__, __LINE__); |
---|
[1] | 146 | return false; |
---|
| 147 | } |
---|
[42] | 148 | |
---|
[129] | 149 | if (!is_writable($app->getParam('log_directory') . '/' . $log_file) && !is_writable($app->getParam('log_directory'))) { |
---|
| 150 | $app->raiseMsg(sprintf(_("Log file %s could not be deleted."), $log_file), MSG_NOTICE, __FILE__, __LINE__); |
---|
| 151 | $app->logMsg(sprintf('Cannot delete log file %s, not writable.', $app->getParam('log_directory') . '/' . $log_file), LOG_INFO, __FILE__, __LINE__); |
---|
[1] | 152 | return false; |
---|
| 153 | } |
---|
[42] | 154 | |
---|
[129] | 155 | if (unlink($app->getParam('log_directory') . '/' . $log_file)) { |
---|
| 156 | $app->raiseMsg(sprintf(_("Log file %s has been deleted."), $log_file), MSG_NOTICE, __FILE__, __LINE__); |
---|
| 157 | $app->logMsg(sprintf('Log file %s has been deleted', $log_file), LOG_INFO, __FILE__, __LINE__); |
---|
[1] | 158 | return true; |
---|
| 159 | } else { |
---|
[129] | 160 | $app->raiseMsg(sprintf(_("Log file %s could not be deleted."), $log_file), MSG_WARNING, __FILE__, __LINE__); |
---|
| 161 | $app->logMsg(sprintf('unlink failed on log file %s', $app->getParam('log_directory') . '/' . $log_file), LOG_WARNING, __FILE__, __LINE__); |
---|
[1] | 162 | return false; |
---|
| 163 | } |
---|
| 164 | } |
---|
| 165 | |
---|
| 166 | function clearLog($log_file) |
---|
[42] | 167 | { |
---|
[129] | 168 | $app =& App::getInstance(); |
---|
| 169 | |
---|
| 170 | if (!$fp = fopen($app->getParam('log_directory') . '/' . $log_file, 'r+')) { |
---|
| 171 | $app->raiseMsg(sprintf(_("Log file %s could not be opened."), $log_file), MSG_NOTICE, __FILE__, __LINE__); |
---|
| 172 | $app->logMsg(sprintf('fopen failed on log file %s', $app->getParam('log_directory') . '/' . $log_file), LOG_INFO, __FILE__, __LINE__); |
---|
[1] | 173 | return false; |
---|
| 174 | } |
---|
[42] | 175 | |
---|
[1] | 176 | flock($fp, LOCK_EX); |
---|
| 177 | $ftruncate_return = ftruncate($fp, 0); |
---|
| 178 | flock($fp, LOCK_UN); |
---|
| 179 | fclose($fp); |
---|
| 180 | if (!$ftruncate_return) { |
---|
[129] | 181 | $app->raiseMsg(sprintf(_("Log file %s could not be cleared."), $log_file), MSG_WARNING, __FILE__, __LINE__); |
---|
| 182 | $app->logMsg(sprintf('ftruncate failed on log file %s', $app->getParam('log_directory') . '/' . $log_file), LOG_WARNING, __FILE__, __LINE__); |
---|
[1] | 183 | return false; |
---|
| 184 | } else { |
---|
[129] | 185 | $app->raiseMsg(sprintf(_("Log file %s has been cleared."), $log_file), MSG_NOTICE, __FILE__, __LINE__); |
---|
| 186 | $app->logMsg(sprintf('Log file %s has been cleared', $log_file), LOG_INFO, __FILE__, __LINE__); |
---|
[1] | 187 | return true; |
---|
| 188 | } |
---|
| 189 | } |
---|
| 190 | |
---|
| 191 | function archiveLog($log_file) |
---|
[42] | 192 | { |
---|
[129] | 193 | $app =& App::getInstance(); |
---|
| 194 | |
---|
[1] | 195 | $old_file_name = $log_file; |
---|
| 196 | $new_file_name = $log_file . '__' . date('Y-m-d'); |
---|
[129] | 197 | If (!is_writable($app->getParam('log_directory') . '')) { |
---|
| 198 | $app->raiseMsg(sprintf('Cannot archive log, log directory not writable: %s', $app->getParam('log_directory')), MSG_WARNING, __FILE__, __LINE__); |
---|
| 199 | $app->logMsg(sprintf('Cannot archive log, log directory not writable: %s', $app->getParam('log_directory')), LOG_WARNING, __FILE__, __LINE__); |
---|
[1] | 200 | return false; |
---|
| 201 | } |
---|
[129] | 202 | If (!copy($app->getParam('log_directory') . '/' . $old_file_name, $app->getParam('log_directory') . '/' . $new_file_name)) { |
---|
| 203 | $app->raiseMsg(sprintf(_("Cannot archive log, copying old log file failed."), null), MSG_WARNING, __FILE__, __LINE__); |
---|
| 204 | $app->logMsg(sprintf('Cannot archive log, copying old log file failed.', null), LOG_WARNING, __FILE__, __LINE__); |
---|
[1] | 205 | return false; |
---|
| 206 | } |
---|
[42] | 207 | |
---|
[129] | 208 | $app->raiseMsg(sprintf(_("Log file %s has been archived to %s."), $old_file_name, $new_file_name), MSG_NOTICE, __FILE__, __LINE__); |
---|
| 209 | $app->logMsg(sprintf('Log file %s has been archived to %s.', $old_file_name, $new_file_name), LOG_NOTICE, __FILE__, __LINE__); |
---|
[1] | 210 | return true; |
---|
| 211 | } |
---|
| 212 | |
---|
| 213 | function printLog($log_file) |
---|
[42] | 214 | { |
---|
[129] | 215 | $app =& App::getInstance(); |
---|
| 216 | |
---|
| 217 | if (!is_file($app->getParam('log_directory') . '/' . $log_file)) { |
---|
| 218 | $app->raiseMsg(sprintf(_("Log file %s not found."), $log_file), MSG_WARNING, __FILE__, __LINE__); |
---|
| 219 | $app->logMsg(sprintf('Log file %s not found.', $app->getParam('log_directory') . '/' . $log_file), LOG_WARNING, __FILE__, __LINE__); |
---|
[1] | 220 | return false; |
---|
| 221 | } |
---|
| 222 | |
---|
[129] | 223 | readfile($app->getParam('log_directory') . '/' . $log_file); |
---|
[1] | 224 | } |
---|
| 225 | |
---|
| 226 | function &getLog($log_file, $search_query='') |
---|
[42] | 227 | { |
---|
[129] | 228 | $app =& App::getInstance(); |
---|
| 229 | |
---|
| 230 | if (!is_file($app->getParam('log_directory') . '/' . $log_file)) { |
---|
| 231 | $app->raiseMsg(sprintf(_("Log file %s not found."), $log_file), MSG_WARNING, __FILE__, __LINE__); |
---|
| 232 | $app->logMsg(sprintf('Log file %s not found.', $app->getParam('log_directory') . '/' . $log_file), LOG_WARNING, __FILE__, __LINE__); |
---|
[1] | 233 | return false; |
---|
| 234 | } |
---|
[129] | 235 | $log = file($app->getParam('log_directory') . '/' . $log_file); |
---|
[42] | 236 | |
---|
[1] | 237 | if ('' != trim($search_query)) { |
---|
| 238 | if (getFormData('search_grep')) { |
---|
| 239 | $log = preg_grep('/' . str_replace('/', '\/', $search_query) . '/', $log); |
---|
| 240 | } else { |
---|
| 241 | $log = preg_grep('/' . preg_quote($search_query, '/') . '/i', $log); |
---|
| 242 | } |
---|
| 243 | } |
---|
| 244 | return array_values($log); |
---|
| 245 | } |
---|
| 246 | |
---|
| 247 | function &getLogList() |
---|
| 248 | { |
---|
| 249 | global $valid_file_extensions; |
---|
[129] | 250 | $app =& App::getInstance(); |
---|
[42] | 251 | |
---|
[1] | 252 | // Get a list of all files in the log directory. |
---|
[129] | 253 | $dir_handle = opendir($app->getParam('log_directory')); |
---|
[1] | 254 | $list = array(); |
---|
| 255 | while ($dir_handle && ($file = readdir($dir_handle)) !== false) { |
---|
[129] | 256 | if (!preg_match('/^\./', $file) && is_file($app->getParam('log_directory') . '/' . $file) && in_array(strtolower(Upload::getFilenameExtension($file)), $valid_file_extensions)) { |
---|
[1] | 257 | $list[] = array( |
---|
| 258 | 'filename' => $file, |
---|
[129] | 259 | 'filesize' => filesize($app->getParam('log_directory') . '/' . $file), |
---|
| 260 | 'modified' => filemtime($app->getParam('log_directory') . '/' . $file), |
---|
[1] | 261 | ); |
---|
| 262 | } |
---|
| 263 | } |
---|
| 264 | if (is_array($list) && !empty($list)) { |
---|
| 265 | sort($list); |
---|
| 266 | return $list; |
---|
| 267 | } else { |
---|
| 268 | return false; |
---|
| 269 | } |
---|
| 270 | } |
---|
| 271 | |
---|
| 272 | |
---|
| 273 | |
---|
| 274 | ?> |
---|