PageRenderTime 40ms CodeModel.GetById 9ms RepoModel.GetById 1ms app.codeStats 0ms

/modules/switchboard-1.0/controllers/switchboard.php

https://github.com/robertleeplummerjr/bluebox
PHP | 395 lines | 273 code | 66 blank | 56 comment | 51 complexity | 114c6fd9c2abffc30dde053d6c0fc15a MD5 | raw file
  1. <?php defined('SYSPATH') or die('No direct access allowed.');
  2. /*
  3. * Bluebox Modular Telephony Software Library / Application
  4. *
  5. * Module:
  6. *
  7. * The contents of this file are subject to the Mozilla Public License
  8. * Version 1.1 (the "License"); you may not use this file except in
  9. * compliance with the License. You may obtain a copy of the License at
  10. * http://www.mozilla.org/MPL/
  11. *
  12. * Software distributed under the License is distributed on an "AS IS"
  13. * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
  14. * License for the specific language governing rights and limitations
  15. * under the License.
  16. *
  17. * The Initial Developer of the Original Code is Michael Phillips <michael.j.phillips@gmail.com>.
  18. *
  19. * Portions created by the Initial Developer are Copyright (C)
  20. * the Initial Developer. All Rights Reserved.
  21. *
  22. * Contributor(s):
  23. *
  24. *
  25. */
  26. /**
  27. * esl.php - Sofia Viewer Module
  28. *
  29. * @author Michael Phillips <michael.j.phillips@gmail.com>
  30. * @author Jon Blanton <jon@2600hz.com>
  31. * @author Rockwood Cataldo <rocco@2600hz.com>
  32. * @license MPL
  33. * @package Esl
  34. */
  35. class switchboard_Controller extends Bluebox_Controller
  36. {
  37. public function index()
  38. {
  39. stylesheet::add('switchboard', 50);
  40. javascript::add('pubsub.js', 50);
  41. $this->view->sofia_status = '';
  42. $sipInterface = Doctrine::getTable('SipInterface')->findAll();
  43. foreach($sipInterface as $interface)
  44. {
  45. $this->view->sofia_status .= form::button(array(
  46. 'id' => 'sofia_profile',
  47. 'class' => 'eslEvent',
  48. 'value' => 'SIP Interface ' . $interface->sipinterface_id .' Status',
  49. 'param' => 'sipinterface_' . $interface->sipinterface_id
  50. ));
  51. }
  52. $this->view->trunk_status = '';
  53. $trunks = Doctrine::getTable('Trunk')->findAll();
  54. foreach($trunks as $trunk)
  55. {
  56. $this->view->trunk_status .= form::button(array(
  57. 'id' => 'gateway_profile',
  58. 'class' => 'eslEvent',
  59. 'value' => 'Trunk ' . $trunk->trunk_id .' Status',
  60. 'param' => 'trunk_' . $trunk->trunk_id
  61. ));
  62. }
  63. //CLEAR THE ESL SESSION
  64. $_SESSION['esl'] = array();
  65. }
  66. public function eslresponse() {
  67. $this->auto_render = FALSE;
  68. $event = $_POST['event'];
  69. $eslManager = new EslManager();
  70. switch ($event) {
  71. case 'switchboard/manual_entry':
  72. if($eslManager->isConnected()) {
  73. $result = $eslManager->sendRecv("api " . $_POST['param']);
  74. $response = $eslManager->getResponse($result);
  75. }
  76. // Can't connect to Freeswitch
  77. else {
  78. $response = "Cannot connect to Freeswitch";
  79. }
  80. break;
  81. }
  82. echo $response;
  83. flush();
  84. die();
  85. }
  86. public function fluxresponse() {
  87. //Turn off the view
  88. $this->auto_render = FALSE;
  89. //Set the timeout for the http request (in seconds)
  90. //Until flux handles firefox correctly, set this to 5
  91. $TIMEOUT = 5;
  92. $response = array();
  93. $eslManager = new EslManager();
  94. $subscribers = $_POST['subscribers'];
  95. $starttime = time();
  96. $exectime = 0;
  97. while(sizeof($response) == 0 && $exectime < $TIMEOUT) {
  98. foreach ($subscribers as $subscriber) {
  99. switch($subscriber) {
  100. case "switchboard/numactivecalls":
  101. if($eslManager->isConnected()) {
  102. $result = $eslManager->calls();
  103. $text = $eslManager->getResponse($result);
  104. preg_match("/[0-9]+(?=\stotal\.)/", $text, $output);
  105. $event = array("name" => $subscriber, "data" => array($output[0]));
  106. }
  107. // Can't connect to Freeswitch
  108. else {
  109. $event = array("name" => $subscriber, "data" => array("N\\A"));
  110. }
  111. break;
  112. case "switchboard/numactivemodules":
  113. if($eslManager->isConnected()) {
  114. $result = $eslManager->show('modules');
  115. $text = $eslManager->getResponse($result);
  116. preg_match("/[0-9]+(?=\stotal\.)/", $text, $output);
  117. $event = array("name" => $subscriber, "data" => array($output[0]));
  118. }
  119. // Can't connect to Freeswitch
  120. else {
  121. $event = array("name" => $subscriber, "data" => array("N\\A"));
  122. }
  123. break;
  124. case "switchboard/numactivechannels":
  125. if($eslManager->isConnected()) {
  126. $result = $eslManager->channels();
  127. $text = $eslManager->getResponse($result);
  128. preg_match("/[0-9]+(?=\stotal\.)/", $text, $output);
  129. $event = array("name" => $subscriber, "data" => array($output[0]));
  130. }
  131. // Can't connect to Freeswitch
  132. else {
  133. $event = array("name" => $subscriber, "data" => array("N\\A"));
  134. }
  135. break;
  136. case "switchboard/channels":
  137. if($eslManager->isConnected()) {
  138. $result = $eslManager->channels();
  139. $text = $eslManager->getResponse($result);
  140. $event = array("name" => $subscriber, "data" => array($text));
  141. }
  142. // Can't connect to Freeswitch
  143. else {
  144. $event = array("name" => $subscriber, "data" => array("N\\A"));
  145. }
  146. break;
  147. case "switchboard/numactivecodecs":
  148. if($eslManager->isConnected()) {
  149. $result = $eslManager->show('codecs');
  150. $text = $eslManager->getResponse($result);
  151. preg_match("/[0-9]+(?=\stotal\.)/", $text, $output);
  152. $event = array("name" => $subscriber, "data" => array($output[0]));
  153. }
  154. // Can't connect to Freeswitch
  155. else {
  156. $event = array("name" => $subscriber, "data" => array("N\\A"));
  157. }
  158. break;
  159. case "switchboard/modules":
  160. if($eslManager->isConnected()){
  161. $result = $eslManager->show('modules');
  162. $text = $eslManager->getResponse($result);
  163. preg_match_all("/(?<=\,)mod_[A-Za-z_0-9]+(?=,)/", $text, $output, PREG_PATTERN_ORDER);
  164. if(isset($output)) {
  165. // A dirty hack to eliminate duplicates
  166. $matches = array();
  167. foreach($output[0] as $value) {
  168. $matches[$value] = 1337;
  169. }
  170. $text = "";
  171. foreach($matches as $key => $value) {
  172. $text .= $key . ',';
  173. }
  174. }
  175. else {
  176. $text = "No modules found... I think something is broken.";
  177. }
  178. $event = array("name" => $subscriber, "data" => array($text));
  179. }
  180. else{
  181. $event=array("name" => $subscriber, "data" => array("Freeswitch not loaded.."));
  182. }
  183. break;
  184. case "switchboard/sipinterfaces":
  185. if($eslManager->isConnected()) {
  186. $result = $eslManager->sofia('status');
  187. $text = $eslManager->getResponse($result);
  188. $event = array("name" => $subscriber, "data" => array($text));
  189. }
  190. // Can't connect to Freeswitch
  191. else {
  192. $event = array("name" => $subscriber, "data" => array("N\\A"));
  193. }
  194. break;
  195. case "switchboard/calls":
  196. if($eslManager->isConnected()) {
  197. $result = $eslManager->calls();
  198. $text = $eslManager->getResponse($result);
  199. $event = array("name" => $subscriber, "data" => array($text));
  200. }
  201. // Can't connect to Freeswitch
  202. else {
  203. $event = array("name" => $subscriber, "data" => array("N\\A"));
  204. }
  205. break;
  206. case "switchboard/activecalls":
  207. if($eslManager->isConnected()) {
  208. $result = $eslManager->calls();
  209. $text = $eslManager->getResponse($result);
  210. $output = explode("\n", $text);
  211. $event = array("name" => $subscriber, "data" => array($text));
  212. }
  213. // Can't connect to Freeswitch
  214. else {
  215. $event = array("name" => $subscriber, "data" => array("N\\A"));
  216. }
  217. break;
  218. case "switchboard/uptime":
  219. if($eslManager->isConnected()) {
  220. $result = $eslManager->status();
  221. $text = $eslManager->getResponse($result);
  222. preg_match("/[0-9]+(?=\syear)/", $text, $output);
  223. $years = $output[0];
  224. preg_match("/[0-9]+(?=\sday)/", $text, $output);
  225. $days = $output[0];
  226. preg_match("/[0-9]+(?=\shour)/", $text, $output);
  227. $hours = $output[0];
  228. preg_match("/[0-9]+(?=\sminute)/", $text, $output);
  229. $mins = $output[0];
  230. preg_match("/[0-9]+(?=\ssecond)/", $text, $output);
  231. $secs = $output[0];
  232. $text = "";
  233. if($years > 0) {
  234. $text .= $years . " year";
  235. //Plurar check
  236. if($years > 1) {
  237. $text .= "s";
  238. }
  239. //check if there will be another field
  240. if($days + $hours + $mins > 0) {
  241. $text .= ", ";
  242. }
  243. }
  244. if($days > 0) {
  245. $text .= $days . " day";
  246. //Plurar check
  247. if($days > 1) {
  248. $text .= "s";
  249. }
  250. //check if there will be another field
  251. if($hours + $mins > 0) {
  252. $text .= ", ";
  253. }
  254. }
  255. if($hours > 0) {
  256. $text .= $hours . " hour";
  257. //Plurar check
  258. if($hours > 1) {
  259. $text .= "s";
  260. }
  261. //check if there will be another field
  262. if($mins > 0) {
  263. $text .= ", ";
  264. }
  265. }
  266. if($mins > 0) {
  267. $text .= $mins . " minute";
  268. //Plurar check
  269. if($mins > 1) {
  270. $text .= "s";
  271. }
  272. }
  273. if($mins + $hours + $days + $years == 0 && $secs > 0) {
  274. $text .= "<0 minutes";
  275. }
  276. $event = array("name" => $subscriber, "data" => array($text));
  277. }
  278. // Can't connect to Freeswitch
  279. else {
  280. $event = array("name" => $subscriber, "data" => array("(Server is down)"));
  281. }
  282. break;
  283. case "switchboard/logviewer":
  284. $logfile = "/usr/local/freeswitch/log/freeswitch.log";
  285. if(!isset($_SESSION["esl"]["logviewer_pos"])) {
  286. $logviewer_pos = filesize($logfile) - 1200;
  287. if($logviewer_pos < 0) {
  288. $logviewer_pos = 0;
  289. }
  290. }
  291. else {
  292. $logviewer_pos = $_SESSION["esl"]["logviewer_pos"];
  293. }
  294. $text = "";
  295. $log_pointer = fopen($logfile, "r");
  296. fseek($log_pointer, $logviewer_pos);
  297. //Skip to the first complete line....
  298. while(($char = fgetc($log_pointer)))
  299. if ($char == "\n") break;
  300. while(!feof($log_pointer)) {
  301. $line = fgets($log_pointer);
  302. if(trim($line) != "")
  303. $text .= $line;
  304. }
  305. $_SESSION["esl"]["logviewer_pos"] = ftell($log_pointer);
  306. fclose($log_pointer);
  307. $event = array("name" => $subscriber, "data" => array($text));
  308. break;
  309. default:
  310. $event = NULL;
  311. break;
  312. }
  313. if(isset($event)) {
  314. if(isset($_SESSION["esl"][$subscriber])) {
  315. if($_SESSION["esl"][$subscriber] == $event["data"]) {
  316. continue;
  317. }
  318. }
  319. $_SESSION["esl"][$subscriber] = $event["data"];
  320. $response[] = $event;
  321. }
  322. }
  323. $exectime = time() - $starttime;
  324. }
  325. echo json_encode($response);
  326. flush();
  327. die();
  328. }
  329. }