Location: PHPKode > projects > Obsessive Website Statistics > ows/include/util.inc.php
<?php
/*
	$Id: util.inc.php 87 2007-08-14 06:00:25Z randomperson83 $

	Obsessive Web Statistics
    Copyright (C) 2007 Dustin Spicuzza <hide@address.com>

    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/>.

	Utility functions used in many different locations

*/

/*

	Easy to use get/post variable functions

*/

function get_post_var($varname){

	if (!isset($_POST[$varname]))
		return "";
	
	return $_POST[$varname];
}

function get_get_var($varname){

	if (!isset($_GET[$varname]))
		return "";
		
	return $_GET[$varname];
}

function get_a_var($varname){
	
	if (!isset($_POST[$varname]))
		if (!isset($_GET[$varname]))
			return "";
		else
			return $_GET[$varname];
			
	return $_POST[$varname];
}

function get_cookie_var($varname){
	
	if (!isset($_COOKIE[$varname]))
		return "";
		
	return $_COOKIE[$varname];
}

/*
	generate_select_option
	
	$id 			html id of <select> element
	$matchItem 		item to match
	$query			select element value is created from the first column of the SQL query
					select element text is created from the second column of the SQL query
	$none			if $none = true, then generate a "None" element

*/
function generate_select_option($id,$matchItem,$query,$none = false){

	$matched = false;

	// get all items
	$result = db_query($query);
	
	if (db_is_valid_result($result)){
	
		$rows = array();
	
		if (db_num_rows($result) > 0)
			while($row = db_fetch_row($result))
				$rows[] = $row;
		
		return generate_select_from_array($id,$matchItem,$rows,$none);
		
	}
	
	return '';
}

/*
	generate_select_from_array
	
	$id 			html id of <select> element
	$matchItem 		item to match
	$items			select element value is created from the first element of the array row
					select element text is created from the second element of the array row
	$none			if $none = true, then generate a "None" element
	$nonetxt		Text of the none element
	$onchange		Javascript to execute when the select element changes

*/
function generate_select_from_array($id,$matchItem,$items,$none = false, $nonetxt = "None", $onchange = ''){

	$matched = false;
	$onchange = ($onchange != '' ? " onchange=\"$onchange\" " : '');
	
	$str = "<select name=\"$id\"$onchange>";
	
	foreach($items as $item){
	
		$row = array();
		if (is_array($item))
			$row = $item;
		else{
			$row[0] = $item;
			$row[1] = $item;
		}
	
		$str .= '<option value="' . $row[0] . '"';
			
		if ($row[0] == $matchItem){
			$str .= " selected";
			$matched = true;
		}
		
		$str .= '>' . htmlentities($row[1]) . '</option>';
	}
	
	if ($none == true){
		$str .= '<option value="-1"';
	
		if (!$matched)
			$str .= ' selected';
			
		$str .= ">$nonetxt</option>";
	}

	$str .= '</select>';
	
	return $str;
}

// at some point in the future, we could use this for logging of some kind
function show_error($message, $escape = false, $retval = false, $strip_tags = true){

	global $cfg;
	
	$backtrace = "";
	
	if ($cfg['debug']){
	
		$trace = debug_backtrace();
		
		$backtrace = "\nError occurred:\n\tat ";
		
		$first = true;
		foreach ($trace as $tline){
			
			if (!$first){
				if (array_key_exists('function',$tline))
					$backtrace .= ' in function ' . (array_key_exists('class',$tline) ? $tline['class'] . $tline['type'] : '' ) . (array_key_exists('function',$tline) ? $tline['function'] . "()" : '' );
			
				$backtrace .= "\n\tcalled from ";
			}
			
			if (array_key_exists('line',$tline))
				$backtrace .= "line $tline[line] of ";
				
			if (array_key_exists('file',$tline))
				$backtrace .= basename($tline['file']);
			else
				$backtrace .= "unknown file";
				
			$first = false;
		}
		
		$backtrace .= "\n\n";
		
	}

	if (php_sapi_name() == 'cli')
		fwrite(STDERR,"Error: " . ($strip_tags ? strip_tags($message) : $message) . "$backtrace");
	else
		echo "<p class=\"error\"><strong>Error:</strong> " . ($escape ? htmlentities($message) : $message ) . str_replace("\t",'===> ',nl2br(htmlentities($backtrace))) . "</p>";
		
	return $retval;
}


// debugging item
function prn($var){
	echo "\n\n<pre>\n";
	print_r($var);
	echo "\n</pre>\n\n";
}

global $util_show_progress;
$util_show_progress = array();

function show_progress($desc, $show_at){
	global $util_show_progress;
	if (!array_key_exists($desc,$util_show_progress)){
		$util_show_progress[$desc] = array();
		$util_show_progress[$desc]['c'] = 0;
		$util_show_progress[$desc]['time'] = time();
		$util_show_progress[$desc]['time_start'] = $util_show_progress[$desc]['time'];
	}else
		$util_show_progress[$desc]['c'] += 1;
		
	if ($util_show_progress[$desc]['c'] % $show_at == 0){
		$time = time();
	
		$tm = $time - $util_show_progress[$desc]['time'];
		$tmm = $time - $util_show_progress[$desc]['time_start'];
		
		echo "$desc: " . $util_show_progress[$desc]['c'] . " : " . date("D M j G:i:s T Y") . " (${tm}s, ${tmm}s total)\n";
		
		$util_show_progress[$desc]['time'] = $time;
	}
}

// gets a human readable filesize
function get_file_size($sz){

	$size = floatval($sz);
	if ($size < 1000)
		return intval($sz) . " bytes";
	if ($size < 1000000)
		return sprintf("%0.1f KB", $size / 1000.0);
	if ($size < 1000000000)
		return sprintf("%0.1f MB", $size / 1000000.0);
	if ($size < 1000000000000)
		return sprintf("%0.1f GB", $size / 1000000000.0);
	
	// these values probably only work on a 64 bit system.. no way to verify this though
	$sz = 1000000000.0;
	$symbols = array('TB','PB','EB','ZB','YB');
	foreach ($symbols as $symbol){
		$sz = $sz * 1000.0;
		if ($size < $sz)
			return sprintf("%00.1u $symbol",$size / $sz);
	}
	
	return "File size overflow.";
}

/*
	Pass this function an array of stuff and it displays a simple padded table. No borders.
*/
function show_console_table($rows, $prepend = '', $header = true){

	$max = array();

	// find max first
	foreach ($rows as $r)
		for ($i = 0;$i < count($r);$i++)
			$max[$i] = max(array_key_exists($i,$max) ? $max[$i] : 0 ,strlen($r[$i]));
	
	// add a header?
	if ($header){

		// remove the first element
		$row = array_shift($rows);
	
		echo "$prepend";
		for ($i = 0;$i<count($row);$i++)
			echo str_pad($row[$i],$max[$i]) . "  ";
		echo "\n$prepend" . str_repeat('=',array_sum($max) + count($max)*2) . "\n";
		
	}
	
	foreach($rows as $row){
		echo "$prepend";
		for ($i = 0;$i<count($row);$i++)
			echo str_pad($row[$i],$max[$i]) . "  ";
		echo "\n";
	}
}

?>
Return current item: Obsessive Website Statistics