-
Notifications
You must be signed in to change notification settings - Fork 17
/
functions.inc.php
268 lines (227 loc) · 6.12 KB
/
functions.inc.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
<?php
// License for all code of this FreePBX module can be found in the license file inside the module directory
// Copyright 2013 Schmooze Com Inc.
//
$dir = dirname(__FILE__);
include($dir . '/functions.inc/api.class.php');
include($dir . '/functions.inc/auth.class.php');
include($dir . '/functions.inc/logger.class.php');
include($dir . '/functions.inc/logger.functions.php');
include($dir . '/functions.inc/router.class.php');
include($dir . '/functions.inc/user.functions.php');
include($dir . '/functions.inc/helper.functions.php');
function restapi_opts_get() {
global $db;
$sql = 'SELECT * FROM restapi_general';
$ret = $db->getAll($sql, DB_FETCHMODE_ASSOC);
db_e($ret);
foreach ($ret as $r) {
$res[$r['keyword']] = $r['value'];
}
$vars = array(
'allow' => '',
'deny' => '',
'status' => 'enabled',
'token' => '',
'tokenkey' => '',
'logging' => 'disabled'
);
foreach ($vars as $k => $v) {
$vars[$k] = isset($res[$k]) ? $res[$k] : $v;
}
return $vars;
}
function restapi_opts_put($vars) {
global $db;
//never accept a new token or key
$orig = restapi_opts_get();
if ($orig['token'] && $orig['tokenkey']) {
$vars['token'] = $orig['token'];
$vars['tokenkey'] = $orig['tokenkey'];
}
foreach ($vars as $k => $v) {
switch ($k) {
case 'status':
case 'token':
case 'tokenkey':
case 'logging':
$data[] = array($k, $v);
break;
}
}
$sql = $db->prepare('REPLACE INTO restapi_general (keyword, value) VALUES (?, ?)');
$ret = $db->executeMultiple($sql, $data);
db_e($ret);
return true;
}
/**
* Get token data
* @param mixed - id to filter by, leave blank for default/generic data
* @param options string - type to stort by
* defaults to id, can be set to token for single entry lookups
*/
function restapi_tokens_get($id = '', $type = 'id') {
global $db;
switch ($id) {
case 'all':
case 'all_detailed':
$tokens = array();
$ret = sql('SELECT * FROM restapi_tokens', 'getAll', DB_FETCHMODE_ASSOC);
foreach ($ret as $r) {
if ($id == 'all_detailed') {
$tokens[$r['id']] = restapi_tokens_get($r['id']);
} else {
$tokens[$r['id']] = $r;
}
}
return $tokens;
break;
default:
if ($type == 'token') {
$sql = 'SELECT token_id FROM restapi_token_details WHERE `key` = "token" AND value = ?';
$id = $db->getOne($sql, array($id));
db_e($id);
if (!$id) {
return false;
}
}
//if called with no id, we might be after a blank dataset. No need to hit the db to
//find out that a blank string doesnt exists :)
if ($id) {
$sql = 'SELECT * FROM restapi_tokens WHERE id = ?';
$ret = $db->getAll($sql, array($id), DB_FETCHMODE_ASSOC);
db_e($ret);
if (count($ret)) {
$ret = $ret[0];
}
if ($ret) {
$sql = 'SELECT * FROM restapi_token_details WHERE token_id = ?';
$ret2 = $db->getAll($sql, array($id), DB_FETCHMODE_ASSOC);
db_e($ret2);
if (count($ret2)) {
foreach ($ret2 as $r) {
$ret[$r['key']] = $r['value'];
}
}
}
//get associated user
$ret['assoc_user'] = restapi_user_get_token_user($id);
}
$vars = array(
'allow' => json_encode(array('0.0.0.0')),
'assoc_user' => '',
'deny' => json_encode(array('0.0.0.0')),
'desc' => '',
'id' => '',
'modules' => array(),
'name' => '',
'rate' => '1000',
'token' => '',
'tokenkey' => '',
'token_status' => 'enabled',
'users' => ''
);
foreach ($vars as $k => $v) {
$vars[$k] = isset($ret[$k]) ? $ret[$k] : $v;
}
//decode strings stored as json
$decode = array(
'allow',
'deny',
'users',
'modules'
);
foreach ($decode as $d) {
$vars[$d] = isset($ret[$d]) ? json_decode($ret[$d], true) : array();
}
return $vars;
break;
}
}
function restapi_tokens_put($vars) {
global $db, $amp_conf;
//reuse old token/key on non-new tokens
if ($vars['id']) {
$orig = restapi_tokens_get($vars['id']);
$vars['token'] = $orig['token'];
$vars['tokenkey'] = $orig['tokenkey'];
}
if($vars['id'] == ''){
$vars['id'] = NULL;
}
//insert headers
$sql = 'REPLACE INTO restapi_tokens (id, name, `desc`) VALUES (?, ?, ?)';
$ret = $db->query($sql, array($vars['id'], $vars['name'], $vars['desc']));
db_e($ret);
//get an id if we dont alredy have one
if (empty($vars['id'])) {
$vars['id'] = $db->getOne(
($amp_conf["AMPDBENGINE"] == "sqlite3")
? 'SELECT last_insert_rowid()'
: 'SELECT LAST_INSERT_ID()'
);
}
//clear stale data
$sql = 'DELETE FROM restapi_token_details WHERE token_id = ?';
$ret = $db->query($sql, array($vars['id']));
//dbug($vars['id'], $db->last_query);
db_e($ret);
//insert fresh values
foreach ($vars as $k => $v) {
switch ($k) {
case 'allow':
case 'deny':
//TODO: validate ip's
$data[] = array($vars['id'], $k, json_encode($v));
break;
case 'users':
//TODO: validate that users really exist
$data[] = array($vars['id'], $k, json_encode($v));
break;
case 'modules':
//TODO: validate that modules really exist
$modules = is_array($v) ? $v : array();
if (in_array('*', $modules)) {
$modules = array('*');
}
$data[] = array($vars['id'], $k, json_encode($modules));
break;
case 'token':
case 'tokenkey':
case 'token_status':
case 'rate':
$data[] = array($vars['id'], $k, $v);
break;
default:
break;
}
}
//insert fresh data
$sql = $db->prepare('INSERT INTO restapi_token_details (token_id, `key`, value) VALUES (?, ?, ?)');
$ret = $db->executeMultiple($sql, $data);
db_e($ret);
//update user mappings
if ($vars['assoc_user']) {
restapi_user_set_token($vars['assoc_user'], $vars['id']);
}
return $vars['id'];
}
function restapi_tokens_del($id) {
global $db;
//delete token
$sql = 'DELETE FROM restapi_tokens WHERE id = ?';
$ret = $db->query($sql, array($id));
db_e($ret);
$sql = 'DELETE FROM restapi_token_details WHERE token_id = ?';
$ret = $db->query($sql, array($id));
db_e($ret);
//delete user mapping
restapi_user_del_token($id);
return '';
}
/**
* @return a token
*/
function restapi_tokens_generate() {
return sha1(microtime(true) . mt_rand(1000,9999999));
}