Skip to content

Commit 37a366a

Browse files
committed
Better handling of Connect App
Update connect app when site settings are updated and during install so that we control the proper urls. Laid groundwork for handling connect user declining to give OpenVBX Access. Updates for Presence integration. Lays foundation for using Twilio Presence to track online users. Better handles long start-up of Client on first iframe page load. Better sub-account handling with new PHP API library. Minor usability tweaks.
1 parent 51886f1 commit 37a366a

23 files changed

+368
-107
lines changed

INSTALL.markdown

+3-4
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,9 @@ When creating your application use these settings:
119119

120120
1. **Friendly Name:** Any name that makes sense to you.
121121
1. **Company Name & Description:** _Optional_. Anything that makes sense to you.
122-
1. **Homepage URL:** The full url to your webroot. ie: _http://example.org_
123-
1. **Authorize URL:** The path to OpenVBX's Authorize Callback. ie: _http://example.org/authorize/connect_
124-
1. **Deauthorize URL:** _Not used. Leave blank_.
125-
1. **Access Required:** Get All & Post All.
122+
1. **Homepage URL & Authorize URL:** The full url to your webroot. ie: _http://example.org_ - these will be updated by OpenVBX during the install process.
123+
1. **Deauthorize URL:** _Leave blank_. This will be set by OpenVBX during install.
124+
1. **Access Required:** Select "Read all account data" & "Charge account for usage".
126125

127126

128127
## Optional Settings

OpenVBX/config/config.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
| you're developing, you'll want to see the plain javascript for debugging.
4747
|
4848
*/
49-
$config['use_unminimized_js'] = TRUE;
49+
$config['use_unminimized_js'] = FALSE;
5050

