1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Gidkom\OpenFireRestApi; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Client; |
6
|
|
|
|
7
|
|
|
class OpenFireRestApi |
8
|
|
|
{ |
9
|
|
|
public $host = 'localhost'; |
10
|
|
|
public $port = '9090'; |
11
|
|
|
public $plugin = '/plugins/restapi/v1'; |
12
|
|
|
public $secret = 'SuperSecret'; |
13
|
|
|
public $useSSL = false; |
14
|
|
|
protected $params = array(); |
15
|
|
|
public $client; |
16
|
|
|
public $bcastRoles = array(); |
17
|
|
|
public $useBasicAuth = false; |
18
|
|
|
public $basicUser = 'admin'; |
19
|
|
|
public $basicPwd = '1234'; |
20
|
|
|
|
21
|
|
|
public function __construct() |
22
|
|
|
{ |
23
|
|
|
$this->client = new Client(); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Make the request and analyze the result |
28
|
|
|
* |
29
|
|
|
* @param string $type Request method |
30
|
|
|
* @param string $endpoint Api request endpoint |
31
|
|
|
* @param array $params Parameters |
32
|
|
|
* @return array|false Array with data or error, or False when something went fully wrong |
33
|
|
|
*/ |
34
|
|
|
|
35
|
|
|
protected function doRequest($type, $endpoint, $params=array()) |
36
|
|
|
{ |
37
|
|
|
$base = ($this->useSSL) ? "https" : "http"; |
38
|
|
|
$url = $base . "://" . $this->host . ":" .$this->port.$this->plugin.$endpoint; |
39
|
|
|
|
40
|
|
|
if ($this->useBasicAuth) |
41
|
|
|
$auth = 'Basic ' . base64_encode($this->basicUser . ':' . $this->basicPwd); |
42
|
|
|
else |
43
|
|
|
$auth = $this->secret; |
44
|
|
|
|
45
|
|
|
$headers = array( |
46
|
|
|
'Accept' => 'application/json', |
47
|
|
|
'Authorization' => $auth |
48
|
|
|
); |
49
|
|
|
|
50
|
|
|
$body = json_encode($params); |
51
|
|
|
|
52
|
|
|
switch ($type) { |
53
|
|
|
case 'get': |
54
|
|
|
$result = $this->client->get($url, compact('headers')); |
55
|
|
|
break; |
56
|
|
View Code Duplication |
case 'post': |
57
|
|
|
$headers += ['Content-Type'=>'application/json']; |
58
|
|
|
$result = $this->client->post($url, compact('headers','body')); |
59
|
|
|
break; |
60
|
|
View Code Duplication |
case 'delete': |
61
|
|
|
$headers += ['Content-Type'=>'application/json']; |
62
|
|
|
$result = $this->client->delete($url, compact('headers','body')); |
63
|
|
|
break; |
64
|
|
View Code Duplication |
case 'put': |
65
|
|
|
$headers += ['Content-Type'=>'application/json']; |
66
|
|
|
$result = $this->client->put($url, compact('headers','body')); |
67
|
|
|
break; |
68
|
|
|
default: |
69
|
|
|
$result = null; |
70
|
|
|
break; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
if ($result->getStatusCode() == 200 || $result->getStatusCode() == 201) { |
74
|
|
|
return array('status'=>true, 'message'=>json_decode($result->getBody())); |
75
|
|
|
} |
76
|
|
|
return array('status'=>false, 'message'=>json_decode($result->getBody())); |
77
|
|
|
|
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Get all registered users |
83
|
|
|
* |
84
|
|
|
* @return json|false Json with data or error, or False when something went fully wrong |
85
|
|
|
*/ |
86
|
|
|
public function getUsers() |
87
|
|
|
{ |
88
|
|
|
$endpoint = '/users'; |
89
|
|
|
return $this->doRequest('get',$endpoint); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Get information for a specified user |
95
|
|
|
* |
96
|
|
|
* @return json|false Json with data or error, or False when something went fully wrong |
97
|
|
|
*/ |
98
|
|
|
public function getUser($username) |
99
|
|
|
{ |
100
|
|
|
$endpoint = '/users/'.$username; |
101
|
|
|
return $this->doRequest('get', $endpoint); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Creates a new OpenFire user |
107
|
|
|
* |
108
|
|
|
* @param string $username Username |
109
|
|
|
* @param string $password Password |
110
|
|
|
* @param string|false $name Name (Optional) |
111
|
|
|
* @param string|false $email Email (Optional) |
112
|
|
|
* @param string[]|false $groups Groups (Optional) |
113
|
|
|
* @return json|false Json with data or error, or False when something went fully wrong |
114
|
|
|
*/ |
115
|
|
|
public function addUser($username, $password, $name=false, $email=false, $groups=false) |
116
|
|
|
{ |
117
|
|
|
$endpoint = '/users'; |
118
|
|
|
return $this->doRequest('post', $endpoint, compact('username', 'password','name','email', 'groups')); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Deletes an OpenFire user |
124
|
|
|
* |
125
|
|
|
* @param string $username Username |
126
|
|
|
* @return json|false Json with data or error, or False when something went fully wrong |
127
|
|
|
*/ |
128
|
|
|
public function deleteUser($username) |
129
|
|
|
{ |
130
|
|
|
$endpoint = '/users/'.$username; |
131
|
|
|
return $this->doRequest('delete', $endpoint); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Updates an OpenFire user |
136
|
|
|
* |
137
|
|
|
* @param string $username Username |
138
|
|
|
* @param string|false $password Password (Optional) |
139
|
|
|
* @param string|false $name Name (Optional) |
140
|
|
|
* @param string|false $email Email (Optional) |
141
|
|
|
* @param string[]|false $groups Groups (Optional) |
142
|
|
|
* @return json|false Json with data or error, or False when something went fully wrong |
143
|
|
|
*/ |
144
|
|
|
public function updateUser($username, $password, $name=false, $email=false, $groups=false) |
145
|
|
|
{ |
146
|
|
|
$endpoint = '/users/'.$username; |
147
|
|
|
return $this->doRequest('put', $endpoint, compact('username', 'password','name','email', 'groups')); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* locks/Disables an OpenFire user |
152
|
|
|
* |
153
|
|
|
* @param string $username Username |
154
|
|
|
* @return json|false Json with data or error, or False when something went fully wrong |
155
|
|
|
*/ |
156
|
|
|
public function lockoutUser($username) |
157
|
|
|
{ |
158
|
|
|
$endpoint = '/lockouts/'.$username; |
159
|
|
|
return $this->doRequest('post', $endpoint); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* unlocks an OpenFire user |
165
|
|
|
* |
166
|
|
|
* @param string $username Username |
167
|
|
|
* @return json|false Json with data or error, or False when something went fully wrong |
168
|
|
|
*/ |
169
|
|
|
public function unlockUser($username) |
170
|
|
|
{ |
171
|
|
|
$endpoint = '/lockouts/'.$username; |
172
|
|
|
return $this->doRequest('delete', $endpoint); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Adds to this OpenFire user's roster |
178
|
|
|
* |
179
|
|
|
* @param string $username Username |
180
|
|
|
* @param string $jid JID |
181
|
|
|
* @param string|false $name Name (Optional) |
182
|
|
|
* @param int|false $subscriptionType Subscription (Optional) |
183
|
|
|
* @return json|false Json with data or error, or False when something went fully wrong |
184
|
|
|
*/ |
185
|
|
|
public function addToRoster($username, $jid, $name=false, $subscriptionType=false) |
186
|
|
|
{ |
187
|
|
|
$endpoint = '/users/'.$username.'/roster'; |
188
|
|
|
return $this->doRequest('post', $endpoint, compact('jid','name','subscriptionType')); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Removes from this OpenFire user's roster |
194
|
|
|
* |
195
|
|
|
* @param string $username Username |
196
|
|
|
* @param string $jid JID |
197
|
|
|
* @return json|false Json with data or error, or False when something went fully wrong |
198
|
|
|
*/ |
199
|
|
|
public function deleteFromRoster($username, $jid) |
200
|
|
|
{ |
201
|
|
|
$endpoint = '/users/'.$username.'/roster/'.$jid; |
202
|
|
|
return $this->doRequest('delete', $endpoint, $jid); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* Updates this OpenFire user's roster |
207
|
|
|
* |
208
|
|
|
* @param string $username Username |
209
|
|
|
* @param string $jid JID |
210
|
|
|
* @param string|false $nickname Nick Name (Optional) |
211
|
|
|
* @param int|false $subscriptionType Subscription (Optional) |
212
|
|
|
* @return json|false Json with data or error, or False when something went fully wrong |
213
|
|
|
*/ |
214
|
|
|
public function updateRoster($username, $jid, $nickname=false, $subscriptionType=false) |
|
|
|
|
215
|
|
|
{ |
216
|
|
|
$endpoint = '/users/'.$username.'/roster/'.$jid; |
217
|
|
|
return $this->doRequest('put', $endpoint, $jid, compact('jid','username','subscriptionType')); |
|
|
|
|
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* Get all groups |
222
|
|
|
* |
223
|
|
|
* @return json|false Json with data or error, or False when something went fully wrong |
224
|
|
|
*/ |
225
|
|
|
public function getGroups() |
226
|
|
|
{ |
227
|
|
|
$endpoint = '/groups'; |
228
|
|
|
return $this->doRequest('get', $endpoint); |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* Retrieve a group |
233
|
|
|
* |
234
|
|
|
* @param string $name Name of group |
235
|
|
|
* @return json|false Json with data or error, or False when something went fully wrong |
236
|
|
|
*/ |
237
|
|
|
public function getGroup($name) |
238
|
|
|
{ |
239
|
|
|
$endpoint = '/groups/'.$name; |
240
|
|
|
return $this->doRequest('get', $endpoint); |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* Create a group |
245
|
|
|
* |
246
|
|
|
* @param string $name Name of the group |
247
|
|
|
* @param string $description Some description of the group |
248
|
|
|
* |
249
|
|
|
* @return json|false Json with data or error, or False when something went fully wrong |
250
|
|
|
*/ |
251
|
|
|
public function createGroup($name, $description = false) |
252
|
|
|
{ |
253
|
|
|
$endpoint = '/groups/'; |
254
|
|
|
return $this->doRequest('post', $endpoint, compact('name','description')); |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* Delete a group |
259
|
|
|
* |
260
|
|
|
* @param string $name Name of the Group to delete |
261
|
|
|
* @return json|false Json with data or error, or False when something went fully wrong |
262
|
|
|
*/ |
263
|
|
|
public function deleteGroup($name) |
264
|
|
|
{ |
265
|
|
|
$endpoint = '/groups/'.$name; |
266
|
|
|
return $this->doRequest('delete', $endpoint); |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
/** |
270
|
|
|
* Update a group (description) |
271
|
|
|
* |
272
|
|
|
* @param string $name Name of group |
273
|
|
|
* @param string $description Some description of the group |
274
|
|
|
* |
275
|
|
|
*/ |
276
|
|
|
public function updateGroup($name, $description) |
277
|
|
|
{ |
278
|
|
|
$endpoint = '/groups/'.$name; |
279
|
|
|
return $this->doRequest('put', $endpoint, compact('name','description')); |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* Gell all active sessions |
284
|
|
|
* |
285
|
|
|
* @return json|false Json with data or error, or False when something went fully wrong |
286
|
|
|
*/ |
287
|
|
|
public function getSessions() |
288
|
|
|
{ |
289
|
|
|
$endpoint = '/sessions'; |
290
|
|
|
return $this->doRequest('get', $endpoint); |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
public function getChatRoom($name) |
294
|
|
|
{ |
295
|
|
|
return $this->doRequest('get', '/chatrooms/'.$name); |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
public function getAllChatRooms() |
299
|
|
|
{ |
300
|
|
|
return $this->doRequest('get', '/chatrooms?type=all'); |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
public function createChatRoom($naturalName, $roomName, $description, $maxUsers = '30', $persistent = 'false', $publicRoom = 'false') |
304
|
|
|
{ |
305
|
|
|
$broadcastPresenceRoles = $this->bcastRoles; |
306
|
|
|
return $this->doRequest('post', '/chatrooms', compact('naturalName', 'roomName', 'description', 'maxUsers', 'persistent', 'publicRoom', 'broadcastPresenceRoles')); |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
public function deleteChatRoom($name) |
310
|
|
|
{ |
311
|
|
|
return $this->doRequest('delete', '/chatrooms/'.$name); |
312
|
|
|
} |
313
|
|
|
} |
314
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.