This repository has been archived by the owner on Mar 4, 2021. It is now read-only.
/
twilio_helper.php
131 lines (116 loc) · 3.41 KB
/
twilio_helper.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<?php
/**
* "The contents of this file are subject to the Mozilla Public License
* Version 1.1 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
* License for the specific language governing rights and limitations
* under the License.
* The Original Code is OpenVBX, released June 15, 2010.
* The Initial Developer of the Original Code is Twilio Inc.
* Portions created by Twilio Inc. are Copyright (C) 2010.
* All Rights Reserved.
* Contributor(s):
**/
if (!function_exists('generate_capability_token')) {
/**
* Generate a capability token for Twilio Client
*
* @param string $allow_incoming
* @return string
*/
function generate_capability_token($rest_access, $allow_incoming = true) {
$ci =& get_instance();
$capability = new Services_Twilio_Capability($ci->twilio_sid, $ci->twilio_token);
$user_id = intval($ci->session->userdata('user_id'));
$user = VBX_user::get(array('id' => $user_id));
$params = array(
'user_id' => $user->user_id,
'rest_access' => $rest_access
);
$token = null;
try {
$capability->allowClientOutgoing($ci->application_sid, $params);
if ($allow_incoming) {
$capability->allowClientIncoming($user->id);
}
$token = $capability->generateToken(VBX_Settings::CLIENT_TOKEN_TIMEOUT);
}
catch (Exception $e) {
error_log($e->getMessage());
}
return $token;
}
}
if (!function_exists('validate_rest_request')) {
/**
* Validate that an incoming rest request is from Twilio
*
* @param string $failure_message
* @return void
*/
function validate_rest_request($failure_message = 'Could not validate this request. Goodbye.') {
if (!OpenVBX::validateRequest()) {
$response = new TwimlResponse;
$response->say($failure_message);
$response->hangup();
$response->respond();
exit;
}
}
}
if (!function_exists('clean_digits')) {
/**
* Cleans Hash and Star characters from returned Digits
* If digits only contains Hash or Star characters then
* the raw digits param is returned
*
* @param string $digits
* @return string
*/
function clean_digits($digits) {
$trimmed = str_replace(array('#', '*'), '', $digits);
return strlen($trimmed) > 0 ? $trimmed : $digits;
}
}
if (!function_exists('version_url')) {
/**
* Append the current site rev to a url to force asset reload on upgrade/change
*
* @param string $url
* @return string
*/
function version_url($url) {
if (strpos($url, 'v=') === false)
{
$ci =& get_instance();
$vers = 'v='.$ci->config->item('site_rev');
$pre = (strpos($url, '?') === false ? '?' : '&');
$url .= $pre.$vers;
}
return $url;
}
}
if (!function_exists('_deprecated_notice')) {
/**
* Throw a deprecated method warning
* Allows backwards compatibility, but with a nag
*
* @param string $method
* @param float $version
* @param string $replacement optional, but recommended
* @return void
*/
function _deprecated_notice($method, $version, $replacement = null) {
if (!is_null($replacement)) {
$message = sprintf('`%s` is deprecated since version %f. Use `%s` instead.');
}
else {
$message = sprintf('`%s` is deprecated since version %f.');
}
trigger_error($message, E_WARNING);
}
}
?>