Showtable

Some code which will be later formatted and explained.

<?php
/* set error reporting */
error_reporting(E_ALL);
 
class DefaultShowTable
{
    protected $table_head;
    protected $table_rows;
    protected $table_size;
    protected $separator;
 
    // this is executed before parsing the header
    // lines. Argument is a string
    function pre_parse_head ( &$head_line )
    {
    }
 
    // this is executed after parsing the header
    // lines. Argument is an array
    function post_parse_head ( &$head_array )
    {
    }
 
    // This method parses the first string in it's
    // argument into the table_head array
    function parse_head ( $headerln )
    {
        $this->pre_parse_head ( $headerln );
        $headerln   = trim ( preg_replace("/$this->separator+/",$this->separator,$headerln) );
        $headerar    = explode ( $this->separator, $headerln );
        $this->post_parse_head ( $headerar );
        $this->table_head = $headerar;
        $this->table_size = sizeof ( $headerar ) - 1;
    }
 
    // this is executed before parsing the header
    // lines. 
    function pre_parse_body ( &$body_line )
    {
    }
 
    // this is executed before parsing the header
    // lines. 
    function post_parse_body ( &$body_array )
    {
    }
 
    // This method parses the first string in it's
    // argument into the table_head array
    function parse_body ( $table_list )
    {
        $i = 0;
        foreach ( $table_list as $bodyln )
        {
            $newbodyln = trim ( preg_replace("/$this->separator+/",$this->separator,$bodyln) );
            $this->pre_parse_body ( $newbodyln );
            $newbodyar = explode ( $this->separator, $newbodyln );
            $this->post_parse_body ( $newbodyar );
            $this->table_body[$i++] = $newbodyar;
        }
    }
 
    function __construct ( $textual_table, $textual_separator )
    {
        $table_head = array_shift ( $textual_table );
        $table_list = $textual_table;
        $this->separator  = $textual_separator;
        $this->parse_head ( $table_head );
        $this->parse_body ( $table_list );
    }
 
    function print_table ()
    {
        echo "<table border=\"1\">\n";
        // print the table headers
        echo "<tr>\n";
        foreach ( $this->table_head as $title ) 
        {
            echo "<th>".$title."</th>";
        }
        echo "</tr>\n";
        foreach ( $this->table_body as $bodylnar )
        {
            echo "<tr>";
            // print the cells
            foreach ( $bodylnar as $entry )
            {
                echo "<td>".$entry."</td>";
            }
            echo "</tr>\n";
        }
        echo "</table>\n";
    }
 
}
 
class CrontabShowTable extends DefaultShowTable
{
 
    function __construct ()
    {
        // read the configuration file
        $crontab=file("/etc/crontab");
        // filter out lines which don't belong to the configuration
        $crontab=preg_grep ( "/^#/", $crontab, PREG_GREP_INVERT );
        // $crontab=preg_grep ( "/^\s*PATH/", $crontab, PREG_GREP_INVERT );
        // $crontab=preg_grep ( "/^\s*HOME/", $crontab, PREG_GREP_INVERT );
        // $crontab=preg_grep ( "/^\s*SHELL/", $crontab, PREG_GREP_INVERT );
        // there is more! To you could have a lot of variable sets.
        // And not every shell uses the = equal sign
        $crontab=preg_grep ( "/SET /i", $crontab, PREG_GREP_INVERT );
        $crontab=preg_grep ( "/=/", $crontab, PREG_GREP_INVERT );
        // add the header (language translations friendly?)
        array_unshift ( $crontab, "minute hour day_of_month month day_of_week user command" );
        parent::__construct( $crontab, " " );
    }
 
    function post_parse_body ( &$body_array )
    {
        // shrink elements above table size as last element
        $therest        = array_splice    ( $body_array, $this->table_size );
        $body_array[]    = implode        ( " ", $therest );
    }
 
}
 
class RouteShowTable extends DefaultShowTable
{
 
    function __construct ()
    {
        exec('route', $route, $error );
        array_shift ( $route );
        parent::__construct( $route, " " );
    }
 
}
 
class ProcessShowTable extends DefaultShowTable
{
 
    function __construct ()
    {
        exec('ps', $ps, $error );
        parent::__construct( $ps, " " );
    }
 
    function pre_parse_body  ( &$body_line )
    {
        $body_line = preg_replace("/ S N /"," SN ",$body_line);
    }
 
    function post_parse_body ( &$body_array )
    {
        // insert the 2 element if missing
        if ( !preg_match ( "/\d+/", $body_array[2] ) )
        {
            array_splice ( $body_array, 2, 0, "-" );
        }
        // 
        // shrink elements above table size as last element
        $therest        = array_splice    ( $body_array, $this->table_size );
        $body_array[]    = implode             ( " ", $therest );
    }
 
}
 
class DiskfreeShowTable extends DefaultShowTable
{
 
    function __construct ()
    {
        exec('df -h', $df, $error );
        parent::__construct( $df, " " );
    }
 
    function pre_parse_head ( &$head_line )
    {
        $head_line = preg_replace("/Mounted on/","WhateverYouWant",$head_line);
    }
 
    function post_parse_head ( &$head_array )
    {
        $i = 0;
        foreach ( $head_array as $items )
        {
            $items=preg_replace("/WhateverYouWant/","Mounted on",$items);
            $head_array[$i++]=$items;
        }
    }
 
}
 
class NetstatShowTable extends DefaultShowTable
{
 
    function __construct ()
    {
        exec("netstat -l", $netstat, $error );
        array_shift ( $netstat );
        parent::__construct( $netstat, " " );
    }
 
    function pre_parse_head ( &$head_line )
    {
        $head_line = preg_replace("/ Address/","_WhateverYouWant",$head_line);
    }
 
    function post_parse_head ( &$head_array )
    {
        $i = 0;
        foreach ( $head_array as $items )
        {
            $items=preg_replace("/_WhateverYouWant/"," Address",$items);
            $head_array[$i++]=$items;
        }
    }
 
}
 
class mtdaapdShowTable extends DefaultShowTable
{
 
    function __construct ()
    {
        // read the configuration file
        $mtdaapd=file("/volume1/public/debian/chroottarget/usr/local/etc/mt-daapd.conf");
        // filter out lines which don't belong to the configuration
        $mtdaapd=preg_grep ( "/^#/", $mtdaapd, PREG_GREP_INVERT );
        // All empty lines
        $mtdaapd=preg_grep ( "/^\s+$/", $mtdaapd, PREG_GREP_INVERT );
        array_unshift ( $mtdaapd, "variable=value" );
        parent::__construct( $mtdaapd, "=" );
    }
 
}
 
// $route = new RouteShowTable ();
//$ps = new ProcessShowTable ();
// $df = new DiskfreeShowTable ();
// $netstat = new NetstatShowTable ();
$mymtdaapd = new mtdaapdShowTable ();
$mymtdaapd->print_table ();
// $route->print_table();
//$ps->print_table();
// $df->print_table();
// $netstat->print_table();
// echo nl2br(implode( "\n",$ps));
 
?>
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-Share Alike 2.5 License.