source: trunk/services/logs.php @ 189

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

Q - decided to use standard instantiation for Prefs and Cache instead of singleton methods.

File size: 10.3 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// Configure the prefs object.
40$tmp_prefs = new Prefs('admin_logs');
41$tmp_prefs->setParam(array('persistent' => false));
42$tmp_prefs->setDefaults(array(
43    'log_file' => $app->getParam('log_filename')
44));
45$tmp_prefs->set('log_file', getFormData('log'));
46
47// Titles and navigation header.
48$nav->addPage(sprintf(_("Viewing log: <em>%s</em>"), $tmp_prefs->get('log_file')), '/admin/logs.php');
49
50/********************************************************************
51* MAIN
52********************************************************************/
53
54// Allow realtime file stats.
55clearstatcache();
56
57// What action to take.
58switch (getFormData('op')) {
59case 'delete' :
60//     $auth->requireAccessClearance(ZONE_ADMIN_APPLOG_FUNC_RESET);
61    deleteLog($tmp_prefs->get('log_file'));
62    $tmp_prefs->set('log_file', $app->getParam('log_filename'));
63    if ($app->validBoomerangURL('app_log')) {
64        // Display boomerang page.
65        $app->dieBoomerangURL('app_log');
66    }
67    // Display default page.
68    $app->dieURL($_SERVER['PHP_SELF']);
69    break;
70
71case 'clear' :
72//     $auth->requireAccessClearance(ZONE_ADMIN_APPLOG_FUNC_RESET);
73    clearLog($tmp_prefs->get('log_file'));
74    if ($app->validBoomerangURL('app_log')) {
75        // Display boomerang page.
76        $app->dieBoomerangURL('app_log');
77    }
78    // Display default page.
79    $app->dieURL($_SERVER['PHP_SELF']);
80    break;
81
82case 'archive' :
83//     $auth->requireAccessClearance(ZONE_ADMIN_APPLOG_FUNC_RESET);
84    if (archiveLog($tmp_prefs->get('log_file'))) {
85        // Now flush current log.
86        $app->dieURL($_SERVER['PHP_SELF'] . '?op=clear');
87    }
88    if ($app->validBoomerangURL('app_log')) {
89        // Display boomerang page.
90        $app->dieBoomerangURL('app_log');
91    }
92    // Display default page.
93    $app->dieURL($_SERVER['PHP_SELF']);
94    break;
95
96// case 'ouput' :
97//     $main_template = 'ouput';
98//     break;
99
100case 'download' :
101    header('Content-Type: application/octet-stream');
102    header(sprintf('Content-Disposition: attachment; filename=%s.txt', $tmp_prefs->get('log_file')));
103    printLog($tmp_prefs->get('log_file'));
104    die;
105    break;
106
107default :
108    $list =& getLog($tmp_prefs->get('log_file'), getFormData('search_query'));
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
128include 'header.ihtml';
129if ('output' == $main_template) {
130    printLog($tmp_prefs->get('log_file'));
131} else {
132    include 'codebase/services/templates/' . $main_template;
133}
134include 'footer.ihtml';
135
136
137/********************************************************************
138* FUNCTIONS
139********************************************************************/
140
141function deleteLog($log_file)
142{
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__);
148        return false;
149    }
150
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__);
154        return false;
155    }
156
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__);
160        return true;
161    } else {
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__);
164        return false;
165    }
166}
167
168function clearLog($log_file)
169{
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__);
175        return false;
176    }
177
178    flock($fp, LOCK_EX);
179    $ftruncate_return = ftruncate($fp, 0);
180    flock($fp, LOCK_UN);
181    fclose($fp);
182    if (!$ftruncate_return) {
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__);
185        return false;
186    } else {
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__);
189        return true;
190    }
191}
192
193function archiveLog($log_file)
194{
195    $app =& App::getInstance();
196
197    $old_file_name = $log_file;
198    $new_file_name = $log_file . '__' . date('Y-m-d');
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__);
202        return false;
203    }
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__);
207        return false;
208    }
209
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__);
212    return true;
213}
214
215function printLog($log_file)
216{
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__);
222        return false;
223    }
224
225    readfile($app->getParam('log_directory') . '/' . $log_file);
226}
227
228function &getLog($log_file, $search_query='')
229{
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__);
235        return false;
236    }
237    $log = file($app->getParam('log_directory') . '/' . $log_file);
238
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
249function &getLogList()
250{
251    global $valid_file_extensions;
252    $app =& App::getInstance();
253
254    // Get a list of all files in the log directory.
255    $dir_handle = opendir($app->getParam('log_directory'));
256    $list = array();
257    while ($dir_handle && ($file = readdir($dir_handle)) !== false) {
258        if (!preg_match('/^\./', $file) && is_file($app->getParam('log_directory') . '/' . $file) && in_array(strtolower(Upload::getFilenameExtension($file)), $valid_file_extensions)) {
259            $list[] = array(
260                'filename' => $file,
261                'filesize' => filesize($app->getParam('log_directory') . '/' . $file),
262                'modified' => filemtime($app->getParam('log_directory') . '/' . $file),
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?>
Note: See TracBrowser for help on using the repository browser.