<?php
class PHPConsole {
private static $initiated = false;
private static $buffer;
private static $tempo;
private static $maxCycles;
//PHPConsole::init() sets settings required for the console
//Note: it is not normally necessary to manually run this function, it will get run before any of the class methods get run
//In cases when the headers have already been sent before running the console functions it might be necessary to call this function manually near the top of the page
public static function init(){
self::$initiated = true;
if(session_status() != 2) session_write_close();
ob_implicit_flush(true);
ob_end_flush();
@session_start();
ob_start();
set_time_limit(10);
self::$buffer = str_repeat(' ',1024);
self::$tempo = 100000;
self::$maxCycles = (int)((((1000000)*60)*60)/self::$tempo);
if(!isset($_SESSION['console'])) $_SESSION['console'] = array();
if(!isset($_SESSION['console']['message_x'])) $_SESSION['console']['message_x'] = 0;
if(!isset($_SESSION['console']['command_x'])) $_SESSION['console']['command_x'] = 0;
}
//PHPConsole::run() will evaluate code on the console page
//useful for testing functionality on the console page
public static function run($cmd){
if(!self::$initiated) self::init();
if(session_status() != 2) session_start();
$_SESSION['console']['command'] = $cmd;
$_SESSION['console']['command_x'] = ($_SESSION['console']['command_x'] + 1);
session_write_close();
usleep(self::$tempo);
@session_start();
}
//PHPConsole::log() writes contents of $message to the console
//useful for logging errors and data while debugging
public static function log($message){
if(!self::$initiated) self::init();
if(session_status() != 2) session_start();
$_SESSION['console']['message'] = print_r($message,true);
$_SESSION['console']['message_x'] = ($_SESSION['console']['message_x'] + 1);
session_write_close();
usleep(self::$tempo);
@session_start();
}
//PHPConsole::alert() writes contents of $message to the console and grabs attention
//useful for bad errors that you want strong notifications for
public static function alert($message){
if(!self::$initiated) self::init();
if(session_status() != 2) session_start();
$_SESSION['console']['message'] = '<script> alert("'.$message.'"); </script>'.$message;
$_SESSION['console']['message_x'] = ($_SESSION['console']['message_x'] + 1);
session_write_close();
usleep(self::$tempo);
@session_start();
}
//PHPConsole::backtrace() prints a backtrace to the console from the function it's called at
//useful for debugging the nested functions
public static function backtrace(){
if(!self::$initiated) self::init();
if(session_status() != 2) session_start();
$_SESSION['console']['message'] = print_r(debug_backtrace(),true);
$_SESSION['console']['message_x'] = ($_SESSION['console']['message_x'] + 1);
session_write_close();
usleep(self::$tempo);
@session_start();
}
//PHPConsole::printDisplay() runs on your console logging page
//The page will not progress beyond the point that this function is called until the console expires
public static function printDisplay(){
if(!self::$initiated) self::init();
$i = 0;
$messageCount = $_SESSION['console']['message_x'];
$commandCount = $_SESSION['console']['command_x'];
echo '<hr/>'.self::$buffer;
while($i++ < self::$maxCycles){
if(session_status() != 2) @session_start();
if($_SESSION['console']['message_x'] > $messageCount){
set_time_limit(10);
echo '<div>';
if(strpos($_SESSION['console']['message'], 'alert(') !== false) echo $_SESSION['console']['message'].'<br/>';
else echo '<xmp style="display:inline; color:grey;"> '.$_SESSION['console']['message'].'</xmp><br/>';
echo '<hr/>';
echo '</div>';
echo self::$buffer;
$messageCount = $_SESSION['console']['message_x'];
} elseif($_SESSION['console']['command_x'] > $commandCount){
set_time_limit(10);
echo '<div>';
if($_SESSION['console']['command'] == 'exit();') exit('<b>exit();</b>');
echo '<i><xmp style="display:inline; color:lightblue;"> '.$_SESSION['console']['command'].'</xmp></i><br/>';
eval($_SESSION['console']['command']);
echo '<hr/>';
echo '</div>';
echo self::$buffer;
$commandCount = $_SESSION['console']['command_x'];
}
session_write_close();
ob_flush();
usleep(self::$tempo);
}
}
}
?>
|