source: trunk/services/logs.php @ 767

Last change on this file since 767 was 767, checked in by anonymous, 23 months ago

Add App param ‘template_ext’ used to inform services where to find header and footer templates. Minor fixes.

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