source: trunk/services/logs.php @ 137

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

Q - Merged branches/2.0singleton into trunk. Completed updating classes to use singleton methods. Implemented tests. Fixed some bugs. Changed some interfaces.

File size: 10.4 KB
Line 
1<?php
2/**
3 * logs.php
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();
10// $auth->requireAccessClearance(ZONE_ADMIN_APPLOG);
11$app->sslOn();
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.
41$prefs =& Prefs::getInstance();
42$prefs->setDefault('log_file', $app->getParam('log_filename'), 'logs_module');
43$prefs->set('log_file', getFormData('log'), 'logs_module');
44
45// Titles and navigation header.
46$nav->addPage(sprintf(_("Viewing log: <em>%s</em>"), $prefs->get('log_file', 'logs_module')), '/admin/logs.php');
47
48/******************************************************************************
49 * MAIN
50 *****************************************************************************/
51
52// Allow realtime file stats.
53clearstatcache();
54
55// What action to take.
56switch (getFormData('op')) {
57case 'delete' :
58//     $auth->requireAccessClearance(ZONE_ADMIN_APPLOG_FUNC_RESET);
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')) {
62        // Display boomerang page.
63        $app->dieBoomerangURL('app_log');
64    }
65    // Display default page.
66    $app->dieURL($_SERVER['PHP_SELF']);
67    break;
68
69case 'clear' :
70//     $auth->requireAccessClearance(ZONE_ADMIN_APPLOG_FUNC_RESET);
71    clearLog($prefs->get('log_file', 'logs_module'));
72    if ($app->validBoomerangURL('app_log')) {
73        // Display boomerang page.
74        $app->dieBoomerangURL('app_log');
75    }
76    // Display default page.
77    $app->dieURL($_SERVER['PHP_SELF']);
78    break;
79
80case 'archive' :
81//     $auth->requireAccessClearance(ZONE_ADMIN_APPLOG_FUNC_RESET);
82    if (archiveLog($prefs->get('log_file', 'logs_module'))) {
83        // Now flush current log.
84        $app->dieURL($_SERVER['PHP_SELF'] . '?op=clear');
85    }
86    if ($app->validBoomerangURL('app_log')) {
87        // Display boomerang page.
88        $app->dieBoomerangURL('app_log');
89    }
90    // Display default page.
91    $app->dieURL($_SERVER['PHP_SELF']);
92    break;
93
94// case 'ouput' :
95//     $main_template = 'ouput';
96//     break;
97
98case 'download' :
99    header('Content-Type: application/octet-stream');
100    header(sprintf('Content-Disposition: attachment; filename=%s.txt', $prefs->get('log_file', 'logs_module')));
101    printLog($prefs->get('log_file', 'logs_module'));
102    die;
103    break;
104
105default :
106    $list =& getLog($prefs->get('log_file', 'logs_module'), getFormData('search_query'));
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) {
128    printLog($prefs->get('log_file', 'logs_module'));
129} else {
130    include 'codebase/services/templates/' . $main_template;
131}
132include 'footer.ihtml';
133
134
135/******************************************************************************
136 * FUNCTIONS
137 *****************************************************************************/
138
139function deleteLog($log_file)
140{
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__);
146        return false;
147    }
148
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__);
152        return false;
153    }
154
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__);
158        return true;
159    } else {
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__);
162        return false;
163    }
164}
165
166function clearLog($log_file)
167{
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__);
173        return false;
174    }
175
176    flock($fp, LOCK_EX);
177    $ftruncate_return = ftruncate($fp, 0);
178    flock($fp, LOCK_UN);
179    fclose($fp);
180    if (!$ftruncate_return) {
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__);
183        return false;
184    } else {
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__);
187        return true;
188    }
189}
190
191function archiveLog($log_file)
192{
193    $app =& App::getInstance();
194
195    $old_file_name = $log_file;
196    $new_file_name = $log_file . '__' . date('Y-m-d');
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__);
200        return false;
201    }
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__);
205        return false;
206    }
207
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__);
210    return true;
211}
212
213function printLog($log_file)
214{
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__);
220        return false;
221    }
222
223    readfile($app->getParam('log_directory') . '/' . $log_file);
224}
225
226function &getLog($log_file, $search_query='')
227{
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__);
233        return false;
234    }
235    $log = file($app->getParam('log_directory') . '/' . $log_file);
236
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;
250    $app =& App::getInstance();
251
252    // Get a list of all files in the log directory.
253    $dir_handle = opendir($app->getParam('log_directory'));
254    $list = array();
255    while ($dir_handle && ($file = readdir($dir_handle)) !== false) {
256        if (!preg_match('/^\./', $file) && is_file($app->getParam('log_directory') . '/' . $file) && in_array(strtolower(Upload::getFilenameExtension($file)), $valid_file_extensions)) {
257            $list[] = array(
258                'filename' => $file,
259                'filesize' => filesize($app->getParam('log_directory') . '/' . $file),
260                'modified' => filemtime($app->getParam('log_directory') . '/' . $file),
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.