source: trunk/services/logs.php @ 480

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

Convert tabs to spaces, and lineendings to LF in all files.

File size: 10.9 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
27// require_once dirname(__FILE__) . '/_config.inc.php';
28
[457]29$app->sslOn();
[1]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
[334]43// Files with these extensions will be displayed at the top of the log list.
[1]44$valid_file_extensions = array('', 'txt', 'log');
45
[354]46// Files that shouldn't be deleted (preg search expressions).
[274]47$no_delete_files = '/^php_error_log$|^access_log$|^error_log$|^ssl_request_log$/';
[1]48
[354]49// Files that shouldn't be cleared (preg search expressions).
[1]50$no_clear_files = '/__|^access_log$|^error_log$|^ssl_request_log$/';
51
[354]52// Files that shouldn't be archived (preg search expressions).
[1]53$no_archive_files = '/__|^access_log$|^error_log$|^ssl_request_log$/';
54
[354]55// Files that shouldn't be downloaded (preg search expressions).
[1]56$no_download_files = '/^$/';
57
[153]58// Configure the prefs object.
59$tmp_prefs = new Prefs('admin_logs');
60$tmp_prefs->setParam(array('persistent' => false));
61$tmp_prefs->setDefaults(array(
[152]62    'log_file' => $app->getParam('log_filename')
63));
[254]64if (getFormData('log', false)) {
[457]65    $tmp_prefs->set('log_file', getFormData('log'));
[254]66}
[1]67
68// Titles and navigation header.
[202]69$nav->add(sprintf(_("Viewing log: <em>%s</em>"), $tmp_prefs->get('log_file')), '/admin/logs.php');
[1]70
[143]71/********************************************************************
72* MAIN
73********************************************************************/
[42]74
[1]75// Allow realtime file stats.
[42]76clearstatcache();
[1]77
78// What action to take.
79switch (getFormData('op')) {
80case 'delete' :
[153]81    deleteLog($tmp_prefs->get('log_file'));
82    $tmp_prefs->set('log_file', $app->getParam('log_filename'));
[136]83    if ($app->validBoomerangURL('app_log')) {
[1]84        // Display boomerang page.
[136]85        $app->dieBoomerangURL('app_log');
[1]86    }
87    // Display default page.
[136]88    $app->dieURL($_SERVER['PHP_SELF']);
[1]89    break;
[42]90
[1]91case 'clear' :
[153]92    clearLog($tmp_prefs->get('log_file'));
[136]93    if ($app->validBoomerangURL('app_log')) {
[1]94        // Display boomerang page.
[136]95        $app->dieBoomerangURL('app_log');
[1]96    }
97    // Display default page.
[136]98    $app->dieURL($_SERVER['PHP_SELF']);
[1]99    break;
[42]100
[1]101case 'archive' :
[153]102    if (archiveLog($tmp_prefs->get('log_file'))) {
[1]103        // Now flush current log.
[136]104        $app->dieURL($_SERVER['PHP_SELF'] . '?op=clear');
[1]105    }
[136]106    if ($app->validBoomerangURL('app_log')) {
[1]107        // Display boomerang page.
[136]108        $app->dieBoomerangURL('app_log');
[1]109    }
110    // Display default page.
[136]111    $app->dieURL($_SERVER['PHP_SELF']);
[1]112    break;
[42]113
[334]114// case 'output' :
115//     $main_template = 'output';
[1]116//     break;
[42]117
[1]118case 'download' :
119    header('Content-Type: application/octet-stream');
[153]120    header(sprintf('Content-Disposition: attachment; filename=%s.txt', $tmp_prefs->get('log_file')));
121    printLog($tmp_prefs->get('log_file'));
[1]122    die;
123    break;
[42]124
[1]125default :
[153]126    $list =& getLog($tmp_prefs->get('log_file'), getFormData('search_query'));
[1]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) {
[153]148    printLog($tmp_prefs->get('log_file'));
[1]149} else {
150    include 'codebase/services/templates/' . $main_template;
151}
152include 'footer.ihtml';
153
154
[143]155/********************************************************************
156* FUNCTIONS
157********************************************************************/
[1]158
159function deleteLog($log_file)
[42]160{
[479]161    $app =& App::getInstance();
[136]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__);
[1]166        return false;
167    }
[42]168
[136]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__);
[1]172        return false;
173    }
[42]174
[136]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__);
[1]178        return true;
179    } else {
[136]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__);
[1]182        return false;
183    }
184}
185
186function clearLog($log_file)
[42]187{
[479]188    $app =& App::getInstance();
[136]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__);
[1]193        return false;
194    }
[42]195
[1]196    flock($fp, LOCK_EX);
197    $ftruncate_return = ftruncate($fp, 0);
198    flock($fp, LOCK_UN);
199    fclose($fp);
200    if (!$ftruncate_return) {
[136]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__);
[1]203        return false;
204    } else {
[136]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__);
[1]207        return true;
208    }
209}
210
211function archiveLog($log_file)
[42]212{
[479]213    $app =& App::getInstance();
[136]214
[1]215    $old_file_name = $log_file;
216    $new_file_name = $log_file . '__' . date('Y-m-d');
[136]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__);
[1]220        return false;
221    }
[136]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__);
[1]225        return false;
226    }
[42]227
[136]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__);
[1]230    return true;
231}
232
233function printLog($log_file)
[42]234{
[479]235    $app =& App::getInstance();
[136]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__);
[1]240        return false;
241    }
242
[136]243    readfile($app->getParam('log_directory') . '/' . $log_file);
[1]244}
245
246function &getLog($log_file, $search_query='')
[42]247{
[479]248    $app =& App::getInstance();
[136]249
[254]250    $log = array();
251
[136]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__);
[254]255        return $log;
[1]256    }
[136]257    $log = file($app->getParam('log_directory') . '/' . $log_file);
[42]258
[1]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    }
[254]266    $log = array_values($log);
267    return $log;
[1]268}
269
270function &getLogList()
271{
272    global $valid_file_extensions;
[479]273    $app =& App::getInstance();
[42]274
[1]275    // Get a list of all files in the log directory.
[136]276    $dir_handle = opendir($app->getParam('log_directory'));
[1]277    $list = array();
278    while ($dir_handle && ($file = readdir($dir_handle)) !== false) {
[247]279        if (!preg_match('/^\./', $file) && is_file($app->getParam('log_directory') . '/' . $file) && in_array(mb_strtolower(Upload::getFilenameExtension($file)), $valid_file_extensions)) {
[1]280            $list[] = array(
281                'filename' => $file,
[136]282                'filesize' => filesize($app->getParam('log_directory') . '/' . $file),
283                'modified' => filemtime($app->getParam('log_directory') . '/' . $file),
[1]284            );
285        }
286    }
287    if (is_array($list) && !empty($list)) {
288        sort($list);
289    }
[254]290    return $list;
[1]291}
292
293
294
Note: See TracBrowser for help on using the repository browser.