|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* Copyright 2014 Brian Smith <[email protected]>. |
|
5
|
|
|
* |
|
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
7
|
|
|
* you may not use this file except in compliance with the License. |
|
8
|
|
|
* You may obtain a copy of the License at |
|
9
|
|
|
* |
|
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
|
11
|
|
|
* |
|
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
|
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
|
14
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
15
|
|
|
* See the License for the specific language governing permissions and |
|
16
|
|
|
* limitations under the License. |
|
17
|
|
|
*/ |
|
18
|
|
|
|
|
19
|
|
|
namespace phparia\Api; |
|
20
|
|
|
|
|
21
|
|
|
use GuzzleHttp\Exception\BadResponseException; |
|
22
|
|
|
use GuzzleHttp\Exception\RequestException; |
|
23
|
|
|
use phparia\Client\AriClientAware; |
|
24
|
|
|
use phparia\Exception\ConflictException; |
|
25
|
|
|
use phparia\Exception\ForbiddenException; |
|
26
|
|
|
use phparia\Exception\NotFoundException; |
|
27
|
|
|
use phparia\Resources\AsteriskInfo; |
|
28
|
|
|
use phparia\Resources\ConfigTuple; |
|
29
|
|
|
use phparia\Resources\LogChannel; |
|
30
|
|
|
use phparia\Resources\Module; |
|
31
|
|
|
use phparia\Resources\Variable; |
|
32
|
|
|
use phparia\Exception\InvalidParameterException; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Asterisk API |
|
36
|
|
|
* |
|
37
|
|
|
* @author Brian Smith <[email protected]> |
|
38
|
|
|
*/ |
|
39
|
|
|
class Asterisk extends AriClientAware |
|
40
|
|
|
{ |
|
41
|
|
|
const INFO_BUILD = 'build'; |
|
42
|
|
|
const INFO_SYSTEM = 'system'; |
|
43
|
|
|
const INFO_CONFIG = 'config'; |
|
44
|
|
|
const INFO_STATUS = 'status'; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Retrieve a dynamic configuration object. |
|
48
|
|
|
* |
|
49
|
|
|
* @param string $configClass The configuration class containing dynamic configuration objects. |
|
50
|
|
|
* @param string $objectType The type of configuration object to retrieve. |
|
51
|
|
|
* @param string $id The unique identifier of the object to retrieve. |
|
52
|
|
|
* @return ConfigTuple[] |
|
53
|
|
|
* @throws NotFoundException |
|
54
|
|
|
*/ |
|
55
|
|
View Code Duplication |
public function getObject($configClass, $objectType, $id) |
|
56
|
|
|
{ |
|
57
|
|
|
$uri = "asterisk/config/dynamic/$configClass/$objectType/$id"; |
|
58
|
|
|
|
|
59
|
|
|
try { |
|
60
|
|
|
$response = $this->client->getEndpoint()->get($uri); |
|
61
|
|
|
} catch (RequestException $e) { |
|
62
|
|
|
$this->processRequestException($e); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
$configTuples = []; |
|
66
|
|
|
foreach (\GuzzleHttp\json_decode($response->getBody()) as $configTuple) { |
|
67
|
|
|
$configTuples[] = new ConfigTuple($configTuple); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
return $configTuples; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Create or update a dynamic configuration object. |
|
75
|
|
|
* |
|
76
|
|
|
* @param string $configClass The configuration class containing dynamic configuration objects. |
|
77
|
|
|
* @param string $objectType The type of configuration object to retrieve. |
|
78
|
|
|
* @param string $id The unique identifier of the object to retrieve. |
|
79
|
|
|
* @param ConfigTuple[] $fields The body object should have a value that is a list of ConfigTuples, which provide the fields to update. Ex. [ { "attribute": "directmedia", "value": "false" } ] |
|
80
|
|
|
* @return ConfigTuple[] |
|
81
|
|
|
* @throws BadResponseException |
|
82
|
|
|
* @throws ForbiddenException |
|
83
|
|
|
* @throws NotFoundException |
|
84
|
|
|
*/ |
|
85
|
|
|
public function updateObject($configClass, $objectType, $id, $fields) |
|
86
|
|
|
{ |
|
87
|
|
|
$uri = "asterisk/config/dynamic/$configClass/$objectType/$id"; |
|
88
|
|
|
|
|
89
|
|
|
try { |
|
90
|
|
|
$response = $this->client->getEndpoint()->put($uri, [ |
|
91
|
|
|
'form_params' => [ |
|
92
|
|
|
'fields' => array_map('strval', $fields) |
|
93
|
|
|
] |
|
94
|
|
|
]); |
|
95
|
|
|
} catch (RequestException $e) { |
|
96
|
|
|
$this->processRequestException($e); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
$configTuples = []; |
|
100
|
|
|
foreach (\GuzzleHttp\json_decode($response->getBody()) as $configTuple) { |
|
101
|
|
|
$configTuples[] = new ConfigTuple($configTuple); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
return $configTuples; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Delete a dynamic configuration object. |
|
109
|
|
|
* |
|
110
|
|
|
* @param string $configClass The configuration class containing dynamic configuration objects. |
|
111
|
|
|
* @param string $objectType The type of configuration object to retrieve. |
|
112
|
|
|
* @param string $id The unique identifier of the object to retrieve. |
|
113
|
|
|
* @throws ForbiddenException |
|
114
|
|
|
* @throws NotFoundException |
|
115
|
|
|
*/ |
|
116
|
|
|
public function deleteObject($configClass, $objectType, $id) |
|
117
|
|
|
{ |
|
118
|
|
|
$uri = "asterisk/config/dynamic/$configClass/$objectType/$id"; |
|
119
|
|
|
|
|
120
|
|
|
try { |
|
121
|
|
|
$this->client->getEndpoint()->delete($uri); |
|
122
|
|
|
} catch (RequestException $e) { |
|
123
|
|
|
$this->processRequestException($e); |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* Gets Asterisk system information. |
|
129
|
|
|
* |
|
130
|
|
|
* @param string $only Filter information returned. Allows comma separated values. Allowed values: build, system, config, status. |
|
131
|
|
|
* @return AsteriskInfo |
|
132
|
|
|
*/ |
|
133
|
2 |
|
public function getInfo($only = null) |
|
134
|
|
|
{ |
|
135
|
2 |
|
if (empty($only)) { |
|
136
|
1 |
|
$uri = 'asterisk/info'; |
|
137
|
1 |
|
} else { |
|
138
|
1 |
|
$uri = "asterisk/info?only=$only"; |
|
139
|
|
|
} |
|
140
|
2 |
|
$response = $this->client->getEndpoint()->get($uri); |
|
141
|
|
|
|
|
142
|
|
|
return new AsteriskInfo(\GuzzleHttp\json_decode($response->getBody())); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* List Asterisk modules. |
|
147
|
|
|
* |
|
148
|
|
|
* @return Module[] |
|
149
|
|
|
*/ |
|
150
|
|
View Code Duplication |
public function listModules() |
|
151
|
|
|
{ |
|
152
|
|
|
$uri = 'asterisk/modules'; |
|
153
|
|
|
|
|
154
|
|
|
$response = $this->client->getEndpoint()->get($uri); |
|
155
|
|
|
|
|
156
|
|
|
$modules = []; |
|
157
|
|
|
foreach (\GuzzleHttp\json_decode($response->getBody()) as $module) { |
|
158
|
|
|
$modules[] = new Module($module); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
return $modules; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* Get Asterisk module information. |
|
166
|
|
|
* |
|
167
|
|
|
* @param string $moduleName Module's name |
|
168
|
|
|
* @return Module |
|
169
|
|
|
* @throws NotFoundException |
|
170
|
|
|
* @throws ConflictException |
|
171
|
|
|
*/ |
|
172
|
|
View Code Duplication |
public function getModule($moduleName) |
|
173
|
|
|
{ |
|
174
|
|
|
$uri = "asterisk/module/$moduleName"; |
|
175
|
|
|
|
|
176
|
|
|
$response = $this->client->getEndpoint()->get($uri); |
|
177
|
|
|
|
|
178
|
|
|
return new Module(\GuzzleHttp\json_decode($response->getBody())); |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* Load an Asterisk module. |
|
183
|
|
|
* |
|
184
|
|
|
* @param string $moduleName Module's name |
|
185
|
|
|
* @return Module |
|
186
|
|
|
* @throws ConflictException |
|
187
|
|
|
*/ |
|
188
|
|
|
public function loadModule($moduleName) |
|
189
|
|
|
{ |
|
190
|
|
|
$uri = "asterisk/module/$moduleName"; |
|
191
|
|
|
|
|
192
|
|
|
$this->client->getEndpoint()->post($uri); |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
/** |
|
196
|
|
|
* Unload an Asterisk module. |
|
197
|
|
|
* |
|
198
|
|
|
* @param string $moduleName Module's name |
|
199
|
|
|
* @return Module |
|
200
|
|
|
* @throws NotFoundException |
|
201
|
|
|
* @throws ConflictException |
|
202
|
|
|
*/ |
|
203
|
|
|
public function unloadModule($moduleName) |
|
204
|
|
|
{ |
|
205
|
|
|
$uri = "asterisk/module/$moduleName"; |
|
206
|
|
|
|
|
207
|
|
|
$this->client->getEndpoint()->delete($uri); |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
/** |
|
211
|
|
|
* Reload an Asterisk module. |
|
212
|
|
|
* |
|
213
|
|
|
* @param string $moduleName Module's name |
|
214
|
|
|
* @return Module |
|
215
|
|
|
* @throws NotFoundException |
|
216
|
|
|
* @throws ConflictException |
|
217
|
|
|
*/ |
|
218
|
|
|
public function reloadModule($moduleName) |
|
219
|
|
|
{ |
|
220
|
|
|
$uri = "asterisk/module/$moduleName"; |
|
221
|
|
|
|
|
222
|
|
|
$this->client->getEndpoint()->put($uri); |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
/** |
|
226
|
|
|
* Gets Asterisk log channel information. |
|
227
|
|
|
* |
|
228
|
|
|
* @return LogChannel[] |
|
229
|
|
|
*/ |
|
230
|
|
View Code Duplication |
public function listLogChannels() |
|
231
|
|
|
{ |
|
232
|
|
|
$uri = 'asterisk/logging'; |
|
233
|
|
|
|
|
234
|
|
|
$response = $this->client->getEndpoint()->get($uri); |
|
235
|
|
|
|
|
236
|
|
|
$logChannels = []; |
|
237
|
|
|
foreach (\GuzzleHttp\json_decode($response->getBody()) as $logChannel) { |
|
238
|
|
|
$logChannels[] = new Module($logChannel); |
|
239
|
|
|
} |
|
240
|
|
|
|
|
241
|
|
|
return $logChannels; |
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
|
|
/** |
|
245
|
|
|
* Adds a log channel. |
|
246
|
|
|
* |
|
247
|
|
|
* @param string $logChannelName |
|
248
|
|
|
* @param string $configuration |
|
249
|
|
|
* @throws InvalidParameterException |
|
250
|
|
|
* @throws ConflictException |
|
251
|
|
|
*/ |
|
252
|
|
|
public function addLog($logChannelName, $configuration) |
|
253
|
|
|
{ |
|
254
|
|
|
$uri = "asterisk/logging/$logChannelName"; |
|
255
|
|
|
|
|
256
|
|
|
try { |
|
257
|
|
|
$this->client->getEndpoint()->post($uri, [ |
|
258
|
|
|
'form_params' => [ |
|
259
|
|
|
'configuration' => $configuration |
|
260
|
|
|
] |
|
261
|
|
|
]); |
|
262
|
|
|
} catch (RequestException $e) { |
|
263
|
|
|
$this->processRequestException($e); |
|
264
|
|
|
} |
|
265
|
|
|
} |
|
266
|
|
|
|
|
267
|
|
|
/** |
|
268
|
|
|
* Deletes a log channel. |
|
269
|
|
|
* |
|
270
|
|
|
* @param string $logChannelName |
|
271
|
|
|
* @throws NotFoundException |
|
272
|
|
|
*/ |
|
273
|
|
|
public function deleteLog($logChannelName) |
|
274
|
|
|
{ |
|
275
|
|
|
$uri = "asterisk/logging/$logChannelName"; |
|
276
|
|
|
|
|
277
|
|
|
try { |
|
278
|
|
|
$this->client->getEndpoint()->delete($uri); |
|
279
|
|
|
} catch (RequestException $e) { |
|
280
|
|
|
$this->processRequestException($e); |
|
281
|
|
|
} |
|
282
|
|
|
} |
|
283
|
|
|
|
|
284
|
|
|
/** |
|
285
|
|
|
* Rotates a log channel. |
|
286
|
|
|
* |
|
287
|
|
|
* @param string $logChannelName |
|
288
|
|
|
* @throws NotFoundException |
|
289
|
|
|
*/ |
|
290
|
|
|
public function rotateLog($logChannelName) |
|
291
|
|
|
{ |
|
292
|
|
|
$uri = "asterisk/logging/$logChannelName"; |
|
293
|
|
|
|
|
294
|
|
|
try { |
|
295
|
|
|
$this->client->getEndpoint()->put($uri); |
|
296
|
|
|
} catch (RequestException $e) { |
|
297
|
|
|
$this->processRequestException($e); |
|
298
|
|
|
} |
|
299
|
|
|
} |
|
300
|
|
|
|
|
301
|
|
|
/** |
|
302
|
|
|
* Get the value of a global variable. |
|
303
|
|
|
* |
|
304
|
|
|
* @param string $variable (required) The variable to get |
|
305
|
|
|
* @return Variable |
|
306
|
|
|
* @throws InvalidParameterException |
|
307
|
|
|
*/ |
|
308
|
1 |
View Code Duplication |
public function getGlobalVar($variable) |
|
309
|
|
|
{ |
|
310
|
1 |
|
$uri = "asterisk/variable?variable=$variable"; |
|
311
|
|
|
|
|
312
|
|
|
try { |
|
313
|
1 |
|
$response = $this->client->getEndpoint()->get($uri); |
|
314
|
1 |
|
} catch (RequestException $e) { |
|
315
|
1 |
|
$this->processRequestException($e); |
|
316
|
|
|
} |
|
317
|
|
|
|
|
318
|
|
|
return new Variable(\GuzzleHttp\json_decode($response->getBody())); |
|
319
|
|
|
} |
|
320
|
|
|
|
|
321
|
|
|
/** |
|
322
|
|
|
* Get the value of a global variable. |
|
323
|
|
|
* |
|
324
|
|
|
* @param string $variable (required) The variable to get |
|
325
|
|
|
* @return Variable |
|
326
|
|
|
* @throws InvalidParameterException |
|
327
|
|
|
* @deprecated |
|
328
|
|
|
*/ |
|
329
|
1 |
|
public function getVariable($variable) |
|
330
|
|
|
{ |
|
331
|
1 |
|
return $this->getGlobalVar($variable); |
|
332
|
|
|
} |
|
333
|
|
|
|
|
334
|
|
|
/** |
|
335
|
|
|
* Set the value of a global variable. |
|
336
|
|
|
* |
|
337
|
|
|
* @param string $variable (required) The variable to set |
|
338
|
|
|
* @param string $value The value to set the variable to |
|
339
|
|
|
* @throws InvalidParameterException |
|
340
|
|
|
*/ |
|
341
|
1 |
|
public function setGlobalVar($variable, $value = null) |
|
342
|
|
|
{ |
|
343
|
1 |
|
$uri = 'asterisk/variable'; |
|
344
|
|
|
|
|
345
|
|
|
try { |
|
346
|
1 |
|
$this->client->getEndpoint()->post($uri, [ |
|
347
|
|
|
'form_params' => [ |
|
348
|
1 |
|
'variable' => $variable, |
|
349
|
|
|
'value' => $value |
|
350
|
1 |
|
] |
|
351
|
1 |
|
]); |
|
352
|
1 |
|
} catch (RequestException $e) { |
|
353
|
1 |
|
$this->processRequestException($e); |
|
354
|
|
|
} |
|
355
|
|
|
} |
|
356
|
|
|
|
|
357
|
|
|
/** |
|
358
|
|
|
* Set the value of a global variable. |
|
359
|
|
|
* |
|
360
|
|
|
* @param string $variable (required) The variable to set |
|
361
|
|
|
* @param string $value The value to set the variable to |
|
362
|
|
|
* @throws InvalidParameterException |
|
363
|
|
|
* @deprecated |
|
364
|
|
|
*/ |
|
365
|
1 |
|
public function setVariable($variable, $value) |
|
366
|
|
|
{ |
|
367
|
1 |
|
$this->setGlobalVar($variable, $value); |
|
368
|
|
|
} |
|
369
|
|
|
} |
|
370
|
|
|
|