Location: PHPKode > projects > Yappa-ng > yappa-ng/toolbox_logview.php
<?php

/*
 * ***********************************************************************************************
 * Project:      yappa-ng (yet another php photo album - next generation)
 * Filename:     toolbox_logview.php
 * Type:         Standalone Script
 * Module:       Toolbox / Viewlog
 * Subcategory:  Logfileviewer 
 * Description:  Standalone or Toolbox activated Logfileviewer 
 *               for proprietary yappa-ng logfile format (including Backup-Logfiles)
 * ***********************************************************************************************
 * Author:       Fritz Berger <hide@address.com>
 * Copyright:    2004 Fritz Berger
 * Homepage:     http://www.zirkon.at/zirkon/scripts/yappa-ng/yappa-ng_main_eng.html
 * ***********************************************************************************************
 * $Header: /cvsroot/yappa-ng/yappa-ng/toolbox_logview.php,v 1.12 2004/10/03 13:01:07 zirkon13 Exp $
 * ***********************************************************************************************
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

require("config.inc.php");
require($config['path_src_include'] . "common.inc.php");
require(singleslash($config['source_root'] . "/toolbox_modules/toolbox_lang_eng.inc.php"));

/*
 * *******************************************************************
 * Setting of Error reporting within the Setup-Tool!
 *
 * Syntax
 * error_reporting(E_ALL & ~(E_WARNING | E_NOTICE | ...));
 *
 * Standard:  error_reporting(E_ALL & ~(E_WARNING | E_NOTICE ));
 *            Enable real ERRORS only!
 * Debugging: error_reporting(E_ALL);
 *            Enable Errrors, Warnings and the Notice of undefined variables
 * *******************************************************************
 */
// error_reporting(E_ALL);
error_reporting(E_ALL & ~(E_WARNING | E_NOTICE));

function check_stat($level) {
	global $tools;
	global $config;

	print "<TD align='center'>";
	if((int)$level & (int)$tools['log_level']) {
		print "<font color='green'>enabled</font></TD>";
	} else {
		print "<font color='red'>disabled</font></TD>";
	}
	print "<TD align='center'>";
	if((int)$level & (int)$config['loglevel']) { 
		print "<font color='green'>enabled</font></TD>";
	} else {
		print "<font color='red'>disabled</font></TD>";
	}
}


/*
 * *******************************************************************
 * toolbox_viewlog.php is a standalone yappa-ng tool.
 *
 * You can call it with parameters.
 *
 * Per default the view options are saved in a file
 * You may override these options with parameters for single calls
 * If no parameters or not all are set the LogView Tool has a set of Default Values:
 *  - show the log in revers order (last entry (=most recent) first)
 *  - cut the logfile (show only the first (or last) xx lines of the log)
 *  - The number of lines if cut are 50
 *  - show no debug parameters
 *
 * To make it more easy to call this Tool you may call it from the yappa-ng ToolBox
 * There you may configure the parameters with click and submit.
 * *******************************************************************
 */

$log_options_path = singleslash($config['photo_root'] . "/" . $config['config_dirname'] . "/toolbox_options.txt");
if(!file_exists($log_options_path) || !is_readable($log_options_path) || !$fd = fopen($log_options_path, "r")) {
	// error!
	// log level 1: Administration
	if((int)"1" & (int)$config["loglevel"]) {
		$log_msg = date("U") . "|Administration|" . "ToolBox - Logfile Viewer: Could not read the logfile options [photo_root]/_config/toolbox_options.txt|" . $_SERVER["REMOTE_ADDR"] . "|-|" . basename(__FILE__) . "|" . __LINE__ . "\n";
		@error_log($log_msg, 3, "yappa-ng.log");
	}
	$answer = 0;
} else {
	flock($fd, 1); // get a shared lock
	$data = fread($fd, filesize($log_options_path));
	flock($fd, 3); // release the lock
	fclose($fd);
	$tools = unserialize( stripslashes($l_help));  //strip slashes to prevent 'magic quotes' problems on some servers
	$answer = 1;
}


/* Check if the SortOrder is set.
 * Default sortorder if not set is "yes"
 * If it is set check if its no.
 * If its not set or not no if set than set it to yes
 */
if (isset($_GET['sort'])) {
	$tools['log_sort'] = rawurldecode($_GET['sort']);
} else {
	if(!isset($tools['log_sort'])) {
		$tools['log_sort'] = "yes";
	}
}
if ($tools['log_sort'] != "no") {
	$tools['log_sort'] = "yes";
}

/* Check if the Logfile cut is set.
 * Default if not set is "yes", cut the file at 50 lines
 * If it is set check if its no.
 * If its not set or not no if set than set it to yes
 */
if (isset($_GET['cut'])) {
	$tools['log_cut'] = rawurldecode($_GET['cut']);
} else {
	if(!isset($tools['log_cut'])) {
		$tools['log_cut'] = "yes";
	}
}
if ($tools['log_cut'] != "no") {
	$tools['log_cut'] = "yes";
}
/* Check if the number of lines are set for Logfile Cut.
 * Default (if not set otherwise) is 50 lines
 * If it is set check if its an integer.
 * If its not set or not integer if set than set it to 50
 */
if (isset($_GET['cut_number'])) {
	$tools['log_cut_number'] = intval(rawurldecode($_GET['cut_number']));
} else {
	if(!isset($tools['log_cut_number'])) {
		$tools['log_cut_number'] = 50;
	}
}
if ($tools['log_cut_number'] < 1) {
	$tools['log_cut_number'] = 50;
}

/* Check if the Debug View option is set.
 * Default (if not set otherwise)
 * If it is set check if its no.
 * If its not set or not no if set than set it to yes
 */
if (isset($_GET['debug'])) {
	$tools['log_debug'] = rawurldecode($_GET['debug']);
} else {
	if(!isset($tools['log_debug'])) {
		$tools['log_debug'] = "no";
		$tools['log_debug_on'] = FALSE;
		$tools['log_colspan'] = 4;
	}
}
if ($tools['log_debug'] != "yes") {
	$tools['log_debug'] = "no";
	$tools['log_debug_on'] = FALSE;
	$tools['log_colspan'] = 4;
} else {
	$tools['log_debug_on'] = TRUE;
	$tools['log_colspan'] = 8;
}

/* Check if the level of logging which will get shown is set.
 * Default (if not set otherwise) is 63 (view ALL, but without Debug Levels)
 * If it is set check if its an integer and 1 < type < 256.
 * If its not set or not valid if set than set it to 63.
 * If its not set and DEMO Mode is ON set it to 255.
 *
 * No harm is done if you select a loglevel which 
 * is deselected in the Logfile Configuration. It just is not there! :-)
 */
if (isset($_GET['loglevel'])) {
	$tools['log_level'] = intval(rawurldecode($_GET['loglevel']));
} else {
	if(!isset($tools['log_level'])) {
		$tools['log_level'] = 63;
	}
}
if (($tools['log_level'] < 1) || ($tools['log_level'] > 2048)) {
	$tools['log_level'] = 63;
}

/* Check which logfile to view.
 * Default is the yappa-ng.log
 * If the parameters are set you can view the backup logs.
 */
if (isset($_GET['logfile'])) {
	$logfile_name = rawurldecode($_GET['logfile']);
} else {
	$logfile_name = "yappa-ng.log";
}


?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">


<HTML>
<HEAD>
<TITLE>yappa-ng Logfile Viewer</TITLE>
<META http-equiv="expires" content="0">
<LINK rel="SHORTCUT ICON" href="./images/favicon.ico">
<META http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<META name="author" content="Fritz Berger">
<META name="description" content="yappa-ng Logfile Viewer(Yet Another PHP Photo Album - Next Generation)">
<STYLE type="text/css">
<!--

body { font-family:verdana, arial, helvetica, sans-serif;font-size:100%;background-color:#FFFFFF;margin-left:0;margin-right:0;margin-top:0;margin-bottom:0;}
.bgcolor2 {background-color: #FFFFFF;}
.textTitle {font-family:Times New Roman, Times, serif;font-size:200%;color:#0000CC;font-weight: 700;}
.headerMain {font-family:Arial, Geneva, Helvetica, sans-serif;font-size:12px;font-weight:700;background-color: #FFFFFF;color:#000000;}
.riderTxtActive {font-family:Arial, Geneva, Helvetica, sans-serif;font-size:12px;font-weight:700;color:#FF9966;}
.riderTxtNotactive {font-family:Arial, Geneva, Helvetica, sans-serif;font-size:12px;font-weight:700;color:#000000;}
.riderbarTr {vertical-align:middle;	background-color:#FFFFFF;}
.riderbarNotactive {vertical-align:middle;background-color:#FFFFFF;background-image:url(themes/themes_riders_1.gif);}
.riderbarActive {vertical-align:middle;background-color:#FFFFFF;background-image:url(themes/themes_riders_2.gif);}
.riderbarBlank {background-color:#FFFFFF;background-image:url(themes/themes_riders_0.gif);}
.dateTxt {font-family:Arial, Geneva, Helvetica, sans-serif;font-size:12px;font-weight:700;color:#FF9966;}
.menubarCell {background-color: #0044b6;}
.subalbumsBar {background-color: #F0CC51;}
.albumCellA {background-color: #EEEEFF}
.albumCellB {background-color: #DDDDFF}
.thumbnailCell {background-color: #DAE9FC;}
.adminHeading {font-family: Arial, Geneva, Helvetica, sans-serif;font-size:150%;font-weight:bold;color:#000000;margin-bottom:5px;}
.adminDescription {font-family: Arial, Geneva, Helvetica, sans-serif;font-size:100%;font-weight:bold;color:#000000;margin-top:10px;}
.adminComments {font-family: Arial, Geneva, Helvetica, sans-serif;font-size:70%;font-style:italic;color: #000000;}

.admin {font-family: Arial, Geneva, Helvetica, sans-serif;font-size:70%;font-weight:bold;color:#000066;}
.critical {font-family: Arial, Geneva, Helvetica, sans-serif;font-size:70%;font-weight:bold;color:#FF0000;}
.error {font-family: Arial, Geneva, Helvetica, sans-serif;font-size:70%;color:#FF0000;}
.warning {font-family: Arial, Geneva, Helvetica, sans-serif;font-size:70%;color:#ff0000;}
.remote {font-family: Arial, Geneva, Helvetica, sans-serif;font-size:70%;color:#FF0000;}
.informal {font-family: Arial, Geneva, Helvetica, sans-serif;font-size:70%;font-style:italic;color:#006000;}
.debug1 {font-family: Arial, Geneva, Helvetica, sans-serif;font-size:70%;font-style:italic;color:#000000;}
.debug2 {font-family: Arial, Geneva, Helvetica, sans-serif;font-size:70%;font-style:italic;color:#000000;}

.Heading { font-family: Arial, Geneva, Helvetica, sans-serif; font-size: 200%; font-weight:bold; color: #000000; margin-bottom:5px; }
.links { font-family: Arial, Geneva, Helvetica, sans-serif; font-size: 70%; color: #000000; margin-bottom:5px; }
.Description { font-size: 120%; font-weight:bold; color: #0000FF; margin-top:10px; }
.mainComment { font-family: Arial, Geneva, Helvetica, sans-serif; font-size: 100%; font-weight:bold; color: #000000;  margin-bottom:0px; }
.Comments { font-family: Arial, Geneva, Helvetica, sans-serif; font-size: 100%; font-style:italic; color: #000000; }
.thumbnailCell { background-color: #DAE9FC; }
.hint { font-family: Arial, Geneva, Helvetica, sans-serif; font-size: 120%; font-weight:bold; color: #008000; }
.errormsg { font-family: Arial, Geneva, Helvetica, sans-serif; font-size: 120%; font-weight:bold; color: #FF0000; }
.errordesc { font-family: Arial, Geneva, Helvetica, sans-serif; font-size: 100%; font-weight:bold; color: #FF0000; }
.gratulation { font-family: Arial, Geneva, Helvetica, sans-serif; font-size: 150%; font-weight:bold; color: #008000; }

-->
</STYLE>
</HEAD>
<BODY>
<TABLE border="1">


<?php
$l_found_one_item = 0;
$arrLog = array();
$h_array = array();
$date_first = date("U");
$date_last = 0;
if(file_exists($logfile_name) && is_readable($logfile_name) && ($fd = fopen($logfile_name, "r"))) {

	$i = 0;
	while(!feof($fd)) {
		$data = fgets($fd, 2048);
		$h_array = explode("|",$data);
		$arrLog[$i]["date"] = $h_array[0];
		$arrLog[$i]["loglevel"] = $h_array[1];
		$arrLog[$i]["msg"] = $h_array[2];
		$arrLog[$i]["IP"] = $h_array[3];
		$arrLog[$i]["browserinfo"] = $h_array[4];
		$arrLog[$i]["filename"] = $h_array[5];
		$arrLog[$i]["line"] = $h_array[6];
		if($arrLog[$i]['date'] > $date_last) {
			$date_last = $arrLog[$i]['date'];
		}
		if(($arrLog[$i]['date'] < $date_first) && ($arrLog[$i]['date'] != 0)){
			$date_first = $arrLog[$i]['date'];
		}
		$i++;
	}
	fclose($fd);
	if ($i > 1) {
		$i--;
	}
	$days_help = $date_last - $date_first;
	if($days_help == 0) {
		$days = 0;
	} else {
		// 1 day = 60 * 60 * 24 (86400) seconds
		/*
		$days = (int) ($days_help / 86400);
		$days_name = " days";
		if($days == 0) {
		*/
		$days = round(($days_help / 86400),1);
		$days_name = "&nbsp;" . $lang_tools["days"];
		if($days < 1) {
			$days = round(($days_help / 3600),2);
			$days_name = "&nbsp;" . $lang_tools["hours"];
		}

	}
	/* check filesize of logfile */
	$l_logsize_help = filesize($logfile_name);
	/* function get_verbose_filesize is located in common.inc.php */
	$l_logsize = get_verbose_fsize($l_logsize_help);

}
$l_sortmsg = "";
$l_cutmsg = "";
$l_use = FALSE;
if($tools['log_sort'] == "yes") {
	rsort($arrLog);
	reset($arrLog);
	$l_sortmsg = $lang_tools["view_sort"];
	$l_use = TRUE;
}
$lines = $i;
if($tools['log_cut'] == "yes") {
	if($tools['log_cut_number'] < $i) {
		$i = (int) $tools['log_cut_number'];
	}
	$l_cutmsg = $lang_tools["view_cut1"] . $i . $lang_tools["view_cut2"];
	$l_use = TRUE;
}
?>
<BR>
<p class="adminHeading"><?php print $lang_tools['view_mainh']; ?>  <a name="top">&nbsp;</a> ( <a href="#stat"><?php print $lang_tools['view_statlink']; ?></a> ) </p>
<TABLE border="1" width="100%">
<TR><TD colspan="<?php print $tools['log_colspan']; ?>" bgcolor="green"><CENTER><B><font color="yellow"><?php print $lang_tools["view_main"] . $logfile_name . "<BR>" . date('d.M y-H:i:s', $date_first) . " - " . date('d.M y-H:i:s', $date_last) . " [ " . $days . $days_name . " ]"; ?></font></B></CENTER></TD></TR>
<TR><TD colspan="<?php print $tools['log_colspan']; ?>"><CENTER><?php print $lang_tools["view_size1"] . $lines . $lang_tools["view_size2"] . " <b>(" . $l_logsize . ")</b>."; ?> </CENTER></TD></TR>
<?php
if($l_use) { ?>
<TR><TD colspan="<?php print $tools['log_colspan']; ?>"><CENTER><?php print $l_cutmsg . $l_sortmsg; ?></CENTER></TD></TR>
<?php } ?>
<?php
if($tools['log_debug_on']) { 
	print "<TR><TH>#</TH><TH>" . $lang_tools["view_date"] . "</TH><TH>" . $lang_tools["view_loglevel"] . "</TH><TH>" . $lang_tools["view_message"] . "</TH><TH>" . $lang_tools["view_remIP"] . "</TH><TH>" . $lang_tools["view_remBrows"] . "</TH><TH>" . $lang_tools["view_source"] . "</TH><TH>" . $lang_tools["view_line"] . "</TH></TR>";
} else { 
	print "<TR><TH>#</TH><TH>" . $lang_tools["view_date"] . "</TH><TH>" . $lang_tools["view_loglevel"] . "</TH><TH>" . $lang_tools["view_message"] . "</TH></TR>";
}
$log = array();

$log['adm']['abs'] = 0;
$log['adm']['show'] = 0;
$log['cri']['abs'] = 0;
$log['cri']['show'] = 0;
$log['err']['abs'] = 0;
$log['err']['show'] = 0;
$log['war']['abs'] = 0;
$log['war']['show'] = 0;
$log['inf']['abs'] = 0;
$log['inf']['show'] = 0;
$log['rem']['abs'] = 0;
$log['rem']['show'] = 0;
$log['c_r']['abs'] = 0;
$log['c_r']['show'] = 0;
$log['dl1']['abs'] = 0;
$log['dl1']['show'] = 0;
$log['dl2']['abs'] = 0;
$log['dl2']['show'] = 0;
$log['nop']['abs'] = 0;
$log['nop']['show'] = 0;

if (count($arrLog) > 0 ) {

	$k = $i+1;
	$printcount = 0;
	for ($j = 0; $j < $lines; $j++) {
		switch ($arrLog[$j]['loglevel']) {
			case "Administration":	$l_level = 1;
						$log_type = "adm";
						$start_tag = "<TR class='admin'>";
						break;
			case "Critical":	$l_level = 2;
						$log_type = "cri";
						$start_tag = "<TR class='critical'>";
						break;
			case "Error":	$l_level = 4;
						$log_type = "err";
						$start_tag = "<TR class='error'>";
						break;
			case "Warning":	$l_level = 8;
						$log_type = "war";
						$start_tag = "<TR class='warning'>";
						break;
			case "Informal":	$l_level = 16;
						$log_type = "inf";
						$start_tag = "<TR class='informal'>";
						break;
			case "Remote":	$l_level = 32;
						$log_type = "rem";
						$start_tag = "<TR class='remote'>";
						break;
			case "Critical&Remote":	$l_level = 34;
						$log_type = "c_r";
						$start_tag = "<TR class='critical'>";
						break;
			case "Debug Level 1":	$l_level = 64;
						$log_type = "dl1";
						$start_tag = "<TR class='debug1'>";
						break;
			case "Debug Level 2":	$l_level = 128;
						$log_type = "dl2";
						$start_tag = "<TR class='debug2'>";
						break;
			default:		$l_level = 63;
						$log_type = "nop";
						$start_tag = "<TR class='debug1'>";
						break;
		}		
		$log[$log_type]['abs']++;
		if((int)$l_level & (int)$tools['log_level']) {
			if ($printcount < $i) {
				$log[$log_type]['show']++;
				$k--;
				print $start_tag . "<TD align='center'>";
				print $k . "</TD><TD align='center'>" . date('d.m.y-H:i:s', $arrLog[$j]['date']) . "</TD><TD align='center'>" . $arrLog[$j]['loglevel'] . "</TD><TD align='left'>" . $arrLog[$j]['msg'];
				if($tools['log_debug_on']) {
					print "</TD><TD align='center'>" . $arrLog[$j]['IP'] .  "</TD><TD align='left'>" . $arrLog[$j]['browserinfo'] . "</TD><TD align='left'>" . $arrLog[$j]['filename'] . "</TD><TD align='center'>" . $arrLog[$j]['line'];
				}
				print "</TD></TR>";
				$printcount++;
			} else {
				// nop
			}
		} else {
			// nop
		}

	}
}
?>
</TABLE>
<BR>
&nbsp;&nbsp;<a href="#top"><?php print $lang_tools["top"]; ?></a>
<a name="stat">&nbsp;</a>
<p class="adminHeading"><?php print $lang_tools["view_stath"]; ?></p>
<p class="mainComment">The <?php print $lines . $lang_tools["view_stat"]; ?></p>
<TABLE border="1">
<?php 
print "<TR><TH>" . $lang_tools["view_stat1"] . "</TH><TH>" . $lang_tools["view_stat2"] . "</TH><TH>" . $lang_tools["view_stat3"] . "</TH><TH>" . $lang_tools["view_stat4"] . "</TH><TH>" . $lang_tools["view_stat5"] . "</TH></TR>"; 
print "<TR><TD>Administration: </TD><TD align='center'> " . $log['adm']['abs'] . " </TD><TD align='center'> " . $log['adm']['show'] . " </TD> ";
	check_stat("1");
print "</TR>";
print "<TR><TD>Critical: </TD><TD align='center'> " . $log['cri']['abs'] . " </TD><TD align='center'> " . $log['cri']['show'] . " </TD> ";
	check_stat("2");
print "</TR>";
print "<TR><TD>Error: </TD><TD align='center'> " . $log['err']['abs'] . " </TD><TD align='center'> " . $log['err']['show'] . " </TD> ";
	check_stat("4");
print "</TR>";
print "<TR><TD>Warning: </TD><TD align='center'> " . $log['war']['abs'] . " </TD><TD align='center'> " . $log['war']['show'] . " </TD> ";
	check_stat("8");
print "</TR>";
print "<TR><TD>Informal: </TD><TD align='center'> " . $log['inf']['abs'] . " </TD><TD align='center'> " . $log['inf']['show'] . " </TD> ";
	check_stat("16");
print "</TR>";
print "<TR><TD>Remote: </TD><TD align='center'> " . $log['rem']['abs'] . " </TD><TD align='center'> " . $log['rem']['show'] . " </TD> ";
	check_stat("32");
print "</TR>";
print "<TR><TD>Critical & Remote: </TD><TD align='center'> " . $log['c_r']['abs'] . " </TD><TD align='center'> " . $log['c_r']['show'] . " </TD> ";
	check_stat("34");
print "</TR>";
print "<TR><TD>Debug Level 1: </TD><TD align='center'> " . $log['dl1']['abs'] . " </TD><TD align='center'> " . $log['dl1']['show'] . " </TD> ";
	check_stat("64");
print "</TR>";
print "<TR><TD>Debug Level 2: </TD><TD align='center'> " . $log['dl2']['abs'] . " </TD><TD align='center'> " . $log['dl2']['show'] . " </TD> ";
	check_stat("128");
print "</TR>";
?>
</TABLE>
<BR><BR>
&nbsp;&nbsp;<a href="#top"><?php print $lang_tools["top"]; ?></a>
<FORM action=""><center> <input type="button" value="<?php print $lang_tools['close']; ?>" onClick="window.close()"> </center></form>
<BR>
<hr noshade>
<p style="font-size: 60%; margin-top: 0" >Powered by yappa-ng <?php print $config['release']; ?> (Fritz 'wizard' Berger):  >> <a href="http://www.zirkon.at/zirkon/scripts/yappa-ng/yappa-ng_main_eng.html">yappa-ng Homepage</a> <<</p>
<?php
// log level 1: Administration 
if((int)"1" & (int)$config["loglevel"]) {
	$log_msg = date("U") . "|Administration|" . "ToolBox - Logfile Viewer: viewed logfile " . $logfile_name . "|" . $_SERVER["REMOTE_ADDR"] . "|-|" . basename(__FILE__) . "|" . __LINE__ . "\n";
	@error_log($log_msg, 3, "yappa-ng.log"); 
} 
?>
</BODY>
</HTML>
<?php
// END 
?>
Return current item: Yappa-ng