source: trunk/services/logs.php @ 457

Last change on this file since 457 was 457, checked in by anonymous, 10 years ago

Removed use of requireAccessClearance(). Adjusted sequence of sslOn() and requireLogin(). Added ACL::requireAllow() method. Added arguments to SortOrder::set(). Changed behavior of Validator::validateStrDate(). Added use of Validator::validateStrDate() to module maker templates.

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