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