#!/usr/bin/php -q
<?PHP

set_time_limit(5);
ob_implicit_flush(false);
error_reporting(0);


// growl.agi (IPADDR|PASSWORD|NOTIFICATION_TYPE|NOTIFICATION_STRING)
// Asterisk AGI script to send Growl notifications
// Put in /var/lib/asterisk/agi-bin (must be marked executable)
// Example:
// exten => 1234,1,NoOp("Growl")
// exten => 1234,n,AGI(growl.agi,192.168.1.222|growlpw|INCOMING_CALL|${CALLERID(all)})
//
// This script can be tested from the shell as it does not depend on asterisk for any
// of its data -->  ./growl.agi 192.168.1.222 growlpw INCOMING_CALL 555-1212
//
//
// class growl is from: 
// class.growl.php (part of the php-growl project by Tyler Hall hosted on Google Code)
//  http://creativecommons.org/licenses/by/2.5/
//


	class Growl
	{
		var $appName = "PHP Notify";
		var $address;
		var $password;

		var $port = 9887;
		var $notifications = array();

		function Growl($app_name = null)
		{
			if(isset($app_name))
				$this->appName = $app_name;
		}
		
		function setAddress($address, $password = "")
		{
			$this->address = $address;
			$this->password = $password;
		}
		
		function addNotification($name, $enabled = true)
		{
			if($name != "")
				$this->notifications[] = array("name" => $name, "enabled" => $enabled);
		}
		
		function register($address = null, $password = "")
		{
			if(isset($address))
			{
				$this->address = $address;
				$this->password = $password;
			}
			
			$data = "";
			$defaults = "";
			$num_defaults = 0;
			
			for($i = 0; $i < count($this->notifications); $i++)
			{
				$data .= pack("n", strlen($this->notifications[$i]["name"])) . $this->notifications[$i]["name"];
				if($this->notifications[$i]["enabled"])
				{
					$defaults .= pack("c", $i);
					$num_defaults++;
				}
			}

			// pack(Protocol version, type, app name, number of notifications to register)
			$data  = pack("c2nc2", 1, 0, strlen($this->appName), count($this->notifications), $num_defaults) . $this->appName . $data . $defaults;
			$data .= pack("H32", md5($data . $this->password));

			$this->send($data);
		}
		
		function notify($name, $title, $message, $priority = 0, $sticky = false)
		{
			
			$priority = intval($priority);
			
			$flags = ($priority & 7) * 2;
			if($priority < 0) $flags |= 8;
			if($sticky) $flags |= 1;

			// pack(protocol version, type, priority/sticky flags, notification name length, title length, message length. app name length)
			$data = pack("c2n5", 1, 1, $flags, strlen($name), strlen($title), strlen($message), strlen($this->appName));
			$data .= $name . $title . $message . $this->appName;
			$data .= pack("H32", md5($data . $this->password));

			$this->send($data);
		}
		
		function send($data)
		{
			if(function_exists("socket_create") && function_exists("socket_sendto"))
			{
				$sck = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
				socket_sendto($sck, $data, strlen($data), 0x100, $this->address, $this->port);
			}
			elseif(function_exists("fsockopen"))
			{
				$fp = fsockopen("udp://" . $this->address, $this->port);
				fwrite($fp, $data);
				fclose($fp);
			}
		}
	}



if (!defined('STDERR')) 
{ 
   define('STDERR', fopen('php://stderr', 'w')); 
} 


// Display arguments received from command line or Asterisk

fwrite(STDERR,"Got $argc Arguments\n");
fflush(STDERR);

foreach ($argv as $key => $value)
  {
     fwrite(STDERR,"-- $key => $value ");
     fflush(STDERR); 
  }




$client = new Growl("Asterisk PBX");
$client->setAddress($argv[1],"asterisk");
$client->addNotification($argv[3]);
$client->register();
$client->notify($argv[3],$argv[3],$argv[4]);

exit;
?>

