Advertisement
Guest User

Untitled

a guest
Jun 20th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.63 KB | None | 0 0
  1. #!/usr/bin/php -q
  2.  
  3. <?php
  4.    
  5.     /**
  6.      * phpari - A PHP Class Library for interfacing with Asterisk(R) ARI
  7.      * Copyright (C) 2014  Nir Simionovich
  8.      *
  9.      * This library is free software; you can redistribute it and/or
  10.      * modify it under the terms of the GNU Lesser General Public
  11.      * License as published by the Free Software Foundation; either
  12.      * version 2.1 of the License, or (at your option) any later version.
  13.      *
  14.      * This library is distributed in the hope that it will be useful,
  15.      * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16.      * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  17.      * Lesser General Public License for more details.
  18.      *
  19.      * You should have received a copy of the GNU Lesser General Public
  20.      * License along with this library; if not, write to the Free Software
  21.      * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
  22.      * Also add information on how to contact you by electronic and paper mail.
  23.      *
  24.      * Greenfield Technologies Ltd., hereby disclaims all copyright interest in
  25.      * the library `phpari' (a library for creating smart telephony applications)
  26.      * written by Nir Simionovich and its respective list of contributors.
  27.      */
  28.  
  29.     /* DO NOT MODIFY THIS PART, YOU WILL BREAK THIS! */
  30.     $pathinfo = pathinfo($_SERVER['PHP_SELF']);
  31.     $dir = $pathinfo['dirname'] . "/";
  32.     require_once $dir . "../../vendor/autoload.php";
  33.  
  34.     /* START YOUR MODIFICATIONS HERE */
  35.    
  36.     class Dial
  37.     {
  38.        
  39.         public $stasisLogger;
  40.         private $ariEndpoint;
  41.         private $stasisClient;
  42.         private $stasisLoop;
  43.         private $phpariObject;
  44.         private $stasisChannelID;
  45.         private $dtmfSequence = "";
  46.         private $channelStorage = array();
  47.        
  48.         public function __construct($appname = NULL)
  49.         {
  50.             try {
  51.                 if (is_null($appname))
  52.                     throw new Exception("[" . __FILE__ . ":" . __LINE__ . "] Stasis application name must be defined!", 500);
  53.                
  54.                 $this->phpariObject = new phpari($appname, 'phpari.ini');
  55.                
  56.                 $this->ariEndpoint = $this->phpariObject->ariEndpoint;
  57.                 $this->stasisClient = $this->phpariObject->stasisClient;
  58.                 $this->stasisLoop = $this->phpariObject->stasisLoop;
  59.                 $this->stasisLogger = $this->phpariObject->stasisLogger;
  60.                 $this->stasisEvents = $this->phpariObject->stasisEvents;
  61.                
  62.             } catch (Exception $e) {
  63.                 echo $e->getMessage();
  64.                 exit(99);
  65.             }
  66.         }
  67.        
  68.         public function setDtmf($digit = NULL)
  69.         {
  70.             try {
  71.                
  72.                 $this->dtmfSequence .= $digit;
  73.                
  74.                 return TRUE;
  75.                
  76.             } catch (Exception $e) {
  77.                 return FALSE;
  78.             }
  79.         }
  80.        
  81.         // process stasis events
  82.         public function StasisAppEventHandler()
  83.         {
  84.             $this->stasisEvents->on('StasisStart', function ($event) {
  85.                
  86.                 $this->stasisLogger->notice("Event received: StasisStart");
  87.                
  88.                 $args = $event->args;
  89.                 if (isset($args[0])) {
  90.                     $this->stasisChannelID = $event->channel->id;
  91.                     $this->stasisLogger->notice("Creating new instance in channelStorage");
  92.                     $this->stasisLogger->notice("channelStorage: " . print_r($this->channelStorage, TRUE));
  93.                     $this->stasisLogger->notice("About to originate a call to another party, and bridge to us");
  94.                     $response = $this->phpariObject->channels()->channel_originate(
  95.                         $args[0],
  96.                         NULL,
  97.                         array(
  98.                             "app"    => "stasis-dial",
  99.                             "appArgs" => '',
  100.                             "timeout" => $args[1]
  101.                         )
  102.                     );
  103.                    
  104.                     /* Creating a Bridge resource */
  105.                     $this->phpariObject->bridges()->create('mixing', 'bridge_' . $response['id']);
  106.                    
  107.                     /* Populate the Storage */
  108.                     $this->channelStorage[$response['id']]['epoch'] = time();
  109.                     $this->channelStorage[$response['id']]['bridge'] = "bridge_" . $response['id'];
  110.                     $this->channelStorage[$response['id']]['A'] = $event->channel->id;
  111.                     $this->channelStorage[$response['id']]['B'] = $response['id'];
  112.                     $this->channelStorage[$event->channel->id]['bridge'] = "bridge_" . $response['id'];
  113.                     $this->channelStorage[$event->channel->id]['B'] = $event->channel->id;
  114.                     $this->channelStorage[$event->channel->id]['A'] = $response['id'];
  115.                    
  116.                     /* Join the bridge */
  117.                     $this->phpariObject->channels()->channel_ringing_start($event->channel->id);
  118.                    
  119.                 } else {
  120.                     $this->stasisLogger->notice("First channel is joinging the bridge: " . $event->channel->id . " -> bridge_" . $response['id']);
  121.                     $this->phpariObject->channels()->channel_ringing_stop($this->channelStorage[$event->channel->id]['A']);
  122.                     $this->phpariObject->channels()->channel_answer($this->channelStorage[$event->channel->id]['A']);
  123.                     $this->phpariObject->bridges()->addchannel($this->channelStorage[$event->channel->id]['bridge'], $this->channelStorage[$event->channel->id]['A']);
  124.                     $this->stasisLogger->notice("Second channel is joining the bridge: " . $event->channel->id . " -> bridge_" . $event->channel->id);
  125.                     $this->phpariObject->channels()->channel_answer($event->channel->id);
  126.                     $this->phpariObject->bridges()->addchannel($this->channelStorage[$event->channel->id]['bridge'], $event->channel->id);
  127.                 }
  128.             });
  129.            
  130.             $this->stasisEvents->on('StasisEnd', function ($event) {
  131.                 /*
  132.                  * The following section will produce an error, as the channel no longer exists in this state - this is intentional
  133.                  */
  134.                 $this->stasisLogger->notice("Event received: StasisEnd");
  135.                 if (isset($this->channelStorage[$event->channel->id])) {
  136.                     $this->stasisLogger->notice("channelStorage: " . print_r($this->channelStorage, TRUE));
  137.                    
  138.                     $this->stasisLogger->notice("Terminating: " . $event->channel->id);
  139.                     $this->phpariObject->channels()->channel_delete($event->channel->id);
  140.                    
  141.                     $this->stasisLogger->notice("Terminating: " . $this->channelStorage[$event->channel->id]['A']);
  142.                     $this->phpariObject->channels()->channel_delete($this->channelStorage[$event->channel->id]['A']);
  143.                    
  144.                     $this->stasisLogger->notice("Terminating: " . $this->channelStorage[$event->channel->id]['bridge']);
  145.                     $this->phpariObject->bridges()->terminate($this->channelStorage[$event->channel->id]['bridge']);
  146.                    
  147.                     unset($this->channelStorage[$this->channelStorage[$event->channel->id]['A']]);
  148.                     unset($this->channelStorage[$event->channel->id]);
  149.                     $this->stasisLogger->notice("channelStorage: " . print_r($this->channelStorage, TRUE));
  150.                 }
  151.             });
  152.         }
  153.        
  154.         public function StasisAppConnectionHandlers()
  155.         {
  156.             try {
  157.                 $this->stasisClient->on("request", function ($headers) {
  158.                     $this->stasisLogger->notice("Request received!");
  159.                 });
  160.                
  161.                 $this->stasisClient->on("handshake", function () {
  162.                     $this->stasisLogger->notice("Handshake received!");
  163.                 });
  164.                
  165.                 $this->stasisClient->on("message", function ($message) {
  166.                     $this->stasisLogger->notice("channelStorage: " . print_r($this->channelStorage, TRUE));
  167.                     $event = json_decode($message->getData());
  168.                     $this->stasisLogger->notice('Received event: ' . $event->type);
  169.                     $this->stasisEvents->emit($event->type, array($event));
  170.                 });
  171.                
  172.             } catch (Exception $e) {
  173.                 echo $e->getMessage();
  174.                 exit(99);
  175.             }
  176.         }
  177.        
  178.         public function execute()
  179.         {
  180.             try {
  181.                 $this->stasisClient->open();
  182.                 $this->stasisLoop->run();
  183.             } catch (Exception $e) {
  184.                 echo $e->getMessage();
  185.                 exit(99);
  186.             }
  187.         }
  188.        
  189.     }
  190.    
  191.     $basicAriClient = new Dial("stasis-dial");
  192.    
  193.     $basicAriClient->stasisLogger->info("Starting Stasis Program... Waiting for handshake...");
  194.     $basicAriClient->StasisAppEventHandler();
  195.    
  196.     $basicAriClient->stasisLogger->info("Initializing Handlers... Waiting for handshake...");
  197.     $basicAriClient->StasisAppConnectionHandlers();
  198.    
  199.     $basicAriClient->stasisLogger->info("Connecting... Waiting for handshake...");
  200.     $basicAriClient->execute();
  201.    
  202.     exit(0);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement