PluginProbe
WebTotem Security / 2.4.4
WebTotem Security v2.4.4
3.0.1 3.0.0 trunk 1.0 1.1 1.2 1.3 1.3.1 1.3.2 1.3.3 2.0 2.1 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.1.9 2.2.1 2.2.2 2.2.3 2.2.4 All 109 releases
wt-security / lib / Option.php

Option.php in WebTotem Security 2.4.4, at lib/Option.php

441 lines 9.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if (!defined('WEBTOTEM_INIT') || WEBTOTEM_INIT !== true) {
4 if (!headers_sent()) {
5 header('HTTP/1.1 403 Forbidden');
6 }
7 exit(1);
8 }
9
10 /**
11 * WebTotem Option class.
12 */
13 class WebTotemOption {
14
15 /**
16 * Get all config options name.
17 *
18 * @param string $option
19 * Option name.
20 *
21 * @return mixed
22 * Returns saved data by option name.
23 */
24 public static function getAllOptions() {
25 return [
26 'api_key',
27 'activated',
28 'auth_token_expired',
29 'auth_token',
30 'am_file',
31 'waf_file',
32 'av_file',
33 'am_installed',
34 'av_installed',
35 'waf_installed',
36 'time_zone_check',
37 'time_zone_offset',
38 'all_hosts',
39 'plugin_version',
40
41 'host_id',
42 'host_name',
43 ];
44 }
45
46 /**
47 * Get config option.
48 *
49 * @param string $option
50 * Option name.
51 *
52 * @return mixed
53 * Returns saved data by option name.
54 */
55 public static function getOption($option) {
56 // Control is passed to get_option() when the MultiSite mode is not used.
57 return get_site_option('wtotem_' . $option);
58 }
59
60 /**
61 * Save multiple configuration options.
62 *
63 * @param array $options
64 * Array of data, key is name of option.
65 *
66 * @return bool
67 * Returns TRUE after setting the options.
68 */
69 public static function setOptions(array $options) {
70
71 foreach ($options as $option => $value) {
72 //If the function is not used in a MultiSite assembly, then control is passed to
73 // the update_option() function with the parameter $autoload = 'no'
74 update_site_option('wtotem_' . $option, $value);
75 }
76
77 return TRUE;
78 }
79
80 /**
81 * Clear multiple configuration options.
82 *
83 * @param array $options
84 * Array of data, key is name of option.
85 *
86 * @return bool
87 * Returns TRUE after clearing the options.
88 */
89 public static function clearOptions(array $options) {
90
91 foreach ($options as $option) {
92 delete_option('wtotem_' . $option);
93 delete_site_option('wtotem_' . $option);
94 }
95
96 return TRUE;
97 }
98
99 /**
100 * Save multiple some options to session.
101 *
102 * @param array $options
103 * Array of data, key is name of option.
104 *
105 * @return bool
106 * Returns TRUE after setting the session options.
107 */
108 public static function setSessionOptions(array $options) {
109
110 foreach ($options as $option => $value) {
111 $_SESSION['wtotem.' . $option] = $value;
112 }
113 return TRUE;
114 }
115
116 /**
117 * Get option from session.
118 *
119 * @param string $option
120 * Option name.
121 *
122 * @return mixed
123 * Returns saved data by option name.
124 */
125 public static function getSessionOption($option) {
126 if(!isset($_SESSION)) {
127 return [];
128 }
129 if(isset($_SESSION['wtotem.' . $option])){
130 return $_SESSION['wtotem.' . $option];
131 }
132 else {
133 return FALSE;
134 }
135 }
136
137 /**
138 * Save authentication token and token expiration dates in settings.
139 *
140 * @param array $params
141 * Parameters for authorization.
142 *
143 * @return string
144 * Returns TRUE after setting the options.
145 */
146 public static function login(array $params) {
147 $token_expired = time() + $params['token']['expiresIn'] - 60;
148
149 self::setOptions([
150 'activated' => TRUE,
151 'auth_token_expired' => $token_expired,
152 'auth_token' => $params['token']['value'],
153 'api_key' => $params['api_key'],
154 ]);
155
156 return TRUE;
157 }
158
159 /**
160 * Checks whether the user has activated the plugin using the API key.
161 *
162 * @return bool
163 * Returns the module activation status.
164 */
165 public static function isActivated() {
166 return (boolean) self::getOption('activated') or get_site_option('wtsec_authorized');
167 }
168
169 /**
170 * Remove module settings.
171 *
172 * @return string
173 * Returns TRUE after clearing the options.
174 */
175 public static function logout() {
176
177 self::clearOptions([
178 'activated',
179 'auth_token_expired',
180 'auth_token',
181 'api_key',
182 'host_id',
183 'host_name',
184 ]);
185 return TRUE;
186 }
187
188 /**
189 * Set notification.
190 *
191 * @param string $type
192 * Notification Type.
193 * @param string $notice
194 * Notification Text.
195 */
196 public static function setNotification($type, $notice) {
197 $notifications = self::getSessionOption('notifications') ?: [];
198
199 if (array_key_exists($type, $notifications)) {
200 if (!in_array($notice, $notifications[$type])) {
201 $notifications[$type][] = $notice;
202 self::setSessionOptions(['notifications' => $notifications]);
203 }
204 }
205 else {
206 $notifications[$type][] = $notice;
207 self::setSessionOptions(['notifications' => $notifications]);
208 }
209
210 }
211
212 /**
213 * Get notifications.
214 *
215 * @return array
216 * Notifications array.
217 */
218 public static function getNotificationsData() {
219 $types = ['error', 'info', 'warning', 'success'];
220
221 $notifications = self::getSessionOption('notifications') ?: [];
222 $result = [];
223
224 foreach ($types as $type) {
225 if (array_key_exists($type, $notifications)) {
226 foreach ($notifications[$type] as $notification) {
227 $result[] = ['type' => $type, 'notice' => $notification];
228 }
229 }
230 }
231
232 // Remove notifications.
233 self::setSessionOptions(['notifications' => []]);
234
235 return $result;
236 }
237
238 /**
239 * Set host data.
240 *
241 * @return void
242 */
243 public static function setHost($host_name, $host_id) {
244
245 if(WebTotem::isMultiSite()){
246 $blog_id = self::getBlogId($host_name);
247
248 add_blog_option($blog_id, 'wtotem_host_id', $host_id);
249 add_blog_option($blog_id, 'wtotem_host_name', $host_name);
250
251 if(!is_main_site($blog_id)){
252 $all_hosts = self::getOption('all_hosts') ?: [];
253 $all_hosts[$host_name] = $host_id;
254
255 self::setOptions([
256 'all_hosts' => $all_hosts,
257 ]);
258 }
259
260 }
261 else {
262 self::setOptions([
263 'host_id' => $host_id,
264 'host_name' => $host_name,
265 ]);
266 }
267 }
268
269 /**
270 * Get host data.
271 *
272 * @param string $hid
273 * Host id.
274 *
275 * @return array
276 * Host data.
277 */
278 public static function getHost($hid = false) {
279 if($hid){
280 $all_hosts = self::getAllHosts() ?: [];
281 if($all_hosts and in_array($hid, $all_hosts)){
282 return [
283 'id' => $hid,
284 'name' => array_search($hid, $all_hosts),
285 ];
286 }
287 }
288 return [
289 'id' => get_option('wtotem_host_id'),
290 'name' => get_option('wtotem_host_name'),
291 ];
292 }
293
294 /**
295 * Get host data.
296 *
297 * @return array
298 * Host data.
299 */
300 public static function getAllHosts() {
301 $all_hosts = self::getOption('all_hosts') ?: [];
302
303 $main_host = self::getMainHost();
304 $all_hosts = ($main_host['id']) ? [$main_host['name'] => $main_host['id']] + $all_hosts : $all_hosts;
305
306 return $all_hosts;
307 }
308
309 /**
310 * Get main host data.
311 *
312 * @return array
313 * Main host data.
314 */
315 public static function getMainHost() {
316
317 $host['id'] = get_blog_option(0, 'wtotem_host_id');
318 $host['name'] = get_blog_option(0, 'wtotem_host_name');
319
320 return $host;
321 }
322
323 /**
324 * Delete host data from DB.
325 *
326 * @return void
327 */
328 public static function clearAllHosts() {
329
330 $data = WebTotemAPI::getSites();
331 foreach ($data['edges'] as $site) {
332 $site = $site['node'];
333 $blog_id = self::getBlogId($site['hostname']);
334 delete_blog_option($blog_id, 'wtotem_host_id');
335 delete_blog_option($blog_id, 'wtotem_host_name');
336 }
337
338 }
339
340 /**
341 * Get an array of new sites.
342 *
343 * @return array
344 * Returns either an empty array or an array with new sites.
345 */
346 // public static function checkNewSites() {
347 // $hosts = self::getAllHosts();
348 // $sites = get_sites();
349 // $new_sites = [];
350 //
351 // foreach ($sites as $site){
352 // $host_name = untrailingslashit($site->domain . $site->path);
353 // if(!array_key_exists($host_name, $hosts) and !array_key_exists('www.' . $host_name, $hosts)) {
354 // $new_sites[] = $host_name;
355 // }
356 // }
357 // return $new_sites;
358 // }
359
360 /**
361 * Get host id from host name.
362 *
363 * @param $host_name
364 * Host name.
365 *
366 * @return integer
367 * Blog id.
368 */
369 public static function getBlogId($host_name){
370 $current_network = get_network();
371 $patterns = [ '/' . $current_network->domain . '/', '/\./', '/\//', ];
372
373 $slug = preg_replace( $patterns, '', $host_name );
374 return ($slug) ? get_id_from_blogname($slug) : 0;
375 }
376
377 /**
378 * Checking the old version of options.
379 *
380 * @return boolean
381 * If there are old options, it will return true.
382 */
383 public static function checkOldOptions() {
384
385 $api_key = get_option('wtsec_api_key');
386 $am_file = get_option('wtsec_am_installed_file');
387 $waf_file = get_option('wtsec_waf_installed_file');
388
389 if($api_key && $am_file && $waf_file){
390
391 self::setOptions([
392 'api_key' => $api_key,
393 'am_file' => $am_file,
394 'waf_file' => $waf_file,
395 'activated' => true,
396 'am_installed' => true,
397 'av_installed' => true,
398 'waf_installed' => true,
399 ]);
400
401 $old_options = [
402 'api_key',
403 'api_key_safe',
404 'api_key_activated',
405 'authorized',
406 'authToken',
407 'waf_installed_file',
408 'am_installed_file',
409 'am_installed',
410 'logout',
411 'av_installed',
412 'waf_installed',
413 'agents_installed',
414 'api_url',
415 'color_scheme' ,
416 'time_zone',
417 'token_expired',
418 'deactivated',
419 'antivirus_event',
420 'antivirus_permissions_changed',
421 'antivirus_endCursor',
422 'antivirus_hasNextPage',
423 'firewall_endCursor',
424 'firewall_hasNextPage',
425 'reports_endCursor',
426 'reports_hasNextPage'
427 ];
428
429 foreach ($old_options as $option) {
430 delete_option('wtsec_' . $option);
431 delete_site_option('wtsec_' .$option);
432 }
433
434 return true;
435 }
436
437 return false;
438 }
439
440 }
441