Location: PHPKode > projects > RadMacLab > radmaclab/admin/includes/func_vars_inc.php
<?php
session_start();

//database info
$dw_dbase	=	"radmind";						//name of mysql database
$dw_host 	=	"localhost";					//database host
$dw_user	=	"FILL THIS IN";					//db user name
$dw_pass	=	"FILL THIS IN";					//db user password
$uri_scheme =	"https://";						//should be either http:// or https:// depending on ssl or not


//universal database function
function dbase_query($query){
	global $dw_host, $dw_user, $dw_pass, $dw_dbase;
	
	$db  = mysql_pconnect($dw_host, $dw_user, $dw_pass);
	if (!$db){  //sees if there is an error
		echo "Error: Could not connect to database.<br /><br /><a href=\"javascript:history.back()\">Go Back</a><br /><br />\n\n";
		echo "QUERY: ".$query;
		return;
	}
	
	mysql_select_db($dw_dbase);	
	
	$result = mysql_query($query);
	
	if (!$result) {  //sees if there is an error
	 	echo "Error putting data in database.<br /><br /><a href=\"javascript:history.back()\">Go Back</a><br /><br />\n\n";
		echo "QUERY: ".$query;		
	} else { //no error, we set return the result
		return($result);
	}
}



//universal database function, v2 with clean error reporting
function dbase_query_clean($query){
	global $dw_host, $dw_user, $dw_pass, $dw_dbase;
	
	$db  = mysql_pconnect($dw_host, $dw_user, $dw_pass);
	if (!$db){  //sees if there is an error
		$error =  "DB error 1: connect failed.";
		return(array(1,$error));
	}
	
	mysql_select_db($dw_dbase);	
	
	$result = mysql_query($query);
	
	if (!$result) {  //sees if there is an error
	 	$error = "DB error 2: query failed.";
		return(array(2,$error));	
	} else { //no error, we set return the result
		return($result);
	}
}




function br2nl($string) {
	$string = str_replace('<br />|<br/>', '\r\n', $string);
	
	return $string;
	
}


/*
from php.net
telefoontoestel at officexpert dot digitaal dot biz
12-Feb-2003 12:13
I've created a small function for the optimization of an array. It removes all empty elements and sets the next filled element
to the place the empty element was. It is a function wich argument is passed by reference, so it will only return true, in case
the value of the argument was correct or false if the value was incorrect.
*/

function arropt(&$array){ // pass by reference
       if(!is_array($array)){
              return false;
       }
       $temp; // create a temporary array;
       for($i = 0; $i < sizeof($array); $i++){
              if(!empty($array[$i])){
                      $temp[sizeof($temp)] = $array[$i];
               }
       }
       $array = $temp;
       return true;
}



/* Create an option list of all the possible build configurations / certificates that we'll use
   selected should be 0 unless the selected of a certain build is set.
*/

function listbuilds($selected){
	$query = "SELECT * FROM `builds`";
	$result = dbase_query($query);
	
	for($i = 0; $i < mysql_num_rows($result); $i++){
		$row = mysql_fetch_array($result);
		if ($row['buildID'] != $selected){
			$buffer = $buffer."<option value=\"".$row['buildID']."\">".$row['buildname']."</option>\r\n";
		} elseif ($row['buildID'] == $selected){
			$buffer = $buffer."<option value=\"".$row['buildID']."\" selected>".$row['buildname']."</option>\r\n";
		}
	}
	return $buffer;

}

/* returns the specific name of the build when a buildID is given
*/

function buildname($theID){
	$query = "SELECT * FROM `builds` WHERE `buildID` = ".$theID;
	$result = dbase_query($query);
	$row = mysql_fetch_array($result);
	return $row['buildname'];
}

/* Create an option list of all the possible network configs, returning the requesting IP as the selected one 
   if it exists in the database.
   if $theID is set, it will make that item selected, if not, it will attempt to select it from the IP address 
   of the requesting user
*/

function listnetwork($theID,$useCurrent=1){
	$exp_ip = explode(".",$_SERVER['REMOTE_ADDR']);
	$my_ip_range = $exp_ip[0].".".$exp_ip[1].".".$exp_ip[2];
	$query = "SELECT * FROM `network`";
	$result = dbase_query($query);
	
	for($i = 0; $i < mysql_num_rows($result); $i++){
		$row = mysql_fetch_array($result);
		if ($theID == 0){
		    if ($row['ip_range'] != $my_ip_range OR !$useCurrent){
			    $buffer = $buffer."<option value=\"".$row['netID']."\">".$row['ip_range']."</option>\r\n";
		    } elseif ($row['ip_range'] == $my_ip_range OR $useCurrent){
			    $buffer = $buffer."<option value=\"".$row['netID']."\" selected>".$row['ip_range']."</option>\r\n";
		    }
		} else {
			if ($row['netID'] != $theID){
			    $buffer = $buffer."<option value=\"".$row['netID']."\">".$row['ip_range']."</option>\r\n";
		    } elseif ($row['netID'] == $theID){
			    $buffer = $buffer."<option value=\"".$row['netID']."\" selected>".$row['ip_range']."</option>\r\n";
		    }
		
		
		}
	}
	return $buffer;

}

