<?php
//
// phpSysInfo - A PHP System Information Script
// http://phpsysinfo.sourceforge.net/
//
// 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.
//
// $Id: system_functions.php,v 1.15 2001/05/30 18:24:22 precision Exp $


// So that stupid warnings do not appear when we stats files that do not exist.
error_reporting(5);


/// FOR LM_SENSORS
// URL: http://fudd.thegoop.com/~dragon/phpSysInfo-1.1_sensors.diff


// Returns F if arg2 is "F" or C if arg2 is "C". If arg2 is niether C or F, C is returned.
function temp_conv( $temp, $type, $dec_places = 1 ) {

        if ( $type == "F" ) {
                $result = sprintf("%." . $dec_places . "f", ( ( $temp * 9 )/ 5 ) + 32 );
        }
        else if ( $type == "C" ) {
                $result = sprintf("%." . $dec_places . "f", ( ( $temp - 32) * 5 ) / 9  );
        }
        else {
                $result = sprintf("%." . $dec_places . "f", ( ( $temp - 32) * 5 ) / 9  );
        }

        return $result;
}

function sys_checksensors() {
        exec('/bin/cat /proc/sys/dev/sensors/chips', $results, $tmparray);
       return $results;
}

function sys_cleanarray($dirty = array()) {

        $clean = array();
        $j = 0;
        for ($i=0;$i<sizeof($dirty);$i++) {
                if (strlen($dirty[$i]) != 0) {
                        $clean[$j] = $dirty[$i];
                        $j++;
                }
        }
        return $clean;

}

function pre_grep( $subString, $subject = array() )
{
 $result = array();
 $j=0;
 for ($i=0;$i<sizeof($subject);$i++)
 {
  if ( preg_match( $subString, $subject[$i] ) )
  {
    $result[$j] = $subject[$i];
    $j++;
  }
 }
 return $result;
}

function sys_parsesensors() {

       $sensors = "/usr/local/bin/sensors";
       
       $results = array();
       if ($sensors == $false || sys_checksensors() == 0) {
               return $results;
       }

       $sensors = `$sensors`;

        $lines = sys_cleanarray(split( "\n", $sensors ));

       // Add label for Chip name.
       $lines[0] = "Sensors Chip: " . $lines[0];
//for ($i=0;$i<sizeof($lines);$i++) echo $lines[$i], "<br>";

        $volts = pre_grep( "/ V /", $lines);
        $temps = pre_grep("/C /", $lines);
        $fans = pre_grep("/ RPM /", $lines);

        $others = array();
        $j = 0;
        for ( $i = 0; $i < sizeof($lines); $i++ ) {
                if ( !preg_match("/\(min|\(limit/",$lines[$i])) {
                        $others[$j] = $lines[$i];
                        $j++;
                }
        }
       $others = sys_cleanarray($others);

       $results['othercount'] = sizeof($others);
        for ( $i = 0; $i < sizeof($others); $i++ ) {
               list($label, $value) = split( ":", $others[$i]);

                $results['others'.$i]['label'] = trim($label.":");
                $results['others'.$i]['current'] = trim($value);
       }


// voltage
       $results['voltcount'] = sizeof($volts);
       for ( $i = 0; $i < sizeof($volts); $i++ ) {
               //$tmp = cho "$volts[$i]" |sed -e "s/ V).*//;s/=//g;s/min//g; s/max//g; s/(//; s/,//g; s/ V //g";
               $tmparray[$i] = split( ":", $volts[$i]);
               $tmparray[$i][1] = ereg_replace("/|[)].*|[=(,]|min |max |V|/i", "", $tmparray[$i][1]);
               $results['volt'.$i]['label'] = $tmparray[$i][0].":";
               $tmparray[$i] = preg_split( "/\s+/", trim($tmparray[$i][1]),3);
                $results['volt'.$i]['current'] = $tmparray[$i][0]." V";
                $results['volt'.$i]['min'] = $tmparray[$i][1]." V";
                $results['volt'.$i]['max'] = $tmparray[$i][2]." V";

                if ( eregi_replace("[-+]","",$tmparray[$i][0]) < eregi_replace("[-+]","",$tmparray[$i][1]) || eregi_replace("[-+]","",$tmparray[$i][0]) > eregi_replace("[-+]","",$tmparray[$i][2])) {
                        $results['volt'.$i]['status'] = "<b><blink><font color=\"#FF0000\">[RED ALERT]</font></blink></b>";
                }
                else {
                        $results['volt'.$i]['status'] = "<b><font color=\"#238E23\">[Normal]</font></b>";
                }
       }

// fans
       $results['fancount'] = sizeof($fans);
        for ( $i = 1; $i < sizeof($fans) + 1; $i++ ) {
                //$tmp = cho "$fans[$i]" |sed -e "s/).*//;s/=//g;s/min//g; s/div//g; s/(//; s/,//g; s/ RPM //g";
                $tmparray[$i] = split( ":", $fans[$i - 1]);
               $tmparray[$i][1] = ereg_replace("/|[)].*|[=(,]|min |div|RPM|/i", "", $tmparray[$i][1]);
                $results['fan'.$i]['label'] = $tmparray[$i][0].":";
                $tmparray[$i] = preg_split( "/[\s+-]+/", trim($tmparray[$i][1]),3);
                $results['fan'.$i]['current'] = $tmparray[$i][0]." RPM";
                $results['fan'.$i]['min'] = $tmparray[$i][1]." RPM";
                $results['fan'.$i]['div'] = $tmparray[$i][2];

                if ( $tmparray[$i][0] < $tmparray[$i][1] ) {
                        $results['fan'.$i]['status'] = "<b><blink><font color=\"#FF0000\">[RED ALERT]</font></b>";
                }
                else {
                        $results['fan'.$i]['status'] = "<b><font color=\"#238E23\">[Normal]</font></b>";
                }
        }

// temparatures
        $results['tempcount'] = sizeof($temps);
       for ( $i = 1; $i < sizeof($temps) + 1; $i++ ) {
                //$tmp = cho "$lines[$i]" |sed -e "s/ C).*//;s/=//g;s/limit//g; s/hysteresis//g; s/(//; s/,//g; s/ C //g";
                $tmparray[$i] = split( ":", $temps[$i - 1]);
                $tmparray[$i][1] = ereg_replace("/|[)].*|[=(,]|limit|hysteresis | C|/i", "", $tmparray[$i][1]);
                $results['temp'.$i]['label'] = $tmparray[$i][0].":";
                $tmparray[$i] = preg_split( "/\s+/", eregi_replace("[+]","",trim($tmparray[$i][1])),3);
                $results['temp'.$i]['current'] = $tmparray[$i][0]."C".'<br>'."(".temp_conv($tmparray[$i][0],"F")." F)";
                $results['temp'.$i]['min'] = $tmparray[$i][2]."C".'<br>'."(".temp_conv($tmparray[$i][2],"F")." F)";
                $results['temp'.$i]['max'] = $tmparray[$i][1]."C".'<br>'."(".temp_conv($tmparray[$i][1],"F")." F)";

                if ( $tmparray[$i][0] > $tmparray[$i][1] ) {
                        $results['temp'.$i]['status'] = "<b><blink><font color=\"#FF0000\">[RED ALERT]</font></b>";
                }
                else if ( $tmparray[$i][0] >= $tmparray[$i][2] ) {
                        $results['temp'.$i]['status'] = "<b><font color=\"#CD7F32\">[YELLOW ALERT]</font></b>";
                }
                else {
                        $results['temp'.$i]['status'] = "<b><font color=\"#238E23\">[Normal]</font></b>";
                }
        }

        return $results;
               
}

?>
