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