Source for file AmiLib.php

Documentation is available at AmiLib.php

  1. <?php
  2. /* 
  3.  *  Copyright (c) 22/03/2008, Carlos Cesario <carloscesario@gmail.com>
  4.  *  All rights reserved.
  5.  * 
  6.  *  Redistribution and use in source and binary forms, with or without modification,
  7.  *  are permitted provided that the following conditions are met:
  8.  * 
  9.  *      * Redistributions of source code must retain the above copyright notice,
  10.  *        this list of conditions and the following disclaimer.
  11.  *      * Redistributions in binary form must reproduce the above copyright notice,
  12.  *        this list of conditions and the following disclaimer in the documentation
  13.  *        and/or other materials provided with the distribution.
  14.  *      * Neither the name of the DagMoller nor the names of its contributors
  15.  *        may be used to endorse or promote products derived from this software
  16.  *        without specific prior written permission.
  17.  * 
  18.  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19.  *  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20.  *  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  21.  *  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  22.  *  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  23.  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  24.  *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  25.  *  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  26.  *  OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  27.  *  OF THE POSSIBILITY OF SUCH DAMAGE.
  28.  * 
  29.  */
  30.  
  31. /*
  32.  * Class to work with AMI in Asterisk using PHP 5.x
  33.  *
  34.  * Details:
  35.  *
  36.  * You need to edit your Asterisk configuration files to enable the following
  37.  *
  38.  * In manager.conf create the manager user
  39.  *
  40.  * Asterisk-1.4.x
  41.  *
  42.  * <code>
  43.  * [admin]
  44.  * secret = test
  45.  * read = system,call,log,verbose,command,agent,user,config
  46.  * write = system,call,log,verbose,command,agent,user,config
  47.  * </code>
  48.  *
  49.  *
  50.  * Asterisk-1.6.x
  51.  *
  52.  * <code>
  53.  * [admin]
  54.  * secret = test
  55.  * read=system,call,log,verbose,agent,user,config,dtmf,reporting,cdr,dialplan
  56.  * write=system,call,agent,user,config,command,reporting,originate
  57.  * </code>
  58.  *
  59.  *
  60.  * @since 22/03/2008
  61.  * @modified 22/04/2010
  62.  * @author Carlos Alberto Cesario <carloscesario@gmail.com>
  63.  * @license http://www.gnu.org/copyleft/gpl.html GPL
  64.  * @access public
  65.  * @version 0.3.1
  66. */
  67. class AmiLib {
  68.     /**
  69.      * Socket stream
  70.      *
  71.      * @var object $_socket 
  72.      * @access public
  73.     */
  74.     private $_socket = NULL;
  75.  
  76.     /**
  77.      * Asterisk manager server address
  78.      *
  79.      * @var string $_server 
  80.      * @access private
  81.     */
  82.     private $_server;
  83.  
  84.     /**
  85.      * Asterisk manager server port
  86.      *
  87.      * @var integer $_port 
  88.      * @access private
  89.     */
  90.     private $_port;
  91.  
  92.     /**
  93.      * Asterisk manager user
  94.      *
  95.      * @var string $_username 
  96.      * @access private
  97.     */
  98.     private $_username;
  99.  
  100.     /**
  101.      * Asterisk manager secret
  102.      *
  103.      * @var string $_password 
  104.      * @access private
  105.     */
  106.     private $_password;
  107.  
  108.     /**
  109.      * Manager authentication type
  110.      * plaintext or md5
  111.      *
  112.      * @var string $_authtype 
  113.      * @access private
  114.     */
  115.     private $_authtype;
  116.  
  117.     /**
  118.      * Enable or disable debug commands
  119.      *
  120.      * Default: Disabled
  121.      *
  122.      * @var boolean $_debug 
  123.      * @access private
  124.     */
  125.     private $_debug;
  126.  
  127.     /**
  128.      * Enable or disable log commands
  129.      *
  130.      * Default: Disabled
  131.      *
  132.      * @var boolean $_log 
  133.      * @access private
  134.     */
  135.     private $_log;
  136.  
  137.     /**
  138.      * Log filename
  139.      *
  140.      * Default: './ami.log'
  141.      *
  142.      * @var string $_logfile 
  143.      * @access private
  144.     */
  145.     private $_logfile = './ami.log';
  146.  
  147.     /**
  148.      * Event Handlers
  149.      *
  150.      * @var array 
  151.      * @access private
  152.     */
  153.     private $_event_handlers;
  154.  
  155.  
  156.     /**
  157.      * AmiLib::getSocket()
  158.      *
  159.      * Return current socket or NULL value
  160.      *
  161.      * @return object 
  162.      * @access public
  163.     */
  164.     public function getSocket({
  165.         return $this->_socket;
  166.     }
  167.  
  168.     /**
  169.      * AmiLib::setSocket()
  170.      *
  171.      * Define the socket stream
  172.      *
  173.      * @return object 
  174.      * @access public
  175.     */
  176.     private function setSocket($socket{
  177.         $this->_socket = $socket;
  178.     }
  179.  
  180.     /**
  181.      * AmiLib::getServer()
  182.      *
  183.      * Return server address connection  (Asterisk Server)
  184.      *
  185.      * @return string 
  186.      * @access public
  187.     */
  188.     public function getServer({
  189.         return $this->_server;
  190.     }
  191.  
  192.     /**
  193.      * AmiLib::setServer()
  194.      *
  195.      * Define server address connection  (Asterisk Server)
  196.      *
  197.      * @param string $server 
  198.      * @access private
  199.     */
  200.     private function setServer($server{
  201.         $this->_server = $server;
  202.     }
  203.  
  204.     /**
  205.      * AmiLib::getPort()
  206.      *
  207.      * Return current port connection
  208.      *
  209.      * @return integer 
  210.      * @access public
  211.     */
  212.     public function getPort({
  213.         return $this->_port;
  214.     }
  215.  
  216.     /**
  217.      * AmiLib::setPort()
  218.      *
  219.      * Define port connection
  220.      *
  221.      * @param integer $port 
  222.      * @access private
  223.     */
  224.     private function setPort($port{
  225.         $this->_port = $port;
  226.     }
  227.  
  228.     /**
  229.      * AmiLib::getUsername()
  230.      *
  231.      * Return current username manager
  232.      *
  233.      * @return string 
  234.      * @access public
  235.     */
  236.     public function getUsername({
  237.         return $this->_username;
  238.     }
  239.  
  240.     /**
  241.      * AmiLib::setUsername()
  242.      *
  243.      * Define username manager
  244.      *
  245.      * @param string $username 
  246.      * @access private
  247.     */
  248.     private function setUsername($username{
  249.         $this->_username = $username;
  250.     }
  251.  
  252.     /**
  253.      * AmiLib::getPassword()
  254.      *
  255.      * Return current password manager
  256.      *
  257.      * @return string 
  258.      * @access public
  259.     */
  260.     public function getPassword({
  261.         return $this->_password;
  262.     }
  263.  
  264.     /**
  265.      * AmiLib::setPassword
  266.      *
  267.      * Define password manager
  268.      *
  269.      * @param string $password 
  270.      * @access private
  271.     */
  272.     private function setPassword($password{
  273.         $this->_password = $password;
  274.     }
  275.  
  276.     /**
  277.      * AmiLib::getAuthtype()
  278.      *
  279.      * Return current authentication type
  280.      *
  281.      * @return string 
  282.      * @access public
  283.     */
  284.     public function getAuthtype({
  285.         return $this->_authtype;
  286.     }
  287.  
  288.     /**
  289.      * AmiLib::setAuthtype()
  290.      *
  291.      * Define authentication type
  292.      *
  293.      * @param string $authtype 
  294.      * @access private
  295.     */
  296.     private function setAuthtype($authtype{
  297.         $this->_authtype = $authtype;
  298.     }
  299.  
  300.     /**
  301.      * AmiLib::getDebug()
  302.      *
  303.      * Return current debug value
  304.      *
  305.      * @return bool 
  306.      * @access public
  307.     */
  308.     public function getDebug({
  309.         return $this->_debug;
  310.     }
  311.  
  312.     /**
  313.      * AmiLib::setDebug()
  314.      *
  315.      * Define debug value
  316.      *
  317.      * @param bool 
  318.      * @access private
  319.     */
  320.     private function setDebug($debug{
  321.         $this->_debug = $debug;
  322.     }
  323.  
  324.     /**
  325.      * AmiLib::getLog()
  326.      *
  327.      * Return current log value
  328.      *
  329.      * @return bool 
  330.      * @access public
  331.     */
  332.     public function getLog({
  333.         return $this->_log;
  334.     }
  335.  
  336.     /**
  337.      * AmiLib::setLog()
  338.      *
  339.      * Define log value
  340.      *
  341.      * @param bool 
  342.      * @access private
  343.     */
  344.     private function setLog($log{
  345.         $this->_log = $log;
  346.     }
  347.  
  348.     /**
  349.      * AmiLib::getLogFile()
  350.      *
  351.      * Return current log filename value
  352.      *
  353.      * @return string 
  354.      * @access public
  355.     */
  356.     public function getLogFile({
  357.         return $this->_logfile;
  358.     }
  359.  
  360.     /**
  361.      * AmiLib::setLogFile()
  362.      *
  363.      * Define log filename value
  364.      *
  365.      * @param string 
  366.      * @access private
  367.     */
  368.     private function setLogFile($logfile{
  369.         $this->_logfile = $logfile;
  370.     }
  371.  
  372.     /**
  373.      * AmiLib::getEventHandlers()
  374.      * Return event handlers
  375.      *
  376.      * @return array 
  377.      * @access public
  378.     */
  379.     public function getEventHandlers($event{
  380.         echo "A variaevel event vale $event\n";
  381.         return $this->_event_handlers[$event];
  382.     }
  383.  
  384.     /**
  385.      * AmiLib::setEventHandlers()
  386.      *
  387.      * Define event handlers value
  388.      *
  389.      * @param array 
  390.      * @access private
  391.     */
  392.     private function setEventHandlers($eventhandler$callback{
  393.         $this->_event_handlers[$eventhandler$callback;
  394.     }
  395.  
  396.  
  397.     /**
  398.      * AmiLib::__construct()
  399.      *
  400.      * Default constructor of AmiLib class
  401.      *
  402.      * @param array $config Array of the parameters used to connect to the server
  403.      *
  404.      *  <code>
  405.      *       array(
  406.      *         'server'         => '127.0.0.1'        // The server to connect to
  407.      *         'port'             => '5038',            // Port of manager API
  408.      *         'username'         => 'admin',            // Asterisk manager username
  409.      *         'password'         => 'password',        // Asterisk manager password
  410.      *         'authtype'         => 'plaintext'        // Valid plaintext or md5
  411.      *         'debug'            => 'true or false'  // Enable or not debug
  412.      *         'log'            => 'true or false'    // Enable or nor logging
  413.      *         'logfile'        => 'filename'        // Log filename
  414.      *       );
  415.      *  </code>
  416.      *
  417.      * @access public
  418.     */
  419.     public function __construct($config array()) {
  420.  
  421.         // Check if phpversion is 5 or higher
  422.         if (version_compare(PHP_VERSION"5""lt")) {
  423.             throw new Exception('Requires PHP 5 or higher - 'PHP_VERSION);
  424.         }
  425.  
  426.         // Verify if function fsockopen exists
  427.         if (!function_exists('fsockopen')) {
  428.             throw new Exception('Php Socket module is unavailable');
  429.         }
  430.  
  431.         // Check the config array variables
  432.         if (!($config)) {
  433.             throw new Exception('Config array params is missing');
  434.         }
  435.  
  436.  
  437.         // Check the all config array valid variables
  438.         if (!isset($config['server'])) {
  439.             throw new Exception("Config +server+ is missing");
  440.         }
  441.         else {
  442.             $this->setServer($config['server']);
  443.         }
  444.  
  445.         if (!isset($config['port'])) {
  446.             throw new Exception("Config +port+ is missing");
  447.         }
  448.         else {
  449.             $this->setPort($config['port']);
  450.         }
  451.  
  452.         if (!isset($config['username'])) {
  453.             throw new Exception("Config +username+ is missing");
  454.         }
  455.         else {
  456.             $this->setUsername($config['username']);
  457.         }
  458.  
  459.         if (!isset($config['password'])) {
  460.             throw new Exception("Config +password+ is missing");
  461.         }
  462.         else {
  463.             $this->setPassword($config['password']);
  464.         }
  465.  
  466.         if (!isset($config['authtype']))  {
  467.             throw new Exception("Config +authtype+ is missing");
  468.         }
  469.         elseif ((strtolower($config['authtype']!= "plaintext"&& (strtolower($config['authtype']!= "md5"))  {
  470.             throw new Exception("Config +authtype+ is invalid. Use plaintext or md5");
  471.         }
  472.         else {
  473.             $this->setAuthtype($config['authtype']);
  474.         }
  475.  
  476.         if (!isset($config['debug']))  {
  477.             $this->setDebug(false);
  478.         }
  479.         elseif (!is_bool($config['debug'])) {
  480.             throw new Exception("Config +debug+ is invalid. Use true or false");
  481.         }
  482.         else{
  483.             $this->setDebug($config['debug']);
  484.         }
  485.  
  486.         if ((!isset($config['log'])) || ($config['log']== false)  {
  487.             $this->setLog(false);
  488.         }
  489.         elseif (!is_bool($config['log'])) {
  490.             throw new Exception("Config +log+ is invalid. Use true or false");
  491.         }
  492.         elseif ( ($config['log'== true&& (!isset($config['logfile'])) ) {
  493.             $this->setLog($config['log']);
  494.             $this->setLogFile($this->getLogFile());
  495.         }
  496.  
  497.         else {
  498.             $this->setLog($config['log']);
  499.             $this->setLogFile($config['logfile']);
  500.         }
  501.     }
  502.  
  503.  
  504.     /**
  505.      * AmiLib::debug()
  506.      *
  507.      * Debug info
  508.      *
  509.      * @param string or array message
  510.      * @return boolean 
  511.     */
  512.     function debug($info,$message{
  513.         // If the debug variable is true... do...
  514.         if ($this->getDebug(== true{
  515.             echo "===DEBUG: $info===<br>\n";
  516.             if (isset($message)) {
  517.                 // Verify if the variable $message is a array or no
  518.                 if (is_array($message)) {
  519.                     print_r($message);
  520.                 }
  521.                 else {
  522.                     echo "$message";
  523.                 }
  524.             }
  525.         }
  526.     }
  527.  
  528.     /**
  529.      * AmiLib::addLog()
  530.      *
  531.      * Log file
  532.      *
  533.      * @param string $level DEBUG|INFO|WARN|ERROR|FATAL - Default is INFO
  534.      * @param string $message Information message
  535.      * @return boolean 
  536.     */
  537.     function addLog($level$message{
  538.         // If the log variable is true... do...
  539.         if ($this->getLog(== true{
  540.             $filename $this->getLogFile();
  541.  
  542.             if (isset($level)) {
  543.                 switch (strtolower($level)) {
  544.                     case "debug":
  545.                         $level 'debug';
  546.                         break;
  547.  
  548.                     case "info":
  549.                         $level 'info';
  550.                         break;
  551.  
  552.                     case "warn":
  553.                         $level 'warn';
  554.                         break;
  555.  
  556.                     case "error":
  557.                         $level 'error';
  558.                         break;
  559.  
  560.                     case "fatal":
  561.                         $level 'fatal';
  562.                         break;
  563.  
  564.                     default:
  565.                         $level 'info';
  566.                 }
  567.             }
  568.             else {
  569.                 $level 'info';
  570.             }
  571.  
  572.             $message ltrim($message);
  573.             $date date("d-m-Y H:i:s");
  574.             $text "======BEGIN\n $date $level $message\n======END\n";
  575.             $handle @fopen($filename"a");
  576.  
  577.             // If the opening of the file is successfully
  578.             if ($handle != null{
  579.                 if (fwrite($handle$text== false{
  580.                     fclose($handle);
  581.                     throw new Exception("Cannot write to file $filename");
  582.                 }
  583.                 fclose($handle);
  584.             else {
  585.                 throw new Exception("Cannot open file $filename");
  586.             }
  587.             return true;
  588.         }
  589.         return false;
  590.     }
  591.  
  592.  
  593.  
  594.     /**
  595.      * AmiLib::getResponse()
  596.      *
  597.      * Wait for command response
  598.      *
  599.      * Wwill return the response
  600.      *
  601.      * @param none 
  602.      * @return array of parameters, empty on timeout
  603.      * @access private
  604.     */
  605.     private function getResponse({
  606.  
  607.         $str_buffer null;
  608.         $str_key null;
  609.         $str_val null;
  610.  
  611.         $arr_ret array();
  612.         $int_cont 0;
  613.  
  614.         while (($str_buffer fgets($this->getSocket()))) {
  615.             /**
  616.              * If the line contain the follows strings
  617.              * Response: or Message: or Privilege:,
  618.              * Then this line will be splited by separator : (two dots)
  619.             */
  620.             if ((preg_match ('/Response:|Message:|Privilege:/i'$str_buffer))) {
  621.  
  622.                 list($str_key$str_valexplode(': '$str_buffer);
  623.                 /**
  624.                  * Here is create a array with the
  625.                  * response and value
  626.                  * eg.
  627.                  * $Var['Response'] = Sucess
  628.                 */
  629.                 $str_key trim($str_key);
  630.                 $str_val trim($str_val);
  631.  
  632.                 if ($str_key{
  633.                     $arr_ret[$str_key$str_val;
  634.                 }
  635.             }
  636.             else {
  637.                 /**
  638.                  * If the line is blank, then
  639.                  * this line receive null value
  640.                  * and don't will be part of array
  641.                 */
  642.                 if ((preg_match('/^\s+/'$str_buffer))) {
  643.                     $int_cont++;
  644.                     $str_buffer null;
  645.                 }
  646.                 else {
  647.  
  648.                     /**
  649.                      * If match : (two dots) in line,
  650.                      * Again I split the line by separator :
  651.                      * Then I create other array with his value
  652.                     */
  653.                     if (preg_match ('/: /'$str_buffer)) {
  654.                         list($str_key$str_valexplode(': '$str_buffer);
  655.  
  656.                         $str_key trim($str_key);
  657.                         $str_val trim($str_val);
  658.  
  659.                         /**
  660.                          * If the line is not null and the array don't have nothing and
  661.                          * the key don't have nothing value, is create a arraye
  662.                          * with key and value
  663.                          * eg. $arr_ret['data'][0] = [code][010];
  664.                         */
  665.                         if ((!is_null($str_buffer)) && (count($arr_ret["data"][$int_cont]== 0&& ($str_key)) {
  666.                             $arr_ret["data"][$int_contarray($str_key => $str_val);
  667.                         }
  668.                         else {
  669.                             if ($str_key{
  670.  
  671.                                 /**
  672.                                  * Else only is added this array with other key and value
  673.                                 */
  674.                                 $arr_ret["data"][$int_cont][$str_key$str_val;
  675.                             }
  676.                         }
  677.                     }
  678.  
  679.                     /**
  680.                      * Else if, I create a array with all line
  681.                     */
  682.                     else {
  683.                         if ((!is_null($str_buffer)) && (count($arr_ret["data"][$int_cont]== 0)) {
  684.                             $arr_ret["data"][$int_contarray(trim($str_buffer));
  685.                         }
  686.                         else {
  687.                             array_push($arr_ret["data"][$int_cont]trim($str_buffer));
  688.                         }
  689.                     }
  690.                 }
  691.             }
  692.         }
  693.         unset($int_cont);
  694.         return $arr_ret ;
  695.     }
  696.  
  697.     /**
  698.      * AmiLib::sendRequest()
  699.      *
  700.      * Send a request to manager
  701.      *
  702.      * @param string $action Manager Action
  703.      * @param array $parameters AMI Commands
  704.      * @return array of parameters
  705.      * @access public
  706.     */
  707.     public function sendRequest($action$parameters array()) {
  708.         if ($this->getSocket()) {
  709.             $request "Action: $action\r\n";
  710.             foreach($parameters as $var => $val{
  711.                 $request.= "$var$val\r\n";
  712.         }
  713.  
  714.         $request.= "\r\n";
  715.         fwrite($this->getSocket()$request);
  716.         $this->debug("sendRequest requests",$request);
  717.         $this->addLog("info","Executing action $action");
  718.         $requestResult $this->getResponse();
  719.         return $requestResult;
  720.         else {
  721.             $this->addLog("error","Asterisk manager socket is not active");
  722.             throw new Exception('Asterisk manager socket is not active');
  723.         }
  724.     }
  725.  
  726.     /**
  727.      * AmiLib::connect()
  728.      *
  729.      * Connect to Asterisk Manager
  730.      *
  731.      * @return boolean true on success
  732.      * @access public
  733.     */
  734.     public function connect($events 'off'{
  735.         // connect the socket
  736.         $errno $errstr NULL;
  737.         $this->setSocket(@fsockopen($this->getServer()$this->getPort()$errno$errstr));
  738.  
  739.         stream_set_timeout($this->getSocket()1);
  740.  
  741.         if (!$this->getSocket()) {
  742.             $this->addLog("error","Unable to connect to manager {$this->getServer()}:{$this->getPort()} ($errno): $errstr");
  743.             throw new Exception("Unable to connect to manager {$this->getServer()}:{$this->getPort()} ($errno): $errstr");
  744.         }
  745.         // read the header
  746.         $strData fgets($this->getSocket());
  747.  
  748.         if ($strData == false{
  749.             // possible  problem.
  750.             $this->addLog('error','Asterisk Manager header not received.');
  751.             throw new Exception('Asterisk Manager header not received.');
  752.         else {
  753.             // note: nothing until someone looks to see why it mangles the logging
  754.             // $this->addLog('info',$strData);
  755.         }
  756.         switch (strtolower($this->getAuthtype())) {
  757.  
  758.             // Authentication mode using plaintext
  759.             case 'plaintext':
  760.                 // Send login
  761.                 $result $this->sendRequest('login'array(
  762.                     'Username' => $this->getUsername(),
  763.                     'Secret' => $this->getPassword(),
  764.                     'Events' => $events
  765.                 ));
  766.                 if ($result['Response'!= 'Success'{
  767.                     $this->addLog("error","Failed Login {$this->getServer()}:{$this->getPort()}.");
  768.                     throw new Exception("
  769.                             Failed to login {$this->getServer()}:{$this->getPort()} U:{$this->getUsername()} P:{$this->getPassword()}
  770.                     ");
  771.                 }
  772.                 $this->addLog("info","Connected to {$this->getServer()}:{$this->getPort()}.");
  773.                 return true;
  774.  
  775.                 // Authentication mode using md5
  776.                 case 'md5':
  777.                     // Get md5 key
  778.                     $result $this->sendRquest('Challenge'array(
  779.                         'AuthType' => 'md5',
  780.                         'Events' => $events
  781.                     ));
  782.                     if ($result['Response'== 'Success'{
  783.                         $challenge $result['Challenge'];
  784.  
  785.                         // Md5/password hash key
  786.                         $md5_key  md5($challenge $this->getPassword());
  787.  
  788.                         // Send login
  789.                         $result $this->sendRquest('login'array(
  790.                             'AuthType' => $this->getAuthtype(),
  791.                             'Username' => $this->getUsername(),
  792.                             'Key' => $md5_key,
  793.                             'Events' => $events
  794.                         ));
  795.  
  796.                         if ($result['Response'!= 'Success'{
  797.                             $this->addLog("error","Failed Login {$this->host}:{$this->port}");
  798.                             throw new Exception("
  799.                                 Failed to login {$this->getServer()}:{$this->getPort()} U:{$this->getUsername()} P:{$this->getPassword()}
  800.                             ");
  801.                         }
  802.                     }
  803.                     $this->addLog("info","Connected to {$this->getServer()}:{$this->getPort()}.");
  804.                     return true;
  805.         }
  806.     }
  807.  
  808.     /**
  809.      * AmiLib::disconnect()
  810.      *
  811.      * Close the socket connection
  812.      *
  813.      * @return none
  814.      * @access public
  815.     */
  816.     public function disconnect() {
  817.         if ($this->getSocket()) {
  818.             $this->logoff();
  819.             fclose($this->getSocket());
  820.             $this->addLog("info","Disconnected from {$this->getServer()}:{$this->getPort()}.");
  821.         }
  822.         $this->setServer(NULL);
  823.         $this->setPort(NULL);
  824.     }
  825.  
  826.     /**
  827.      * AmiLib::logoff()
  828.      *
  829.      * Logout of the current manager session attached to $this->_socket
  830.      *
  831.      * @return bool
  832.      * @access private
  833.     */
  834.     private function logoff(){
  835.         return $this->sendRequest('Logoff');
  836.     }
  837.  
  838.     /**
  839.      * AmiLib::is_connected()
  840.      *
  841.      * Check if the socket is connected
  842.      *
  843.      * @return bool
  844.      * @access public
  845.     */
  846.     public function is_connected(){
  847.         return (bool)$this->getSocket();
  848.     }
  849.  
  850.     /**
  851.      * AmiLib::commandExecute()
  852.      *
  853.      * Execute Asterisk CLI Command
  854.      *
  855.      * @param string $command
  856.      * @param string $actionid message matching variable
  857.      * @return array data
  858.      * @access public
  859.     */
  860.     public function commandExecute($command, $actionid=NULL){
  861.         $this->debug("commandExecute","Executing command $command");
  862.         $this->addLog("info","Executing command $command");
  863.         $parameters = array('Command'=>$command);
  864.         if($actionid) {
  865.             $parameters['ActionID'] = $actionid;
  866.         }
  867.         return $this->sendRequest('Command'$parameters);
  868.     }
  869. }

Documentation generated on Thu, 22 Apr 2010 13:56:00 -0300 by phpDocumentor 1.4.3