forked from greenfieldtech-nirs/phpari
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphpari.php
312 lines (275 loc) · 12.4 KB
/
phpari.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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
<?php
/**
* phpari - A PHP Class Library for interfacing with Asterisk(R) ARI
* Copyright (C) 2014 Nir Simionovich
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
* Also add information on how to contact you by electronic and paper mail.
*
* Greenfield Technologies Ltd., hereby disclaims all copyright interest in
* the library `phpari' (a library for creating smart telephony applications)
* written by Nir Simionovich and its respective list of contributors.
*/
class phpari
{
private $applications;
private $asterisk;
private $bridges;
private $channels;
private $devicestates;
private $endpoints;
private $events;
private $mailboxes;
private $recordings;
private $sounds;
private $playbacks;
private $configuration;
public $stasisEvents;
public $stasisLogger;
public $debug;
public $logfile;
public $lasterror;
public $lasttrace;
/**
* @param null $stasisApplication
* @param string $configFile
*
* Returns an array containing 5 objects: WebSocket, Pest, EventLoopFactory, Logger, StasisEventHandler
*
*/
public function __construct($stasisApplication = NULL, $configFile = "phpari.ini")
{
try {
/* Get our configuration */
$this->configuration = (object)parse_ini_file($configFile, TRUE);
/* Some general information */
$this->debug = $this->configuration->general['debug'];
$this->logfile = $this->configuration->general['logfile'];
/* Connect to ARI server */
$result = $this->connect($this->configuration->asterisk_ari['username'],
$this->configuration->asterisk_ari['password'],
$stasisApplication,
$this->configuration->asterisk_ari['host'],
$this->configuration->asterisk_ari['port'],
$this->configuration->asterisk_ari['endpoint'],
$this->configuration->asterisk_ari['transport']);
return $result;
} catch (Exception $e) {
die("Exception raised: " . $e->getMessage() . "\nFile: " . $e->getFile() . "\nLine: " . $e->getLine());
}
}
/**
* This function is connecting and returning a phpari client object which
* transferred to any of the interfaces will assist with the connection process
* to the Asterisk Stasis or to the Asterisk 12 web server (Channels list , End Points list)
* etc.
*
* @param $ariUsername
* @param $ariPassword
* @param $stasisApplication
* @param $ariServer
* @param $ariPort
* @param $ariEndpoint
*
* @return array
*/
private function connect($ariUsername, $ariPassword, $stasisApplication, $ariServer, $ariPort, $ariEndpoint, $ariTransport)
{
try {
$this->ariEndpoint = new PestJSON("http://" . $ariServer . ":" . $ariPort . $ariEndpoint);
$this->ariEndpoint->setupAuth($ariUsername, $ariPassword, "basic");
$this->stasisLoop = \React\EventLoop\Factory::create();
$this->stasisLogger = new \Zend\Log\Logger();
if ($this->configuration->general['logfile'] == "console")
$this->logWriter = new Zend\Log\Writer\Stream("php://output");
else
$this->logWriter = new Zend\Log\Writer\Stream($this->configuration->general['logfile']);
$this->stasisLogger->addWriter($this->logWriter);
if ($this->debug) $this->stasisLogger->debug("Initializing WebSocket Information");
$this->stasisClient = new \Devristo\Phpws\Client\WebSocket($ariTransport . "://" . $ariServer . ":" . $ariPort . "/ari/events?api_key=" . $ariUsername . ":" . $ariPassword . "&app=" . $stasisApplication, $this->stasisLoop, $this->stasisLogger);
if ($this->debug) $this->stasisLogger->debug("Initializing Stasis Event Emitter");
$this->stasisEvents = new Evenement\EventEmitter();
return true;
//return array("stasisClient" => $this->stasisClient, "stasisLoop" => $this->stasisLoop, "stasisLogger" => $this->stasisLogger, "ariEndpoint" => $this->ariEndpoint);
} catch (Exception $e) {
die("Exception raised: " . $e->getMessage() . "\nFile: " . $e->getFile() . "\nLine: " . $e->getLine());
}
}
/**
* @return applications|bool - An applications object, or FALSE on failure
*/
public function applications()
{
try {
$this->applications = new applications($this);
if ($this->debug) $this->stasisLogger->debug("applications class had been initiated");
return $this->applications;
} catch (Exception $e) {
if ($this->debug) $this->stasisLogger->debug("applications has failed initialization");
$this->lasterror = "applications class failed to initialize properly";
return FALSE;
}
}
/**
* @return asterisk|bool - An asterisk object, or FALSE on failure
*/
public function asterisk()
{
try {
$this->asterisk = new asterisk($this);
if ($this->debug) $this->stasisLogger->debug("asterisk class had been initiated");
return $this->asterisk;
} catch (Exception $e) {
if ($this->debug) $this->stasisLogger->debug("asterisk has failed initialization");
$this->lasterror = "asterisk class failed to initialize properly";
return FALSE;
}
}
/**
* @return bridges|bool - An bridges object, or FALSE on failure
*/
public function bridges()
{
try {
$this->bridges = new bridges($this);
if ($this->debug) $this->stasisLogger->debug("bridges class had been initiated");
return $this->bridges;
} catch (Exception $e) {
if ($this->debug) $this->stasisLogger->debug("bridges has failed initialization");
$this->lasterror = "bridges class failed to initialize properly";
return FALSE;
}
}
/**
* @return channels|bool - An channels object, or FALSE on failure
*/
public function channels()
{
try {
$this->channels = new channels($this);
if ($this->debug) $this->stasisLogger->debug("channels class had been initiated");
return $this->channels;
} catch (Exception $e) {
if ($this->debug) $this->stasisLogger->debug("channels has failed initialization");
$this->lasterror = "channels class failed to initialize properly";
return FALSE;
}
}
/**
* @return devicestates|bool - An devicestates object, or FALSE on failure
*/
public function devicestates()
{
try {
$this->devicestates = new devicestates($this);
if ($this->debug) $this->stasisLogger->debug("devicestates class had been initiated");
return $this->devicestates;
} catch (Exception $e) {
if ($this->debug) $this->stasisLogger->debug("devicestates has failed initialization");
$this->lasterror = "devicestates class failed to initialize properly";
return FALSE;
}
}
/**
* @return endpoints|bool - An endpoints object, or FALSE on failure
*/
public function endpoints()
{
try {
$this->endpoints = new endpoints($this);
if ($this->debug) $this->stasisLogger->debug("endpoints class had been initiated");
return $this->endpoints;
} catch (Exception $e) {
if ($this->debug) $this->stasisLogger->debug("endpoints has failed initialization");
$this->lasterror = "endpoints class failed to initialize properly";
return FALSE;
}
}
/**
* @return events|bool - An events object, or FALSE on failure
*/
public function events()
{
try {
$this->events = new events($this);
if ($this->debug) $this->stasisLogger->debug("events class had been initiated");
return $this->events;
} catch (Exception $e) {
if ($this->debug) $this->stasisLogger->debug("events has failed initialization");
$this->lasterror = "events class failed to initialize properly";
return FALSE;
}
}
/**
* @return mailboxes|bool - An mailboxes object, or FALSE on failure
*/
public function mailboxes()
{
try {
$this->mailboxes = new mailboxes($this);
if ($this->debug) $this->stasisLogger->debug("mailboxes class had been initiated");
return $this->mailboxes;
} catch (Exception $e) {
if ($this->debug) $this->stasisLogger->debug("mailboxes has failed initialization");
$this->lasterror = "mailboxes class failed to initialize properly";
return FALSE;
}
}
/**
* @return recordings|bool - An recordings object, or FALSE on failure
*/
public function recordings()
{
try {
$this->recordings = new recordings($this);
if ($this->debug) $this->stasisLogger->debug("recordings class had been initiated");
return $this->recordings;
} catch (Exception $e) {
if ($this->debug) $this->stasisLogger->debug("recordings has failed initialization");
$this->lasterror = "recordings class failed to initialize properly";
return FALSE;
}
}
/**
* @return sounds|bool - An sounds object, or FALSE on failure
*/
public function sounds()
{
try {
$this->sounds = new sounds($this);
if ($this->debug) $this->stasisLogger->debug("sounds class had been initiated");
return $this->sounds;
} catch (Exception $e) {
if ($this->debug) $this->stasisLogger->debug("sounds has failed initialization");
$this->lasterror = "sounds class failed to initialize properly";
return FALSE;
}
}
/**
* @return playbacks|bool - An playbacks object, or FALSE on failure
*/
public function playbacks()
{
try {
$this->playbacks = new playbacks($this);
if ($this->debug) $this->stasisLogger->debug("playbacks class had been initiated");
return $this->playbacks;
} catch (Exception $e) {
if ($this->debug) $this->stasisLogger->debug("playbacks has failed initialization");
$this->lasterror = "playbacks class failed to initialize properly";
return FALSE;
}
}
}