Location: PHPKode > projects > ARTIS WaterMon > artis-watermon-beta/functions.php
<?php

// Toowoomba City Council Water Monitoring System
// Developed by Sam Moffatt (hide@address.com)
// Global Functions

require_once("pump.class.php");
require_once("reservoir.class.php");
require_once("flow.class.php");
require_once("raingauge.class.php");
require_once("line.class.php");

$dbuser = "user";
$dbpassword = "password";
$dbname = "artis";

function checkSecurity() {
	// Check for lock file
	$locked = false;
	if($handle = opendir('.')) {
		while(false !== ($file = readdir($handle))) {
			// Parse out this directory and parent
			if ($file != "." && $file != "..") {
				if(strstr($file,"locked")) {
					$locked = true;
				}
			}
		}
	}
	// Check for prefetch
	if(array_key_exists("HTTP_X_MOZ",$_SERVER)) 
	{ 
	  if(strtoupper($_SERVER['HTTP_X_MOZ']) == 'prefetch') 
	  { 
	    header("HTTP/1.x 403 Forbidden"); 
	    header("Content-Type: text/html; charset=iso-8859-1"); 
	    header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); 
	    header("Cache-Control: no-store, no-cache, 
must-revalidate"); 
	    header("Cache-Control: post-check=0, pre-check=0", 
FALSE); 
	    header("Pragma: no-cache"); 
	    header('Accept-Ranges:'); 
	    exit(); 
	  } 
	} 
	if($locked) {
		header("Location: main.php");
	}
}

function getParamGroup($input, $elements) {
	if(is_string($elements)) {
		$elements = explode(" ", $elements);
	}	
	if(is_array($elements)) {
		$result = Array();
		foreach($elements as $element) {
			$result[$element] = $input[$element];
		}
		return $result;
	} else {
		return null;
	}
}

// Builds a SET clause for an update query
// Requires 
// Good fun :)
function buildUpdateQuery($input, $columns, $paramgroup=0) {
	$first = 1; $final = "";
	if($paramgroup) {
		// Pass input through param group first
		$data = getParamGroup($input, $columns);
	} else {
		$data = $input;
	}
	// Check if they gave us a string
/*	if(is_string($columns)) {
		$columns = explode(" ", $columns);
	}*/
	if(is_array($data)) {
		// Is an array
		foreach($data as $column => $datum) {
			// For every row we have...
			if(!$first) {
				$final .= ", `$column` = '$datum'";
			} else {
				$first = 0;
				$final = "`$column` = '$datum'";
			}
		}
		return $final;
	} else {
		// Didn't get an array at any point		
		return false;
	}
}

function printDateTime($input) {
	?>
	
	<div style="color: black; font-weight: bold; text-align: center;">Last Data Update <?php echo $input['day'] ?> / <?php echo $input['month'] ?> / <?php echo $input['year'] ?>   Time <?php echo $input['hour'] ?>:<?php echo $input['minute']?>
		<div style="color: blue">This page will automatically refresh every Two minutes.</div>
	</div>
	<?php
}

function configurePumps(&$pumps,$owner=1) {
	global $database,$data;
	$query = "SELECT * FROM display WHERE type = 'pump' AND owner = $owner";
	$result = mysql_query($query) or die("Error: " . mysql_error());
	while($row = mysql_fetch_array($result)) {
		$pumps[] = new pump($row['itemid'],$row['shortname'],$row['left'],$row['top'],$row['graphurl'],$row['errorurl'],$data);
	}
}

function configureReservoirs(&$reservoirs,$owner=1) {
	global $database,$data;
	$query = "SELECT * FROM display WHERE type = 'reservoir' AND owner = $owner";
	$result = mysql_query($query) or die("Error: " . mysql_error());
	while($row = mysql_fetch_array($result)) {
		$reservoirs[] = new reservoir($row['itemid'],$row['shortname'],$row['left'],$row['top'],$data);
	}	
}

function configureFlowMeters(&$flowmeters,$owner=1) {
	global $database,$data;
	$query = "SELECT * FROM display WHERE type = 'flow meter' AND owner = $owner";
	$result = mysql_query($query) or die("Error: " . mysql_error());
	while($row = mysql_fetch_array($result)) {
		$flowmeters[] = new flowmeter($row['itemid'],$row['shortname'],$row['left'],$row['top'],$data);
	}
}

function configureRainGauges(&$raingauges,$owner=1) {
	global $database,$data;
	$query = "SELECT * FROM display WHERE type = 'rain gauge' AND owner = $owner";
	$result = mysql_query($query) or die("Error: " . mysql_error());
	while($row = mysql_fetch_array($result)) {
		$raingauges[] = new raingauge($row['itemid'],$row['shortname'],$row['left'],$row['top'],$data);
	}
}

function configureLines(&$lines,$owner=1) {
	global $database;
	$query = "SELECT * FROM display WHERE type = 'line' AND owner = $owner";
	$result = mysql_query($query) or die("Error: " . mysql_error());
	while($row = mysql_fetch_array($result)) {
		$lines[] = new line($row['id'],$row['left'],$row['top'],$row['right'],$row['bottom']);
	}
}

function configureEquationReservoirs(&$eqreservoirs,$owner=1) {
	global $database;
	$query = "SELECT * FROM display WHERE type = 'Equation Reservoir' AND owner = $owner";
	$result = mysql_query($query) or die("Error: " . mysql_error());
	while($row = mysql_fetch_array($result)) {
		$eqreservoirs[] = new reservoir_equation($row['itemid'],$row['shortname'],$row['left'],$row['top'],$row['equation'],$data);
	}
}

function loadMetaData($filename="http://www.toowoomba.qld.gov.au/eBiz/artis/SCADA2000.CSV") {
	$contents = implode("",  file ($filename));
	$result = Array();
	$delimiter = ",";
	$elements = explode($delimiter, $contents); //assign to individual elements
	$results = Array();
	// get the meta-data (remove first 7 - date & time)
	$result[1] = array_shift( $elements ); // Does it really matter?
	$result['year'] = array_shift( $elements );
	$result['month'] = array_shift( $elements );
	$result['day'] = array_shift( $elements );
	$result['hour'] = array_shift( $elements );
	$result['minute'] = array_shift( $elements );
	$result['second'] = array_shift( $elements );
	return $result;	
}

function loadCSV($filename="http://www.toowoomba.qld.gov.au/eBiz/artis/SCADA2000.CSV") {
	$contents = implode("",  file ($filename));
	$result = Array();
	$delimiter = ",";
	$elements = explode($delimiter, $contents); //assign to individual elements
	
	// get the meta-data (remove first 7 - date & time)
	$e1 = array_shift( $elements );
	$e2 = array_shift( $elements );
	$e3 = array_shift( $elements );
	$e4 = array_shift( $elements );
	$e5 = array_shift( $elements );
	$e6 = array_shift( $elements );
	$e7 = array_shift( $elements );
	//inialise plant array
	$plants = array();
	
	//count number of elements remaining
	$n = count( $elements );
	// loop throught the rest of the array in blocks of five
	for ($i=0; $i < $n-1; $i+=5) {
		$add = false;
		$id = $elements[$i];
		$result[$id]['title'] = $elements[$i+1];
		$result[$id]['stamp'] = $elements[$i+2];
		$result[$id]['state'] = $elements[$i+3];
		$result[$id]['units'] = $elements[$i+4];
	}
	return $result;
} 

?>
Return current item: ARTIS WaterMon