Advertisement
Guest User

pbxes presence

a guest
Oct 8th, 2012
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.26 KB | None | 0 0
  1. <?php
  2.  
  3. // config section
  4. define('USER', ''); // your pbxes username here
  5. define('PASS', ''); // your pbxes password here
  6. define('SERVER', 'www1.pbxes.com'); // your prefered pbxes server here
  7.  
  8. // constants section
  9. define('LOGIN_SUCCESS_INDICATOR', USER . '@pbxes.org');
  10. define('REQUEST_STR', "<msg data=\"1|contexto0|\" />\r\n\r\n");
  11. define('REGISTERED_STR', 'registrado');
  12. $ckfile = tempnam("/tmp", "curlcookie");
  13.  
  14. header('Content-Type: text/plain');
  15.  
  16. // login
  17. $response = makeHttpRequest('https://' . SERVER . '/config.php?m=login', array('username' => USER, 'password' => PASS));
  18. if (strpos($response, LOGIN_SUCCESS_INDICATOR) === false) {
  19.     die('Incorrect Username/Password');
  20. }
  21.  
  22. // get variables
  23. $response = makeHttpRequest('https://' . SERVER . '/variables.txt?aldope=' . rand(1, 99999));
  24. parse_str($response, $variables);
  25. //print_r($variables);
  26.  
  27. // extract extensions, sub pbx's and trunks
  28. $extensions = array();
  29. $trunks = array();
  30. foreach ($variables as $key => $val) {
  31.     if (strpos($key, 'icono') === 0) {
  32.         $id = substr($key, 5);
  33.         if ($val == 4) {
  34.             $extensions[$id] = $variables['texto' . $id];
  35.         } else if ($val == 3) {
  36.             $trunks[$id] = $variables['texto' . $id];
  37.         }
  38.     }
  39. }
  40. ksort($extensions);
  41. ksort($trunks);
  42. //print_r($extensions);
  43. //print_r($trunks);
  44.  
  45. // query raw socket
  46. $fp = fsockopen(SERVER, $variables['port'], $errno, $errstr, 3);
  47. if (!$fp) {
  48.     die("$errstr ($errno)");
  49. } else {
  50.     fwrite($fp, REQUEST_STR);
  51.     $states = array();
  52.     while (!feof($fp)) {
  53.         $response = stream_get_line($fp, 256, "\0");
  54.         //echo "$response\n";
  55.         if (preg_match('/\<response btn\="(\d+)" cmd\="(.*?)"/', $response, $match)) {
  56.             if (isset($extensions[$match[1]])) {
  57.                 $states[$match[1]] = $match[2];
  58.             } else if (isset($trunks[$match[1]])) {
  59.                 $states[$match[1]] = $match[2];
  60.             }
  61.         }
  62.         if (count($states) == count($extensions) + count($trunks)) break;
  63.     }
  64.     fclose($fp);
  65. }
  66. ksort($states);
  67. //print_r($states);
  68.  
  69. // do output
  70. printXML($extensions, $trunks, $states);
  71.  
  72. // functions
  73. function makeHttpRequest($url, $post = null) {
  74.     global $ckfile;
  75.     $ch = curl_init($url);
  76.     curl_setopt ($ch, CURLOPT_COOKIEFILE, $ckfile);
  77.     curl_setopt ($ch, CURLOPT_COOKIEJAR, $ckfile);
  78.     curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, false);
  79.     curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
  80.     if (is_array($post)) {
  81.         curl_setopt ($ch, CURLOPT_POST, count($post));
  82.         curl_setopt ($ch, CURLOPT_POSTFIELDS, $post);
  83.     }
  84.     $output = curl_exec($ch);
  85.     curl_close($ch);
  86.     return $output;
  87. }
  88.  
  89. function printXML($extensions, $trunks, $states) {
  90.     $xml_root = new SimpleXMLElement("<status></status>");
  91.     foreach ($extensions as $id => $caption) {
  92.         $child = $xml_root->addChild('extension', translateState($states[$id]));
  93.         list($number, $name) = explode(' : ', $caption);
  94.         $child->addAttribute('number', $number);
  95.         $child->addAttribute('name', $name);
  96.     }
  97.     foreach ($trunks as $id => $name) {
  98.         $child = $xml_root->addChild('trunk', translateState($states[$id]));
  99.         $child->addAttribute('name', $name);
  100.     }
  101.     header('Content-type: text/xml');
  102.     echo $xml_root->asXML();
  103. }
  104.  
  105. function translateState(&$state) {
  106.     if (isset($state)) {
  107.         return $state == REGISTERED_STR ? 'on' : 'off';
  108.     } else {
  109.         return 'unknown'; // shouldn't ever happen..
  110.     }
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement