Location: PHPKode > scripts > Backgrounder > backgrounder/background_class_inc.php
<?php

/**
 * Background object
 * This class is a means to access the connection handling API
 * @author Paul Scott
 * @copyright GNU/GPL 2006
 * @package conn_handler
 */
class background
{
	/**
	 * Property to hold user connection status
	 * @var $connStatus
	 */
	var $connStatus;
	
	/**
	 * Property to hold the callback function
	 * @var $callback
	 */
	var $callback;
	
	/**
	 * Property to hold the users email address
	 * @var $address
	 */
	var $address;
	
	/**
	 * Property to hold the email subject
	 * @var $subject
	 */
	var $subject;
	
	/**
	 * Property to hold the message
	 * @var $message
	 */
	var $message;
	
	/**
	 * Method to check users connection status
	 * @param void
	 * @return property $connStatus bool FALSE on user abort
	 */
	function isUserConn()
	{
		//is the user still connected?
		if(connection_status()!=0)
		{
			//set a property saying that the user is dead
			$this->connStatus = FALSE;
			return FALSE;
		}
		else {
			$this->connStatus = TRUE;
			return TRUE;
		}
	}
	
	/**
	 * Method to keep the script going in the background
	 * @param void
	 * @return newline chars
	 */
	function keepAlive()
	{
		set_time_limit(0); 
		//this will force the script to keep running till the end
		ignore_user_abort(TRUE); 
		//funny thing with PHP, it needs output to keep running(?)!
		while($this->connStatus != TRUE) 
		{
			//this will save the loop
			print "\n"; 
   			flush(); //Now php will check the connection
   			sleep(1);
		} 
	}
	
	/**
	 * Method to send mail as the callback function
	 * @param void
	 * @return void
	 */
	function sendit()
	{
		mail($this->address, $this->subject ,$this->message);
	}
	
	/**
	 * Method for the callback function
	 * This is called dynamically be the shutdown function callback
	 * @param string $address - the users email addy
	 * @param string $subject - the subject
	 * @param string $message - the message to say the operation is done
	 * @return void
	 */
	function setCallback($address,$subject,$message)
	{
		//set the properties
		$this->address = $address;
		$this->subject = $subject;
		$this->message = $message;
		
		//pop a mail off to the user when his operation completes
		//create the __Lambda Function
		$callback = create_function('$address, $subject, $message','mail($address, $subject ,$message);');
		//execute the __Lambda function
		$callback($address, $subject, $message);
		//register the callback method
		register_shutdown_function(array(&$this, "sendit"));
	}
	
}//end class
?>
Return current item: Backgrounder