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