source: trunk/services/logs.php @ 1

Last change on this file since 1 was 1, checked in by scdev, 19 years ago

Initial import.

File size: 10.2 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);
11App::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    if (!file_exists(App::getParam('log_directory') . '/' . $log_file)) {
141        App::raiseMsg(sprintf(_("Log file %s does not exist."), $log_file), MSG_NOTICE, __FILE__, __LINE__);
142        App::logMsg(sprintf('Cannot delete nonexistent log file %s', App::getParam('log_directory') . '/' . $log_file), LOG_INFO, __FILE__, __LINE__);
143        return false;
144    }
145   
146    if (!is_writable(App::getParam('log_directory') . '/' . $log_file) && !is_writable(App::getParam('log_directory'))) {
147        App::raiseMsg(sprintf(_("Log file %s could not be deleted."), $log_file), MSG_NOTICE, __FILE__, __LINE__);
148        App::logMsg(sprintf('Cannot delete log file %s, not writable.', App::getParam('log_directory') . '/' . $log_file), LOG_INFO, __FILE__, __LINE__);
149        return false;
150    }
151   
152    if (unlink(App::getParam('log_directory') . '/' . $log_file)) {
153        App::raiseMsg(sprintf(_("Log file %s has been deleted."), $log_file), MSG_NOTICE, __FILE__, __LINE__);
154        App::logMsg(sprintf('Log file %s has been deleted', $log_file), LOG_INFO, __FILE__, __LINE__);
155        return true;
156    } else {
157        App::raiseMsg(sprintf(_("Log file %s could not be deleted."), $log_file), MSG_WARNING, __FILE__, __LINE__);
158        App::logMsg(sprintf('unlink failed on log file %s', App::getParam('log_directory') . '/' . $log_file), LOG_WARNING, __FILE__, __LINE__);
159        return false;
160    }
161}
162
163function clearLog($log_file)
164{   
165    if (!$fp = fopen(App::getParam('log_directory') . '/' . $log_file, 'r+')) {
166        App::raiseMsg(sprintf(_("Log file %s could not be opened."), $log_file), MSG_NOTICE, __FILE__, __LINE__);
167        App::logMsg(sprintf('fopen failed on log file %s', App::getParam('log_directory') . '/' . $log_file), LOG_INFO, __FILE__, __LINE__);
168        return false;
169    }
170   
171    flock($fp, LOCK_EX);
172    $ftruncate_return = ftruncate($fp, 0);
173    flock($fp, LOCK_UN);
174    fclose($fp);
175    if (!$ftruncate_return) {
176        App::raiseMsg(sprintf(_("Log file %s could not be cleared."), $log_file), MSG_WARNING, __FILE__, __LINE__);
177        App::logMsg(sprintf('ftruncate failed on log file %s', App::getParam('log_directory') . '/' . $log_file), LOG_WARNING, __FILE__, __LINE__);
178        return false;
179    } else {
180        App::raiseMsg(sprintf(_("Log file %s has been cleared."), $log_file), MSG_NOTICE, __FILE__, __LINE__);
181        App::logMsg(sprintf('Log file %s has been cleared', $log_file), LOG_INFO, __FILE__, __LINE__);
182        return true;
183    }
184}
185
186function archiveLog($log_file)
187{   
188    $old_file_name = $log_file;
189    $new_file_name = $log_file . '__' . date('Y-m-d');
190    If (!is_writable(App::getParam('log_directory') . '')) {
191        App::raiseMsg(sprintf('Cannot archive log, log directory not writable: %s', App::getParam('log_directory')), MSG_WARNING, __FILE__, __LINE__);
192        App::logMsg(sprintf('Cannot archive log, log directory not writable: %s', App::getParam('log_directory')), LOG_WARNING, __FILE__, __LINE__);
193        return false;
194    }
195    If (!copy(App::getParam('log_directory') . '/' . $old_file_name, App::getParam('log_directory') . '/' . $new_file_name)) {
196        App::raiseMsg(sprintf(_("Cannot archive log, copying old log file failed."), null), MSG_WARNING, __FILE__, __LINE__);
197        App::logMsg(sprintf('Cannot archive log, copying old log file failed.', null), LOG_WARNING, __FILE__, __LINE__);
198        return false;
199    }
200   
201    App::raiseMsg(sprintf(_("Log file %s has been archived to %s."), $old_file_name, $new_file_name), MSG_NOTICE, __FILE__, __LINE__);
202    App::logMsg(sprintf('Log file %s has been archived to %s.', $old_file_name, $new_file_name), LOG_NOTICE, __FILE__, __LINE__);
203    return true;
204}
205
206function printLog($log_file)
207{       
208    if (!is_file(App::getParam('log_directory') . '/' . $log_file)) {
209        App::raiseMsg(sprintf(_("Log file %s not found."), $log_file), MSG_WARNING, __FILE__, __LINE__);
210        App::logMsg(sprintf('Log file %s not found.', App::getParam('log_directory') . '/' . $log_file), LOG_WARNING, __FILE__, __LINE__);
211        return false;
212    }
213
214    readfile(App::getParam('log_directory') . '/' . $log_file);
215}
216
217function &getLog($log_file, $search_query='')
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    $log = file(App::getParam('log_directory') . '/' . $log_file);
225   
226    if ('' != trim($search_query)) {
227        if (getFormData('search_grep')) {
228            $log = preg_grep('/' . str_replace('/', '\/', $search_query) . '/', $log);
229        } else {
230            $log = preg_grep('/' . preg_quote($search_query, '/') . '/i', $log);
231        }
232    }
233    return array_values($log);
234}
235
236function &getLogList()
237{
238    global $valid_file_extensions;
239   
240    // Get a list of all files in the log directory.
241    $dir_handle = opendir(App::getParam('log_directory'));
242    $list = array();
243    while ($dir_handle && ($file = readdir($dir_handle)) !== false) {
244        if (!preg_match('/^\./', $file) && is_file(App::getParam('log_directory') . '/' . $file) && in_array(strtolower(Upload::getFilenameExtension($file)), $valid_file_extensions)) {
245            $list[] = array(
246                'filename' => $file,
247                'filesize' => filesize(App::getParam('log_directory') . '/' . $file),
248                'modified' => filemtime(App::getParam('log_directory') . '/' . $file),
249            );
250        }
251    }
252    if (is_array($list) && !empty($list)) {
253        sort($list);
254        return $list;
255    } else {
256        return false;
257    }
258}
259
260
261
262?>
Note: See TracBrowser for help on using the repository browser.