source: branches/2.0singleton/services/logs.php @ 131

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

finished updating to 2.0singleton

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