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
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.
41Prefs::setDefault('log_file', $app->getParam('log_filename'), 'logs_module');
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 *****************************************************************************/
50
51// Allow realtime file stats.
52clearstatcache();
53
54// What action to take.
55switch (getFormData('op')) {
56case 'delete' :
57//     $auth->requireAccessClearance(ZONE_ADMIN_APPLOG_FUNC_RESET);
58    deleteLog(Prefs::getValue('log_file', 'logs_module'));
59    Prefs::setValue('log_file', $app->getParam('log_filename'), 'logs_module');
60    if ($app->validBoomerangURL('app_log')) {
61        // Display boomerang page.
62        $app->dieBoomerangURL('app_log');
63    }
64    // Display default page.
65    $app->dieURL($_SERVER['PHP_SELF']);
66    break;
67
68case 'clear' :
69//     $auth->requireAccessClearance(ZONE_ADMIN_APPLOG_FUNC_RESET);
70    clearLog(Prefs::getValue('log_file', 'logs_module'));
71    if ($app->validBoomerangURL('app_log')) {
72        // Display boomerang page.
73        $app->dieBoomerangURL('app_log');
74    }
75    // Display default page.
76    $app->dieURL($_SERVER['PHP_SELF']);
77    break;
78
79case 'archive' :
80//     $auth->requireAccessClearance(ZONE_ADMIN_APPLOG_FUNC_RESET);
81    if (archiveLog(Prefs::getValue('log_file', 'logs_module'))) {
82        // Now flush current log.
83        $app->dieURL($_SERVER['PHP_SELF'] . '?op=clear');
84    }
85    if ($app->validBoomerangURL('app_log')) {
86        // Display boomerang page.
87        $app->dieBoomerangURL('app_log');
88    }
89    // Display default page.
90    $app->dieURL($_SERVER['PHP_SELF']);
91    break;
92
93// case 'ouput' :
94//     $main_template = 'ouput';
95//     break;
96
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;
103
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)
139{
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__);
145        return false;
146    }
147
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__);
151        return false;
152    }
153
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__);
157        return true;
158    } else {
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__);
161        return false;
162    }
163}
164
165function clearLog($log_file)
166{
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__);
172        return false;
173    }
174
175    flock($fp, LOCK_EX);
176    $ftruncate_return = ftruncate($fp, 0);
177    flock($fp, LOCK_UN);
178    fclose($fp);
179    if (!$ftruncate_return) {
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__);
182        return false;
183    } else {
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__);
186        return true;
187    }
188}
189
190function archiveLog($log_file)
191{
192    $app =& App::getInstance();
193
194    $old_file_name = $log_file;
195    $new_file_name = $log_file . '__' . date('Y-m-d');
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__);
199        return false;
200    }
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__);
204        return false;
205    }
206
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__);
209    return true;
210}
211
212function printLog($log_file)
213{
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__);
219        return false;
220    }
221
222    readfile($app->getParam('log_directory') . '/' . $log_file);
223}
224
225function &getLog($log_file, $search_query='')
226{
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__);
232        return false;
233    }
234    $log = file($app->getParam('log_directory') . '/' . $log_file);
235
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;
249    $app =& App::getInstance();
250
251    // Get a list of all files in the log directory.
252    $dir_handle = opendir($app->getParam('log_directory'));
253    $list = array();
254    while ($dir_handle && ($file = readdir($dir_handle)) !== false) {
255        if (!preg_match('/^\./', $file) && is_file($app->getParam('log_directory') . '/' . $file) && in_array(strtolower(Upload::getFilenameExtension($file)), $valid_file_extensions)) {
256            $list[] = array(
257                'filename' => $file,
258                'filesize' => filesize($app->getParam('log_directory') . '/' . $file),
259                'modified' => filemtime($app->getParam('log_directory') . '/' . $file),
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.