This browser does not support basic Web standards, preventing the display of our site's intended design. May we suggest that you upgrade your browser?

Global Class

This class should be implemented on every PHP application. It has been standardized and includes the major functions that are needed by every PHP application developed by webdev. Simply include this at the top of every file in your app.
Note that any functions that return or require private variables are within the class (things like creating a connection), while functions that are essentially shortcuts for good coding practices are included at the end of the file outside for the GlobalClass object (like properlly terminating a session before calling a redirect).
A section is even included for you to add your functions that are specific to your own project.

<?
include_once('adodb/adodb.inc.php');

class GlobalClass{
// To set this file to be your own, do a find/replace for 049 and replace it with your app number
// Also, change the 'name' part of $database

	# variables that may need to be re-used in another class
	# can be used to define a second connection
	protected $driver;
	protected $server;
	protected $user = 'app049';
	protected $password;
	protected $database;
	
	# read-only variables
	private $base_url;
	private $base_path;
	private $file_path;
	private $image_path;
	private $email_server;
	
	function GlobalClass()
	{
		$this->driver = 'odbc_mssql';
		$this->server = 'dbms04.uwec.edu';
		$this->password = 'pw049';
		$this->database = 'app049nursing';
	
		$this->base_url = 'http://apptest.uwec.edu/nurs/scripts/advising';
		$this->base_path = 'e:/sites/nurs/public/scripts';
		$this->file_path = 'e:/sites/nurs/priv';
		$this->image_path = $this->base_path.'/images';
		$this->email_server = 'smtp2.uwec.edu';
	}
	
	# returns a database object
	public function connect()
	{
		$db = ADONewConnection($this->driver);# create a connection
		//$db->debug = true;
		$dsn = "Driver={SQL Server};Server=$this->server;Database=$this->database;";
		$db->PConnect($dsn, $this->user, $this->password);
		if (!$db)
		{
			die("&quot;Database configuration error in GlobalFile.php\n!&quot;");
		}
		return $db;
	}
		
	# Executes all sql statments to the DB.
	# Only to be used if there is only one query on a page
	public function dbExe($sql){
			
			$conn = $this->connect();
			// no SPs becuase there is no security difference, 
			// I still need to connect as the same user to run the same
			// query, still open to injection attacks.
			$rs = $conn->Execute($sql);
			if(!$rs){
				$this->logError("DB ERROR: [ $sql ]");
			}
		
		return $rs;
	}
	
	public function get_base_url()
	{
		return $this->base_url;
	}
	
	public function get_base_path()
	{
		return $this->base_path;
	}
	
	public function get_file_path()
	{
		return $this->file_path;
	}
	
	public function get_image_path()
	{
		return $this->image_path;
	}
	
	public function get_email_server()
	{
		return $this->email_server;
	}	

	# Provides logging at the application level
	public function logError($data){
		$log = $this->file_path ."/log.txt"; // CHANGE ME: full path only, no relative paths!
		// - get current time
		$currTime = date("j-m-y @ g:i a"); 
		$this->writeToFile($log, "$data - $currTime\r\n");
		echo $data;
	}
	
	# Writes to a text file
	public function writeToFile($file, $str){
		$path = $file;
		// - open the location of the log as set in config.php
		$handle = fopen($path, "a");
		// - if we cannot open the log file kill the application
		if($handle == NULL) {
			die("Error opening log file: ".$log."\n");
		}
		// - write error message and time it occured
		fwrite($handle, "$str");
		// - close the file handle
		fclose($handle);
	}
	
	# starts the session
	public function start_session()
	{
		# $user is used because it is unique to the application
		session_name($this->user);
		session_start();
	}
	
	# resets the session	
	public function reset_session(){
		$this->kill_session();
		$this->start_session();
	}
	
	# kills the session
	public function kill_session(){
		$_SESSION = NULL;
		session_destroy();
	}
	
	# starts the session, then redirects the user if they aren't signed in
	public function startSecurePage(){
		$this->start_session();
		if($_SESSION['user'] === "" ||$_SESSION['user'] === NULL){
			$_SESSION['Secure'] = 0;
			$folders = explode("/", $_SERVER['PHP_SELF']);
			if ($folders[4] == "admin")
			{
				redirect("$this->base_url/public/adminlogin.php?msg=Please sign in");
			}else{
				redirect("$this->base_url/public/login.php?msg=Please sign in");
			}
		}else{
			$_SESSION['Secure'] = 1;
		}
	}
} // end of Global Class

####### global functions that don't need variables from the object #######
	
# confirms a uwec user
function confirmUser($user, $pass)
{
	error_reporting(0);
	// Designate a few variables
	$host = "ldap";
	$user .= "@uwec.edu";
	$ad = ldap_connect($host)
		  or die( "Could not connect!" );
	// Set version number
	ldap_set_option($ad, LDAP_OPT_PROTOCOL_VERSION, 3)
		 or die ("Could not set ldap protocol");
	// Binding to ldap server
	$bd = ldap_bind($ad, $user, $pass);
	if ($bd == 1/* && $pass != ""*/)
	{
		$result = 1;
	}
	else
	{
		$result = 0;
	}
	error_reporting(E_ALL);
	return $result;
}
	
# closes the session, then redirects
function redirect($loc){
	session_write_close();
	header("location: $loc");
}

################################## Project Specific Functions ####################################

?>
	
Excellence. Our Measure. Our Motto. Our Goal.