|
| 1 | +<?php |
| 2 | + |
| 3 | +$autoloader = __DIR__.'/../vendor/autoload.php'; |
| 4 | +if (!file_exists($autoloader)) { |
| 5 | + die('You must run `composer install` in the sample app directory'); |
| 6 | +} |
| 7 | + |
| 8 | +require($autoloader); |
| 9 | + |
| 10 | +// remove this before push |
| 11 | +$autoloader2 = __DIR__.'/../../../vendor/autoload.php'; |
| 12 | +require($autoloader2); |
| 13 | + |
| 14 | +use Slim\Slim; |
| 15 | +use Gregwar\Cache\Cache; |
| 16 | + |
| 17 | +use OpenTok\OpenTok; |
| 18 | +use OpenTok\MediaMode; |
| 19 | + |
| 20 | +// PHP CLI webserver compatibility, serving static files |
| 21 | +$filename = __DIR__.preg_replace('#(\?.*)$#', '', $_SERVER['REQUEST_URI']); |
| 22 | +if (php_sapi_name() === 'cli-server' && is_file($filename)) { |
| 23 | + return false; |
| 24 | +} |
| 25 | + |
| 26 | +// Verify that the API Key and API Secret are defined |
| 27 | +if (!(getenv('API_KEY') && getenv('API_SECRET') && getenv('SIP_URI') && getenv('SIP_SECURE'))) { |
| 28 | + die('You must define API_KEY, API_SECRET, SIP_URI, and SIP_SECURE in the run-demo file'); |
| 29 | +} |
| 30 | + |
| 31 | +// Initialize Slim application |
| 32 | +$app = new Slim(array( |
| 33 | + 'templates.path' => __DIR__.'/../templates' |
| 34 | +)); |
| 35 | + |
| 36 | +// Intialize a cache, store it in the app container |
| 37 | +$app->container->singleton('cache', function() { |
| 38 | + return new Cache; |
| 39 | +}); |
| 40 | + |
| 41 | +// Initialize OpenTok instance, store it in the app contianer |
| 42 | +$app->container->singleton('opentok', function () { |
| 43 | + return new OpenTok(getenv('API_KEY'), getenv('API_SECRET')); |
| 44 | +}); |
| 45 | +// Store the API Key in the app container |
| 46 | +$app->apiKey = getenv('API_KEY'); |
| 47 | + |
| 48 | +$app->sip = array( |
| 49 | + 'uri' => getenv('SIP_URI'), |
| 50 | + 'username' => getenv('SIP_USERNAME'), |
| 51 | + 'password' => getenv('SIP_PASSWORD'), |
| 52 | + 'secure' => (getenv('SIP_SECURE') === 'true') |
| 53 | +); |
| 54 | + |
| 55 | +// Configure routes |
| 56 | +$app->get('/', function () use ($app) { |
| 57 | + // If a sessionId has already been created, retrieve it from the cache |
| 58 | + $sessionId = $app->cache->getOrCreate('sessionId', array(), function() use ($app) { |
| 59 | + // If the sessionId hasn't been created, create it now and store it |
| 60 | + $session = $app->opentok->createSession(array('mediaMode' => MediaMode::ROUTED)); |
| 61 | + return $session->getSessionId(); |
| 62 | + }); |
| 63 | + |
| 64 | + // Generate a fresh token for this client |
| 65 | + $token = $app->opentok->generateToken($sessionId, array('role' => 'moderator')); |
| 66 | + |
| 67 | + $app->render('index.php', array( |
| 68 | + 'apiKey' => $app->apiKey, |
| 69 | + 'sessionId' => $sessionId, |
| 70 | + 'token' => $token |
| 71 | + )); |
| 72 | +}); |
| 73 | + |
| 74 | +$app->post('/sip/start', function () use ($app) { |
| 75 | + $sessionId = $app->request->post('sessionId'); |
| 76 | + |
| 77 | + // generate a token |
| 78 | + $token = $app->opentok->generateToken($sessionId, array('data' => 'sip=true')); |
| 79 | + |
| 80 | + // create the options parameter |
| 81 | + $options = array( |
| 82 | + 'secure' => $app->sip['secure'] |
| 83 | + ); |
| 84 | + if ($app->sip['username'] !== false) { |
| 85 | + $options['auth'] = array('username' => $app->sip['username'], 'password' => $app->sip['password']); |
| 86 | + } |
| 87 | + |
| 88 | + // make the sip call |
| 89 | + $sipCall = $app->opentok->dial($sessionId, $token, $app->sip['uri'], $options); |
| 90 | + |
| 91 | + echo $sipCall->toJson(); |
| 92 | +}); |
| 93 | + |
| 94 | +$app->run(); |
0 commit comments