1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Uses the settings to provide some high-level functions. |
4
|
|
|
* |
5
|
|
|
* Please do not use this directly. Better use |
6
|
|
|
* {@link ThreemaGateway_Handler_PhpSdk->getSettings()}. If you want to use the |
7
|
|
|
* settings before initiating the SDK, you can use this class before, but please |
8
|
|
|
* pass an instance of it to {@link ThreemaGateway_Handler_PhpSdk} in this case. |
9
|
|
|
* |
10
|
|
|
* @package ThreemaGateway |
11
|
|
|
* @author rugk |
12
|
|
|
* @copyright Copyright (c) 2015-2016 rugk |
13
|
|
|
* @license MIT |
14
|
|
|
*/ |
15
|
|
|
|
16
|
|
|
class ThreemaGateway_Handler_Settings |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var string $gatewayId Your own Threema Gateway ID |
20
|
|
|
*/ |
21
|
|
|
protected $gatewayId = ''; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var string $gatewaySecret Your own Threema Gateway secret |
25
|
|
|
*/ |
26
|
|
|
protected $gatewaySecret = ''; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var string $privateKey Your own private key |
30
|
|
|
*/ |
31
|
|
|
protected $privateKey = ''; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var string $privateKeyBase The unconverted private key from settings. |
35
|
|
|
*/ |
36
|
|
|
private $privateKeyBase = ''; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var string $publicKey the public key converted from the private key {@see $privateKey} |
40
|
|
|
*/ |
41
|
|
|
protected $publicKey = ''; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Initiate settings. |
45
|
|
|
*/ |
46
|
|
|
public function __construct() |
47
|
|
|
{ |
48
|
|
|
/** @var XenForo_Options $xenOptions */ |
49
|
|
|
$xenOptions = XenForo_Application::getOptions(); |
50
|
|
|
|
51
|
|
|
// get options (if not hard-coded) |
52
|
|
|
if (!$this->gatewayId) { |
53
|
|
|
$this->gatewayId = $xenOptions->threema_gateway_threema_id; |
54
|
|
|
} |
55
|
|
|
if (!$this->gatewaySecret) { |
56
|
|
|
$this->gatewaySecret = $xenOptions->threema_gateway_threema_id_secret; |
57
|
|
|
} |
58
|
|
|
if (!$this->privateKey) { |
59
|
|
|
if (!$this->privateKeyBase) { |
60
|
|
|
$this->privateKeyBase = $xenOptions->threema_gateway_privatekeyfile; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
// vadility check & processing is later done when private key is actually requested |
64
|
|
|
// {@see convertPrivateKey()} |
|
|
|
|
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Checks whether the Gateway is basically set up. |
70
|
|
|
* |
71
|
|
|
* Note that this may not check all requirements (like installed libsodium |
72
|
|
|
* and so on). |
73
|
|
|
* In contrast to {@link isReady()} this only checks whether it is possible |
74
|
|
|
* to query the Threema Server for data, not whether sending/receiving |
75
|
|
|
* messages is actually possible. |
76
|
|
|
* This does not check any permissions! Use |
77
|
|
|
* {@link ThreemaGateway_Handler_Permissions->hasPermission()} for this |
78
|
|
|
* instead! |
79
|
|
|
* |
80
|
|
|
* @return bool |
81
|
|
|
*/ |
82
|
|
|
public function isAvaliable() |
83
|
|
|
{ |
84
|
|
|
if (!$this->gatewayId || |
85
|
|
|
!$this->gatewaySecret || |
86
|
|
|
XenForo_Application::getOptions()->threema_gateway_e2e == '' |
87
|
|
|
) { |
88
|
|
|
return false; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return true; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Checks whether everything is comple, so sending and receiving messages |
96
|
|
|
* is (theoretically) possible. |
97
|
|
|
* |
98
|
|
|
* This includes {@link isAvaliable()} as a basic check. |
99
|
|
|
* This does not check any permissions! Use |
100
|
|
|
* {@link ThreemaGateway_Handler_Permissions->hasPermission()} for this |
101
|
|
|
* instead! |
102
|
|
|
* |
103
|
|
|
* @return bool |
104
|
|
|
*/ |
105
|
|
|
public function isReady() |
106
|
|
|
{ |
107
|
|
|
// basic check |
108
|
|
|
if (!$this->isAvaliable()) { |
109
|
|
|
return false; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
//check whether sending and receiving is possible |
113
|
|
|
if ($this->isEndToEnd()) { |
114
|
|
|
// fast check |
115
|
|
|
if (!$this->privateKey && !$this->privateKeyBase) { |
116
|
|
|
return false; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
// get private key if necessary |
120
|
|
|
if (!$this->privateKey) { |
121
|
|
|
try { |
122
|
|
|
$this->convertPrivateKey(); |
123
|
|
|
} catch (Exception $e) { |
124
|
|
|
// in case of an error, it is not ready |
125
|
|
|
return false; |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
// if the key is (still) invalid, return error |
130
|
|
|
if (!$this->isPrivateKey($this->privateKey)) { |
131
|
|
|
return false; |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
return true; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Checks whether sending uses the end-to-end encrypted mode. |
140
|
|
|
* |
141
|
|
|
* Note: When E2E mode is not used it is also not possible to receive |
142
|
|
|
* messages. |
143
|
|
|
* |
144
|
|
|
* @return bool |
145
|
|
|
*/ |
146
|
|
|
public function isEndToEnd() |
147
|
|
|
{ |
148
|
|
|
return (XenForo_Application::getOptions()->threema_gateway_e2e == 'e2e'); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Checks whether the Gateway is running in debug mode. |
153
|
|
|
* |
154
|
|
|
* You may use this to show scary messages to the admins ;-) or to |
155
|
|
|
* conditionally disable functionality. |
156
|
|
|
* |
157
|
|
|
* @return bool |
158
|
|
|
*/ |
159
|
|
|
public function isDebug() |
160
|
|
|
{ |
161
|
|
|
/** @var XenForo_Options $xenOptions */ |
162
|
|
|
$xenOptions = XenForo_Application::getOptions(); |
163
|
|
|
|
164
|
|
|
return (($xenOptions->threema_gateway_logreceivedmsgs['enabled'] || |
165
|
|
|
$xenOptions->threema_gateway_allow_get_receive) && |
166
|
|
|
XenForo_Application::debugMode()); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Returns the gateway ID. |
171
|
|
|
* |
172
|
|
|
* @return string |
173
|
|
|
*/ |
174
|
|
|
public function getId() |
175
|
|
|
{ |
176
|
|
|
return $this->gatewayId; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Returns the gateway secret. |
181
|
|
|
* |
182
|
|
|
* @return string |
183
|
|
|
*/ |
184
|
|
|
public function getSecret() |
185
|
|
|
{ |
186
|
|
|
return $this->gatewaySecret; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* Returns the private key. |
191
|
|
|
* |
192
|
|
|
* @return string |
193
|
|
|
*/ |
194
|
|
|
public function getPrivateKey() |
195
|
|
|
{ |
196
|
|
|
if (!$this->privateKey) { |
197
|
|
|
$this->convertPrivateKey(); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
return $this->privateKey; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* Returns the public key. |
205
|
|
|
* |
206
|
|
|
* @return string |
207
|
|
|
*/ |
208
|
|
|
public function getOwnPublicKey() |
209
|
|
|
{ |
210
|
|
|
if (!$this->publicKey) { |
211
|
|
|
/** @var ThreemaGateway_Handler_Action_KeyConverter $keyConverter */ |
212
|
|
|
$keyConverter = new ThreemaGateway_Handler_Action_KeyConverter; |
213
|
|
|
$this->publicKey = $keyConverter->derivePublicKey($this->getPrivateKey()); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
return $this->publicKey; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* Checks and processes the private key. Throws an exception if something |
221
|
|
|
* is wrong. |
222
|
|
|
* |
223
|
|
|
* @throws XenForo_Exception |
224
|
|
|
*/ |
225
|
|
|
protected function convertPrivateKey() |
226
|
|
|
{ |
227
|
|
|
// use raw key (undocumented, not recommend) |
228
|
|
|
if (ThreemaGateway_Helper_Key::check($this->privateKeyBase, 'private:')) { |
229
|
|
|
$this->privateKey = $this->privateKeyBase; |
230
|
|
|
return; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
// find path of private key file |
234
|
|
|
if (!file_exists(__DIR__ . '/../' . $this->privateKeyBase)) { |
235
|
|
|
throw new XenForo_Exception(new XenForo_Phrase('threemagw_invalid_privatekey')); |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
// open file |
239
|
|
|
/** @var resource|false $fileres */ |
240
|
|
|
$fileres = fopen(__DIR__ . '/../' . $this->privateKeyBase, 'r'); |
241
|
|
|
|
242
|
|
|
// read content of private key file |
243
|
|
|
if (empty($fileres) || !is_resource($fileres)) { |
244
|
|
|
//error opening file |
245
|
|
|
throw new XenForo_Exception(new XenForo_Phrase('threemagw_invalid_keystorepath')); |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
$this->privateKey = fgets($fileres); |
249
|
|
|
fclose($fileres); |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* Checks whether the string actually is a private key. |
254
|
|
|
* |
255
|
|
|
* @param string $privateKey The string to check. |
256
|
|
|
* @return bool |
257
|
|
|
*/ |
258
|
|
|
protected function isPrivateKey($privateKey) |
259
|
|
|
{ |
260
|
|
|
return ThreemaGateway_Helper_Key::check($privateKey, 'private:'); |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
/** |
264
|
|
|
* Convert object to string. |
265
|
|
|
* |
266
|
|
|
* @return string |
267
|
|
|
*/ |
268
|
|
|
public function __toString() |
269
|
|
|
{ |
270
|
|
|
return __CLASS__ . ' of ' . $this->gatewayId; |
271
|
|
|
} |
272
|
|
|
} |
273
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.