5151
/*
5252
|--------------------------------------------------------------------------

OpenVBX/controllers/account.php

+20-4
Original file line numberDiff line numberDiff line change
@@ -124,10 +124,9 @@ public function edit()
124124
$success = $user->update($this->user_id, $params);
125125

126126
if ($this->response_type == 'json') {
127-
$data = array(
128-
'error' => !$success,
129-
'message' => (!$success ? 'an error occurred while updating the user' : 'user status updated')
130-
);
127+
$data = (isset($this->data) ? $this->data : array());
128+
$data['json']['error'] = !$success;
129+
$data['json']['message'] = (!$success ? 'an error occurred while updating the user' : 'user status updated');
131130
$this->respond('', null, $data);
132131
}
133132
else {
@@ -204,4 +203,21 @@ public function save_voicemail()
204203
return $data;
205204
}
206205

206+
public function client_status() {
207+
$this->load->helper('twilio');
208+
if ($this->input->post('clientstatus')) {
209+
$accept_incoming = ($this->input->post('online') == 1 ? true : false);
210+
$this->data = array(
211+
'json' => array(
212+
'client_status' => ($accept_incoming ? 'online' : 'offline'),
213+
'client_capability' => generate_capability_token($this->make_rest_access(), $accept_incoming)
214+
)
215+
);
216+
$this->edit();
217+
}
218+
else {
219+
throw new TwilioException('Invalid Request', 400);
220+
exit;
221+
}
222+
}
207223
}

OpenVBX/controllers/iframe.php

+8-4
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ class Iframe extends User_Controller {
2727

2828
public function __construct() {
2929
parent::__construct();
30-
// make tokens valid for 8 hours
31-
$this->client_token_timeout = 3600*8;
3230
}
3331

3432
function index() {
@@ -49,12 +47,18 @@ function index() {
4947
$twilio_js_file = 'twilio'.($this->config->item('use_unminimized_js') ? '' : '.min').'.js';
5048
$data['twilio_js'] = $tjs_baseurl.'/libs/twiliojs/1.0/'.$twilio_js_file;
5149

50+
$data['client_capability'] = null;
5251
if (!empty($this->application_sid))
5352
{
54-
$data['client_capability'] = $this->capability->generateToken($this->client_token_timeout);
55-
$data['capability'] = $this->capability;
53+
$user_id = intval($this->session->userdata('user_id'));
54+
$user = VBX_user::get(array('id' => $user_id));
55+
$data['client_capability'] = generate_capability_token($this->make_rest_access(), ($user->online == 1));
5656
}
5757

58+
// internal dev haxies
59+
if (function_exists('twilio_dev_mods')) {
60+
$data = twilio_dev_mods($data);
61+
}
5862
$this->load->view('iframe', $data);
5963
}
6064
}

OpenVBX/controllers/install.php

+39-5
Original file line numberDiff line numberDiff line change
@@ -181,10 +181,13 @@ private function get_database_params($database)
181181
$database["char_set"] = "utf8";
182182
$database["dbcollat"] = "utf8_general_ci";
183183

184-
return array('global' => array('active_group' => "default",
185-
'active_record' => TRUE,
186-
),
187-
'default' => $database);
184+
return array(
185+
'global' => array(
186+
'active_group' => "default",
187+
'active_record' => TRUE,
188+
),
189+
'default' => $database
190+
);
188191
}
189192

190193
public function setup()
@@ -562,8 +565,39 @@ function validate_step3()
562565
// check the connect app if a sid is provided
563566
if (!empty($connect_app)) {
564567
try {
565-
$application = $account->connect_apps->get($connect_app);
568+
$connect_application = $account->connect_apps->get($connect_app);
566569
$friendly_name = $application->friendly_name;
570+
571+
$required_settings = array(
572+
'HomepageUrl' => site_url(),
573+
'AuthorizeRedirectUrl' => site_url('/auth/connect'),
574+
'DeauthorizeCallbackUrl' => site_url('/auth/connect/deauthorize'),
575+
'Permissions' => array(
576+
'get-all',
577+
'post-all'
578+
)
579+
);
580+
581+
$updated = false;
582+
foreach ($required_settings as $key => $setting) {
583+
$app_key = Services_Twilio::decamelize($key);
584+
if ($connect_application->$app_key != $setting) {
585+
$connect_application->$app_key = $setting;
586+
$updated = true;
587+
}
588+
}
589+
590+
if ($updated) {
591+
$connect_application->update(array(
592+
'FriendlyName' => $connect_application->friendly_name,
593+
'Description' => $connect_application->description,
594+
'CompanyName' => $connect_application->company_name,
595+
'HomepageUrl' => $required_settings['HomepageUrl'],
596+
'AuthorizeRedirectUrl' => $required_settings['AuthorizeRedirectUrl'],
597+
'DeauthorizeCallbackUrl' => $required_settings['DeauthorizeCallbackUrl'],
598+
'Permissions' => implode(',', $required_settings['Permissions'])
599+
));
600+
}
567601
}
568602
catch (Exception $e) {
569603
switch ($e->getCode()) {

OpenVBX/controllers/settings/site.php

+49-6
Original file line numberDiff line numberDiff line change
@@ -162,9 +162,50 @@ private function update_site()
162162
{
163163
$app_sid = $value;
164164
}
165+
if ($name == 'connect_application_sid') {
166+
$connect_app_sid = $value;
167+
}
165168
$this->settings->set($name, trim($value), $this->tenant->id);
166169
}
167170

171+
// Connect App (if applicable)
172+
if (!empty($connect_app_sid) && $this->tenant->id == VBX_PARENT_TENANT) {
173+
$account = OpenVBX::getAccount();
174+
$connect_app = $account->connect_apps->get($connect_app_sid);
175+
176+
$required_settings = array(
177+
'HomepageUrl' => site_url(),
178+
'AuthorizeRedirectUrl' => site_url('/auth/connect'),
179+
'DeauthorizeCallbackUrl' => site_url('/auth/connect/deauthorize'),
180+
'Permissions' => array(
181+
'get-all',
182+
'post-all'
183+
)
184+
);
185+
186+
$updated = false;
187+
foreach ($required_settings as $key => $setting) {
188+
$app_key = Services_Twilio::decamelize($key);
189+
if ($connect_app->$app_key != $setting) {
190+
$connect_app->$app_key = $setting;
191+
$updated = true;
192+
}
193+
}
194+
195+
if ($updated) {
196+
$connect_app->update(array(
197+
'FriendlyName' => $connect_app->friendly_name,
198+
'Description' => $connect_app->description,
199+
'CompanyName' => $connect_app->company_name,
200+
'HomepageUrl' => $required_settings['HomepageUrl'],
201+
'AuthorizeRedirectUrl' => $required_settings['AuthorizeRedirectUrl'],
202+
'DeauthorizeCallbackUrl' => $required_settings['DeauthorizeCallbackUrl'],
203+
'Permissions' => implode(',', $required_settings['Permissions'])
204+
));
205+
}
206+
}
207+
208+
// Client App
168209
$update_app = false;
169210
if (empty($app_sid) && !empty($current_app_sid))
170211
{
@@ -211,7 +252,9 @@ private function update_site()
211252

212253
if (!empty($update_app))
213254
{
214-
$account = OpenVBX::getAccount();
255+
if (empty($account)) {
256+
$account = OpenVBX::getAccount();
257+
}
215258

216259
foreach ($update_app as $app)
217260
{
@@ -223,7 +266,7 @@ private function update_site()
223266
$this->session->set_flashdata('error', 'Could not update Application: '.$e->getMessage());
224267
throw new SiteException($e->getMessage());
225268
}
226-
}
269+
}
227270
}
228271

229272
$this->session->set_flashdata('error', 'Settings have been saved');
@@ -249,8 +292,8 @@ private function create_application_for_subaccount($tenant_id, $name, $accountSi
249292

250293
$application = false;
251294
try {
252-
$account = OpenVBX::getAccount();
253-
$sub_account = $account->accounts->get($accountSid);
295+
$accounts = OpenVBX::getAccounts();
296+
$sub_account = $accounts->get($accountSid);
254297
foreach ($sub_account->applications as $_application)
255298
{
256299
if ($application->friendly_name == $appName)
@@ -345,10 +388,10 @@ private function add_tenant()
345388
if ($auth_type === VBX_Settings::AUTH_TYPE_SUBACCOUNT)
346389
{
347390
try {
348-
$account = OpenVBX::getAccount();
391+
$accounts = OpenVBX::getAccounts();
349392

350393
// default, sub-account
351-
$sub_account = $account->accounts->create(array(
394+
$sub_account = $accounts->create(array(
352395
'FriendlyName' => $friendlyName
353396
));
354397
$tenant_sid = $sub_account->sid;

OpenVBX/controllers/twiml.php

-1
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,6 @@ function dial()
368368
$dial = $this->response->dial(NULL, $options);
369369
$dial->client($to);
370370
}
371-
372371
}
373372
else
374373
{

OpenVBX/controllers/welcome.php

+4
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ public function index() {
4949
),
5050
'title' => 'Welcome'
5151
);
52+
53+
if ($tenant_sid = $this->vbx_settings->get('twilio_sid', $this->tenant->id)) {
54+
$data['tenant_sid'] = $tenant_sid;
55+
}
5256
$this->load->view('steps', $data);
5357
}
5458

OpenVBX/helpers/twilio_helper.php

+37
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,41 @@
2020
* Contributor(s):
2121
**/
2222

