diff --git a/.gitignore b/.gitignore index fcd8c07..8941c05 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,4 @@ .env -config.php composer.phar /vendor/ diff --git a/README.md b/README.md index ae43191..8b51218 100644 --- a/README.md +++ b/README.md @@ -23,6 +23,10 @@ cp .env.example .env Open `.env` in your favorite text editor and configure the following values. +On production, to avoid showing errors, you should set the variable `DISPLAY_ERRORS` to +`0`. For development, it can be set to `1` on development. For more information, read +[this](https://phpdelusions.net/articles/error_reporting) + ### Configure account information Every sample in the demo requires some basic credentials from your Twilio account. Configure these first. @@ -89,13 +93,12 @@ Check your config values, and follow the links to the demo applications! ## Warning: You may need to update the phpdotenv dependency -The phpdotenv dependency (for loading configuration information) was recently updated, so you may need to run composer update on your directory if you have downloaded this project in the past. For new installations, this step won't be necessary. The error you will get is: +The phpdotenv dependency (for loading configuration information) was recently updated, so you may need to run `composer update` on your project if you have downloaded this project in the past. For new installations, this step won't be necessary. The error you will get is: ``` /config-check.php - Uncaught Error: Call to undefined method Dotenv\Dotenv::create() ``` - ## Running the SDK Starter Kit with ngrok If you are going to connect to this SDK Starter Kit with a mobile app (and you should try it out!), your phone won't be able to access localhost directly. You'll need to create a publicly accessible URL using a tool like [ngrok](https://ngrok.com/) to send HTTP/HTTPS traffic to a server running on your localhost. Use HTTPS to make web connections that retrieve a Twilio access token. diff --git a/composer.json b/composer.json index 54baad4..5eb43fb 100644 --- a/composer.json +++ b/composer.json @@ -1,6 +1,6 @@ { "require": { - "twilio/sdk": "5.22.0", - "vlucas/phpdotenv": "dev-master" + "twilio/sdk": "6.13.0", + "vlucas/phpdotenv": "5.2.0" } } diff --git a/composer.lock b/composer.lock index 50714d2..3950598 100644 --- a/composer.lock +++ b/composer.lock @@ -1,36 +1,417 @@ { "_readme": [ "This file locks the dependencies of your project to a known state", - "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", + "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "0cc1742d0846f4fd1089fd678bf269ef", + "content-hash": "966f401005b2b3203497276ae7a8e93c", "packages": [ + { + "name": "graham-campbell/result-type", + "version": "v1.0.1", + "source": { + "type": "git", + "url": "https://github.com/GrahamCampbell/Result-Type.git", + "reference": "7e279d2cd5d7fbb156ce46daada972355cea27bb" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/GrahamCampbell/Result-Type/zipball/7e279d2cd5d7fbb156ce46daada972355cea27bb", + "reference": "7e279d2cd5d7fbb156ce46daada972355cea27bb", + "shasum": "" + }, + "require": { + "php": "^7.0|^8.0", + "phpoption/phpoption": "^1.7.3" + }, + "require-dev": { + "phpunit/phpunit": "^6.5|^7.5|^8.5|^9.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.0-dev" + } + }, + "autoload": { + "psr-4": { + "GrahamCampbell\\ResultType\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Graham Campbell", + "email": "graham@alt-three.com" + } + ], + "description": "An Implementation Of The Result Type", + "keywords": [ + "Graham Campbell", + "GrahamCampbell", + "Result Type", + "Result-Type", + "result" + ], + "support": { + "issues": "https://github.com/GrahamCampbell/Result-Type/issues", + "source": "https://github.com/GrahamCampbell/Result-Type/tree/v1.0.1" + }, + "funding": [ + { + "url": "https://github.com/GrahamCampbell", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/graham-campbell/result-type", + "type": "tidelift" + } + ], + "time": "2020-04-13T13:17:36+00:00" + }, + { + "name": "phpoption/phpoption", + "version": "1.7.5", + "source": { + "type": "git", + "url": "https://github.com/schmittjoh/php-option.git", + "reference": "994ecccd8f3283ecf5ac33254543eb0ac946d525" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/schmittjoh/php-option/zipball/994ecccd8f3283ecf5ac33254543eb0ac946d525", + "reference": "994ecccd8f3283ecf5ac33254543eb0ac946d525", + "shasum": "" + }, + "require": { + "php": "^5.5.9 || ^7.0 || ^8.0" + }, + "require-dev": { + "bamarni/composer-bin-plugin": "^1.4.1", + "phpunit/phpunit": "^4.8.35 || ^5.7.27 || ^6.5.6 || ^7.0 || ^8.0 || ^9.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.7-dev" + } + }, + "autoload": { + "psr-4": { + "PhpOption\\": "src/PhpOption/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "Apache-2.0" + ], + "authors": [ + { + "name": "Johannes M. Schmitt", + "email": "schmittjoh@gmail.com" + }, + { + "name": "Graham Campbell", + "email": "graham@alt-three.com" + } + ], + "description": "Option Type for PHP", + "keywords": [ + "language", + "option", + "php", + "type" + ], + "support": { + "issues": "https://github.com/schmittjoh/php-option/issues", + "source": "https://github.com/schmittjoh/php-option/tree/1.7.5" + }, + "funding": [ + { + "url": "https://github.com/GrahamCampbell", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/phpoption/phpoption", + "type": "tidelift" + } + ], + "time": "2020-07-20T17:29:33+00:00" + }, + { + "name": "symfony/polyfill-ctype", + "version": "v1.20.0", + "source": { + "type": "git", + "url": "https://github.com/symfony/polyfill-ctype.git", + "reference": "f4ba089a5b6366e453971d3aad5fe8e897b37f41" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/polyfill-ctype/zipball/f4ba089a5b6366e453971d3aad5fe8e897b37f41", + "reference": "f4ba089a5b6366e453971d3aad5fe8e897b37f41", + "shasum": "" + }, + "require": { + "php": ">=7.1" + }, + "suggest": { + "ext-ctype": "For best performance" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-main": "1.20-dev" + }, + "thanks": { + "name": "symfony/polyfill", + "url": "https://github.com/symfony/polyfill" + } + }, + "autoload": { + "psr-4": { + "Symfony\\Polyfill\\Ctype\\": "" + }, + "files": [ + "bootstrap.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Gert de Pagter", + "email": "BackEndTea@gmail.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Symfony polyfill for ctype functions", + "homepage": "https://symfony.com/", + "keywords": [ + "compatibility", + "ctype", + "polyfill", + "portable" + ], + "support": { + "source": "https://github.com/symfony/polyfill-ctype/tree/v1.20.0" + }, + "funding": [ + { + "url": "https://symfony.com/sponsor", + "type": "custom" + }, + { + "url": "https://github.com/fabpot", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/symfony/symfony", + "type": "tidelift" + } + ], + "time": "2020-10-23T14:02:19+00:00" + }, + { + "name": "symfony/polyfill-mbstring", + "version": "v1.20.0", + "source": { + "type": "git", + "url": "https://github.com/symfony/polyfill-mbstring.git", + "reference": "39d483bdf39be819deabf04ec872eb0b2410b531" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/polyfill-mbstring/zipball/39d483bdf39be819deabf04ec872eb0b2410b531", + "reference": "39d483bdf39be819deabf04ec872eb0b2410b531", + "shasum": "" + }, + "require": { + "php": ">=7.1" + }, + "suggest": { + "ext-mbstring": "For best performance" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-main": "1.20-dev" + }, + "thanks": { + "name": "symfony/polyfill", + "url": "https://github.com/symfony/polyfill" + } + }, + "autoload": { + "psr-4": { + "Symfony\\Polyfill\\Mbstring\\": "" + }, + "files": [ + "bootstrap.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Nicolas Grekas", + "email": "p@tchwork.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Symfony polyfill for the Mbstring extension", + "homepage": "https://symfony.com/", + "keywords": [ + "compatibility", + "mbstring", + "polyfill", + "portable", + "shim" + ], + "support": { + "source": "https://github.com/symfony/polyfill-mbstring/tree/v1.20.0" + }, + "funding": [ + { + "url": "https://symfony.com/sponsor", + "type": "custom" + }, + { + "url": "https://github.com/fabpot", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/symfony/symfony", + "type": "tidelift" + } + ], + "time": "2020-10-23T14:02:19+00:00" + }, + { + "name": "symfony/polyfill-php80", + "version": "v1.20.0", + "source": { + "type": "git", + "url": "https://github.com/symfony/polyfill-php80.git", + "reference": "e70aa8b064c5b72d3df2abd5ab1e90464ad009de" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/polyfill-php80/zipball/e70aa8b064c5b72d3df2abd5ab1e90464ad009de", + "reference": "e70aa8b064c5b72d3df2abd5ab1e90464ad009de", + "shasum": "" + }, + "require": { + "php": ">=7.1" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-main": "1.20-dev" + }, + "thanks": { + "name": "symfony/polyfill", + "url": "https://github.com/symfony/polyfill" + } + }, + "autoload": { + "psr-4": { + "Symfony\\Polyfill\\Php80\\": "" + }, + "files": [ + "bootstrap.php" + ], + "classmap": [ + "Resources/stubs" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Ion Bazan", + "email": "ion.bazan@gmail.com" + }, + { + "name": "Nicolas Grekas", + "email": "p@tchwork.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Symfony polyfill backporting some PHP 8.0+ features to lower PHP versions", + "homepage": "https://symfony.com/", + "keywords": [ + "compatibility", + "polyfill", + "portable", + "shim" + ], + "support": { + "source": "https://github.com/symfony/polyfill-php80/tree/v1.20.0" + }, + "funding": [ + { + "url": "https://symfony.com/sponsor", + "type": "custom" + }, + { + "url": "https://github.com/fabpot", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/symfony/symfony", + "type": "tidelift" + } + ], + "time": "2020-10-23T14:02:19+00:00" + }, { "name": "twilio/sdk", - "version": "5.22.0", + "version": "6.13.0", "source": { "type": "git", "url": "https://github.com/twilio/twilio-php.git", - "reference": "9984743a96015258a7fec3a382cd789001278710" + "reference": "df14c92ae0c6604ffe88c9880ad78851c42f7b21" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/twilio/twilio-php/zipball/9984743a96015258a7fec3a382cd789001278710", - "reference": "9984743a96015258a7fec3a382cd789001278710", + "url": "https://api.github.com/repos/twilio/twilio-php/zipball/df14c92ae0c6604ffe88c9880ad78851c42f7b21", + "reference": "df14c92ae0c6604ffe88c9880ad78851c42f7b21", "shasum": "" }, "require": { - "php": ">=5.5.0" + "php": ">=7.1.0" }, "require-dev": { - "apigen/apigen": "^4.1", - "phpunit/phpunit": "4.5.*" + "guzzlehttp/guzzle": "^6.3 || ^7.0", + "phpunit/phpunit": ">=4.5", + "theseer/phpdox": "^0.12.0" + }, + "suggest": { + "guzzlehttp/guzzle": "An HTTP client to execute the API requests" }, "type": "library", "autoload": { "psr-4": { - "Twilio\\": "Twilio/" + "Twilio\\": "src/Twilio/" } }, "notification-url": "https://packagist.org/downloads/", @@ -50,32 +431,47 @@ "sms", "twilio" ], - "time": "2018-09-21T21:35:30+00:00" + "support": { + "issues": "https://github.com/twilio/twilio-php/issues", + "source": "https://github.com/twilio/twilio-php/tree/6.13.0" + }, + "time": "2020-11-19T00:40:06+00:00" }, { "name": "vlucas/phpdotenv", - "version": "dev-master", + "version": "v5.2.0", "source": { "type": "git", "url": "https://github.com/vlucas/phpdotenv.git", - "reference": "54d599d414f8cf3f6129e287acaf44b610486aa1" + "reference": "fba64139db67123c7a57072e5f8d3db10d160b66" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/vlucas/phpdotenv/zipball/54d599d414f8cf3f6129e287acaf44b610486aa1", - "reference": "54d599d414f8cf3f6129e287acaf44b610486aa1", + "url": "https://api.github.com/repos/vlucas/phpdotenv/zipball/fba64139db67123c7a57072e5f8d3db10d160b66", + "reference": "fba64139db67123c7a57072e5f8d3db10d160b66", "shasum": "" }, "require": { - "php": ">=5.3.9" + "ext-pcre": "*", + "graham-campbell/result-type": "^1.0.1", + "php": "^7.1.3 || ^8.0", + "phpoption/phpoption": "^1.7.4", + "symfony/polyfill-ctype": "^1.17", + "symfony/polyfill-mbstring": "^1.17", + "symfony/polyfill-php80": "^1.17" }, "require-dev": { - "phpunit/phpunit": "^4.8.35 || ^5.0" + "bamarni/composer-bin-plugin": "^1.4.1", + "ext-filter": "*", + "phpunit/phpunit": "^7.5.20 || ^8.5.2 || ^9.0" + }, + "suggest": { + "ext-filter": "Required to use the boolean validator." }, "type": "library", "extra": { "branch-alias": { - "dev-master": "2.5-dev" + "dev-master": "5.2-dev" } }, "autoload": { @@ -88,10 +484,15 @@ "BSD-3-Clause" ], "authors": [ + { + "name": "Graham Campbell", + "email": "graham@alt-three.com", + "homepage": "https://gjcampbell.co.uk/" + }, { "name": "Vance Lucas", "email": "vance@vancelucas.com", - "homepage": "http://www.vancelucas.com/" + "homepage": "https://vancelucas.com/" } ], "description": "Loads environment variables from `.env` to `getenv()`, `$_ENV` and `$_SERVER` automagically.", @@ -100,17 +501,30 @@ "env", "environment" ], - "time": "2018-08-22 19:34:05" + "support": { + "issues": "https://github.com/vlucas/phpdotenv/issues", + "source": "https://github.com/vlucas/phpdotenv/tree/v5.2.0" + }, + "funding": [ + { + "url": "https://github.com/GrahamCampbell", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/vlucas/phpdotenv", + "type": "tidelift" + } + ], + "time": "2020-09-14T15:57:31+00:00" } ], "packages-dev": [], "aliases": [], "minimum-stability": "stable", - "stability-flags": { - "vlucas/phpdotenv": 20 - }, + "stability-flags": [], "prefer-stable": false, "prefer-lowest": false, "platform": [], - "platform-dev": [] + "platform-dev": [], + "plugin-api-version": "2.0.0" } diff --git a/webroot/.env.example b/webroot/.env.example index 61512b1..a4ee486 100644 --- a/webroot/.env.example +++ b/webroot/.env.example @@ -1,3 +1,6 @@ +# Set it to 0 when running on production. See the README for further explanation +DISPLAY_ERRORS=0 + # Required for all uses TWILIO_ACCOUNT_SID=ACXXXXXXXXXXXXXXXXX TWILIO_API_KEY=SKXXXXXXXXXXXXXXXXX diff --git a/webroot/chat/index.html b/webroot/chat/index.html index 9acc7ba..e03ec33 100755 --- a/webroot/chat/index.html +++ b/webroot/chat/index.html @@ -19,8 +19,7 @@ - - + diff --git a/webroot/chat/index.js b/webroot/chat/index.js index c4513e3..58950c7 100755 --- a/webroot/chat/index.js +++ b/webroot/chat/index.js @@ -40,12 +40,8 @@ $(function() { // Alert the user they have been assigned a random username print('Logging in...'); - // Get an access token for the current user, passing a username (identity) - // and a device ID - for browser-based apps, we'll always just use the - // value "browser" - $.getJSON('/token.php', { - device: 'browser' - }, function(data) { + // Get an access token for the current user + $.getJSON('/token.php', function(data) { // Initialize the Chat client @@ -54,6 +50,16 @@ $(function() { chatClient = client; chatClient.getSubscribedChannels().then(createOrJoinGeneralChannel); + // when the access token is about to expire, refresh it + chatClient.on('tokenAboutToExpire', function() { + refreshToken(username); + }); + + // if the access token already expired, refresh it + chatClient.on('tokenExpired', function() { + refreshToken(username); + }); + // Alert the user they have been assigned a random username username = data.identity; print('You have been assigned a random username of: ' @@ -66,6 +72,16 @@ $(function() { }); }); + function refreshToken(identity) { + console.log('Token about to expire'); + // Make a secure request to your backend to retrieve a refreshed access token. + // Use an authentication mechanism to prevent token exposure to 3rd parties. + $.getJSON('/token.php', {identity, identity}, function(data) { + console.log('updated token for chat client'); + chatClient.updateToken(data.token); + }); + } + function createOrJoinGeneralChannel() { // Get the general chat channel, which is where all the messages are // sent in this simple application diff --git a/webroot/config-check.js b/webroot/config-check.js index 90e1cc5..c92ecfc 100644 --- a/webroot/config-check.js +++ b/webroot/config-check.js @@ -23,7 +23,7 @@ $(function() { }; var configureField = function(fields, response) { - var htmlContent = 'Not configured in config.php'; + var htmlContent = 'Not configured in .env'; var cssClass = 'unset'; return function(fieldId) { var configKey = strToConfig(fieldId); diff --git a/webroot/config-check.php b/webroot/config-check.php index 84051ec..47fcbb0 100644 --- a/webroot/config-check.php +++ b/webroot/config-check.php @@ -2,26 +2,26 @@ include('../vendor/autoload.php'); // Load environment variables from .env, or environment if available -$dotenv = Dotenv\Dotenv::create(__DIR__); +$dotenv = Dotenv\Dotenv::createImmutable(__DIR__); $dotenv->load(); header('Content-type:application/json;charset=utf-8'); -$syncServiceSID = getenv('TWILIO_SYNC_SERVICE_SID'); +$syncServiceSID = $_ENV['TWILIO_SYNC_SERVICE_SID']; if (empty($syncServiceSID)) { $syncServiceSID = 'default'; } // Ensure that the Sync Default Service is provisioned if ($syncServiceSID === 'default') { - $client = new Twilio\Rest\Client(getenv('TWILIO_API_KEY'), getenv('TWILIO_API_SECRET'), getenv('TWILIO_ACCOUNT_SID')); + $client = new Twilio\Rest\Client($_ENV['TWILIO_API_KEY'], $_ENV['TWILIO_API_SECRET'], $_ENV['TWILIO_ACCOUNT_SID']); $client->sync->services($syncServiceSID)->fetch(); } echo json_encode(array( - 'TWILIO_ACCOUNT_SID' => getenv('TWILIO_ACCOUNT_SID'), - 'TWILIO_NOTIFICATION_SERVICE_SID' => getenv('TWILIO_NOTIFICATION_SERVICE_SID'), - 'TWILIO_CHAT_SERVICE_SID' => getenv('TWILIO_CHAT_SERVICE_SID'), + 'TWILIO_ACCOUNT_SID' => $_ENV['TWILIO_ACCOUNT_SID'], + 'TWILIO_NOTIFICATION_SERVICE_SID' => $_ENV['TWILIO_NOTIFICATION_SERVICE_SID'], + 'TWILIO_CHAT_SERVICE_SID' => $_ENV['TWILIO_CHAT_SERVICE_SID'], 'TWILIO_SYNC_SERVICE_SID' => $syncServiceSID, - 'TWILIO_API_KEY' => getenv('TWILIO_API_KEY'), - 'TWILIO_API_SECRET' => !empty(getenv('TWILIO_API_SECRET')), + 'TWILIO_API_KEY' => $_ENV['TWILIO_API_KEY'], + 'TWILIO_API_SECRET' => !empty($_ENV['TWILIO_API_SECRET']), )); diff --git a/webroot/randos.php b/webroot/randos.php index 21b3a57..64747d6 100644 --- a/webroot/randos.php +++ b/webroot/randos.php @@ -3,10 +3,10 @@ // Generate a random username for the connecting client function randomUsername() { $ADJECTIVES = array( - 'Abrasive', 'Brash', 'Callous', 'Daft', 'Eccentric', 'Fiesty', 'Golden', - 'Holy', 'Ignominious', 'Joltin', 'Killer', 'Luscious', 'Mushy', 'Nasty', - 'OldSchool', 'Pompous', 'Quiet', 'Rowdy', 'Sneaky', 'Tawdry', - 'Unique', 'Vivacious', 'Wicked', 'Xenophobic', 'Yawning', 'Zesty', + 'Awesome', 'Bold', 'Creative', 'Dapper', 'Eccentric', 'Fiesty', 'Golden', + 'Holy', 'Ignominious', 'Jolly', 'Kindly', 'Lucky', 'Mushy', 'Natural', + 'Oaken', 'Precise', 'Quiet', 'Rowdy', 'Sunny', 'Tall', + 'Unique', 'Vivid', 'Wonderful', 'Xtra', 'Yawning', 'Zesty', ); $FIRST_NAMES = array( @@ -30,4 +30,4 @@ function randomUsername() { $ln = $LAST_NAMES[array_rand($LAST_NAMES)]; return $adj . $fn . $ln; -} \ No newline at end of file +} diff --git a/webroot/register.php b/webroot/register.php index d9cb917..e0cbbce 100644 --- a/webroot/register.php +++ b/webroot/register.php @@ -2,14 +2,17 @@ include('../vendor/autoload.php'); // Load environment variables from .env, or environment if available -$dotenv = Dotenv\Dotenv::create(__DIR__); +$dotenv = Dotenv\Dotenv::createImmutable(__DIR__); $dotenv->load(); +$DISPLAY_ERRORS = $_ENV['DISPLAY_ERRORS']; +ini_set('display_errors', $DISPLAY_ERRORS); + // Authenticate with Twilio -$client = new Twilio\Rest\Client(getenv('TWILIO_API_KEY'), getenv('TWILIO_API_SECRET'), getenv('TWILIO_ACCOUNT_SID')); +$client = new Twilio\Rest\Client($_ENV['TWILIO_API_KEY'], $_ENV['TWILIO_API_SECRET'], $_ENV['TWILIO_ACCOUNT_SID']); // Get a reference to the user notification service instance -$service = $client->notify->v1->services(getenv('TWILIO_NOTIFICATION_SERVICE_SID')); +$service = $client->notify->v1->services($_ENV['TWILIO_NOTIFICATION_SERVICE_SID']); $json = json_decode(file_get_contents('php://input'), true); diff --git a/webroot/send-notification.php b/webroot/send-notification.php index 588e09b..674fecb 100644 --- a/webroot/send-notification.php +++ b/webroot/send-notification.php @@ -2,18 +2,20 @@ include('../vendor/autoload.php'); // Load environment variables from .env, or environment if available -$dotenv = Dotenv\Dotenv::create(__DIR__); +$dotenv = Dotenv\Dotenv::createImmutable(__DIR__); $dotenv->load(); +$DISPLAY_ERRORS = $_ENV['DISPLAY_ERRORS']; +ini_set('display_errors', $DISPLAY_ERRORS); + // Authenticate with Twilio -$client = new Twilio\Rest\Client(getenv('TWILIO_API_KEY'), getenv('TWILIO_API_SECRET'), getenv('TWILIO_ACCOUNT_SID')); +$client = new Twilio\Rest\Client($_ENV['TWILIO_API_KEY'], $_ENV['TWILIO_API_SECRET'], $_ENV['TWILIO_ACCOUNT_SID']); // Send a notification -$service = $client->notify->v1->services(getenv('TWILIO_NOTIFICATION_SERVICE_SID')); +$service = $client->notify->v1->services($_ENV['TWILIO_NOTIFICATION_SERVICE_SID']); $json = json_decode(file_get_contents('php://input'), true); - try { $notification = $service->notifications->create( [ diff --git a/webroot/token.php b/webroot/token.php index 0c635d9..f58120b 100644 --- a/webroot/token.php +++ b/webroot/token.php @@ -9,20 +9,28 @@ use Twilio\Jwt\Grants\ChatGrant; // Load environment variables from .env, or environment if available -$dotenv = Dotenv\Dotenv::create(__DIR__); +$dotenv = Dotenv\Dotenv::createImmutable(__DIR__); $dotenv->load(); -// An identifier for your app - can be anything you'd like -$appName = 'TwilioStarterDemo'; +$DISPLAY_ERRORS = $_ENV['DISPLAY_ERRORS']; +ini_set('display_errors', $DISPLAY_ERRORS); -// choose a random username for the connecting user -$identity = randomUsername(); +$identity = ''; + +if (isset($_GET['identity'])) { + $identity = $_GET['identity']; +} + +if (empty($identity)) { + // choose a random username for the connecting user (if one is not supplied) + $identity = randomUsername(); +} // Create access token, which we will serialize and send to the client $token = new AccessToken( - getenv('TWILIO_ACCOUNT_SID'), - getenv('TWILIO_API_KEY'), - getenv('TWILIO_API_SECRET'), + $_ENV['TWILIO_ACCOUNT_SID'], + $_ENV['TWILIO_API_KEY'], + $_ENV['TWILIO_API_SECRET'], 3600, $identity ); @@ -33,17 +41,17 @@ // Grant access to Sync $syncGrant = new SyncGrant(); -if (empty(getenv('TWILIO_SYNC_SERVICE_SID'))) { +if (empty($_ENV['TWILIO_SYNC_SERVICE_SID'])) { $syncGrant->setServiceSid('default'); } else { - $syncGrant->setServiceSid(getenv('TWILIO_SYNC_SERVICE_SID')); + $syncGrant->setServiceSid($_ENV['TWILIO_SYNC_SERVICE_SID']); } $token->addGrant($syncGrant); // Grant access to Chat -if (!empty(getenv('TWILIO_CHAT_SERVICE_SID'))) { +if (!empty($_ENV['TWILIO_CHAT_SERVICE_SID'])) { $chatGrant = new ChatGrant(); - $chatGrant->setServiceSid(getenv('TWILIO_CHAT_SERVICE_SID')); + $chatGrant->setServiceSid($_ENV['TWILIO_CHAT_SERVICE_SID']); $token->addGrant($chatGrant); }