<?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: class.FreeBSD.inc.php,v 1.1 2001/08/05 08:56:37 jengo Exp $
//

echo "<center><b>Note: The FreeBSD version of phpSysInfo is work in progress, some things currently don't work</b></center>";

class sysinfo
{
	function grab_key($key)
	{
		$s = `sysctl $key`;
		$s = ereg_replace($key . ': ','',$s);
		return $s;
	}

	function hostname()
	{
		if ( !($result = getenv('SERVER_NAME')) ) {
			$result = "N.A.";
		}
	
		return $result;
	}

	function chostname()
	{
		return `hostname`;
	}

    function ip_addr()
    {
        if (!($result = getenv('SERVER_ADDR'))) {
            $result = gethostbyname(sys_chostname());
        }
        return $result;
    }

	function kernel()
	{
		$s = $this->grab_key('kern.version');
		$a = explode(':',$s);
		return $a[0];
	}

	function uptime()
	{
		$a = explode(' ',$this->grab_key('kern.boottime'));
		$sys_ticks = $a[6];

		$min   = $sys_ticks / 60;
		$hours = $min / 60;
		$days  = floor( $hours / 24 );
		$hours = floor( $hours - ($days * 24) );
		$min   = floor( $min - ($days * 60 * 24) - ($hours * 60) );

		if ( $days != 0 ) {
			$result = "$days days, ";
		}

		if ( $hours != 0 ) {
			$result .= "$hours hours, ";
		}
		$result .= "$min minutes";

		return $result;
	}

	function users()
	{
		return trim(`/usr/bin/who | wc -l`);
	}

	function loadavg()
	{
		$s = $this->grab_key('vm.loadavg');
		$s = ereg_replace('{ ','',$s);
		$s = ereg_replace(' }','',$s);
		$results = explode(' ',$s);

		return $results;		
	}

	// This currently only works on single CPU systems.
	// I do not have a dual CPU machine to make it work
	function cpu_info()
	{
		$results = array();
		$ar_buf  = array();

		$results['model'] = $this->grab_key('hw.model');
		$results['cpus']  = $this->grab_key('hw.ncpu');

/*			if ($fd = fopen('/proc/cpuinfo', 'r'))
			{
				while ($buf = fgets($fd, 4096))
				{
					list($key, $value) = preg_split('/\s+:\s+/', trim($buf), 2);

					// All of the tags here are highly architecture dependant.
					// the only way I could reconstruct them for machines I don't
					// have is to browse the kernel source.  So if your arch isn't
					// supported, tell me you want it written in. <infinite@sigkill.com>
					switch ($key)
					{
						case 'model name':
							$results['model'] = $value;
							break;
						case 'cpu MHz':
							$results['mhz'] = sprintf('%.2f', $value);
							break;
						case 'clock': // For PPC arch (damn borked POS)
							$results['mhz'] = sprintf('%.2f', $value);
							break;
						case 'cpu': // For PPC arch (damn borked POS)
							$results['model'] = $value;
							break;
						case 'revision': // For PPC arch (damn borked POS)
							$results['model'] .= ' ( rev: ' . $value . ')';
							break;
						case 'cache size':
							$results['cache'] = $value;
							break;
						case 'bogomips':
							$results['bogomips'] += $value;
							break;
						case 'processor':
							$results['cpus'] += 1;
							break;
					}    
				}
				fclose($fd);
			}

*/
		$keys = compat_array_keys( $results );
		$keys2be = array( "model", "mhz", "cache", "bogomips", "cpus" );
	
		while ( $ar_buf = each( $keys2be ) ) {
			if (! compat_in_array( $ar_buf[1], $keys ) ) {
				$results[$ar_buf[1]] = 'N.A.';
			}
		}
	
		return $results;

	}

	function pci()
	{
		
	}

	function ide()
	{
	
	}

	function scsi()
	{
		
	}

	function network()
	{
		
	}

	function memory()
	{
		
	}

	function filesystems()
	{
		$df = `/bin/df -kP`;
		$mounts = split( "\n", $df );
		$fstype = array();

		$s     = `mount`;
		$lines = explode("\n",$s);

		$i = 0;
		while (list(,$line) = each($lines)) {
			ereg('(.*) \((.*)\,(.*)\)',$line,$a);

			$m = explode(' ',$a[0]);
			$fsdev[$m[0]] = $a[2];
		}

		for ( $i = 1; $i < sizeof($mounts) - 1; $i++ ) {
			$ar_buf = preg_split("/\s+/", $mounts[$i], 6);
	
			$results[$i - 1] = array();
	
			$results[$i - 1]['disk'] = $ar_buf[0];
			$results[$i - 1]['size'] = $ar_buf[1];
			$results[$i - 1]['used'] = $ar_buf[2];
			$results[$i - 1]['free'] = $ar_buf[3];
			$results[$i - 1]['percent'] = $ar_buf[4];
			$results[$i - 1]['mount'] = $ar_buf[5];
			($fstype[$ar_buf[5]]) ? $results[$i - 1]['fstype'] = $fstype[$ar_buf[5]] : $results[$i - 1]['fstype'] = $fsdev[$ar_buf[0]];
		}

		return $results;
	}

}
