<? 
class library extends query{
	function writelog ($time,$modules,$action){
		// Format of log would be, Date/Time|Modules Accessed|Action Done
		$log_entry=$time."|".$modules."|".$action;
		$filename="logfiles/".$_SESSION['system_user_id'].".log";
		if (file_exists($filename)){
			$handle = fopen($filename, 'a');
			fwrite($handle, $log_entry);			
		}else{
			$handle = fopen($filename, "w");
			fwrite($handle, $log_entry);			
		}
		fclose($handle);
		return true;
	}

// function to get local time. Singapore = 8 hours GMT offset.
	function get_local_time($time="none"){
		$hour_delay=8;$minute_delay=0;
		if($time!='none')
			return date("Y-m-d H:i:s",mktime (gmdate("H")+$hour_delay,gmdate("i")+$minute_delay,gmdate("s"),gmdate("m"),gmdate("d"),gmdate("Y")));
		else
			return date("Y-m-d",mktime(gmdate("H")+$hour_delay,gmdate("i")+$minute_delay,gmdate("s"),gmdate("m"),gmdate("d"),gmdate("Y")));
	}
		
//  function to encode and decode 3 times
	function encode3t($str){	
		for($i=0; $i<3;$i++){
			$str=strrev(base64_encode($str));
		}
		return $str;
	}
	
	function decode3t($str){
		for($i=0; $i<3;$i++){					
			$str=base64_decode(strrev($str));
		}
		return $str;
	}

// function to check email addresses
	function emailValidation($email){
		if (eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)) 
			return true;
		else 
			return false;//"Invalid e-mail";
		}

################################# CHange Date Format ################################################		
	function gmtsecondstoDB(){
		$GMToffset = 8*3600; // Singapore's Time
		return time()+$GMToffset;
	}

	function localdatetimefromDB($giventime){
		$GMToffset = 8*3600; // Australia Melbourne's time
		return date("d-m-Y H:i:s",$giventime+($GMToffset));
	}
	
	
	// function to generate password
	function createRandomPassword() {
		$chars = "abcdefghijkmnopqrstuvwxyz023456789";
		srand((double)microtime()*1000000);
		$i = 0;
		$pass = '' ;
		while ($i <= 7) {
			$num = rand() % 33;
			$tmp = substr($chars, $num, 1);
			$pass = $pass . $tmp;
			$i++;
		}
		return $pass;
	}

	function emailConfirmation($id){
		global $qry,$lib,$mail;
		$SQL = "SELECT * FROM tbl_email WHERE email_key='confirmation';";
		$result=$qry->querySelectSingle($SQL);
		$subject = $result['email_subject'];
		$message = $result['email_content'];
		
		$SQL = "SELECT * FROM tbl_registrant WHERE registrant_id='$id';";
		$result=$qry->querySelectSingle($SQL);
		$registrant_salutation = $result['registrant_salutation'];
		$registrant_fullname = $result['registrant_fullname'];
		$email = $result['registrant_email'];

		$message = str_replace("@registrant_salutation@",$registrant_salutation,$message);
		$message = str_replace("@registrant_fullname@",$registrant_fullname,$message);
				
		$mail->IsHTML(true);
		//$mail->From = "gillian_wee@splanner.com.sg";
		$mail->SetFrom("gillian_wee@splanner.com.sg","Gillian Wee");
		$mail->AddAddress($email);
		$mail->Subject = $subject;
		$mail->Body = $message;
		$mail->Send(); // send message
	}

	function emailRejection($id){
		global $qry,$lib,$mail;
		$SQL = "SELECT * FROM tbl_email WHERE email_key='denied';";
		$result=$qry->querySelectSingle($SQL);
		$subject = $result['email_subject'];
		$message = $result['email_content'];
		
		$SQL = "SELECT * FROM tbl_registrant WHERE registrant_id='$id';";
		$result=$qry->querySelectSingle($SQL);
		$registrant_salutation = $result['registrant_salutation'];
		$registrant_fullname = $result['registrant_fullname'];
		$email = $result['registrant_email'];

		$message = str_replace("@registrant_salutation@",$registrant_salutation,$message);
		$message = str_replace("@registrant_fullname@",$registrant_fullname,$message);
				
		$mail->IsHTML(true);
		$mail->SetFrom("gillian_wee@splanner.com.sg","Gillian Wee");
		$mail->AddAddress($email);
		$mail->Subject = $subject;
		$mail->Body = $message;
		$mail->Send(); // send message
	}

}//end class

$lib=new library;
?>