Ejemplo n.º 1
0
Archivo: esl.php Proyecto: swk/bluebox
 public function eslreponse()
 {
     $eslManager = new EslManager();
     $errors = message::render(NULL, array('html' => TRUE, 'htmlTemplate' => '{text}' . "\n", 'growl' => FALSE, 'inline' => FALSE));
     if (!empty($errors['html'])) {
         echo implode('', $errors['html']);
     }
     if (!$eslManager->isConnected()) {
         if (empty($errors['html'])) {
             echo __("Failed to load ESL. Check logs");
         }
         flush();
         die;
     }
     switch ($_POST['type']) {
         case 'status':
             $result = $eslManager->status();
             break;
         case 'version':
             $result = $eslManager->version();
             break;
         case 'reloadacl':
             $result = $eslManager->reloadacl();
             break;
         case 'reloadxml':
             $result = $eslManager->reloadxml();
             break;
         case 'channels':
             $result = $eslManager->channels();
             break;
         case 'calls':
             $result = $eslManager->calls();
             break;
         case 'show_codec':
             $result = $eslManager->show('codec');
             break;
         case 'show_modules':
             $result = $eslManager->show('modules');
             break;
         case 'show_files':
             $result = $eslManager->show('file');
             break;
         case 'nat_status':
             $result = $eslManager->nat('status');
             break;
         case 'nat_reinit':
             $result = $eslManager->nat('reinit');
             break;
         case 'nat_republish':
             $result = $eslManager->nat('republish');
             break;
         case 'reload_sofia':
             $result = $eslManager->reload('mod_sofia');
             break;
         case 'sofia_status':
             $result = $eslManager->sofia('status');
             break;
         case 'sofia_profile':
             $result = $eslManager->sofia('status', 'profile', $_POST['param']);
             break;
         case 'gateway_profile':
             $result = $eslManager->sofia('status', 'gateway', $_POST['param']);
             break;
         case 'recvEvent':
             if (empty($_POST['param'])) {
                 $result = '== HELP ==' . "\n";
                 $result .= __('Please use the text input to enable an event listener that you are interested in.') . "\n";
                 $result .= 'Example: events plain all' . "\n";
                 break;
             }
             $result = $eslManager->sendRecv($_POST['param']);
             $reply = $result->getHeader('Reply-Text');
             if (!strstr($reply, '+OK event listener enabled')) {
                 $result = '== HELP ==' . "\n";
                 $result .= __('The supplied command failed to start an event listener, please ensure you are using the events command.') . "\n";
                 break;
             }
             $result = $eslManager->recvEvent();
             break;
         case 'sendRecv':
             if (empty($_POST['param'])) {
                 $result = '== HELP ==' . "\n";
                 $result .= 'Please use the text input to provide a command to execute.' . "\n";
                 $result .= 'Example: api version' . "\n";
                 break;
             }
             $result = $eslManager->sendRecv($_POST['param']);
             break;
         default:
             $result = $_POST['type'] . __(' command not supported by Bluebox ESL plugin.');
             break;
     }
     $text = $eslManager->getResponse($result);
     echo htmlentities($text);
     flush();
     die;
 }
