source: trunk/services/logs.php @ 143

Last change on this file since 143 was 143, checked in by scdev, 18 years ago

Q - global comments formatting change

File size: 10.3 KB
RevLine 
[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
13require_once 'codebase/lib/PageNumbers.inc.php';
14require_once 'codebase/lib/SortOrder.inc.php';
15require_once 'codebase/lib/TemplateGlue.inc.php';
16require_once 'codebase/lib/Prefs.inc.php';
17require_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.
[136]41$prefs =& Prefs::getInstance();
42$prefs->setDefault('log_file', $app->getParam('log_filename'), 'logs_module');
43$prefs->set('log_file', getFormData('log'), 'logs_module');
[1]44
45// Titles and navigation header.
[136]46$nav->addPage(sprintf(_("Viewing log: <em>%s</em>"), $prefs->get('log_file', 'logs_module')), '/admin/logs.php');
[1]47
[143]48/********************************************************************
49* MAIN
50********************************************************************/
[42]51
[1]52// Allow realtime file stats.
[42]53clearstatcache();
[1]54
55// What action to take.
56switch (getFormData('op')) {
57case 'delete' :
[22]58//     $auth->requireAccessClearance(ZONE_ADMIN_APPLOG_FUNC_RESET);
[136]59    deleteLog($prefs->get('log_file', 'logs_module'));
60    $prefs->set('log_file', $app->getParam('log_filename'), 'logs_module');
61    if ($app->validBoomerangURL('app_log')) {
[1]62        // Display boomerang page.
[136]63        $app->dieBoomerangURL('app_log');
[1]64    }
65    // Display default page.
[136]66    $app->dieURL($_SERVER['PHP_SELF']);
[1]67    break;
[42]68
[1]69case 'clear' :
[22]70//     $auth->requireAccessClearance(ZONE_ADMIN_APPLOG_FUNC_RESET);
[136]71    clearLog($prefs->get('log_file', 'logs_module'));
72    if ($app->validBoomerangURL('app_log')) {
[1]73        // Display boomerang page.
[136]74        $app->dieBoomerangURL('app_log');
[1]75    }
76    // Display default page.
[136]77    $app->dieURL($_SERVER['PHP_SELF']);
[1]78    break;
[42]79
[1]80case 'archive' :
[22]81//     $auth->requireAccessClearance(ZONE_ADMIN_APPLOG_FUNC_RESET);
[136]82    if (archiveLog($prefs->get('log_file', 'logs_module'))) {
[1]83        // Now flush current log.
[136]84        $app->dieURL($_SERVER['PHP_SELF'] . '?op=clear');
[1]85    }
[136]86    if ($app->validBoomerangURL('app_log')) {
[1]87        // Display boomerang page.
[136]88        $app->dieBoomerangURL('app_log');
[1]89    }
90    // Display default page.
[136]91    $app->dieURL($_SERVER['PHP_SELF']);
[1]92    break;
[42]93
[1]94// case 'ouput' :
95//     $main_template = 'ouput';
96//     break;
[42]97
[1]98case 'download' :
99    header('Content-Type: application/octet-stream');
[136]100    header(sprintf('Content-Disposition: attachment; filename=%s.txt', $prefs->get('log_file', 'logs_module')));
101    printLog($prefs->get('log_file', 'logs_module'));
[1]102    die;
103    break;
[42]104
[1]105default :
[136]106    $list =& getLog($prefs->get('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
126include 'header.ihtml';
127if ('output' == $main_template) {
[136]128    printLog($prefs->get('log_file', 'logs_module'));
[1]129} else {
130    include 'codebase/services/templates/' . $main_template;
131}
132include 'footer.ihtml';
133
134
[143]135/********************************************************************
136* FUNCTIONS
137********************************************************************/
[1]138
139function deleteLog($log_file)
[42]140{
[136]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
[136]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
[136]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 {
[136]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
166function clearLog($log_file)
[42]167{
[136]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) {
[136]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 {
[136]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
191function archiveLog($log_file)
[42]192{
[136]193    $app =& App::getInstance();
194
[1]195    $old_file_name = $log_file;
196    $new_file_name = $log_file . '__' . date('Y-m-d');
[136]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    }
[136]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
[136]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
213function printLog($log_file)
[42]214{
[136]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
[136]223    readfile($app->getParam('log_directory') . '/' . $log_file);
[1]224}
225
226function &getLog($log_file, $search_query='')
[42]227{
[136]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    }
[136]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
247function &getLogList()
248{
249    global $valid_file_extensions;
[136]250    $app =& App::getInstance();
[42]251
[1]252    // Get a list of all files in the log directory.
[136]253    $dir_handle = opendir($app->getParam('log_directory'));
[1]254    $list = array();
255    while ($dir_handle && ($file = readdir($dir_handle)) !== false) {
[136]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,
[136]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?>
Note: See TracBrowser for help on using the repository browser.