1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Status managment for Threema Gateway. |
4
|
|
|
* |
5
|
|
|
* @package ThreemaGateway |
6
|
|
|
* @author rugk |
7
|
|
|
* @copyright Copyright (c) 2015-2016 rugk |
8
|
|
|
* @license MIT |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
class ThreemaGateway_Option_Status |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var int Below this amount of credits a warning is shown. |
15
|
|
|
*/ |
16
|
|
|
const CREDITS_WARN = 100; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Renders the status "option". |
20
|
|
|
* |
21
|
|
|
* @param XenForo_View $view View object |
22
|
|
|
* @param string $fieldPrefix Prefix for the HTML form field name |
23
|
|
|
* @param array $preparedOption Prepared option info |
24
|
|
|
* @param bool $canEdit True if an "edit" link should appear |
25
|
|
|
* |
26
|
|
|
* @return XenForo_Template_Abstract Template object |
27
|
|
|
*/ |
28
|
|
|
public static function renderHtml(XenForo_View $view, $fieldPrefix, array $preparedOption, $canEdit) |
29
|
|
|
{ |
30
|
|
|
/** @var array $status */ |
31
|
|
|
$status = ['libsodium', 'libsodiumphp', 'phpsdk', 'credits']; |
32
|
|
|
/** @var string $extraError */ |
33
|
|
|
$extraError = ''; |
34
|
|
|
|
35
|
|
|
/** @var bool $technSuccess */ |
36
|
|
|
$technSuccess = self::checkTecRequire($status, $extraError); |
|
|
|
|
37
|
|
|
|
38
|
|
|
/** @var ThreemaGateway_Handler_Settings $gwSettings */ |
39
|
|
|
$gwSettings = new ThreemaGateway_Handler_Settings; |
40
|
|
|
|
41
|
|
|
// only go on if technically everything is okay to prevent PHP errors when accessing the SDK |
42
|
|
|
if ($technSuccess) { |
43
|
|
|
/** @var bool $phpSdkSuccess */ |
44
|
|
|
$phpSdkSuccess = self::checkPhpSdk($status, $extraError, $gwSettings); |
45
|
|
|
|
46
|
|
|
// Only continue if there are no errors in the PHP SDK. |
47
|
|
|
// We could try it anyway, but this would be useless as it certainly |
48
|
|
|
// fails anyway and if you have a broken setup, you also have bigger |
49
|
|
|
// issues than your credits count. |
50
|
|
|
if ($phpSdkSuccess) { |
51
|
|
|
self::queryCredits($status, $extraError); |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
if ($gwSettings->isDebug()) { |
56
|
|
|
if (XenForo_Application::debugMode()) { |
57
|
|
|
$extraError[] = [ |
58
|
|
|
'text' => new XenForo_Phrase('option_threema_gateway_status_debug_mode_active'), |
59
|
|
|
'descclass' => 'warning' |
60
|
|
|
]; |
61
|
|
|
} else { |
62
|
|
|
$extraError[] = [ |
63
|
|
|
'text' => new XenForo_Phrase('option_threema_gateway_status_debug_mode_potentially_active'), |
64
|
|
|
'descclass' => 'warning' |
65
|
|
|
]; |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$editLink = $view->createTemplateObject('option_list_option_editlink', [ |
70
|
|
|
'preparedOption' => $preparedOption, |
71
|
|
|
'canEditOptionDefinition' => $canEdit |
72
|
|
|
]); |
73
|
|
|
|
74
|
|
|
return $view->createTemplateObject('threemagw_option_list_status', [ |
75
|
|
|
'fieldPrefix' => $fieldPrefix, |
76
|
|
|
'listedFieldName' => $fieldPrefix . '_listed[]', |
77
|
|
|
'preparedOption' => $preparedOption, |
78
|
|
|
'editLink' => $editLink, |
79
|
|
|
'status' => $status, |
80
|
|
|
'additionalerror' => $extraError, |
81
|
|
|
'isConfError' => (!$technSuccess), |
82
|
|
|
'isPhpSdkError' => (!$phpSdkSuccess), |
|
|
|
|
83
|
|
|
]); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Checks whether all technical requirements of the add-on installation |
88
|
|
|
* are fullfilled. |
89
|
|
|
* |
90
|
|
|
* Mostly only checks for Libsodium and Libsodium-PHP. |
91
|
|
|
* Return false when a serious error happens, which indicates that the |
92
|
|
|
* requirements are not fullfilled. |
93
|
|
|
* |
94
|
|
|
* @param array $status Will be filled with required statuses (sic) |
95
|
|
|
* @param array $extraError Optional other errors may be added here |
96
|
|
|
* @return bool |
97
|
|
|
*/ |
98
|
|
|
protected static function checkTecRequire(&$status, &$extraError) |
99
|
|
|
{ |
100
|
|
|
// optional check: HTTPS |
101
|
|
|
if (!XenForo_Application::$secure) { |
102
|
|
|
$extraError[]['text'] = new XenForo_Phrase('option_threema_gateway_status_no_https'); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
//libsodium |
106
|
|
|
if (extension_loaded('libsodium')) { |
107
|
|
|
if (method_exists('Sodium', 'sodium_version_string')) { |
108
|
|
|
$status['libsodium']['text'] = new XenForo_Phrase('option_threema_gateway_status_libsodium_version', ['version' => Sodium::sodium_version_string()]); |
109
|
|
|
$status['libsodium']['descr'] = new XenForo_Phrase('option_threema_gateway_status_libsodium_outdated'); |
110
|
|
|
$status['libsodium']['descclass'] = 'warning'; |
111
|
|
|
} else { |
112
|
|
|
$status['libsodium']['text'] = new XenForo_Phrase('option_threema_gateway_status_libsodium_version', ['version' => \Sodium\version_string()]); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
// & libsodium-php |
116
|
|
|
$status['libsodiumphp']['text'] = new XenForo_Phrase('option_threema_gateway_status_libsodiumphp_version', ['version' => phpversion('libsodium')]); |
117
|
|
|
if (version_compare(phpversion('libsodium'), '1.0.1', '<')) { |
118
|
|
|
$status['libsodiumphp']['descr'] = new XenForo_Phrase('option_threema_gateway_status_libsodiumphp_outdated'); |
119
|
|
|
$status['libsodiumphp']['descclass'] = 'warning'; |
120
|
|
|
} |
121
|
|
|
} else { |
122
|
|
|
$status['libsodium']['text'] = new XenForo_Phrase('option_threema_gateway_status_libsodium_not_installed'); |
123
|
|
View Code Duplication |
if (PHP_INT_SIZE < 8) { |
|
|
|
|
124
|
|
|
$status['libsodium']['descr'] = new XenForo_Phrase('option_threema_gateway_status_libsodium_not_installed_required_64bit'); |
125
|
|
|
$status['libsodium']['descclass'] = 'error'; |
126
|
|
|
return false; |
127
|
|
|
} else { |
128
|
|
|
$status['libsodium']['descr'] = new XenForo_Phrase('option_threema_gateway_status_libsodium_not_installed_recommend'); |
129
|
|
|
$status['libsodium']['descclass'] = 'warning'; |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
// there may be warnings, but apart from that all is okay |
134
|
|
|
return true; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Checks whether the add-on/PHP-SDK is correctly configured and this ready |
139
|
|
|
* to use. |
140
|
|
|
* |
141
|
|
|
* It also automatically includes status indicators from the SDK. |
142
|
|
|
* Return false when a serious error happens, which indicates that the |
143
|
|
|
* requirements are not fullfilled. |
144
|
|
|
* |
145
|
|
|
* @param array $status Will be filled with required statuses (sic) |
146
|
|
|
* @param array $extraError Optional other errors may be added here |
147
|
|
|
* @param ThreemaGateway_Handler_Settings $gwSettings |
148
|
|
|
* @return bool |
149
|
|
|
*/ |
150
|
|
|
protected static function checkPhpSdk(&$status, &$extraError, ThreemaGateway_Handler_Settings $gwSettings = null) |
151
|
|
|
{ |
152
|
|
|
// auto-create Gateway settings if not given |
153
|
|
|
if ($gwSettings == null) { |
154
|
|
|
$gwSettings = new ThreemaGateway_Handler_Settings; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
//show PHP SDK version, checks if PHP-SDK is correctly setup |
158
|
|
|
try { |
159
|
|
|
$sdk = ThreemaGateway_Handler_PhpSdk::getInstance($gwSettings); |
160
|
|
|
//Note: When the SDK throws an exception the two lines below cannot be executed, so the version number cannot be determinated |
161
|
|
|
$status['phpsdk']['text'] = new XenForo_Phrase('option_threema_gateway_status_phpsdk_version', ['version' => $sdk->getVersion()]); |
162
|
|
|
$status['phpsdk']['addition'] = new XenForo_Phrase('option_threema_gateway_status_phpsdk_featurelevel', ['level' => $sdk->getFeatureLevel()]); |
163
|
|
|
} catch (Exception $e) { |
164
|
|
|
$extraError[]['text'] = new XenForo_Phrase('option_threema_gateway_status_custom_phpsdk_error') . $e->getMessage(); |
165
|
|
|
return false; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
// check whether Gateway is ready to use |
169
|
|
|
if (!$gwSettings->isReady()) { |
170
|
|
|
// If SDK is not ready, check whether it is at least available |
171
|
|
|
if ($gwSettings->isAvaliable()) { |
172
|
|
|
// presumambly an error in setup |
173
|
|
|
$extraError[]['text'] = new XenForo_Phrase('option_threema_gateway_status_phpsdk_not_ready'); |
174
|
|
|
} else { |
175
|
|
|
// presumambly not yet setup (default settings or so) |
176
|
|
|
$extraError[] = [ |
177
|
|
|
'text' => new XenForo_Phrase('option_threema_gateway_status_phpsdk_not_ready_yet'), |
178
|
|
|
'descclass' => 'warning' |
179
|
|
|
]; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
return false; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
// there may be warnings, but apart from that all is okay |
186
|
|
|
return true; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* When the user is allowed to view the credits, this queries them and adds |
191
|
|
|
* the result as a status message. |
192
|
|
|
* |
193
|
|
|
* Return false when the permissions could not be fetched. |
194
|
|
|
* When the user has not enough permissions to do so, this method returns |
195
|
|
|
* true anyway. |
196
|
|
|
* |
197
|
|
|
* @param array $status Will be filled with required statuses (sic) |
198
|
|
|
* @param array $extraError Optional other errors may be added here |
199
|
|
|
* @return bool |
200
|
|
|
*/ |
201
|
|
|
protected static function queryCredits(&$status, &$extraError) |
202
|
|
|
{ |
203
|
|
|
/** @var ThreemaGateway_Handler_Permissions $permissions */ |
204
|
|
|
$permissions = ThreemaGateway_Handler_Permissions::getInstance(); |
205
|
|
|
|
206
|
|
|
// check permissions for accessing credits |
207
|
|
|
if (!$permissions->hasPermission('credits')) { |
208
|
|
|
$status['credits']['text'] = new XenForo_Phrase('option_threema_gateway_status_credits', ['credits' => 'No permission']); |
209
|
|
|
$status['credits']['descr'] = new XenForo_Phrase('option_threema_gateway_status_credits_permission'); |
210
|
|
|
$status['credits']['descclass'] = 'warning'; |
211
|
|
|
|
212
|
|
|
return true; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
// always there credit text |
216
|
|
|
$status['credits']['addition'] = new XenForo_Phrase('option_threema_gateway_status_credits_recharge'); |
217
|
|
|
|
218
|
|
|
// try to fetch credits |
219
|
|
|
try { |
220
|
|
|
$gwServer = new ThreemaGateway_Handler_Action_GatewayServer; |
221
|
|
|
/** @var int|string $credits */ |
222
|
|
|
$credits = $gwServer->getCredits(); |
223
|
|
|
} catch (Exception $e) { |
224
|
|
|
// special error handling |
225
|
|
|
$extraError[]['text'] = new XenForo_Phrase('option_threema_gateway_status_custom_gwserver_error') . $e->getMessage(); |
226
|
|
|
$credits = 'N/A'; |
227
|
|
|
|
228
|
|
|
// add (general) status error |
229
|
|
|
$status['credits']['descr'] = new XenForo_Phrase('option_threema_gateway_status_credits_error'); |
230
|
|
|
$status['credits']['descclass'] = 'error'; |
231
|
|
|
|
232
|
|
|
// wanna use finally block here, but is only available in PHP >= 5.5 :( |
233
|
|
|
// so duplicate code… |
234
|
|
|
$status['credits']['text'] = new XenForo_Phrase('option_threema_gateway_status_credits', ['credits' => $credits]); |
235
|
|
|
return false; |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
// when no error happens, check whether credits are "enough" |
239
|
|
|
$status['credits']['text'] = new XenForo_Phrase('option_threema_gateway_status_credits', ['credits' => $credits]); |
240
|
|
View Code Duplication |
if ($credits == 0) { |
|
|
|
|
241
|
|
|
$status['credits']['descr'] = new XenForo_Phrase('option_threema_gateway_status_credits_out'); |
242
|
|
|
$status['credits']['descclass'] = 'error'; |
243
|
|
|
return false; |
244
|
|
|
} elseif ($credits < self::CREDITS_WARN) { |
245
|
|
|
$status['credits']['descr'] = new XenForo_Phrase('option_threema_gateway_status_credits_low'); |
246
|
|
|
$status['credits']['descclass'] = 'warning'; |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
// there may be warnings, but apart from that all is okay |
250
|
|
|
return true; |
251
|
|
|
} |
252
|
|
|
} |
253
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: