1
|
|
|
<?php
|
2
|
|
|
/**
|
3
|
|
|
* OpenFireRestAPI is based entirely on official documentation of the REST API
|
4
|
|
|
* Plugin and you can extend it by following the directives of the documentation
|
5
|
|
|
*
|
6
|
|
|
* For the full copyright and license information, please read the LICENSE
|
7
|
|
|
* file that was distributed with this source code. For the full list of
|
8
|
|
|
* contributors, visit https://github.com/gnello/PHPOpenFireRestAPI/contributors
|
9
|
|
|
*
|
10
|
|
|
* @author Luca Agnello <[email protected]>
|
11
|
|
|
* @link https://www.igniterealtime.org/projects/openfire/plugins/restapi/readme.html
|
12
|
|
|
*/
|
13
|
|
|
|
14
|
|
|
namespace Gnello\OpenFireRestAPI\Settings;
|
15
|
|
|
|
16
|
|
|
use Gnello\OpenFireRestAPI\AuthenticationToken;
|
17
|
|
|
use Gnello\OpenFireRestAPI\Wrappers\AbstractRegistryWrapper;
|
18
|
|
|
|
19
|
|
|
/**
|
20
|
|
|
* Class Settings
|
21
|
|
|
* @package Gnello\OpenFireRestAPI\Settings
|
22
|
|
|
*/
|
23
|
|
|
class Settings extends AbstractRegistryWrapper
|
24
|
|
|
{
|
25
|
|
|
/**
|
26
|
|
|
* Default Settings
|
27
|
|
|
* Edit this section to configure your client
|
28
|
|
|
*/
|
29
|
|
|
const PLUGIN_PATH = '/plugins/restapi/v1';
|
30
|
|
|
|
31
|
|
|
/**
|
32
|
|
|
* @var Settings
|
33
|
|
|
*/
|
34
|
|
|
private static $instance;
|
35
|
|
|
|
36
|
|
|
/**
|
37
|
|
|
* Settings constructor.
|
38
|
|
|
*/
|
39
|
|
|
private function __construct() {}
|
40
|
|
|
|
41
|
|
|
/**
|
42
|
|
|
* @return Settings
|
43
|
|
|
*/
|
44
|
|
|
public static function getInstance()
|
45
|
|
|
{
|
46
|
|
|
if (is_null(self::$instance)) {
|
47
|
|
|
$settings = new Settings();
|
48
|
|
|
$settings->setPlugin(self::PLUGIN_PATH);
|
49
|
|
|
$settings->setSSL(false);
|
50
|
|
|
$settings->setDebug(false);
|
51
|
|
|
|
52
|
|
|
self::$instance = $settings;
|
53
|
|
|
}
|
54
|
|
|
|
55
|
|
|
return self::$instance;
|
56
|
|
|
}
|
57
|
|
|
|
58
|
|
|
/**
|
59
|
|
|
* @param $host
|
60
|
|
|
* @return string
|
61
|
|
|
*/
|
62
|
|
View Code Duplication |
public function setHost($host)
|
|
|
|
|
63
|
|
|
{
|
64
|
|
|
$parsed_host = parse_url($host, PHP_URL_HOST);
|
65
|
|
|
|
66
|
|
|
if (is_null($parsed_host)) {
|
67
|
|
|
$parsed_host = $host;
|
68
|
|
|
} else {
|
69
|
|
|
$scheme = parse_url($host, PHP_URL_SCHEME);
|
70
|
|
|
$this->setSSL($scheme === 'https');
|
71
|
|
|
}
|
72
|
|
|
|
73
|
|
|
return $this->set('host', $parsed_host);
|
74
|
|
|
}
|
75
|
|
|
|
76
|
|
|
/**
|
77
|
|
|
* @param $port
|
78
|
|
|
* @return string
|
79
|
|
|
*/
|
80
|
|
|
public function setPort($port)
|
81
|
|
|
{
|
82
|
|
|
return $this->set('port', $port);
|
83
|
|
|
}
|
84
|
|
|
|
85
|
|
|
/**
|
86
|
|
|
* @param $plugin
|
87
|
|
|
* @return string
|
88
|
|
|
*/
|
89
|
|
|
public function setPlugin($plugin)
|
90
|
|
|
{
|
91
|
|
|
return $this->set('plugin', $plugin);
|
92
|
|
|
}
|
93
|
|
|
|
94
|
|
|
/**
|
95
|
|
|
* @param $useSSL
|
96
|
|
|
* @return bool
|
97
|
|
|
*/
|
98
|
|
|
public function setSSL($useSSL)
|
99
|
|
|
{
|
100
|
|
|
return $this->set('useSSL', $useSSL);
|
101
|
|
|
}
|
102
|
|
|
|
103
|
|
|
/**
|
104
|
|
|
* @param $secretKey
|
105
|
|
|
* @return string
|
106
|
|
|
*/
|
107
|
|
|
public function setSecretKey($secretKey)
|
108
|
|
|
{
|
109
|
|
|
return $this->set('secret_key', $secretKey);
|
110
|
|
|
}
|
111
|
|
|
|
112
|
|
|
/**
|
113
|
|
|
* @param $serverName
|
114
|
|
|
* @return string
|
115
|
|
|
*/
|
116
|
|
|
public function setServerName($serverName)
|
117
|
|
|
{
|
118
|
|
|
return $this->set('serverName', $serverName);
|
119
|
|
|
}
|
120
|
|
|
|
121
|
|
|
/**
|
122
|
|
|
* @param $host
|
123
|
|
|
* @return string
|
124
|
|
|
*/
|
125
|
|
View Code Duplication |
public function setServerNameFromHost($host)
|
|
|
|
|
126
|
|
|
{
|
127
|
|
|
$parsed_host = parse_url($host, PHP_URL_HOST);
|
128
|
|
|
|
129
|
|
|
if (is_null($parsed_host)) {
|
130
|
|
|
$serverName = $host;
|
131
|
|
|
} else {
|
132
|
|
|
$serverName = preg_replace('/^www\./', '', $parsed_host);
|
133
|
|
|
}
|
134
|
|
|
|
135
|
|
|
return $this->set('serverName', $serverName);
|
136
|
|
|
}
|
137
|
|
|
|
138
|
|
|
/**
|
139
|
|
|
* @param $bool
|
140
|
|
|
* @return string
|
141
|
|
|
*/
|
142
|
|
|
public function setDebug($bool)
|
143
|
|
|
{
|
144
|
|
|
return $this->set('debug', $bool);
|
145
|
|
|
}
|
146
|
|
|
|
147
|
|
|
/**
|
148
|
|
|
* @param AuthenticationToken $authenticationToken
|
149
|
|
|
* @return mixed
|
150
|
|
|
*/
|
151
|
|
|
public function setAuthenticationToken(AuthenticationToken $authenticationToken)
|
152
|
|
|
{
|
153
|
|
|
return $this->set('authenticationToken', $authenticationToken);
|
154
|
|
|
}
|
155
|
|
|
|
156
|
|
|
/**
|
157
|
|
|
* @return string
|
158
|
|
|
*/
|
159
|
|
|
public function getHost()
|
160
|
|
|
{
|
161
|
|
|
return $this->get('host');
|
162
|
|
|
}
|
163
|
|
|
|
164
|
|
|
/**
|
165
|
|
|
* @return string
|
166
|
|
|
*/
|
167
|
|
|
public function getPort()
|
168
|
|
|
{
|
169
|
|
|
return $this->get('port');
|
170
|
|
|
}
|
171
|
|
|
|
172
|
|
|
/**
|
173
|
|
|
* @return string
|
174
|
|
|
*/
|
175
|
|
|
public function getPlugin()
|
176
|
|
|
{
|
177
|
|
|
return $this->get('plugin');
|
178
|
|
|
}
|
179
|
|
|
|
180
|
|
|
/**
|
181
|
|
|
* @return bool
|
182
|
|
|
*/
|
183
|
|
|
public function isSSL()
|
184
|
|
|
{
|
185
|
|
|
return $this->get('useSSL');
|
186
|
|
|
}
|
187
|
|
|
|
188
|
|
|
/**
|
189
|
|
|
* @return string
|
190
|
|
|
*/
|
191
|
|
|
public function getServerName()
|
192
|
|
|
{
|
193
|
|
|
return $this->get('serverName');
|
194
|
|
|
}
|
195
|
|
|
|
196
|
|
|
/**
|
197
|
|
|
* @return bool
|
198
|
|
|
*/
|
199
|
|
|
public function isDebug()
|
200
|
|
|
{
|
201
|
|
|
return $this->get('debug');
|
202
|
|
|
}
|
203
|
|
|
|
204
|
|
|
/**
|
205
|
|
|
* @return AuthenticationToken
|
206
|
|
|
*/
|
207
|
|
|
public function getAuthenticationToken()
|
208
|
|
|
{
|
209
|
|
|
return $this->get('authenticationToken');
|
210
|
|
|
}
|
211
|
|
|
|
212
|
|
|
/**
|
213
|
|
|
* Returns the URL under which query the webservice
|
214
|
|
|
* @return string
|
215
|
|
|
*/
|
216
|
|
|
public function getBaseURL()
|
217
|
|
|
{
|
218
|
|
|
$base = ($this->isSSL()) ? "https" : "http";
|
219
|
|
|
return $base . "://" . $this->getHost() . ":" . $this->getPort() . $this->getPlugin();
|
220
|
|
|
}
|
221
|
|
|
|
222
|
|
|
/**
|
223
|
|
|
* Returns the headers to be sent to web service
|
224
|
|
|
* @return array
|
225
|
|
|
* @throws \Exception
|
226
|
|
|
*/
|
227
|
|
|
public function getHeaders()
|
228
|
|
|
{
|
229
|
|
|
$authenticationToken = $this->getAuthenticationToken();
|
230
|
|
|
|
231
|
|
|
switch ($authenticationToken->getAuthMode()) {
|
232
|
|
|
case AuthenticationToken::AUTH_BASE:
|
233
|
|
|
$authHeader = "basic " . base64_encode($authenticationToken->getUsername() . ":" . $authenticationToken->getPassword());
|
234
|
|
|
break;
|
235
|
|
|
case AuthenticationToken::AUTH_SECRET_KEY:
|
236
|
|
|
$authHeader = $authenticationToken->getSharedSecretKey();
|
237
|
|
|
break;
|
238
|
|
|
default:
|
239
|
|
|
$authHeader = "Unrecognized authentication token!";
|
240
|
|
|
break;
|
241
|
|
|
}
|
242
|
|
|
|
243
|
|
|
return array(
|
244
|
|
|
'Accept: application/json',
|
245
|
|
|
'Authorization: ' . $authHeader,
|
246
|
|
|
'Content-Type: application/json',
|
247
|
|
|
);
|
248
|
|
|
}
|
249
|
|
|
}
|
250
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.