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

Documentation generated on Wed, 16 Nov 2005 12:49:00 -0700 by phpDocumentor 1.3.0RC3