// *********************************************************************************************************
// ** MISC **
// *********************************************************************************************************
/*
* Log a message
*
* @param string $message
* @param integer $level from 1 to 4
*/
function log($message, $level=1)
{
if($this->pagi != false)
$this->pagi->conlog($message, $level);
else
error_log(date('r') . ' - ' . $message);
}
/**
* Add event handler
*
* Known Events include ( http://www.voip-info.org/wiki-asterisk+manager+events )
* Link - Fired when two voice channels are linked together and voice data exchange commences.
* Unlink - Fired when a link between two voice channels is discontinued, for example, just before call completion.
* Newexten -
* Hangup -
* Newchannel -
* Newstate -
* Reload - Fired when the "RELOAD" console command is executed.
* Shutdown -
* ExtensionStatus -
* Rename -
* Newcallerid -
* Alarm -
* AlarmClear -
* Agentcallbacklogoff -
* Agentcallbacklogin -
* Agentlogoff -
* MeetmeJoin -
* MessageWaiting -
* join -
* leave -
* AgentCalled -
* ParkedCall - Fired after ParkedCalls
* Cdr -
* ParkedCallsComplete -
* QueueParams -
* QueueMember -
* QueueStatusEnd -
* Status -
* StatusComplete -
* ZapShowChannels - Fired after ZapShowChannels
* ZapShowChannelsComplete -
*
* @param string $event type or * for default handler
* @param string $callback function
* @return boolean sucess
*/
function add_event_handler($event, $callback)
{
$event = strtolower($event);
if(isset($this->event_handlers[$event]))
{
$this->log("$event handler is already defined, not over-writing.");
return false;
}
$this->event_handlers[$event] = $callback;
return true;
}
/**
* Process event
*
* @access private
* @param array $parameters
* @return mixed result of event handler or false if no handler was found
*/
function process_event($parameters)
{
$ret = false;
$e = strtolower($parameters['Event']);
$this->log("Got event.. $e");
$handler = '';
if(isset($this->event_handlers[$e])) $handler = $this->event_handlers[$e];
elseif(isset($this->event_handlers['*'])) $handler = $this->event_handlers['*'];
if(function_exists($handler))
{
$this->log("Execute handler $handler");
$ret = $handler($e, $parameters, $this->server, $this->port);
}
else
$this->log("No event handler for event '$e'");
return $ret;
}