public static function getRegistrations($SIPInterface) { $cache = Cache::instance(); $sipRegCache = $cache->get('cache_' . $SIPInterface); if (!$sipRegCache) { $eslManager = new EslManager(); $cmd = 'sofia xmlstatus profile ' . $SIPInterface; $result = $eslManager->api($cmd); $xml = $eslManager->getResponse($result); $registrations = array(); if ($xml !== 'Command execution failed.') { $xml = @simplexml_load_string($xml); if ($xml and $xml->registrations and $xml->registrations->registration and $xml->registrations->registration != '') { $registrations = $xml->registrations->registration; } else { Kohana::log('info', 'No XML returned'); } } else { Kohana::log('info', $cmd . ': ' . 'Command execution failed.'); } $result = array(); foreach ($registrations as $r) { $r = (array) $r; $r['interface'] = $SIPInterface; $result[] = $r; } return $result; //array ('user' => 'blah', '') } else { Kohana::log('info', 'Using cached registration'); return $sipRegCache; } }
public static function getList($user, $domain) { //@TODO 10 second cache? //@TODO Put this in a cron self::updateVMTables($user, $domain); $eslManager = new EslManager(); $cmd = sprintf('vm_list %s@%s xml', $user, $domain); $call = $eslManager->api($cmd); $resp = $eslManager->getResponse($call); kohana::log('debug', 'ESL: Cmd ' . $cmd . ' failed to execute in an expected way. Result: ' . $resp); $xml = simplexml_load_string("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n" . $resp); $voicemails = array(); foreach ($xml as $voicemail) { $voicemails[] = (array) $voicemail; } return $voicemails; }
public function _get_conferences() { $bbconf = array(); foreach (Doctrine_Query::create()->select("n.number,e.name,e.conference_id")->from("Number n,Conference e")->where("n.class_type='ConferenceNumber'")->andWhere("n.foreign_id=e.conference_id")->orderBy("e.name")->execute(array(), Doctrine::HYDRATE_SCALAR) as $conference) { $bbconf["conference_" . $conference["e_conference_id"]] = $conference; } $clid = array(); foreach (Doctrine::getTable('Device')->findAll() as $ext) { if (array_key_exists('callerid', $ext['plugins']) && array_key_exists('internal_name', $ext['plugins']['callerid']) && array_key_exists('internal_number', $ext['plugins']['callerid'])) { if (array_key_exists('sip', $ext['plugins']) && array_key_exists('username', $ext['plugins']['sip'])) { $clid[$ext['plugins']['sip']['username']] = array('name' => $ext['plugins']['callerid']['internal_name'], 'ext' => $ext['plugins']['callerid']['internal_number']); } } } $esl = new EslManager(); $res = $esl->api("conference xml_list"); $xml = new XMLReader(); $xml->xml($esl->getResponse($res)); $conferences = array(); $path = array(); while ($xml->read()) { if ($xml->nodeType == XMLReader::ELEMENT) { array_unshift($path, $xml->name); if ($xml->name == 'conference') { $conferences[] = array(); end($conferences); $conf =& $conferences[key($conferences)]; $current =& $conferences[key($conferences)]; $conf['members'] = array(); } elseif ($xml->name == 'member') { $key = count($conf['members']); $conf['members'][$key] = array(); $member =& $conf['members'][$key]; $current =& $conf['members'][$key]; } if ($xml->hasAttributes) { while ($xml->moveToNextAttribute()) { $current[$xml->name] = $xml->value; if ($xml->name == 'name' && array_key_exists($xml->value, $bbconf)) { $current['bluebox'] = $bbconf[$xml->value]; } } } } elseif ($xml->nodeType == XMLReader::END_ELEMENT) { array_shift($path); } elseif ($xml->nodeType == XMLReader::TEXT) { $current[$path[0]] = $xml->value; if ($path[0] == "caller_id_number" && array_key_exists($xml->value, $clid)) { $current['clid'] = $clid[$xml->value]; } } } return $conferences; }
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; }
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; }