23+
if (!function_exists('generate_capability_token')) {
24+
/**
25+
* Generate a capability token for Twilio Client
26+
*
27+
* @param string $allow_incoming
28+
* @return string
29+
*/
30+
function generate_capability_token($rest_access, $allow_incoming = true) {
31+
$ci =& get_instance();
32+
$capability = new Services_Twilio_Capability($ci->twilio_sid, $ci->twilio_token);
33+
34+
$user_id = intval($ci->session->userdata('user_id'));
35+
$user = VBX_user::get(array('id' => $user_id));
36+
37+
$params = array(
38+
'user_id' => $user->user_id,
39+
'rest_access' => $rest_access
40+
);
41+
42+
$token = null;
43+
try {
44+
$capability->allowClientOutgoing($ci->application_sid, $params);
45+
if ($allow_incoming) {
46+
$capability->allowClientIncoming($user->id);
47+
}
48+
$token = $capability->generateToken(VBX_Settings::CLIENT_TOKEN_TIMEOUT);
49+
}
50+
catch (Exception $e) {
51+
error_log($e->getMessage());
52+
}
53+
54+
return $token;
55+
}
56+
}
57+
2358
if (!function_exists('validate_rest_request')) {
2459
/**
2560
* Validate that an incoming rest request is from Twilio
@@ -31,7 +66,9 @@ function validate_rest_request($failure_message = 'Could not validate this reque
3166
if (!OpenVBX::validateRequest()) {
3267
$response = new TwimlResponse;
3368
$response->say($failure_message);
69+
$response->hangup();
3470
$response->respond();
71+
exit;
3572
}
3673
}
3774
}

OpenVBX/libraries/MY_Controller.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public function __construct()
8383
{
8484
$this->session->set_userdata('loggedin', 0);
8585
$this->session->set_flashdata('error', 'This tenant is no longer active');
86-
return redirect(site_url('auth/logout'));
86+
return redirect(asset_url('auth/logout'));
8787
}
8888

8989
if($this->tenant === false)
@@ -100,6 +100,7 @@ public function __construct()
100100
$this->twilio_sid = $this->settings->get('twilio_sid', $this->tenant->id);
101101
$token_from = ($this->tenant->type == VBX_Settings::AUTH_TYPE_CONNECT ? VBX_PARENT_TENANT : $this->tenant->id);
102102
$this->twilio_token = $this->settings->get('twilio_token', $token_from);
103+
$this->application_sid = $this->settings->get('application_sid', $this->tenant->id);
103104

104105
// @deprecated, will be removed in a future release
105106
$this->twilio_endpoint = $this->settings->get('twilio_endpoint', VBX_PARENT_TENANT);
@@ -214,6 +215,7 @@ protected function json_respond($json)
214215
/* Filter out standard templates vars */
215216
$json = $this->build_json_response($json);
216217
$json_str = json_encode($json);
218+
header('content-type: text/javascript');
217219
if(!$pprint)
218220
{
219221
echo $json_str;

OpenVBX/libraries/OpenVBX.php

+13
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,14 @@ public static function getAccount($twilio_sid = false, $twilio_token = false, $a
316316
return self::$_twilioService->account;
317317
}
318318

319+
public function getAccounts() {
320+
if (!(self::$_twilioService instanceof Services_Twilio)) {
321+
$ci =& get_instance();
322+
self::getAccount();
323+
}
324+
return self::$_twilioService->accounts;
325+
}
326+
319327
/**
320328
* Validate that the current request came from Twilio
321329
*
@@ -330,6 +338,11 @@ public static function getAccount($twilio_sid = false, $twilio_token = false, $a
330338
*/
331339
public static function validateRequest($url = false, $post_vars = false)
332340
{
341+
$ci =& get_instance();
342+
if ($ci->tenant->type == VBX_Settings::AUTH_TYPE_CONNECT) {
343+
return true;
344+
}
345+
333346
if (!(self::$_twilioValidator instanceof Services_Twilio_RequestValidator))
334347
{
335348
$ci =& get_instance();

OpenVBX/libraries/Template.php

+7-3
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,11 @@ function add_js($script, $type = 'import', $defer = FALSE)
471471
switch ($type)
472472
{
473473
case 'dynamic':
474-
$filepath = site_url().preg_replace('|^(/)|', '', $script);
474+
$siteurl = site_url();
475+
if (!preg_match('|.*?/$|', $siteurl)) {
476+
$siteurl .= '/';
477+
}
478+
$filepath = $siteurl.preg_replace('|^(/)|', '', $script);
475479
$js = '<script type="text/javascript" src="'.version_url($filepath).'"';
476480
if ($defer)
477481
{
@@ -546,7 +550,7 @@ function add_css($style, $type = 'link', $media = FALSE)
546550
if (!function_exists('version_url')) {
547551
$this->CI->load->helper('twilio');
548552
}
549-
553+
550554
switch ($type)
551555
{
552556
case 'dynamic':
@@ -560,7 +564,7 @@ function add_css($style, $type = 'link', $media = FALSE)
560564
break;
561565

562566
case 'link':
563-
$filepath = (preg_match('|https?://|', $style) ? $style : $filepath);
567+
$filepath = (preg_match('|https?://|', $style) ? $style : site_url().$style);
564568
$css = '<link type="text/css" rel="stylesheet" href="'.version_url($filepath).'"';
565569
if ($media)
566570
{

0 commit comments

Comments
 (0)