Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $password = "ClueCon";
- $port = "8021";
- $host = "127.0.0.1";
- function event_socket_create($host, $port, $password) {
- $fp = fsockopen($host, $port, $errno, $errdesc)
- or die("Connection to $host failed");
- socket_set_blocking($fp, false);
- if ($fp) {
- while (!feof($fp)) {
- $buffer = fgets($fp, 1024);
- usleep(100);
- if (trim($buffer) == "Content-Type: auth/request") {
- fputs($fp, "auth $password\n\n");
- break;
- }
- }
- return $fp;
- } else {
- return false;
- }
- }
- function event_socket_request($fp, $cmd) {
- if ($fp) {
- fputs($fp, $cmd . "\n\n");
- usleep(100);
- $response = "";
- $i = 0;
- $contentlength = 0;
- while (!feof($fp)) {
- $buffer = fgets($fp, 4096);
- if ($contentlength > 0) {
- $response .= $buffer;
- }
- if ($contentlength == 0) {
- if (strlen(trim($buffer)) > 0) {
- $temparray = explode(":", trim($buffer));
- if ($temparray[0] == "Content-Length") {
- $contentlength = trim($temparray[1]);
- }
- }
- }
- usleep(100);
- if ($i > 10000) {
- break;
- }
- if ($contentlength > 0) {
- if (strlen($response) >= $contentlength) {
- break;
- }
- }
- $i++;
- }
- return $response;
- } else {
- echo "no handle";
- }
- }
- function getFileName($id) {
- $dsn = 'mysql:host=127.0.0.1;dbname=fs';
- $username = 'fs';
- $passwd = 'wperngfsewneb';
- $sql = "select `name` from file where id = :id ;";
- $dbh = new PDO($dsn, $username, $passwd);
- $stmt = $dbh->prepare($sql);
- $stmt->bindParam(':id', $id, PDO::PARAM_INT);
- $stmt->execute();
- $result = $stmt->fetch();
- return $result['name'];
- }
- function createUUID($host, $port, $password) {
- $fp = event_socket_create($host, $port, $password);
- $cmd = "api create_uuid";
- $response = event_socket_request($fp, $cmd);
- fclose($fp);
- return $response;
- }
- function createCall($number, $uuid, $host, $port, $password) {
- $fp = event_socket_create($host, $port, $password);
- echo "api originate {origination_uuid=" . trim($uuid) . "}sofia/gateway/sipnet/" . $number . " &park()" . "<br>";
- $cmd = "bgapi originate {origination_uuid=" . trim($uuid) . "}sofia/gateway/sipnet/" . $number . " &park()";
- $response = event_socket_request($fp, $cmd);
- fclose($fp);
- return;
- }
- function sendVoice($file, $uuid, $host, $port, $password) {
- $fp = event_socket_create($host, $port, $password);
- echo "bgapi uuid_transfer " . trim($uuid) . " " . $file . " XML default <br>";
- $cmd = "bgapi uuid_transfer " . trim($uuid) . " " . $file . " XML default";
- $response = event_socket_request($fp, $cmd);
- fclose($fp);
- return;
- }
- //mysql> grant all on fs.* to fs@'127.0.0.1' identified by 'wperngfsewneb';
- //CREATE TABLE
- // `file` (
- // `id` INT(11) NOT NULL AUTO_INCREMENT,
- // `name` CHAR(30) NOT NULL,
- // PRIMARY KEY(`id`)
- // )
- if (isset($_GET['number']) && isset($_GET['file'])) {
- $file = getFileName($_GET['file']);
- $number = $_GET['number'];
- if (!empty($file)) {
- $uuid = createUUID($host, $port, $password);
- echo $uuid . '<br>';
- createCall($number, $uuid, $host, $port, $password);
- $i = 15;
- while ($i) {
- $fp = event_socket_create($host, $port, $password);
- $cmd = "api show channels like " . trim($uuid) . " as json";
- $response = event_socket_request($fp, $cmd);
- $status = json_decode(trim($response, true));
- if ($status->row_count) {
- echo $status->rows[0]->callstate . '<br>';
- if ($status->rows[0]->callstate == "ACTIVE") {
- sendVoice($file, $uuid, $host, $port, $password);
- break;
- }
- }
- fclose($fp);
- sleep(1);
- $i = $i - 1;
- }
- }
- } else {
- echo "no params";
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement