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