Source for file phpagi-asmanager.php

Documentation is available at phpagi-asmanager.php

  1. <?php
  2.  /**
  3.   * phpagi-asmanager.php : PHP Asterisk Manager functions
  4.   * Website: http://phpagi.sourceforge.net
  5.   *
  6.   * $Id: phpagi-asmanager.php,v 1.10 2005/05/25 18:43:48 pinhole Exp $
  7.   *
  8.   * Copyright (c) 2004 - 2010 Matthew Asham <matthew@ochrelabs.com>, David Eder <david@eder.us> and others
  9.   * All Rights Reserved.
  10.   *
  11.   * This software is released under the terms of the GNU Lesser General Public License v2.1
  12.   *  A copy of which is available from http://www.gnu.org/copyleft/lesser.html
  13.   * 
  14.   * We would be happy to list your phpagi based application on the phpagi
  15.   * website.  Drop me an Email if you'd like us to list your program.
  16.   *
  17.   * @package phpAGI
  18.   * @version 2.0
  19.   */
  20.  
  21.  
  22.  /**
  23.   * Written for PHP 4.3.4, should work with older PHP 4.x versions.
  24.   * Please submit bug reports, patches, etc to http://sourceforge.net/projects/phpagi/
  25.   * Gracias. :)
  26.   *
  27.   */
  28.  
  29.   if(!class_exists('AGI'))
  30.   {
  31.     require_once(dirname(__FILE__DIRECTORY_SEPARATOR 'phpagi.php');
  32.   }
  33.  
  34.  /**
  35.   * Asterisk Manager class
  36.   *
  37.   * @link http://www.voip-info.org/wiki-Asterisk+config+manager.conf
  38.   * @link http://www.voip-info.org/wiki-Asterisk+manager+API
  39.   * @example examples/sip_show_peer.php Get information about a sip peer
  40.   * @package phpAGI
  41.   */
  42.   {
  43.    /**
  44.     * Config variables
  45.     *
  46.     * @var array 
  47.     * @access public
  48.     */
  49.     public $config;
  50.  
  51.    /**
  52.     * Socket
  53.     *
  54.     * @access public
  55.     */
  56.     public $socket = NULL;
  57.  
  58.    /**
  59.     * Server we are connected to
  60.     *
  61.     * @access public
  62.     * @var string 
  63.     */
  64.     public $server;
  65.  
  66.    /**
  67.     * Port on the server we are connected to
  68.     *
  69.     * @access public
  70.     * @var integer 
  71.     */
  72.     public $port;
  73.  
  74.    /**
  75.     * Parent AGI
  76.     *
  77.     * @access private
  78.     * @var AGI 
  79.     */
  80.     private $pagi;
  81.  
  82.    /**
  83.     * Event Handlers
  84.     *
  85.     * @access private
  86.     * @var array 
  87.     */
  88.     private $event_handlers;
  89.  
  90.     /**
  91.      * Whether we're successfully logged in
  92.      *
  93.      * @access private
  94.      * @var boolean 
  95.      */
  96.     private $_logged_in = FALSE;
  97.     
  98.    /**
  99.     * Constructor
  100.     *
  101.     * @param string $config is the name of the config file to parse or a parent agi from which to read the config
  102.     * @param array $optconfig is an array of configuration vars and vals, stuffed into $this->config['asmanager']
  103.     */
  104.     function AGI_AsteriskManager($config=NULL$optconfig=array())
  105.     {
  106.       // load config
  107.       if(!is_null($config&& file_exists($config))
  108.         $this->config = parse_ini_file($configtrue);
  109.       elseif(file_exists(DEFAULT_PHPAGI_CONFIG))
  110.         $this->config = parse_ini_file(DEFAULT_PHPAGI_CONFIGtrue);
  111.  
  112.       // If optconfig is specified, stuff vals and vars into 'asmanager' config array.
  113.       foreach($optconfig as $var=>$val)
  114.         $this->config['asmanager'][$var$val;
  115.  
  116.       // add default values to config for uninitialized values
  117.       if(!isset($this->config['asmanager']['server'])) $this->config['asmanager']['server''localhost';
  118.       if(!isset($this->config['asmanager']['port'])) $this->config['asmanager']['port'5038;
  119.       if(!isset($this->config['asmanager']['username'])) $this->config['asmanager']['username''phpagi';
  120.       if(!isset($this->config['asmanager']['secret'])) $this->config['asmanager']['secret''phpagi';
  121.     }
  122.  
  123.    /**
  124.     * Send a request
  125.     *
  126.     * @param string $action 
  127.     * @param array $parameters 
  128.     * @return array of parameters
  129.     */
  130.     function send_request($action$parameters=array())
  131.     {
  132.       $req "Action: $action\r\n";
  133.       foreach($parameters as $var=>$val)
  134.         $req .= "$var$val\r\n";
  135.       $req .= "\r\n";
  136.       fwrite($this->socket$req);
  137.       return $this->wait_response();
  138.     }
  139.  
  140.    /**
  141.     * Wait for a response
  142.     *
  143.     * If a request was just sent, this will return the response.
  144.     * Otherwise, it will loop forever, handling events.
  145.     *
  146.     * @param boolean $allow_timeout if the socket times out, return an empty array
  147.     * @return array of parameters, empty on timeout
  148.     */
  149.     function wait_response($allow_timeout=false)
  150.     {
  151.       $timeout false;
  152.       do
  153.       {
  154.         $type NULL;
  155.         $parameters array();
  156.  
  157.         $buffer trim(fgets($this->socket4096));
  158.         while($buffer != '')
  159.         {
  160.           $a strpos($buffer':');
  161.           if($a)
  162.           {
  163.             if(!count($parameters)) // first line in a response?
  164.             {
  165.               $type strtolower(substr($buffer0$a));
  166.               if(substr($buffer$a 2== 'Follows')
  167.               {
  168.                 // A follows response means there is a miltiline field that follows.
  169.                 $parameters['data''';
  170.                 $buff fgets($this->socket4096);
  171.                 while(substr($buff06!= '--END ')
  172.                 {
  173.                   $parameters['data'.= $buff;
  174.                   $buff fgets($this->socket4096);
  175.                 }
  176.               }
  177.             }
  178.  
  179.             // store parameter in $parameters
  180.             $parameters[substr($buffer0$a)substr($buffer$a 2);
  181.           }
  182.           $buffer trim(fgets($this->socket4096));
  183.         }
  184.  
  185.         // process response
  186.         switch($type)
  187.         {
  188.           case ''// timeout occured
  189.             $timeout $allow_timeout;
  190.             break;
  191.           case 'event':
  192.             $this->process_event($parameters);
  193.             break;
  194.           case 'response':
  195.             break;
  196.           default:
  197.             $this->log('Unhandled response packet from Manager: ' print_r($parameterstrue));
  198.             break;
  199.         }
  200.       while($type != 'response' && !$timeout);
  201.       return $parameters;
  202.     }
  203.  
  204.    /**
  205.     * Connect to Asterisk
  206.     *
  207.     * @example examples/sip_show_peer.php Get information about a sip peer
  208.     *
  209.     * @param string $server 
  210.     * @param string $username 
  211.     * @param string $secret 
  212.     * @return boolean true on success
  213.     */
  214.     function connect($server=NULL$username=NULL$secret=NULL)
  215.     {
  216.       // use config if not specified
  217.       if(is_null($server)) $server $this->config['asmanager']['server'];
  218.       if(is_null($username)) $username $this->config['asmanager']['username'];
  219.       if(is_null($secret)) $secret $this->config['asmanager']['secret'];
  220.  
  221.       // get port from server if specified
  222.       if(strpos($server':'!== false)
  223.       {
  224.         $c explode(':'$server);
  225.         $this->server = $c[0];
  226.         $this->port = $c[1];
  227.       }
  228.       else
  229.       {
  230.         $this->server = $server;
  231.         $this->port = $this->config['asmanager']['port'];
  232.       }
  233.  
  234.       // connect the socket
  235.       $errno $errstr NULL;
  236.       $this->socket = @fsockopen($this->server$this->port$errno$errstr);
  237.       if($this->socket == false)
  238.       {
  239.         $this->log("Unable to connect to manager {$this->server}:{$this->port} ($errno): $errstr");
  240.         return false;
  241.       }
  242.  
  243.       // read the header
  244.       $str = fgets($this->socket);
  245.       if($str == false)
  246.       {
  247.         // a problem.
  248.         $this->log("Asterisk Manager header not received.");
  249.         return false;
  250.       }
  251.       else
  252.       {
  253.         // note: don't $this->log($str) until someone looks to see why it mangles the logging
  254.       }
  255.  
  256.       // login
  257.       $res = $this->send_request('login'array('Username'=>$username'Secret'=>$secret));
  258.       if($res['Response'!= 'Success')
  259.       {
  260.         $this->_logged_in = FALSE;
  261.         $this->log("Failed to login.");
  262.         $this->disconnect();
  263.         return false;
  264.       }
  265.       $this->_logged_in = TRUE;
  266.       return true;
  267.     }
  268.  
  269.    /**
  270.     * Disconnect
  271.     *
  272.     * @example examples/sip_show_peer.php Get information about a sip peer
  273.     */
  274.     function disconnect()
  275.     {
  276.       if($this->_logged_in==TRUE)
  277.         $this->logoff();
  278.       fclose($this->socket);
  279.     }
  280.  
  281.    // *********************************************************************************************************
  282.    // **                       COMMANDS                                                                      **
  283.    // *********************************************************************************************************
  284.    /**
  285.     * Set Absolute Timeout
  286.     *
  287.     * Hangup a channel after a certain time.
  288.     *
  289.     * @link http://www.voip-info.org/wiki-Asterisk+Manager+API+Action+AbsoluteTimeout
  290.     * @param string $channel Channel name to hangup
  291.     * @param integer $timeout Maximum duration of the call (sec)
  292.     */
  293.     function AbsoluteTimeout($channel, $timeout)
  294.     {
  295.       return $this->send_request('AbsoluteTimeout'array('Channel'=>$channel'Timeout'=>$timeout));
  296.     }
  297.  
  298.    /**
  299.     * Change monitoring filename of a channel
  300.     *
  301.     * @link http://www.voip-info.org/wiki-Asterisk+Manager+API+Action+ChangeMonitor
  302.     * @param string $channel the channel to record.
  303.     * @param string $file the new name of the file created in the monitor spool directory.
  304.     */
  305.     function ChangeMonitor($channel, $file)
  306.     {
  307.       return $this->send_request('ChangeMontior'array('Channel'=>$channel'File'=>$file));
  308.     }
  309.  
  310.    /**
  311.     * Execute Command
  312.     *
  313.     * @example examples/sip_show_peer.php Get information about a sip peer
  314.     * @link http://www.voip-info.org/wiki-Asterisk+Manager+API+Action+Command
  315.     * @link http://www.voip-info.org/wiki-Asterisk+CLI
  316.     * @param string $command
  317.     * @param string $actionid message matching variable
  318.     */
  319.     function Command($command, $actionid=NULL)
  320.     {
  321.       $parameters = array('Command'=>$command);
  322.       if($actionid) $parameters['ActionID'] = $actionid;
  323.       return $this->send_request('Command'$parameters);
  324.     }
  325.  
  326.    /**
  327.     * Enable/Disable sending of events to this manager
  328.     *
  329.     * @link http://www.voip-info.org/wiki-Asterisk+Manager+API+Action+Events
  330.     * @param string $eventmask is either 'on', 'off', or 'system,call,log'
  331.     */
  332.     function Events($eventmask)
  333.     {
  334.       return $this->send_request('Events'array('EventMask'=>$eventmask));
  335.     }
  336.  
  337.    /**
  338.     * Check Extension Status
  339.     *
  340.     * @link http://www.voip-info.org/wiki-Asterisk+Manager+API+Action+ExtensionState
  341.     * @param string $exten Extension to check state on
  342.     * @param string $context Context for extension
  343.     * @param string $actionid message matching variable
  344.     */
  345.     function ExtensionState($exten, $context, $actionid=NULL)
  346.     {
  347.       $parameters = array('Exten'=>$exten, 'Context'=>$context);
  348.       if($actionid) $parameters['ActionID'] = $actionid;
  349.       return $this->send_request('ExtensionState'$parameters);
  350.     }
  351.  
  352.    /**
  353.     * Gets a Channel Variable
  354.     *
  355.     * @link http://www.voip-info.org/wiki-Asterisk+Manager+API+Action+GetVar
  356.     * @link http://www.voip-info.org/wiki-Asterisk+variables
  357.     * @param string $channel Channel to read variable from
  358.     * @param string $variable
  359.     * @param string $actionid message matching variable
  360.     */
  361.     function GetVar($channel, $variable, $actionid=NULL)
  362.     {
  363.       $parameters = array('Channel'=>$channel, 'Variable'=>$variable);
  364.       if($actionid) $parameters['ActionID'] = $actionid;
  365.       return $this->send_request('GetVar'$parameters);
  366.     }
  367.  
  368.    /**
  369.     * Hangup Channel
  370.     *
  371.     * @link http://www.voip-info.org/wiki-Asterisk+Manager+API+Action+Hangup
  372.     * @param string $channel The channel name to be hungup
  373.     */
  374.     function Hangup($channel)
  375.     {
  376.       return $this->send_request('Hangup'array('Channel'=>$channel));
  377.     }
  378.  
  379.    /**
  380.     * List IAX Peers
  381.     *
  382.     * @link http://www.voip-info.org/wiki-Asterisk+Manager+API+Action+IAXpeers
  383.     */
  384.     function IAXPeers()
  385.     {
  386.       return $this->send_request('IAXPeers');
  387.     }
  388.  
  389.    /**
  390.     * List available manager commands
  391.     *
  392.     * @link http://www.voip-info.org/wiki-Asterisk+Manager+API+Action+ListCommands
  393.     * @param string $actionid message matching variable
  394.     */
  395.     function ListCommands($actionid=NULL)
  396.     {
  397.       if($actionid)
  398.         return $this->send_request('ListCommands'array('ActionID'=>$actionid));
  399.       else
  400.         return $this->send_request('ListCommands');
  401.     }
  402.  
  403.    /**
  404.     * Logoff Manager
  405.     *
  406.     * @link http://www.voip-info.org/wiki-Asterisk+Manager+API+Action+Logoff
  407.     */
  408.     function Logoff()
  409.     {
  410.       return $this->send_request('Logoff');
  411.     }
  412.  
  413.    /**
  414.     * Check Mailbox Message Count
  415.     *
  416.     * Returns number of new and old messages.
  417.     *   Message: Mailbox Message Count
  418.     *   Mailbox: <mailboxid>
  419.     *   NewMessages: <count>
  420.     *   OldMessages: <count>
  421.     *
  422.     * @link http://www.voip-info.org/wiki-Asterisk+Manager+API+Action+MailboxCount
  423.     * @param string $mailbox Full mailbox ID <mailbox>@<vm-context>
  424.     * @param string $actionid message matching variable
  425.     */
  426.     function MailboxCount($mailbox, $actionid=NULL)
  427.     {
  428.       $parameters = array('Mailbox'=>$mailbox);
  429.       if($actionid) $parameters['ActionID'] = $actionid;
  430.       return $this->send_request('MailboxCount'$parameters);
  431.     }
  432.  
  433.    /**
  434.     * Check Mailbox
  435.     *
  436.     * Returns number of messages.
  437.     *   Message: Mailbox Status
  438.     *   Mailbox: <mailboxid>
  439.     *   Waiting: <count>
  440.     *
  441.     * @link http://www.voip-info.org/wiki-Asterisk+Manager+API+Action+MailboxStatus
  442.     * @param string $mailbox Full mailbox ID <mailbox>@<vm-context>
  443.     * @param string $actionid message matching variable
  444.     */
  445.     function MailboxStatus($mailbox, $actionid=NULL)
  446.     {    
  447.       $parameters = array('Mailbox'=>$mailbox);
  448.       if($actionid) $parameters['ActionID'] = $actionid;
  449.       return $this->send_request('MailboxStatus'$parameters);
  450.     }
  451.  
  452.    /**
  453.     * Monitor a channel
  454.     *
  455.     * @link http://www.voip-info.org/wiki-Asterisk+Manager+API+Action+Monitor
  456.     * @param string $channel
  457.     * @param string $file
  458.     * @param string $format
  459.     * @param boolean $mix
  460.     */
  461.     function Monitor($channel, $file=NULL, $format=NULL, $mix=NULL)
  462.     {
  463.       $parameters = array('Channel'=>$channel);
  464.       if($file) $parameters['File'] = $file;
  465.       if($format) $parameters['Format'] = $format;
  466.       if(!is_null($file)) $parameters['Mix'] = ($mix) ? 'true' : 'false';
  467.       return $this->send_request('Monitor'$parameters);
  468.     }
  469.  
  470.    /**
  471.     * Originate Call
  472.     *
  473.     * @link http://www.voip-info.org/wiki-Asterisk+Manager+API+Action+Originate
  474.     * @param string $channel Channel name to call
  475.     * @param string $exten Extension to use (requires 'Context' and 'Priority')
  476.     * @param string $context Context to use (requires 'Exten' and 'Priority')
  477.     * @param string $priority Priority to use (requires 'Exten' and 'Context')
  478.     * @param string $application Application to use
  479.     * @param string $data Data to use (requires 'Application')
  480.     * @param integer $timeout How long to wait for call to be answered (in ms)
  481.     * @param string $callerid Caller ID to be set on the outgoing channel
  482.     * @param string $variable Channel variable to set (VAR1=value1|VAR2=value2)
  483.     * @param string $account Account code
  484.     * @param boolean $async true fast origination
  485.     * @param string $actionid message matching variable
  486.     */
  487.     function Originate($channel,
  488.                        $exten=NULL, $context=NULL, $priority=NULL,
  489.                        $application=NULL, $data=NULL,
  490.                        $timeout=NULL, $callerid=NULL, $variable=NULL, $account=NULL, $async=NULL, $actionid=NULL)
  491.     {
  492.       $parameters = array('Channel'=>$channel);
  493.  
  494.       if($exten) $parameters['Exten'] = $exten;
  495.       if($context) $parameters['Context'] = $context;
  496.       if($priority) $parameters['Priority'] = $priority;
  497.  
  498.       if($application) $parameters['Application'] = $application;
  499.       if($data) $parameters['Data'] = $data;
  500.  
  501.       if($timeout) $parameters['Timeout'] = $timeout;
  502.       if($callerid) $parameters['CallerID'] = $callerid;
  503.       if($variable) $parameters['Variable'] = $variable;
  504.       if($account) $parameters['Account'] = $account;
  505.       if(!is_null($async)) $parameters['Async'] = ($async) ? 'true' : 'false';
  506.       if($actionid) $parameters['ActionID'] = $actionid;
  507.  
  508.       return $this->send_request('Originate'$parameters);
  509.     }    
  510.  
  511.    /**
  512.     * List parked calls
  513.     *
  514.     * @link http://www.voip-info.org/wiki-Asterisk+Manager+API+Action+ParkedCalls
  515.     * @param string $actionid message matching variable
  516.     */
  517.     function ParkedCalls($actionid=NULL)
  518.     {
  519.       if($actionid)
  520.         return $this->send_request('ParkedCalls'array('ActionID'=>$actionid));
  521.       else
  522.         return $this->send_request('ParkedCalls');
  523.     }
  524.  
  525.    /**
  526.     * Ping
  527.     *
  528.     * @link http://www.voip-info.org/wiki-Asterisk+Manager+API+Action+Ping
  529.     */
  530.     function Ping()
  531.     {
  532.       return $this->send_request('Ping');
  533.     }
  534.  
  535.    /**
  536.     * Queue Add
  537.     *
  538.     * @link http://www.voip-info.org/wiki-Asterisk+Manager+API+Action+QueueAdd
  539.     * @param string $queue
  540.     * @param string $interface
  541.     * @param integer $penalty
  542.     */
  543.     function QueueAdd($queue, $interface, $penalty=0)
  544.     {
  545.       $parameters = array('Queue'=>$queue, 'Interface'=>$interface);
  546.       if($penalty) $parameters['Penalty'] = $penalty;
  547.       return $this->send_request('QueueAdd'$parameters);
  548.     }
  549.  
  550.    /**
  551.     * Queue Remove
  552.     *
  553.     * @link http://www.voip-info.org/wiki-Asterisk+Manager+API+Action+QueueRemove
  554.     * @param string $queue
  555.     * @param string $interface
  556.     */
  557.     function QueueRemove($queue, $interface)
  558.     {
  559.       return $this->send_request('QueueRemove'array('Queue'=>$queue'Interface'=>$interface));
  560.     }
  561.  
  562.    /**
  563.     * Queues
  564.     *
  565.     * @link http://www.voip-info.org/wiki-Asterisk+Manager+API+Action+Queues
  566.     */
  567.     function Queues()
  568.     {
  569.       return $this->send_request('Queues');
  570.     }
  571.  
  572.    /**
  573.     * Queue Status
  574.     *
  575.     * @link http://www.voip-info.org/wiki-Asterisk+Manager+API+Action+QueueStatus
  576.     * @param string $actionid message matching variable
  577.     */
  578.     function QueueStatus($actionid=NULL)
  579.     {
  580.       if($actionid)
  581.         return $this->send_request('QueueStatus'array('ActionID'=>$actionid));
  582.       else
  583.         return $this->send_request('QueueStatus');
  584.     }
  585.  
  586.    /**
  587.     * Redirect
  588.     *
  589.     * @link http://www.voip-info.org/wiki-Asterisk+Manager+API+Action+Redirect
  590.     * @param string $channel
  591.     * @param string $extrachannel
  592.     * @param string $exten
  593.     * @param string $context
  594.     * @param string $priority
  595.     */
  596.     function Redirect($channel, $extrachannel, $exten, $context, $priority)
  597.     {
  598.       return $this->send_request('Redirect'array('Channel'=>$channel'ExtraChannel'=>$extrachannel'Exten'=>$exten,
  599.                                                    'Context'=>$context'Priority'=>$priority));
  600.     }
  601.  
  602.    /**
  603.     * Set the CDR UserField
  604.     *
  605.     * @link http://www.voip-info.org/wiki-Asterisk+Manager+API+Action+SetCDRUserField
  606.     * @param string $userfield
  607.     * @param string $channel
  608.     * @param string $append
  609.     */
  610.     function SetCDRUserField($userfield, $channel, $append=NULL)
  611.     {
  612.       $parameters = array('UserField'=>$userfield, 'Channel'=>$channel);
  613.       if($append) $parameters['Append'] = $append;
  614.       return $this->send_request('SetCDRUserField'$parameters);
  615.     }
  616.  
  617.    /**
  618.     * Set Channel Variable
  619.     *
  620.     * @link http://www.voip-info.org/wiki-Asterisk+Manager+API+Action+SetVar
  621.     * @param string $channel Channel to set variable for
  622.     * @param string $variable name
  623.     * @param string $value
  624.     */
  625.     function SetVar($channel, $variable, $value)
  626.     {
  627.       return $this->send_request('SetVar'array('Channel'=>$channel'Variable'=>$variable'Value'=>$value));
  628.     }
  629.  
  630.    /**
  631.     * Channel Status
  632.     *
  633.     * @link http://www.voip-info.org/wiki-Asterisk+Manager+API+Action+Status
  634.     * @param string $channel
  635.     * @param string $actionid message matching variable
  636.     */
  637.     function Status($channel, $actionid=NULL)
  638.     {
  639.       $parameters = array('Channel'=>$channel);
  640.       if($actionid) $parameters['ActionID'] = $actionid;
  641.       return $this->send_request('Status'$parameters);
  642.     }
  643.  
  644.    /**
  645.     * Stop monitoring a channel
  646.     *
  647.     * @link http://www.voip-info.org/wiki-Asterisk+Manager+API+Action+StopMonitor
  648.     * @param string $channel
  649.     */
  650.     function StopMonitor($channel)
  651.     {
  652.       return $this->send_request('StopMonitor'array('Channel'=>$channel));
  653.     }
  654.  
  655.    /**
  656.     * Dial over Zap channel while offhook
  657.     *
  658.     * @link http://www.voip-info.org/wiki-Asterisk+Manager+API+Action+ZapDialOffhook
  659.     * @param string $zapchannel
  660.     * @param string $number
  661.     */
  662.     function ZapDialOffhook($zapchannel, $number)
  663.     {
  664.       return $this->send_request('ZapDialOffhook'array('ZapChannel'=>$zapchannel'Number'=>$number));
  665.     }
  666.  
  667.    /**
  668.     * Toggle Zap channel Do Not Disturb status OFF
  669.     *
  670.     * @link http://www.voip-info.org/wiki-Asterisk+Manager+API+Action+ZapDNDoff
  671.     * @param string $zapchannel
  672.     */
  673.     function ZapDNDoff($zapchannel)
  674.     {
  675.       return $this->send_request('ZapDNDoff'array('ZapChannel'=>$zapchannel));
  676.     }
  677.  
  678.    /**
  679.     * Toggle Zap channel Do Not Disturb status ON
  680.     *
  681.     * @link http://www.voip-info.org/wiki-Asterisk+Manager+API+Action+ZapDNDon
  682.     * @param string $zapchannel
  683.     */
  684.     function ZapDNDon($zapchannel)
  685.     {
  686.       return $this->send_request('ZapDNDon'array('ZapChannel'=>$zapchannel));
  687.     }
  688.  
  689.    /**
  690.     * Hangup Zap Channel
  691.     *
  692.     * @link http://www.voip-info.org/wiki-Asterisk+Manager+API+Action+ZapHangup
  693.     * @param string $zapchannel
  694.     */
  695.     function ZapHangup($zapchannel)
  696.     {
  697.       return $this->send_request('ZapHangup'array('ZapChannel'=>$zapchannel));
  698.     }
  699.  
  700.    /**
  701.     * Transfer Zap Channel
  702.     *
  703.     * @link http://www.voip-info.org/wiki-Asterisk+Manager+API+Action+ZapTransfer
  704.     * @param string $zapchannel
  705.     */
  706.     function ZapTransfer($zapchannel)
  707.     {
  708.       return $this->send_request('ZapTransfer'array('ZapChannel'=>$zapchannel));
  709.     }
  710.  
  711.    /**
  712.     * Zap Show Channels
  713.     *
  714.     * @link http://www.voip-info.org/wiki-Asterisk+Manager+API+Action+ZapShowChannels
  715.     * @param string $actionid message matching variable
  716.     */
  717.     function ZapShowChannels($actionid=NULL)
  718.     {
  719.       if($actionid)
  720.         return $this->send_request('ZapShowChannels'array('ActionID'=>$actionid));
  721.       else
  722.         return $this->send_request('ZapShowChannels');
  723.     }
  724.  
  725.    // *********************************************************************************************************
  726.    // **                       MISC                                                                          **
  727.    // *********************************************************************************************************
  728.    /*
  729.     * Log a message
  730.     *
  731.     * @param string $message
  732.     * @param integer $level from 1 to 4
  733.     */
  734.     function log($message, $level=1)
  735.     {
  736.       if($this->pagi != false)
  737.         $this->pagi->conlog($message$level);
  738.       else
  739.         error_log(date('r'' - ' $message);
  740.     }
  741.  
  742.    /**
  743.     * Add event handler
  744.     *
  745.     * Known Events include ( http://www.voip-info.org/wiki-asterisk+manager+events )
  746.     *   Link - Fired when two voice channels are linked together and voice data exchange commences.
  747.     *   Unlink - Fired when a link between two voice channels is discontinued, for example, just before call completion.
  748.     *   Newexten -
  749.     *   Hangup -
  750.     *   Newchannel -
  751.     *   Newstate -
  752.     *   Reload - Fired when the "RELOAD" console command is executed.
  753.     *   Shutdown -
  754.     *   ExtensionStatus -
  755.     *   Rename -
  756.     *   Newcallerid -
  757.     *   Alarm -
  758.     *   AlarmClear -
  759.     *   Agentcallbacklogoff -
  760.     *   Agentcallbacklogin -
  761.     *   Agentlogoff -
  762.     *   MeetmeJoin -
  763.     *   MessageWaiting -
  764.     *   join -
  765.     *   leave -
  766.     *   AgentCalled -
  767.     *   ParkedCall - Fired after ParkedCalls
  768.     *   Cdr -
  769.     *   ParkedCallsComplete -
  770.     *   QueueParams -
  771.     *   QueueMember -
  772.     *   QueueStatusEnd -
  773.     *   Status -
  774.     *   StatusComplete -
  775.     *   ZapShowChannels - Fired after ZapShowChannels
  776.     *   ZapShowChannelsComplete -
  777.     *
  778.     * @param string $event type or * for default handler
  779.     * @param string $callback function
  780.     * @return boolean sucess
  781.     */
  782.     function add_event_handler($event, $callback)
  783.     {
  784.       $event = strtolower($event);
  785.       if(isset($this->event_handlers[$event]))
  786.       {
  787.         $this->log("$event handler is already defined, not over-writing.");
  788.         return false;
  789.       }
  790.       $this->event_handlers[$event$callback;
  791.       return true;
  792.     }
  793.  
  794.    /**
  795.     * Process event
  796.     *
  797.     * @access private
  798.     * @param array $parameters
  799.     * @return mixed result of event handler or false if no handler was found
  800.     */
  801.     function process_event($parameters)
  802.     {
  803.       $ret = false;
  804.       $e = strtolower($parameters['Event']);
  805.       $this->log("Got event.. $e");        
  806.  
  807.       $handler = '';
  808.       if(isset($this->event_handlers[$e])) $handler $this->event_handlers[$e];
  809.       elseif(isset($this->event_handlers['*'])) $handler $this->event_handlers['*'];
  810.  
  811.       if(function_exists($handler))
  812.       {
  813.         $this->log("Execute handler $handler");
  814.         $ret = $handler($e, $parameters, $this->server$this->port);
  815.       }
  816.       else
  817.         $this->log("No event handler for event '$e'");
  818.       return $ret;
  819.     }
  820.   }

Documentation generated on Thu, 30 Sep 2010 02:21:47 -0700 by phpDocumentor 1.4.2