/*
	create a select list of all available printers
	$myarr is either 0 if no selected printers or an array containing the printer IDs of all selected printers
*/


function list_printers($myarr){
	$query = "SELECT * FROM `printers` ORDER BY `printerID`;";
	$result = dbase_query($query);
	//variable = documents.forms[0].elements['printers[]'];
	$num = mysql_num_rows($result);
	$buffer = "<select name='printers[]' size='".$num."' multiple='true' onChange='if (document.images) copySelected(this.form.elements[\"printers[]\"],this.form.default_printer)'>\n";
	
	
	$buffer .= '<option value="0">----------------</option>';
	
	for($i = 0; $i < $num; $i++){
		$row = mysql_fetch_array($result);
		$buffer .="<option value='".$row["printerID"]."'";
		if ($myarr != 0){
			if (in_array($row["printerID"],$myarr)){  //add selected to the printers if they're in the array we're fed
				$buffer .= " selected "; 
			}
		}
		$buffer .= ">".$row["printer_name"]."</option>\n";
	}
	$buffer .= "</select>\n";


	return $buffer;

}

/* clears all assigned printers for a given $machineID
*/

function clear_printers($machineID){
	$query = "DELETE FROM `assigned_printers` WHERE `machineID` = '".$machineID."';";
	dbase_query($query);
}


/* outputs a static list of the printers with <br> seperating them
*/
function display_printers($machineID){
	$query = "SELECT * FROM `assigned_printers` WHERE `machineID` = '".$machineID."';";
	$result = dbase_query($query);
	$num = mysql_num_rows($result);
	if ($num > 0){ 
		for ($i = 0;$i< $num;$i++){
			$row = mysql_fetch_array($result);
			$query = "SELECT * FROM `printers` WHERE `printerID` = '".$row["printerID"]."' LIMIT 1;";
			$rs = dbase_query($query);
			$rr = mysql_fetch_array($rs);
			$buffer .= $rr['printer_name']."<br>\n";
		}
	} else {
		$buffer = "None";
	}
	return $buffer;
}


/*outputs a select list of the selected printers, with the default printer selected, for use in the modify machine page */

function list_selected_printers($machineID){
	$default = get_default_printer($machineID);
	$query = "SELECT * FROM `assigned_printers` WHERE `machineID` = '".$machineID."';";
	$result = dbase_query($query);
	$num = mysql_num_rows($result);
	if ($num > 0){ 
		$buffer = '';
		for ($i = 0;$i< $num;$i++){
			$row = mysql_fetch_array($result);
			$myid = $row['printerID'];
			$name = get_printer_name($myid);
			if ($default == $myid) { 
				$buffer .= "<option value='".$myid."' selected='selected'>".$name."</option>\n";
			} else {
				$buffer .= "<option value='".$myid."'>".$name."</option>\n";
			}
		}
	}
	return $buffer;
}


function get_printer_name($id){
	$query = "SELECT * FROM `printers` WHERE `printerID` = '".$id."' LIMIT 1;";
	$result = dbase_query($query);
	$row = mysql_fetch_array($result);
	return $row['printer_name'];
}


function get_default_printer($machineID){
	$query = "SELECT * FROM `machines` WHERE `machineID` = '".$machineID."' LIMIT 1;";
	$result = dbase_query($query);
	$row = mysql_fetch_array($result);
	return $row['default_printer'];	
}

function clear_schedules($machineID){
	$query = "DELETE FROM `schedules` WHERE `machineID` = '".$machineID."';";
	dbase_query($query);
}

function clear_schedules_repeat($machineID){
	$query = "DELETE FROM `schedules_repeat` WHERE `machineID` = '".$machineID."';";
	dbase_query($query);
}



function list_screens($theID){
	$buffer .= '<option value="0">----------------</option>';
	$query = "SELECT * FROM `cscreen` ORDER BY `screenID`;";
	$result = dbase_query($query);
	//variable = documents.forms[0].elements['printers[]'];
	$num = mysql_num_rows($result);
	for($i = 0; $i < $num; $i++){
		$row = mysql_fetch_array($result);
		
		$buffer .="<option value='".$row["screenID"]."'";	
		if ($row["screenID"] == $theID){ 
			$buffer .= " selected "; 
		}
		$buffer .= ">".$row["setup"]."</option>\n";
	}
	return $buffer;
}

function get_screen($id){
	$query = "SELECT * FROM `cscreen` WHERE `screenID` = '".$id."' LIMIT 1;";
	$result = dbase_query($query);
	$row = mysql_fetch_array($result);
	return $row['command'];
}



?>
Return current item: RadMacLab