Ejemplo n.º 2
0
 public function fluxresponse()
 {
     //Turn off the view
     $this->auto_render = FALSE;
     //Set the timeout for the http request (in seconds)
     //Until flux handles firefox correctly, set this to 5
     $TIMEOUT = 5;
     $response = array();
     $eslManager = new EslManager();
     $subscribers = $_POST['subscribers'];
     $starttime = time();
     $exectime = 0;
     while (sizeof($response) == 0 && $exectime < $TIMEOUT) {
         foreach ($subscribers as $subscriber) {
             switch ($subscriber) {
                 case "switchboard/numactivecalls":
                     if ($eslManager->isConnected()) {
                         $result = $eslManager->calls();
                         $text = $eslManager->getResponse($result);
                         preg_match("/[0-9]+(?=\\stotal\\.)/", $text, $output);
                         $event = array("name" => $subscriber, "data" => array($output[0]));
                     } else {
                         $event = array("name" => $subscriber, "data" => array("N\\A"));
                     }
                     break;
                 case "switchboard/numactivemodules":
                     if ($eslManager->isConnected()) {
                         $result = $eslManager->show('modules');
                         $text = $eslManager->getResponse($result);
                         preg_match("/[0-9]+(?=\\stotal\\.)/", $text, $output);
                         $event = array("name" => $subscriber, "data" => array($output[0]));
                     } else {
                         $event = array("name" => $subscriber, "data" => array("N\\A"));
                     }
                     break;
                 case "switchboard/numactivechannels":
                     if ($eslManager->isConnected()) {
                         $result = $eslManager->channels();
                         $text = $eslManager->getResponse($result);
                         preg_match("/[0-9]+(?=\\stotal\\.)/", $text, $output);
                         $event = array("name" => $subscriber, "data" => array($output[0]));
                     } else {
                         $event = array("name" => $subscriber, "data" => array("N\\A"));
                     }
                     break;
                 case "switchboard/channels":
                     if ($eslManager->isConnected()) {
                         $result = $eslManager->channels();
                         $text = $eslManager->getResponse($result);
                         $event = array("name" => $subscriber, "data" => array($text));
                     } else {
                         $event = array("name" => $subscriber, "data" => array("N\\A"));
                     }
                     break;
                 case "switchboard/numactivecodecs":
                     if ($eslManager->isConnected()) {
                         $result = $eslManager->show('codecs');
                         $text = $eslManager->getResponse($result);
                         preg_match("/[0-9]+(?=\\stotal\\.)/", $text, $output);
                         $event = array("name" => $subscriber, "data" => array($output[0]));
                     } else {
                         $event = array("name" => $subscriber, "data" => array("N\\A"));
                     }
                     break;
                 case "switchboard/modules":
                     if ($eslManager->isConnected()) {
                         $result = $eslManager->show('modules');
                         $text = $eslManager->getResponse($result);
                         preg_match_all("/(?<=\\,)mod_[A-Za-z_0-9]+(?=,)/", $text, $output, PREG_PATTERN_ORDER);
                         if (isset($output)) {
                             // A dirty hack to eliminate duplicates
                             $matches = array();
                             foreach ($output[0] as $value) {
                                 $matches[$value] = 1337;
                             }
                             $text = "";
                             foreach ($matches as $key => $value) {
                                 $text .= $key . ',';
                             }
                         } else {
                             $text = "No modules found... I think something is broken.";
                         }
                         $event = array("name" => $subscriber, "data" => array($text));
                     } else {
                         $event = array("name" => $subscriber, "data" => array("Freeswitch not loaded.."));
                     }
                     break;
                 case "switchboard/sipinterfaces":
                     if ($eslManager->isConnected()) {
                         $result = $eslManager->sofia('status');
                         $text = $eslManager->getResponse($result);
                         $event = array("name" => $subscriber, "data" => array($text));
                     } else {
                         $event = array("name" => $subscriber, "data" => array("N\\A"));
                     }
                     break;
                 case "switchboard/calls":
                     if ($eslManager->isConnected()) {
                         $result = $eslManager->calls();
                         $text = $eslManager->getResponse($result);
                         $event = array("name" => $subscriber, "data" => array($text));
                     } else {
                         $event = array("name" => $subscriber, "data" => array("N\\A"));
                     }
                     break;
                 case "switchboard/activecalls":
                     if ($eslManager->isConnected()) {
                         $result = $eslManager->calls();
                         $text = $eslManager->getResponse($result);
                         $output = explode("\n", $text);
                         $event = array("name" => $subscriber, "data" => array($text));
                     } else {
                         $event = array("name" => $subscriber, "data" => array("N\\A"));
                     }
                     break;
                 case "switchboard/uptime":
                     if ($eslManager->isConnected()) {
                         $result = $eslManager->status();
                         $text = $eslManager->getResponse($result);
                         preg_match("/[0-9]+(?=\\syear)/", $text, $output);
                         $years = $output[0];
                         preg_match("/[0-9]+(?=\\sday)/", $text, $output);
                         $days = $output[0];
                         preg_match("/[0-9]+(?=\\shour)/", $text, $output);
                         $hours = $output[0];
                         preg_match("/[0-9]+(?=\\sminute)/", $text, $output);
                         $mins = $output[0];
                         preg_match("/[0-9]+(?=\\ssecond)/", $text, $output);
                         $secs = $output[0];
                         $text = "";
                         if ($years > 0) {
                             $text .= $years . " year";
                             //Plurar check
                             if ($years > 1) {
                                 $text .= "s";
                             }
                             //check if there will be another field
                             if ($days + $hours + $mins > 0) {
                                 $text .= ", ";
                             }
                         }
                         if ($days > 0) {
                             $text .= $days . " day";
                             //Plurar check
                             if ($days > 1) {
                                 $text .= "s";
                             }
                             //check if there will be another field
                             if ($hours + $mins > 0) {
                                 $text .= ", ";
                             }
                         }
                         if ($hours > 0) {
                             $text .= $hours . " hour";
                             //Plurar check
                             if ($hours > 1) {
                                 $text .= "s";
                             }
                             //check if there will be another field
                             if ($mins > 0) {
                                 $text .= ", ";
                             }
                         }
                         if ($mins > 0) {
                             $text .= $mins . " minute";
                             //Plurar check
                             if ($mins > 1) {
                                 $text .= "s";
                             }
                         }
                         if ($mins + $hours + $days + $years == 0 && $secs > 0) {
                             $text .= "<0 minutes";
                         }
                         $event = array("name" => $subscriber, "data" => array($text));
                     } else {
                         $event = array("name" => $subscriber, "data" => array("(Server is down)"));
                     }
                     break;
                 case "switchboard/logviewer":
                     $logfile = "/usr/local/freeswitch/log/freeswitch.log";
                     if (!isset($_SESSION["esl"]["logviewer_pos"])) {
                         $logviewer_pos = filesize($logfile) - 1200;
                         if ($logviewer_pos < 0) {
                             $logviewer_pos = 0;
                         }
                     } else {
                         $logviewer_pos = $_SESSION["esl"]["logviewer_pos"];
                     }
                     $text = "";
                     $log_pointer = fopen($logfile, "r");
                     fseek($log_pointer, $logviewer_pos);
                     //Skip to the first complete line....
                     while ($char = fgetc($log_pointer)) {
                         if ($char == "\n") {
                             break;
                         }
                     }
                     while (!feof($log_pointer)) {
                         $line = fgets($log_pointer);
                         if (trim($line) != "") {
                             $text .= $line;
                         }
                     }
                     $_SESSION["esl"]["logviewer_pos"] = ftell($log_pointer);
                     fclose($log_pointer);
                     $event = array("name" => $subscriber, "data" => array($text));
                     break;
                 default:
                     $event = NULL;
                     break;
             }
             if (isset($event)) {
                 if (isset($_SESSION["esl"][$subscriber])) {
                     if ($_SESSION["esl"][$subscriber] == $event["data"]) {
                         continue;
                     }
                 }
                 $_SESSION["esl"][$subscriber] = $event["data"];
                 $response[] = $event;
             }
         }
         $exectime = time() - $starttime;
     }
     echo json_encode($response);
     flush();
     die;
 }