PluginProbe
WebTotem Security / 2.4.35
WebTotem Security v2.4.35
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.35, at lib/Option.php

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