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