Advertisement
Guest User

Untitled

a guest
Sep 15th, 2018
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.29 KB | None | 0 0
  1. <?php
  2.  
  3. $password = "ClueCon";
  4. $port = "8021";
  5. $host = "127.0.0.1";
  6.  
  7. function event_socket_create($host, $port, $password) {
  8.     $fp = fsockopen($host, $port, $errno, $errdesc)
  9.             or die("Connection to $host failed");
  10.     socket_set_blocking($fp, false);
  11.  
  12.     if ($fp) {
  13.         while (!feof($fp)) {
  14.             $buffer = fgets($fp, 1024);
  15.             usleep(100);
  16.             if (trim($buffer) == "Content-Type: auth/request") {
  17.                 fputs($fp, "auth $password\n\n");
  18.                 break;
  19.             }
  20.         }
  21.         return $fp;
  22.     } else {
  23.         return false;
  24.     }
  25. }
  26.  
  27. function event_socket_request($fp, $cmd) {
  28.  
  29.     if ($fp) {
  30.         fputs($fp, $cmd . "\n\n");
  31.         usleep(100);
  32.         $response = "";
  33.         $i = 0;
  34.         $contentlength = 0;
  35.         while (!feof($fp)) {
  36.             $buffer = fgets($fp, 4096);
  37.             if ($contentlength > 0) {
  38.                 $response .= $buffer;
  39.             }
  40.             if ($contentlength == 0) {
  41.                 if (strlen(trim($buffer)) > 0) {
  42.                     $temparray = explode(":", trim($buffer));
  43.                     if ($temparray[0] == "Content-Length") {
  44.                         $contentlength = trim($temparray[1]);
  45.                     }
  46.                 }
  47.             }
  48.             usleep(100);
  49.             if ($i > 10000) {
  50.                 break;
  51.             }
  52.  
  53.             if ($contentlength > 0) {
  54.                 if (strlen($response) >= $contentlength) {
  55.                     break;
  56.                 }
  57.             }
  58.             $i++;
  59.         }
  60.  
  61.         return $response;
  62.     } else {
  63.         echo "no handle";
  64.     }
  65. }
  66.  
  67. function getFileName($id) {
  68.     $dsn = 'mysql:host=127.0.0.1;dbname=fs';
  69.     $username = 'fs';
  70.     $passwd = 'wperngfsewneb';
  71.     $sql = "select `name` from file where id = :id ;";
  72.     $dbh = new PDO($dsn, $username, $passwd);
  73.     $stmt = $dbh->prepare($sql);
  74.     $stmt->bindParam(':id', $id, PDO::PARAM_INT);
  75.     $stmt->execute();
  76.     $result = $stmt->fetch();
  77.     return $result['name'];
  78. }
  79.  
  80. function createUUID($host, $port, $password) {
  81.     $fp = event_socket_create($host, $port, $password);
  82.     $cmd = "api create_uuid";
  83.     $response = event_socket_request($fp, $cmd);
  84.     fclose($fp);
  85.     return $response;
  86. }
  87.  
  88. function createCall($number, $uuid, $host, $port, $password) {
  89.     $fp = event_socket_create($host, $port, $password);
  90.     echo "api originate {origination_uuid=" . trim($uuid) . "}sofia/gateway/sipnet/" . $number . " &park()" . "<br>";
  91.     $cmd = "bgapi originate {origination_uuid=" . trim($uuid) . "}sofia/gateway/sipnet/" . $number . " &park()";
  92.     $response = event_socket_request($fp, $cmd);
  93.     fclose($fp);
  94.     return;
  95. }
  96.  
  97. function sendVoice($file, $uuid, $host, $port, $password) {
  98.     $fp = event_socket_create($host, $port, $password);
  99.     echo "bgapi uuid_transfer " . trim($uuid) . " " . $file . " XML default <br>";
  100.     $cmd = "bgapi uuid_transfer " . trim($uuid) . " " . $file . " XML default";
  101.     $response = event_socket_request($fp, $cmd);
  102.     fclose($fp);
  103.     return;
  104. }
  105.  
  106. //mysql> grant all on fs.* to fs@'127.0.0.1' identified by 'wperngfsewneb';
  107. //CREATE TABLE
  108. //    `file` (
  109. //        `id` INT(11) NOT NULL AUTO_INCREMENT,
  110. //        `name` CHAR(30) NOT NULL,
  111. //        PRIMARY KEY(`id`)
  112. //    )
  113.  
  114. if (isset($_GET['number']) && isset($_GET['file'])) {
  115.     $file = getFileName($_GET['file']);
  116.     $number = $_GET['number'];
  117.  
  118.     if (!empty($file)) {
  119.         $uuid = createUUID($host, $port, $password);
  120.         echo $uuid . '<br>';
  121.         createCall($number, $uuid, $host, $port, $password);
  122.         $i = 15;
  123.         while ($i) {
  124.             $fp = event_socket_create($host, $port, $password);
  125.             $cmd = "api show channels like " . trim($uuid) . " as json";
  126.             $response = event_socket_request($fp, $cmd);
  127.             $status = json_decode(trim($response, true));
  128.             if ($status->row_count) {
  129.                 echo $status->rows[0]->callstate . '<br>';
  130.                 if ($status->rows[0]->callstate == "ACTIVE") {
  131.                     sendVoice($file, $uuid, $host, $port, $password);
  132.                     break;
  133.                 }
  134.             }
  135.             fclose($fp);
  136.             sleep(1);
  137.             $i = $i - 1;
  138.         }
  139.     }
  140. } else {
  141.     echo "no params";
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement