Work:error log Reader

From Zoelife4U Wiki
Jump to: navigation, search

Like it's counterpart, this scans all directories from public_html and down. and Creates Links to any error_log file(s) it finds, allowing one to read the error_log without the brutal wait of cPanel. Use with CPU Exceeded and Slow MySQL Queries] script to help troubleshoot your customers issues.
Known Bugs: It's a bit slow, the file searching algorithm could probably be optimized. First production run it showed link to one error_log, when clicked it then loaded all the error_logs in the File System.. :D
TODO: Try is_dir() instead of filetype()

<?php
/*
Loads all the error_log files it finds in the webroot directories for viewing.
Copyright (C) 2008  John D Jones III ampvbmVzX0FUX2JsdWVob3N0LmNvbQ== and Joe Crown amNyb3duX0FUX2JsdWVob3N0LmNvbQ==
 
    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 3 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, see <http://www.gnu.org/licenses/>
*/
//Let's set up our variables first and foremost
ini_set('register_globals', '0');
 
$log = base64_decode($_GET['log']); // We base64 encode the GET VARS to protect paths
$version = "0.1.23-BETA";
 
function filelist($startdir) { // Thanks Joe Crown
    $ignoredDirectory[] = ".";
    $ignoredDirectory[] = "..";
//global $directorylist;
    if(is_dir($startdir)) {
        if($dh = opendir($startdir)) {
            while(($file = readdir($dh)) != false) {
                if(!(array_search($file,$ignoredDirectory) > -1)) {
                    if(filetype($startdir . $file) == "dir") {
                        $directorylist[$startdir . $file]['name'] = $file;
                        $directorylist[$startdir . $file]['path'] = $startdir;
                        filelist($startdir . $file . "/");
                    }else{
                        $directorylist[$startdir . $file]['name'] = $file;
                        $directorylist[$startdir . $file]['path'] = $startdir;
		    }
	        }
	    }
	closedir($dh);
        }
    }
    return $directorylist;
}
 
 
function getHome(){
    $home_array = $_SERVER['SCRIPT_FILENAME'];
    $home_split = split ('/', $home_array);
    $home = "/".$home_split['1']."/".$home_split['2'];
    return $home;
}
 
function getList() {
    //Do Some HTML
    $h1 = "Error Logs for ".$_SERVER['HTTP_HOST'];
    ?>
    <h1><?php echo $h1;?></h1>
    <br />
    <table cellspacing="15" cellpadding="5">
        <tr>
            <th> Filename </th>
            <th> Size </th>
            <th> Modified </th>
        </tr>
    <?php
    $home = getHome();
    $ph = "$home/public_html/";
    $files = filelist($ph);
    $j = 0;
    foreach ($files as $list) {
    	$farr[] = $list['path'] . $list['name'];
    }
    $j = count($farr);
    //clearstatcache();
    for ($i = 0; $i < $j; $i++) {
    	if(is_file($farr[$i])){
    		if ((substr($farr[$i], -9) == 'error_log')){
                $fmodtime = date("F d Y H:i:s", filemtime($farr[$i]));
                $fsize = filesize($farr[$i]);
                $s = $fsize / 1024;
                $size = sprintf ("%01.2f", $s);
                echo "<tr>\n<td>\n";
                $encodedUrl = base64_encode($farr[$i]);
                $link = "http://".$_SERVER["HTTP_HOST"].$_SERVER['PHP_SELF']."?log=".$encodedUrl;
                $ln = split("/", $farr[$i], 5);
                echo '<a href="#" onclick="window.open (\''.$link.'\', \'\', \'width=925px, height=200px, resizable ,scrollbars=1\')">'.$ln['4'].'</a>';
                echo "\n</td>\n<td>$size KB</td>\n<td>$fmodtime</td>\n</tr>\n";
		    }
	    }
    }
    echo '</table>';
}
function displayPage($file) {
    // Sterilize the Input to prevent injections
    strip_tags($file);
    stripslashes($file);
    if(preg_match('/\.\./', $file)) {
        die('<h1>Access Denied</h1>');
    }
    if(!$theFile = fopen($file, "r")) {
        die('File Not Found'); // Make sure the the file ($f) is valid
    }
    while(!feof($theFile)) {
    // This is what's loaded in the new window.
        $contents .= fread($theFile, 8192);
        echo "\n<p>\n<br />";
        echo nl2br($contents);
        echo "\n</p></body></html>\n";
    }
    fclose($theFile);
}
function copyright() {
    $fw = base64_decode('MjAwOA==');
    $ty = date('Y');
    if($fw == $ty) {
        $y = $ty;
    }else{
    $y = $fw."-".$ty;
    }
    return $y;
}
// Do some HTML
echo '<?xml version="1.0" encoding="utf-8"?>';
echo "\n";
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head profile="http://www.w3.org/2000/08/w3c-synd/#">
<?php
if($log) {
    ?>
<title> <?php echo $log;?></title>
<?php }else{ ?>
<title> Error Log Reader <?php echo $version;?></title>
<?php } ?>
<meta http-equiv="content-type" content="application/xhtml+xml; charset=utf-8" />
<meta name="generator" content="Vim 7.2" />
<style type="text/css">
* {
 padding: 0;
 margin: 0;
}
body {
 font: 93%/1.0 arial, helvetica, sans-serif;
 font-size: 14px;
}
#wrapper { 
 margin: 0 auto;
 width: 922px;
}
 
#leftcolumn { 
 color: #333;
 border: 1px solid #ccc;
 background: #CCEDDD;
 margin: 10px 5px 10px 0px;
 padding: 10px;
 min-height: 500px;
 width: 436px;
 float: left;
}
#rightcolumn { 
 float: right;
 color: #333;
 border: 1px solid #ccc;
 background: #DDDEEE;
 margin: 10px 0px 10px 0px;
 padding: 10px;
 min-height: 500px;
 width: 436px;
 display: inline;
 position: relative;
 }
#wholecolumn {
 color: #333;
 border: 1px solid #ccc;
 background: #DDDEEE;
 margin: 10px 0px 10px 0px;
 padding: 10px;
 min-height: 500px;
 width: 900px;
}
 #footer { 
 width: 900px;
 clear: both;
 color: #333;
 border: 1px solid #ccc;
 background: #CDEEFC;
 margin: 0px 0px 10px 0px;
 padding: 10px;
}
 .center {
 text-align: center
}
 
</style>
</head>
<body>
<div id="wrapper">
<?php
if (!$log) {
?>
    <div id="wholecolumn">
        <?php
        //getcpu();
        getList();
        ?>
    </div>
 
<?php } //End if(!$type)
if ($log != "") {
    displayPage($log);
}
elseif (!$log){
$copyright=copyright();
$GPL='<a href="http://www.gnu.org/licenses/gpl-3.0.html" title="GPL 3.0">GPL 3.0</a>';
$usage=memory_get_usage(); //Please leave this for debug purposes.
echo "<div id='footer'>
<p>
    <small>
        <span class='center'>Version $version &copy; $copyright $GPL $usage</span>
    </small>
</p>
</div>";
?>
</div>
</body>
</html>
<?php
}
else {
?>
</div>
</body>
</html>
<?php
// We'll never get this far anymore.
}
?>
Personal tools
Online Users
Zoelife4U: