PluginProbe
WebTotem Security / 2.4.12
WebTotem Security v2.4.12
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.12, at lib/Option.php

637 lines 14.2 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 config option.
17 *
18 * @param string $option
19 * Option name.
20 *
21 * @return mixed
22 * Returns saved data by option name.
23 */
24 public static function getOption($option) {
25 return WebTotemDB::getOption($option);
26 }
27
28 /**
29 * Save multiple configuration options.
30 *
31 * @param array $options
32 * Array of data, key is name of option.
33 *
34 * @return bool
35 * Returns TRUE after setting the options.
36 */
37 public static function setOptions(array $options) {
38
39 foreach ($options as $option => $value) {
40 $value = is_array($value) ? json_encode($value) : $value;
41 WebTotemDB::setOption($option, $value);
42 }
43
44 return TRUE;
45 }
46
47 /**
48 * Clear multiple configuration options.
49 *
50 * @param array $options
51 * Array of data, key is name of option.
52 *
53 * @return bool
54 * Returns TRUE after clearing the options.
55 */
56 public static function clearOptions(array $options) {
57
58 foreach ($options as $option) {
59 WebTotemDB::deleteOption($option);
60 }
61
62 return TRUE;
63 }
64
65 /**
66 * Save multiple some options to session.
67 *
68 * @param array $options
69 * Array of data, key is name of option.
70 *
71 * @return bool
72 * Returns TRUE after setting the session options.
73 */
74 public static function setSessionOptions(array $options) {
75
76 $sessions = json_decode(self::getOption('sessions'), true) ?: [];
77 $user_id = get_current_user_id();
78
79 foreach ($options as $option => $value){
80 $sessions[$user_id][$option] = $value;
81 }
82
83 self::setOptions(['sessions' => $sessions]);
84
85 return TRUE;
86 }
87
88 /**
89 * Get option from session.
90 *
91 * @param string $option
92 * Option name.
93 *
94 * @return mixed
95 * Returns saved data by option name.
96 */
97 public static function getSessionOption($option) {
98
99 $sessions = json_decode(self::getOption('sessions'), true) ?: [];
100 $user_id = get_current_user_id();
101
102 if(array_key_exists($user_id, $sessions) and array_key_exists($option, $sessions[$user_id])){
103 return $sessions[$user_id][$option];
104 } else {
105 return [];
106 }
107
108 }
109
110 /**
111 * Save multiple some plugin settings.
112 *
113 * @param array $options
114 * Array of data, key is name of option.
115 *
116 * @return bool
117 * Returns TRUE after save settings.
118 */
119 public static function setPluginSettings(array $options) {
120
121 $settings = json_decode(self::getOption('settings'), true) ?: [];
122
123 foreach ($options as $option => $value){
124 $settings[$option] = $value;
125 }
126
127 self::setOptions(['settings' => $settings]);
128
129 return TRUE;
130 }
131
132 /**
133 * Get plugin settings.
134 *
135 * @param string $option
136 * Option name.
137 *
138 * @return mixed
139 * Returns saved data by option name.
140 */
141 public static function getPluginSettings($option = null) {
142
143 $settings = json_decode(self::getOption('settings'), true) ?: [];
144
145 if($option){
146 if(array_key_exists($option, $settings)){
147 return $settings[$option];
148 } else {
149 return [];
150 }
151 } else{
152 return $settings;
153 }
154 }
155
156
157 /**
158 * Check has reCaptcha enabled.
159 *
160 * @return bool
161 * Returns TRUE if reCaptcha enabled.
162 */
163 public static function reCaptchaEnabled() {
164 return self::getPluginSettings('recaptcha') ?: false;
165 }
166
167
168 /**
169 * Save authentication token and token expiration dates in settings.
170 *
171 * @param array $params
172 * Parameters for authorization.
173 *
174 * @return string
175 * Returns TRUE after setting the options.
176 */
177 public static function login(array $params) {
178 $token_expired = time() + $params['token']['expiresIn'] - 60;
179
180 self::setOptions([
181 'activated' => TRUE,
182 'auth_token_expired' => $token_expired,
183 'auth_token' => $params['token']['value'],
184 'api_key' => $params['api_key'],
185 'multisite_options' => WebTotem::isMultiSite()
186 ]);
187
188 return TRUE;
189 }
190
191 /**
192 * Checks whether the user has activated the plugin using the API key.
193 *
194 * @return bool
195 * Returns the module activation status.
196 */
197 public static function isActivated() {
198 return (boolean) self::getOption('activated');
199 }
200
201 /**
202 * Remove module settings.
203 *
204 * @return string
205 * Returns TRUE after clearing the options.
206 */
207 public static function logout() {
208
209 self::clearOptions([
210 'activated',
211 'auth_token_expired',
212 'auth_token',
213 'api_key',
214 'host_id',
215 'host_name',
216 ]);
217 return TRUE;
218 }
219
220 /**
221 * Set notification.
222 *
223 * @param string $type
224 * Notification Type.
225 * @param string $notice
226 * Notification Text.
227 */
228 public static function setNotification($type, $notice) {
229 $notifications = self::getSessionOption('notifications') ?: [];
230
231 if (array_key_exists($type, $notifications)) {
232 if (!in_array($notice, $notifications[$type])) {
233 $notifications[$type][] = $notice;
234 self::setSessionOptions(['notifications' => $notifications]);
235 }
236 }
237 else {
238 $notifications[$type][] = $notice;
239 self::setSessionOptions(['notifications' => $notifications]);
240 }
241
242 }
243
244 /**
245 * Get notifications.
246 *
247 * @return array
248 * Notifications array.
249 */
250 public static function getNotificationsData() {
251 $types = ['error', 'info', 'warning', 'success'];
252
253 $notifications = self::getSessionOption('notifications') ?: [];
254 $result = [];
255
256 foreach ($types as $type) {
257 if (array_key_exists($type, $notifications)) {
258 foreach ($notifications[$type] as $notification) {
259 $result[] = ['type' => $type, 'notice' => $notification];
260 }
261 }
262 }
263
264 // Remove notifications.
265 self::setSessionOptions(['notifications' => []]);
266
267 return $result;
268 }
269
270 /**
271 * Set host data.
272 *
273 * @return void
274 */
275 public static function setHost($host_name, $host_id) {
276
277 if(WebTotem::isMultiSite()){
278 $blog_id = self::getBlogId($host_name);
279
280 add_blog_option($blog_id, 'wtotem_host_id', $host_id);
281 add_blog_option($blog_id, 'wtotem_host_name', $host_name);
282
283 if(!is_main_site($blog_id)){
284 $all_hosts = self::getOption('all_hosts') ?: [];
285 $all_hosts[$host_name] = $host_id;
286
287 self::setOptions([
288 'all_hosts' => $all_hosts,
289 ]);
290 }
291
292 }
293 else {
294 self::setOptions([
295 'host_id' => $host_id,
296 'host_name' => $host_name,
297 ]);
298 }
299 }
300
301 /**
302 * Get host data.
303 *
304 * @param string $hid
305 * Host id.
306 *
307 * @return array
308 * Host data.
309 */
310 public static function getHost($hid = false) {
311
312 if ( $hid ) {
313 $all_hosts = self::getAllHosts() ?: [];
314 if ( $all_hosts and in_array( $hid, $all_hosts ) ) {
315 return [
316 'id' => $hid,
317 'name' => array_search( $hid, $all_hosts ),
318 ];
319 }
320 }
321
322 return self::getMainHost();
323 }
324
325 /**
326 * Get host data.
327 *
328 * @return array
329 * Host data.
330 */
331 public static function getAllHosts() {
332 $all_hosts = json_decode(self::getOption('all_hosts'), true) ?: [];
333
334 $main_host = self::getMainHost();
335 $all_hosts = ($main_host['id']) ? [$main_host['name'] => $main_host['id']] + $all_hosts : $all_hosts;
336
337 return $all_hosts;
338 }
339
340 /**
341 * Get main host data.
342 *
343 * @return array
344 * Main host data.
345 */
346 public static function getMainHost() {
347
348 if(WebTotem::isMultiSite()){
349 return [
350 'id' => get_blog_option(0, 'wtotem_host_id'),
351 'name' => get_blog_option(0, 'wtotem_host_name'),
352 ];
353 } else{
354 return [
355 'id' => self::getOption('host_id'),
356 'name' => self::getOption('host_name'),
357 ];
358 }
359
360 }
361
362 /**
363 * Delete host data from DB.
364 *
365 * @return void
366 */
367 public static function clearAllHosts() {
368
369 $data = WebTotemAPI::getSites();
370 foreach ($data['edges'] as $site) {
371 $site = $site['node'];
372 $blog_id = self::getBlogId($site['hostname']);
373 delete_blog_option($blog_id, 'wtotem_host_id');
374 delete_blog_option($blog_id, 'wtotem_host_name');
375 }
376
377 }
378
379 /**
380 * Get an array of new sites.
381 *
382 * @return array
383 * Returns either an empty array or an array with new sites.
384 */
385 // public static function checkNewSites() {
386 // $hosts = self::getAllHosts();
387 // $sites = get_sites();
388 // $new_sites = [];
389 //
390 // foreach ($sites as $site){
391 // $host_name = untrailingslashit($site->domain . $site->path);
392 // if(!array_key_exists($host_name, $hosts) and !array_key_exists('www.' . $host_name, $hosts)) {
393 // $new_sites[] = $host_name;
394 // }
395 // }
396 // return $new_sites;
397 // }
398
399 /**
400 * Get host id from host name.
401 *
402 * @param $host_name
403 * Host name.
404 *
405 * @return integer
406 * Blog id.
407 */
408 public static function getBlogId($host_name){
409 $current_network = get_network();
410 $patterns = [ '/' . $current_network->domain . '/', '/\./', '/\//', ];
411
412 $slug = preg_replace( $patterns, '', $host_name );
413 return ($slug) ? get_id_from_blogname($slug) : 0;
414 }
415
416 /**
417 * Get all config options name.
418 *
419 * @return array
420 * Returns saved data by option name.
421 */
422 public static function getAllOptions() {
423 return [
424 'api_key',
425 'activated',
426 'auth_token_expired',
427 'auth_token',
428 'am_file',
429 'waf_file',
430 'av_file',
431 'am_installed',
432 'av_installed',
433 'waf_installed',
434 'time_zone_check',
435 'time_zone_offset',
436 'all_hosts',
437 'plugin_version',
438 'sessions',
439 'multisite_options',
440
441 'host_id',
442 'host_name',
443 ];
444 }
445
446 /**
447 * Checking the old version of options.
448 *
449 * @return boolean
450 * If there are old options, it will return true.
451 */
452 public static function checkOldOptions() {
453
454 // Creating a database with plugin settings.
455 if(WebTotemDB::install()){
456
457 $api_key = get_option('wtsec_api_key');
458 $am_file = get_option('wtsec_am_installed_file');
459 $waf_file = get_option('wtsec_waf_installed_file');
460
461 if($api_key){
462 self::setOptions([
463 'api_key' => $api_key,
464 'am_file' => $am_file,
465 'waf_file' => $waf_file,
466 'activated' => true,
467 'am_installed' => true,
468 'av_installed' => true,
469 'waf_installed' => true,
470 ]);
471
472 $old_options = [
473 'api_key',
474 'api_key_safe',
475 'api_key_activated',
476 'authorized',
477 'authToken',
478 'waf_installed_file',
479 'av_installed_file',
480 'am_installed_file',
481 'am_installed',
482 'logout',
483 'av_installed',
484 'waf_installed',
485 'agents_installed',
486 'api_url',
487 'color_scheme' ,
488 'time_zone',
489 'token_expired',
490 'deactivated',
491 'antivirus_event',
492 'antivirus_permissions_changed',
493 'antivirus_endCursor',
494 'antivirus_hasNextPage',
495 'firewall_endCursor',
496 'firewall_hasNextPage',
497 'reports_endCursor',
498 'reports_hasNextPage'
499 ];
500
501 foreach ($old_options as $option) {
502 delete_option('wtsec_' . $option);
503 delete_site_option('wtsec_' .$option);
504 }
505
506 }
507
508 $api_key = get_site_option('wtotem_api_key');
509 $am_file = get_site_option('wtotem_am_installed_file');
510 $waf_file = get_site_option('wtotem_waf_installed_file');
511
512 if($api_key){
513 self::setOptions([
514 'api_key' => $api_key,
515 'am_file' => $am_file,
516 'waf_file' => $waf_file,
517 'activated' => true,
518 'am_installed' => true,
519 'av_installed' => true,
520 'waf_installed' => true,
521 ]);
522
523 foreach (self::getAllOptions() as $option) {
524 delete_option('wtotem_' . $option);
525 delete_site_option('wtotem_' .$option);
526 }
527 }
528 }
529
530 return true;
531 }
532
533 /**
534 * Check multisite.
535 */
536 public static function multisiteCheck() {
537 // Check the transition to/from the multisite.
538 if ( ( WebTotem::isMultiSite() && ! WebTotemOption::getOption( 'multisite_options' ) ) or
539 ( ! WebTotem::isMultiSite() && WebTotemOption::getOption( 'multisite_options' ) ) ) {
540
541 self::setOptions([ 'multisite_options' => WebTotem::isMultiSite() ]);
542
543 if(WebTotem::isMultiSite()){
544 WebTotemOption::clearAllHosts();
545 WebTotemOption::clearOptions([ 'host_id', 'host_name' ]);
546 } else {
547 WebTotemOption::clearOptions([ 'host_id', 'host_name' ]);
548 }
549
550 WebTotemAgentManager::removeAgents();
551 }
552 }
553
554 /**
555 * Hide readme file
556 * @param string $readmeFile
557 * @return bool
558 */
559 public static function hideReadme($readmeFile = null) {
560 if ($readmeFile === null) {
561 $readmeFile = ABSPATH . '/readme.html';
562 }
563
564 if (file_exists($readmeFile)) {
565 $readmePathInfo = pathinfo($readmeFile);
566 require_once(ABSPATH . WPINC . '/pluggable.php');
567 $hiddenReadmeFile = $readmePathInfo['filename'] . '.' . wp_hash('readme') . '.' . $readmePathInfo['extension'];
568 return @rename($readmeFile, $readmePathInfo['dirname'] . '/' . $hiddenReadmeFile);
569 }
570
571 return false;
572 }
573
574 /**
575 * Restore readme file
576 * @param string $readmeFile
577 * @return bool
578 */
579 public static function restoreReadme($readmeFile = null) {
580 if ($readmeFile === null) {
581 $readmeFile = ABSPATH . '/readme.html';
582 }
583 $readmePathInfo = pathinfo($readmeFile);
584 require_once(ABSPATH . WPINC . '/pluggable.php');
585 $hiddenReadmeFile = $readmePathInfo['dirname'] . '/' . $readmePathInfo['filename'] . '.' . wp_hash('readme') . '.' . $readmePathInfo['extension'];
586 if (file_exists($hiddenReadmeFile)) {
587 return @rename($hiddenReadmeFile, $readmeFile);
588 }
589
590 return false;
591 }
592 /**
593 * Hide WP version
594 * @return void
595 */
596 public static function hideWPVersion() {
597 global $wp_version;
598 global $wp_styles;
599
600 if (!($wp_styles instanceof WP_Styles)) {
601 $wp_styles = new WP_Styles();
602 }
603 if ($wp_styles->default_version === $wp_version) {
604 $wp_styles->default_version = wp_hash($wp_styles->default_version);
605 }
606
607 foreach ($wp_styles->registered as $key => $val) {
608 if ($wp_styles->registered[$key]->ver === $wp_version) {
609 $wp_styles->registered[$key]->ver = wp_hash($wp_styles->registered[$key]->ver);
610 }
611 }
612
613 global $wp_scripts;
614 if (!($wp_scripts instanceof WP_Scripts)) {
615 $wp_scripts = new WP_Scripts();
616 }
617 if ($wp_scripts->default_version === $wp_version) {
618 $wp_scripts->default_version = wp_hash($wp_scripts->default_version);
619 }
620
621 foreach ($wp_scripts->registered as $key => $val) {
622 if ($wp_scripts->registered[$key]->ver === $wp_version) {
623 $wp_scripts->registered[$key]->ver = wp_hash($wp_scripts->registered[$key]->ver);
624 }
625 }
626 }
627
628 public static function replaceVersion($url) {
629 return preg_replace_callback("/([&;\?]ver)=(.+?)(&|$)/", "WebTotemOption::replaceVersionCallback", $url);
630 }
631
632 public static function replaceVersionCallback($matches) {
633 global $wp_version;
634 return $matches[1] . '=' . ($wp_version === $matches[2] ? wp_hash($matches[2]) : $matches[2]) . $matches[3];
635 }
636 }
637