source: trunk/services/logs.php @ 767

Last change on this file since 767 was 767, checked in by anonymous, 2 years ago

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

File size: 11.1 KB
RevLine 
[1]1<?php
2/**
[362]3 * The Strangecode Codebase - a general application development framework for PHP
4 * For details visit the project site: <http://trac.strangecode.com/codebase/>
[396]5 * Copyright 2001-2012 Strangecode, LLC
[457]6 *
[362]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.
[457]13 *
[362]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.
[457]18 *
[362]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/**
[42]24 * logs.php
[1]25 */
26
[497]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)));
[1]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
[334]46// Files with these extensions will be displayed at the top of the log list.
[1]47$valid_file_extensions = array('', 'txt', 'log');
48
[354]49// Files that shouldn't be deleted (preg search expressions).
[274]50$no_delete_files = '/^php_error_log$|^access_log$|^error_log$|^ssl_request_log$/';
[1]51
[354]52// Files that shouldn't be cleared (preg search expressions).
[1]53$no_clear_files = '/__|^access_log$|^error_log$|^ssl_request_log$/';
54
[354]55// Files that shouldn't be archived (preg search expressions).
[1]56$no_archive_files = '/__|^access_log$|^error_log$|^ssl_request_log$/';
57
[354]58// Files that shouldn't be downloaded (preg search expressions).
[1]59$no_download_files = '/^$/';
60
[153]61// Configure the prefs object.
62$tmp_prefs = new Prefs('admin_logs');
63$tmp_prefs->setParam(array('persistent' => false));
64$tmp_prefs->setDefaults(array(
[152]65    'log_file' => $app->getParam('log_filename')
66));
[254]67if (getFormData('log', false)) {
[457]68    $tmp_prefs->set('log_file', getFormData('log'));
[254]69}
[1]70
71// Titles and navigation header.
[763]72$nav->add(sprintf(_("Viewing <em>%s</em>"), oTxt($tmp_prefs->get('log_file'))), '/admin/logs.php');
[535]73$nav->set('id', 'logs');
[1]74
[143]75/********************************************************************
76* MAIN
77********************************************************************/
[42]78
[1]79// Allow realtime file stats.
[42]80clearstatcache();
[1]81
82// What action to take.
83switch (getFormData('op')) {
84case 'delete' :
[153]85    deleteLog($tmp_prefs->get('log_file'));
86    $tmp_prefs->set('log_file', $app->getParam('log_filename'));
[136]87    if ($app->validBoomerangURL('app_log')) {
[1]88        // Display boomerang page.
[136]89        $app->dieBoomerangURL('app_log');
[1]90    }
91    // Display default page.
[136]92    $app->dieURL($_SERVER['PHP_SELF']);
[1]93    break;
[42]94
[1]95case 'clear' :
[153]96    clearLog($tmp_prefs->get('log_file'));
[136]97    if ($app->validBoomerangURL('app_log')) {
[1]98        // Display boomerang page.
[136]99        $app->dieBoomerangURL('app_log');
[1]100    }
101    // Display default page.
[136]102    $app->dieURL($_SERVER['PHP_SELF']);
[1]103    break;
[42]104
[1]105case 'archive' :
[153]106    if (archiveLog($tmp_prefs->get('log_file'))) {
[1]107        // Now flush current log.
[136]108        $app->dieURL($_SERVER['PHP_SELF'] . '?op=clear');
[1]109    }
[136]110    if ($app->validBoomerangURL('app_log')) {
[1]111        // Display boomerang page.
[136]112        $app->dieBoomerangURL('app_log');
[1]113    }
114    // Display default page.
[136]115    $app->dieURL($_SERVER['PHP_SELF']);
[1]116    break;
[42]117
[334]118// case 'output' :
119//     $main_template = 'output';
[1]120//     break;
[42]121
[1]122case 'download' :
123    header('Content-Type: application/octet-stream');
[153]124    header(sprintf('Content-Disposition: attachment; filename=%s.txt', $tmp_prefs->get('log_file')));
125    printLog($tmp_prefs->get('log_file'));
[1]126    die;
127    break;
[42]128
[1]129default :
[153]130    $list =& getLog($tmp_prefs->get('log_file'), getFormData('search_query'));
[1]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
[767]150include 'header.' . $app->getParam('template_ext');
[1]151if ('output' == $main_template) {
[153]152    printLog($tmp_prefs->get('log_file'));
[1]153} else {
[497]154    include $main_template;
[1]155}
[767]156include 'footer.' . $app->getParam('template_ext');
[1]157
158
[143]159/********************************************************************
160* FUNCTIONS
161********************************************************************/
[1]162
163function deleteLog($log_file)
[42]164{
[479]165    $app =& App::getInstance();
[136]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__);
[1]170        return false;
171    }
[42]172
[136]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__);
[1]176        return false;
177    }
[42]178
[136]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__);
[1]182        return true;
183    } else {
[136]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__);
[1]186        return false;
187    }
188}
189
190function clearLog($log_file)
[42]191{
[479]192    $app =& App::getInstance();
[136]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__);
[1]197        return false;
198    }
[42]199
[1]200    flock($fp, LOCK_EX);
201    $ftruncate_return = ftruncate($fp, 0);
202    flock($fp, LOCK_UN);
203    fclose($fp);
204    if (!$ftruncate_return) {
[136]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__);
[1]207        return false;
208    } else {
[136]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__);
[1]211        return true;
212    }
213}
214
215function archiveLog($log_file)
[42]216{
[479]217    $app =& App::getInstance();
[136]218
[1]219    $old_file_name = $log_file;
220    $new_file_name = $log_file . '__' . date('Y-m-d');
[136]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__);
[1]224        return false;
225    }
[136]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__);
[1]229        return false;
230    }
[42]231
[136]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__);
[1]234    return true;
235}
236
237function printLog($log_file)
[42]238{
[479]239    $app =& App::getInstance();
[136]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__);
[1]244        return false;
245    }
246
[136]247    readfile($app->getParam('log_directory') . '/' . $log_file);
[1]248}
249
250function &getLog($log_file, $search_query='')
[42]251{
[479]252    $app =& App::getInstance();
[136]253
[254]254    $log = array();
255
[136]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__);
[254]259        return $log;
[1]260    }
[136]261    $log = file($app->getParam('log_directory') . '/' . $log_file);
[42]262
[1]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    }
[254]270    $log = array_values($log);
271    return $log;
[1]272}
273
274function &getLogList()
275{
276    global $valid_file_extensions;
[479]277    $app =& App::getInstance();
[42]278
[1]279    // Get a list of all files in the log directory.
[136]280    $dir_handle = opendir($app->getParam('log_directory'));
[1]281    $list = array();
282    while ($dir_handle && ($file = readdir($dir_handle)) !== false) {
[247]283        if (!preg_match('/^\./', $file) && is_file($app->getParam('log_directory') . '/' . $file) && in_array(mb_strtolower(Upload::getFilenameExtension($file)), $valid_file_extensions)) {
[1]284            $list[] = array(
285                'filename' => $file,
[136]286                'filesize' => filesize($app->getParam('log_directory') . '/' . $file),
287                'modified' => filemtime($app->getParam('log_directory') . '/' . $file),
[1]288            );
289        }
290    }
291    if (is_array($list) && !empty($list)) {
292        sort($list);
293    }
[254]294    return $list;
[1]295}
296
297
298
Note: See TracBrowser for help on using the repository browser.