source: trunk/services/logs.php @ 677

Last change on this file since 677 was 535, checked in by anonymous, 9 years ago

Added nav page ids to service scripts. Logging unauthenticated sessions.

File size: 11.1 KB
Line 
1<?php
2/**
3 * The Strangecode Codebase - a general application development framework for PHP
4 * For details visit the project site: <http://trac.strangecode.com/codebase/>
5 * Copyright 2001-2012 Strangecode, LLC
6 *
7 * This file is part of The Strangecode Codebase.
8 *
9 * The Strangecode Codebase is free software: you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as published by the
11 * Free Software Foundation, either version 3 of the License, or (at your option)
12 * any later version.
13 *
14 * The Strangecode Codebase is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
17 * details.
18 *
19 * You should have received a copy of the GNU General Public License along with
20 * The Strangecode Codebase. If not, see <http://www.gnu.org/licenses/>.
21 */
22
23/**
24 * logs.php
25 */
26
27// Redefine include_path including the codebase/services but allow local templates override global ones.
28ini_set('include_path', join(PATH_SEPARATOR, array(
29    get_include_path(),
30    dirname(__FILE__) . '/templates'
31)));
32
33$app->sslOn();
34$auth->requireLogin();
35
36require_once 'codebase/lib/PageNumbers.inc.php';
37require_once 'codebase/lib/SortOrder.inc.php';
38require_once 'codebase/lib/TemplateGlue.inc.php';
39require_once 'codebase/lib/Prefs.inc.php';
40require_once 'codebase/lib/Upload.inc.php';
41
42
43/******************************************************************************
44 * CODE CONFIG
45 *****************************************************************************/
46
47// Files with these extensions will be displayed at the top of the log list.
48$valid_file_extensions = array('', 'txt', 'log');
49
50// Files that shouldn't be deleted (preg search expressions).
51$no_delete_files = '/^php_error_log$|^access_log$|^error_log$|^ssl_request_log$/';
52
53// Files that shouldn't be cleared (preg search expressions).
54$no_clear_files = '/__|^access_log$|^error_log$|^ssl_request_log$/';
55
56// Files that shouldn't be archived (preg search expressions).
57$no_archive_files = '/__|^access_log$|^error_log$|^ssl_request_log$/';
58
59// Files that shouldn't be downloaded (preg search expressions).
60$no_download_files = '/^$/';
61
62// Configure the prefs object.
63$tmp_prefs = new Prefs('admin_logs');
64$tmp_prefs->setParam(array('persistent' => false));
65$tmp_prefs->setDefaults(array(
66    'log_file' => $app->getParam('log_filename')
67));
68if (getFormData('log', false)) {
69    $tmp_prefs->set('log_file', getFormData('log'));
70}
71
72// Titles and navigation header.
73$nav->add(sprintf(_("Viewing <em>%s</em>"), $tmp_prefs->get('log_file')), '/admin/logs.php');
74$nav->set('id', 'logs');
75
76/********************************************************************
77* MAIN
78********************************************************************/
79
80// Allow realtime file stats.
81clearstatcache();
82
83// What action to take.
84switch (getFormData('op')) {
85case 'delete' :
86    deleteLog($tmp_prefs->get('log_file'));
87    $tmp_prefs->set('log_file', $app->getParam('log_filename'));
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
96case 'clear' :
97    clearLog($tmp_prefs->get('log_file'));
98    if ($app->validBoomerangURL('app_log')) {
99        // Display boomerang page.
100        $app->dieBoomerangURL('app_log');
101    }
102    // Display default page.
103    $app->dieURL($_SERVER['PHP_SELF']);
104    break;
105
106case 'archive' :
107    if (archiveLog($tmp_prefs->get('log_file'))) {
108        // Now flush current log.
109        $app->dieURL($_SERVER['PHP_SELF'] . '?op=clear');
110    }
111    if ($app->validBoomerangURL('app_log')) {
112        // Display boomerang page.
113        $app->dieBoomerangURL('app_log');
114    }
115    // Display default page.
116    $app->dieURL($_SERVER['PHP_SELF']);
117    break;
118
119// case 'output' :
120//     $main_template = 'output';
121//     break;
122
123case 'download' :
124    header('Content-Type: application/octet-stream');
125    header(sprintf('Content-Disposition: attachment; filename=%s.txt', $tmp_prefs->get('log_file')));
126    printLog($tmp_prefs->get('log_file'));
127    die;
128    break;
129
130default :
131    $list =& getLog($tmp_prefs->get('log_file'), getFormData('search_query'));
132    $main_template = 'log_list.ihtml';
133    break;
134}
135
136
137/******************************************************************************
138 * TEMPLATE INITIALIZATION
139 *****************************************************************************/
140
141$logs = &getLogList();
142
143// Instantiate page numbers. Total items are set and calculation is done in the getRecordList function.
144$page = new PageNumbers();
145$page->setPerPage(getFormData('per_page'), 500);
146$page->setPageNumber(getFormData('page_number'));
147$page->setTotalItems(sizeof($list));
148$page->per_page_options = array(100, 250, 500, 600, 800, 1000, 2000, 5000, 10000);
149$page->calculate();
150
151include 'header.ihtml';
152if ('output' == $main_template) {
153    printLog($tmp_prefs->get('log_file'));
154} else {
155    include $main_template;
156}
157include 'footer.ihtml';
158
159
160/********************************************************************
161* FUNCTIONS
162********************************************************************/
163
164function deleteLog($log_file)
165{
166    $app =& App::getInstance();
167
168    if (!file_exists($app->getParam('log_directory') . '/' . $log_file)) {
169        $app->raiseMsg(sprintf(_("Log file %s does not exist."), $log_file), MSG_NOTICE, __FILE__, __LINE__);
170        $app->logMsg(sprintf('Cannot delete nonexistent log file %s', $app->getParam('log_directory') . '/' . $log_file), LOG_INFO, __FILE__, __LINE__);
171        return false;
172    }
173
174    if (!is_writable($app->getParam('log_directory') . '/' . $log_file) && !is_writable($app->getParam('log_directory'))) {
175        $app->raiseMsg(sprintf(_("Log file %s could not be deleted."), $log_file), MSG_NOTICE, __FILE__, __LINE__);
176        $app->logMsg(sprintf('Cannot delete log file %s, not writable.', $app->getParam('log_directory') . '/' . $log_file), LOG_INFO, __FILE__, __LINE__);
177        return false;
178    }
179
180    if (unlink($app->getParam('log_directory') . '/' . $log_file)) {
181        $app->raiseMsg(sprintf(_("Log file %s has been deleted."), $log_file), MSG_NOTICE, __FILE__, __LINE__);
182        $app->logMsg(sprintf('Log file %s has been deleted', $log_file), LOG_INFO, __FILE__, __LINE__);
183        return true;
184    } else {
185        $app->raiseMsg(sprintf(_("Log file %s could not be deleted."), $log_file), MSG_WARNING, __FILE__, __LINE__);
186        $app->logMsg(sprintf('unlink failed on log file %s', $app->getParam('log_directory') . '/' . $log_file), LOG_WARNING, __FILE__, __LINE__);
187        return false;
188    }
189}
190
191function clearLog($log_file)
192{
193    $app =& App::getInstance();
194
195    if (!$fp = fopen($app->getParam('log_directory') . '/' . $log_file, 'r+')) {
196        $app->raiseMsg(sprintf(_("Log file %s could not be opened."), $log_file), MSG_NOTICE, __FILE__, __LINE__);
197        $app->logMsg(sprintf('fopen failed on log file %s', $app->getParam('log_directory') . '/' . $log_file), LOG_INFO, __FILE__, __LINE__);
198        return false;
199    }
200
201    flock($fp, LOCK_EX);
202    $ftruncate_return = ftruncate($fp, 0);
203    flock($fp, LOCK_UN);
204    fclose($fp);
205    if (!$ftruncate_return) {
206        $app->raiseMsg(sprintf(_("Log file %s could not be cleared."), $log_file), MSG_WARNING, __FILE__, __LINE__);
207        $app->logMsg(sprintf('ftruncate failed on log file %s', $app->getParam('log_directory') . '/' . $log_file), LOG_WARNING, __FILE__, __LINE__);
208        return false;
209    } else {
210        $app->raiseMsg(sprintf(_("Log file %s has been cleared."), $log_file), MSG_NOTICE, __FILE__, __LINE__);
211        $app->logMsg(sprintf('Log file %s has been cleared', $log_file), LOG_INFO, __FILE__, __LINE__);
212        return true;
213    }
214}
215
216function archiveLog($log_file)
217{
218    $app =& App::getInstance();
219
220    $old_file_name = $log_file;
221    $new_file_name = $log_file . '__' . date('Y-m-d');
222    If (!is_writable($app->getParam('log_directory') . '')) {
223        $app->raiseMsg(sprintf('Cannot archive log, log directory not writable: %s', $app->getParam('log_directory')), MSG_WARNING, __FILE__, __LINE__);
224        $app->logMsg(sprintf('Cannot archive log, log directory not writable: %s', $app->getParam('log_directory')), LOG_WARNING, __FILE__, __LINE__);
225        return false;
226    }
227    If (!copy($app->getParam('log_directory') . '/' . $old_file_name, $app->getParam('log_directory') . '/' . $new_file_name)) {
228        $app->raiseMsg(sprintf(_("Cannot archive log, copying old log file failed."), null), MSG_WARNING, __FILE__, __LINE__);
229        $app->logMsg(sprintf('Cannot archive log, copying old log file failed.', null), LOG_WARNING, __FILE__, __LINE__);
230        return false;
231    }
232
233    $app->raiseMsg(sprintf(_("Log file %s has been archived to %s."), $old_file_name, $new_file_name), MSG_NOTICE, __FILE__, __LINE__);
234    $app->logMsg(sprintf('Log file %s has been archived to %s.', $old_file_name, $new_file_name), LOG_NOTICE, __FILE__, __LINE__);
235    return true;
236}
237
238function printLog($log_file)
239{
240    $app =& App::getInstance();
241
242    if (!is_file($app->getParam('log_directory') . '/' . $log_file)) {
243        $app->raiseMsg(sprintf(_("Log file %s not found."), $log_file), MSG_WARNING, __FILE__, __LINE__);
244        $app->logMsg(sprintf('Log file %s not found.', $app->getParam('log_directory') . '/' . $log_file), LOG_WARNING, __FILE__, __LINE__);
245        return false;
246    }
247
248    readfile($app->getParam('log_directory') . '/' . $log_file);
249}
250
251function &getLog($log_file, $search_query='')
252{
253    $app =& App::getInstance();
254
255    $log = array();
256
257    if (!is_file($app->getParam('log_directory') . '/' . $log_file)) {
258        $app->raiseMsg(sprintf(_("Log file %s not found."), $log_file), MSG_WARNING, __FILE__, __LINE__);
259        $app->logMsg(sprintf('Log file %s not found.', $app->getParam('log_directory') . '/' . $log_file), LOG_WARNING, __FILE__, __LINE__);
260        return $log;
261    }
262    $log = file($app->getParam('log_directory') . '/' . $log_file);
263
264    if ('' != trim($search_query)) {
265        if (getFormData('search_grep')) {
266            $log = preg_grep('/' . str_replace('/', '\/', $search_query) . '/', $log);
267        } else {
268            $log = preg_grep('/' . preg_quote($search_query, '/') . '/i', $log);
269        }
270    }
271    $log = array_values($log);
272    return $log;
273}
274
275function &getLogList()
276{
277    global $valid_file_extensions;
278    $app =& App::getInstance();
279
280    // Get a list of all files in the log directory.
281    $dir_handle = opendir($app->getParam('log_directory'));
282    $list = array();
283    while ($dir_handle && ($file = readdir($dir_handle)) !== false) {
284        if (!preg_match('/^\./', $file) && is_file($app->getParam('log_directory') . '/' . $file) && in_array(mb_strtolower(Upload::getFilenameExtension($file)), $valid_file_extensions)) {
285            $list[] = array(
286                'filename' => $file,
287                'filesize' => filesize($app->getParam('log_directory') . '/' . $file),
288                'modified' => filemtime($app->getParam('log_directory') . '/' . $file),
289            );
290        }
291    }
292    if (is_array($list) && !empty($list)) {
293        sort($list);
294    }
295    return $list;
296}
297
298
299
Note: See TracBrowser for help on using the repository browser.