PageRenderTime 28ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/esl-1.0/libraries/EslManager.php

https://github.com/robertleeplummerjr/bluebox
PHP | 259 lines | 167 code | 43 blank | 49 comment | 17 complexity | 47e1ca20ac29bcd2a9730b955291ce1f MD5 | raw file
  1. <?php defined('SYSPATH') or die('No direct access allowed.');
  2. /**
  3. * EslManager.php - This wraps the two types of ESLconnection and provides additional
  4. * convience functions
  5. *
  6. * @author K Anderson
  7. * @license LGPL
  8. * @package Esl
  9. */
  10. class EslManager
  11. {
  12. private $esl = NULL;
  13. private $extension = FALSE;
  14. static private $instance;
  15. /**
  16. * Initialize the ESL connection. This must be called first
  17. * @access public
  18. * @static
  19. * @return bool
  20. */
  21. public function __construct()
  22. {
  23. $host = Kohana::config('freeswitch.ESLHost');
  24. $port = Kohana::config('freeswitch.ESLPort');
  25. $password = Kohana::config('freeswitch.ESLAuth');
  26. if (!extension_loaded("ESL")) // Is the PHP ESL extension loaded? If not, failback to socket
  27. {
  28. $this->esl = new ESLconnection($host, $port, $password); // socket connection
  29. $this->extension = FALSE;
  30. } else {
  31. include_once(MODPATH .'esl-1.0/assets/ESL.php');
  32. $this->esl = new ESLconnection($host, $port, $password); // FreeSWITCH ESL Swigged class
  33. $this->extension = TRUE;
  34. }
  35. if(!$this->esl->connected())
  36. {
  37. message::set('Failed to connect to ESL. Make sure FreeSWITCH is running...', 'alert');
  38. return FALSE;
  39. }
  40. return TRUE;
  41. }
  42. public static function getInstance()
  43. {
  44. if (!self::$instance)
  45. {
  46. return self::$instance = new EslManager();
  47. }
  48. return self::$instance;
  49. }
  50. public static function eventReloadXML()
  51. {
  52. self::getInstance()->reloadxml();
  53. }
  54. public static function eventReloadACL()
  55. {
  56. self::getInstance()->reloadacl();
  57. }
  58. public static function eventReloadXMLCDR()
  59. {
  60. self::getInstance()->sendRecv('api reload mod_xml_cdr');
  61. }
  62. public static function eventReloadSofia()
  63. {
  64. self::getInstance()->reload('mod_sofia');
  65. }
  66. /*
  67. * Clean up connection when script is done executing.
  68. * If connected, try to disconnect.
  69. */
  70. public function __destruct()
  71. {
  72. if ($this->isConnected()) {
  73. return $this->esl->disconnect();
  74. }
  75. }
  76. /**
  77. * This lets us determine how we are accessing ESL
  78. */
  79. public function isExtension() {
  80. return $this->extension;
  81. }
  82. /**
  83. * Returns the connection status of the current connection
  84. */
  85. public function isConnected()
  86. {
  87. if(!is_object($this->esl)) {
  88. return FALSE;
  89. } else {
  90. return $this->esl->connected();
  91. }
  92. }
  93. /**
  94. * gets the raw ESLconnection
  95. */
  96. public function getESL() {
  97. return $this->esl;
  98. }
  99. /**
  100. * check if a command execution response was successfull
  101. */
  102. public function isSuccessfull($event = NULL) {
  103. if ($event instanceof ESLevent) {
  104. $reply = $event->getHeader('Reply-Text');
  105. if (strstr($reply, '+OK')) {
  106. return TRUE;
  107. }
  108. }
  109. return FALSE;
  110. }
  111. /**
  112. * This will return a string froma event with the most appropriate
  113. * meaning
  114. */
  115. public function getResponse($event = NULL) {
  116. if ($event instanceof ESLevent) {
  117. $body = $event->getBody();
  118. if (!empty($body)) return $body;
  119. $reply = $event->getHeader('Reply-Text');
  120. if (!empty($reply)) return $reply;
  121. return $event->serialize();
  122. }
  123. if (!is_string($event)) {
  124. return 'Command execution failed.';
  125. } else {
  126. return $event;
  127. }
  128. }
  129. /**
  130. * Convience wrapper for nat operations
  131. */
  132. public function nat($operation)
  133. {
  134. if (!$this->isConnected()) return FALSE;
  135. $esl = $this->esl;
  136. switch($operation)
  137. {
  138. case 'status':
  139. return $esl->api('nat_map',' status');
  140. break;
  141. case 'reinit':
  142. return $esl->api('nat_map',' reinit');
  143. break;
  144. case 'republish':
  145. return $esl->api('nat_map',' republish');
  146. break;
  147. default:
  148. return "NAT operation $operation not valid";
  149. }
  150. }
  151. /**
  152. * Make an outoging call by connecting a remote device to a local one
  153. */
  154. public function originateLocal($external, $internal, $from = '')
  155. {
  156. //user->CallerID now
  157. $this->sendBgAPI("originate",
  158. sprintf("{origination_caller_id_number=1231231234,ignore_early_media=true,originate_timeout=30}sofia/external/%s@trunk1.bluebox.com %d", $external, $internal));
  159. }
  160. /**
  161. * Make an outoging call by connecting a remote device to a remote one
  162. */
  163. public function originateRemote($external, $internal, $from = '')
  164. {
  165. //user->CallerID now
  166. $this->sendBgAPI("originate",
  167. sprintf("{origination_caller_id_number=1231231234,ignore_early_media=true,originate_timeout=30}sofia/external/%s@trunk1.bluebox.com %d", $external, $internal));
  168. }
  169. /**
  170. * This lets the eslManager wrap all the ESLconnection methods
  171. */
  172. public function __call($name, $arguments) {
  173. if (!$this->isConnected()) return FALSE;
  174. $esl = $this->esl;
  175. // These are some convience wrappers to support common commands
  176. switch(strtolower($name)) {
  177. case 'version':
  178. return $esl->api('version');
  179. break;
  180. case 'status':
  181. return $esl->api('status');
  182. break;
  183. case 'reloadacl':
  184. return $esl->api('reloadacl');
  185. break;
  186. case 'reloadxml':
  187. return $esl->api('reloadxml');
  188. break;
  189. case 'reload':
  190. array_unshift($arguments, 'bgapi', 'reload', '-f');
  191. return $esl->sendRecv(implode(' ', $arguments));
  192. break;
  193. case 'sofia':
  194. array_unshift($arguments, 'sofia');
  195. return call_user_func(array($esl, 'api'), implode(' ', $arguments));
  196. break;
  197. case 'show':
  198. array_unshift($arguments, 'show');
  199. return call_user_func(array($esl, 'api'), implode(' ', $arguments));
  200. break;
  201. case 'channels':
  202. return $esl->api('show', 'channels');
  203. break;
  204. case 'calls':
  205. return $esl->api('show', 'calls');
  206. break;
  207. default:
  208. if (!method_exists($this->esl, $name)) return FALSE;
  209. return call_user_func(array($this->esl, $name), implode(' ', $arguments));
  210. }
  211. }
  212. public function __clone()
  213. {
  214. trigger_error('Clone is not allowed.', E_USER_ERROR);
  215. }
  216. }