Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // config section
- define('USER', ''); // your pbxes username here
- define('PASS', ''); // your pbxes password here
- define('SERVER', 'www1.pbxes.com'); // your prefered pbxes server here
- // constants section
- define('LOGIN_SUCCESS_INDICATOR', USER . '@pbxes.org');
- define('REQUEST_STR', "<msg data=\"1|contexto0|\" />\r\n\r\n");
- define('REGISTERED_STR', 'registrado');
- $ckfile = tempnam("/tmp", "curlcookie");
- header('Content-Type: text/plain');
- // login
- $response = makeHttpRequest('https://' . SERVER . '/config.php?m=login', array('username' => USER, 'password' => PASS));
- if (strpos($response, LOGIN_SUCCESS_INDICATOR) === false) {
- die('Incorrect Username/Password');
- }
- // get variables
- $response = makeHttpRequest('https://' . SERVER . '/variables.txt?aldope=' . rand(1, 99999));
- parse_str($response, $variables);
- //print_r($variables);
- // extract extensions, sub pbx's and trunks
- $extensions = array();
- $trunks = array();
- foreach ($variables as $key => $val) {
- if (strpos($key, 'icono') === 0) {
- $id = substr($key, 5);
- if ($val == 4) {
- $extensions[$id] = $variables['texto' . $id];
- } else if ($val == 3) {
- $trunks[$id] = $variables['texto' . $id];
- }
- }
- }
- ksort($extensions);
- ksort($trunks);
- //print_r($extensions);
- //print_r($trunks);
- // query raw socket
- $fp = fsockopen(SERVER, $variables['port'], $errno, $errstr, 3);
- if (!$fp) {
- die("$errstr ($errno)");
- } else {
- fwrite($fp, REQUEST_STR);
- $states = array();
- while (!feof($fp)) {
- $response = stream_get_line($fp, 256, "\0");
- //echo "$response\n";
- if (preg_match('/\<response btn\="(\d+)" cmd\="(.*?)"/', $response, $match)) {
- if (isset($extensions[$match[1]])) {
- $states[$match[1]] = $match[2];
- } else if (isset($trunks[$match[1]])) {
- $states[$match[1]] = $match[2];
- }
- }
- if (count($states) == count($extensions) + count($trunks)) break;
- }
- fclose($fp);
- }
- ksort($states);
- //print_r($states);
- // do output
- printXML($extensions, $trunks, $states);
- // functions
- function makeHttpRequest($url, $post = null) {
- global $ckfile;
- $ch = curl_init($url);
- curl_setopt ($ch, CURLOPT_COOKIEFILE, $ckfile);
- curl_setopt ($ch, CURLOPT_COOKIEJAR, $ckfile);
- curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, false);
- curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
- if (is_array($post)) {
- curl_setopt ($ch, CURLOPT_POST, count($post));
- curl_setopt ($ch, CURLOPT_POSTFIELDS, $post);
- }
- $output = curl_exec($ch);
- curl_close($ch);
- return $output;
- }
- function printXML($extensions, $trunks, $states) {
- $xml_root = new SimpleXMLElement("<status></status>");
- foreach ($extensions as $id => $caption) {
- $child = $xml_root->addChild('extension', translateState($states[$id]));
- list($number, $name) = explode(' : ', $caption);
- $child->addAttribute('number', $number);
- $child->addAttribute('name', $name);
- }
- foreach ($trunks as $id => $name) {
- $child = $xml_root->addChild('trunk', translateState($states[$id]));
- $child->addAttribute('name', $name);
- }
- header('Content-type: text/xml');
- echo $xml_root->asXML();
- }
- function translateState(&$state) {
- if (isset($state)) {
- return $state == REGISTERED_STR ? 'on' : 'off';
- } else {
- return 'unknown'; // shouldn't ever happen..
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement