PluginProbe
WebTotem Security / 2.4.11
WebTotem Security v2.4.11
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 +65 -360 trunk2.4.11 View file →
@@ -11,8 +11,41 @@
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 + 'sessions',
41 + 'multisite_options',
42 +
43 + 'host_id',
44 + 'host_name',
45 + ];
46 + }
47 +
15 48 /**
16 49 * Get config option.
17 50 *
18 51 * @param string $option
@@ -21,10 +54,9 @@
21 54 * @return mixed
22 55 * Returns saved data by option name.
23 56 */
24 57 public static function getOption($option) {
25 - $data = WebTotemDB::getData([ 'name' => $option ],'settings');
26 - return (array_key_exists('value', $data)) ? $data['value'] : '';
58 + return WebTotemDB::getOption($option);
27 59 }
28 60
29 61 /**
30 62 * Save multiple configuration options.
@@ -38,9 +70,9 @@
38 70 public static function setOptions(array $options) {
39 71
40 72 foreach ($options as $option => $value) {
41 73 $value = is_array($value) ? json_encode($value) : $value;
42 - WebTotemDB::setData(['name' => $option, 'value' => $value,], 'settings', ['name' => $option]);
74 + WebTotemDB::setOption($option, $value);
43 75 }
44 76
45 77 return TRUE;
46 78 }
@@ -56,9 +88,9 @@
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 + WebTotemDB::deleteOption($option);
61 93 }
62 94
63 95 return TRUE;
64 96 }
@@ -107,67 +139,9 @@
107 139 }
108 140
109 141 }
110 142
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 - /**
143 + /**
170 144 * Save authentication token and token expiration dates in settings.
171 145 *
172 146 * @param array $params
173 147 * Parameters for authorization.
@@ -175,16 +149,14 @@
175 149 * @return string
176 150 * Returns TRUE after setting the options.
177 151 */
178 152 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;
153 + $token_expired = time() + $params['token']['expiresIn'] - 60;
182 154
183 155 self::setOptions([
184 156 'activated' => TRUE,
185 157 'auth_token_expired' => $token_expired,
186 - 'auth_token' => $params['token'],
158 + 'auth_token' => $params['token']['value'],
187 159 'api_key' => $params['api_key'],
188 160 'multisite_options' => WebTotem::isMultiSite()
189 161 ]);
190 162
@@ -190,31 +162,9 @@
190 162
191 163 return TRUE;
192 164 }
193 165
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 - /**
166 + /**
217 167 * Checks whether the user has activated the plugin using the API key.
218 168 *
219 169 * @return bool
220 170 * Returns the module activation status.
@@ -235,40 +185,14 @@
235 185 'activated',
236 186 'auth_token_expired',
237 187 'auth_token',
238 188 'api_key',
239 - 'api_url',
240 - 'host_id',
241 - 'host_name',
189 + 'host_id',
190 + 'host_name',
242 191 ]);
243 192 return TRUE;
244 193 }
245 194
246 - /**
247 - * Return the current API access token, authorizing only when necessary.
248 - *
249 - * The token is reused until it actually expires; a new sign-in happens only
250 - * when there is no token or the stored one is past its expiry.
251 - *
252 - * @return string
253 - * Access token, or an empty string when authorization failed.
254 - */
255 - public static function getAuthToken() {
256 - $token = self::getOption('auth_token');
257 - $expired_at = (int) self::getOption('auth_token_expired');
258 -
259 - if ($token && $expired_at > time()) {
260 - return $token;
261 - }
262 -
263 - $api_key = self::getOption('api_key');
264 - if ($api_key && WebTotemAPI::auth($api_key) === 'success') {
265 - return self::getOption('auth_token');
266 - }
267 -
268 - return $token ?: '';
269 - }
270 -
271 195 /**
272 196 * Set notification.
273 197 *
274 198 * @param string $type
@@ -331,22 +255,18 @@
331 255 add_blog_option($blog_id, 'wtotem_host_id', $host_id);
332 256 add_blog_option($blog_id, 'wtotem_host_name', $host_name);
333 257
334 258 if(!is_main_site($blog_id)){
335 - $all_hosts = json_decode(self::getOption('all_hosts'), true) ?: [];
259 + $all_hosts = self::getOption('all_hosts') ?: [];
336 260 $all_hosts[$host_name] = $host_id;
337 261
338 262 self::setOptions([
339 263 'all_hosts' => $all_hosts,
340 264 ]);
341 - } else {
342 - self::setOptions([
343 - 'host_id' => $host_id,
344 - 'host_name' => $host_name,
345 - ]);
346 265 }
347 266
348 - } else {
267 + }
268 + else {
349 269 self::setOptions([
350 270 'host_id' => $host_id,
351 271 'host_name' => $host_name,
352 272 ]);
@@ -362,20 +282,21 @@
362 282 * @return array
363 283 * Host data.
364 284 */
365 285 public static function getHost($hid = false) {
366 -
367 - if ( $hid ) {
286 + if($hid){
368 287 $all_hosts = self::getAllHosts() ?: [];
369 - if ( $all_hosts and in_array( $hid, $all_hosts ) ) {
288 + if($all_hosts and in_array($hid, $all_hosts)){
370 289 return [
371 - 'id' => $hid,
372 - 'name' => array_search( $hid, $all_hosts ),
290 + 'id' => $hid,
291 + 'name' => array_search($hid, $all_hosts),
373 292 ];
374 293 }
375 294 }
376 -
377 - return self::getMainHost();
295 + return [
296 + 'id' => get_option('wtotem_host_id'),
297 + 'name' => get_option('wtotem_host_name'),
298 + ];
378 299 }
379 300
380 301 /**
381 302 * Get host data.
@@ -399,13 +320,12 @@
399 320 * Main host data.
400 321 */
401 322 public static function getMainHost() {
402 323
403 - return [
404 - 'id' => self::getOption('host_id'),
405 - 'name' => self::getOption('host_name'),
406 - ];
324 + $host['id'] = get_blog_option(0, 'wtotem_host_id');
325 + $host['name'] = get_blog_option(0, 'wtotem_host_name');
407 326
327 + return $host;
408 328 }
409 329
410 330 /**
411 331 * Delete host data from DB.
@@ -413,14 +333,12 @@
413 333 * @return void
414 334 */
415 335 public static function clearAllHosts() {
416 336
417 - $hosts = WebTotemAPI::getSites(1, 1000000);
418 - foreach ($hosts as $site) {
419 - if (empty($site['name'])) {
420 - continue;
421 - }
422 - $blog_id = self::getBlogId($site['name']);
337 + $data = WebTotemAPI::getSites();
338 + foreach ($data['edges'] as $site) {
339 + $site = $site['node'];
340 + $blog_id = self::getBlogId($site['hostname']);
423 341 delete_blog_option($blog_id, 'wtotem_host_id');
424 342 delete_blog_option($blog_id, 'wtotem_host_name');
425 343 }
426 344
@@ -454,48 +372,14 @@
454 372 *
455 373 * @return integer
456 374 * Blog id.
457 375 */
458 - public static function getBlogId($host_name){
459 - $local_sites = get_sites();
376 + public static function getBlogId($host_name){
377 + $current_network = get_network();
378 + $patterns = [ '/' . $current_network->domain . '/', '/\./', '/\//', ];
460 379
461 - foreach ($local_sites as $site){
462 - $domain = untrailingslashit($site->domain . $site->path);
463 - if($host_name == $domain){
464 - return $site->blog_id;
465 - }
466 - }
467 - return 0;
468 - }
469 -
470 - /**
471 - * Get all config options name.
472 - *
473 - * @return array
474 - * Returns saved data by option name.
475 - */
476 - public static function getAllOptions() {
477 - return [
478 - 'api_key',
479 - 'activated',
480 - 'auth_token_expired',
481 - 'auth_token',
482 - 'am_file',
483 - 'waf_file',
484 - 'av_file',
485 - 'am_installed',
486 - 'av_installed',
487 - 'waf_installed',
488 - 'time_zone_check',
489 - 'time_zone_offset',
490 - 'all_hosts',
491 - 'plugin_version',
492 - 'sessions',
493 - 'multisite_options',
494 -
495 - 'host_id',
496 - 'host_name',
497 - ];
380 + $slug = preg_replace( $patterns, '', $host_name );
381 + return ($slug) ? get_id_from_blogname($slug) : 0;
498 382 }
499 383
500 384 /**
501 385 * Checking the old version of options.
@@ -592,188 +476,9 @@
592 476 if ( ( WebTotem::isMultiSite() && ! WebTotemOption::getOption( 'multisite_options' ) ) or
593 477 ( ! WebTotem::isMultiSite() && WebTotemOption::getOption( 'multisite_options' ) ) ) {
594 478
595 479 self::setOptions([ 'multisite_options' => WebTotem::isMultiSite() ]);
596 -
597 - if(WebTotem::isMultiSite()){
598 - WebTotemOption::clearAllHosts();
599 - WebTotemOption::clearOptions([ 'host_id', 'host_name' ]);
600 - } else {
601 - WebTotemOption::clearOptions([ 'host_id', 'host_name' ]);
602 - }
603 -
604 480 WebTotemAgentManager::removeAgents();
605 481 }
606 482 }
607 -
608 - /**
609 - * Hide readme file
610 - * @param string $readmeFile
611 - * @return bool
612 - */
613 - public static function hideReadme($readmeFile = null) {
614 - if ($readmeFile === null) {
615 - $readmeFile = ABSPATH . 'readme.html';
616 - }
617 -
618 - if (file_exists($readmeFile)) {
619 - $readmePathInfo = pathinfo($readmeFile);
620 - require_once(ABSPATH . WPINC . '/pluggable.php');
621 - $hiddenReadmeFile = $readmePathInfo['filename'] . '.' . wp_hash('readme') . '.' . $readmePathInfo['extension'];
622 - return @rename($readmeFile, $readmePathInfo['dirname'] . '/' . $hiddenReadmeFile);
623 - }
624 -
625 - return false;
626 - }
627 -
628 - /**
629 - * Restore readme file
630 - * @param string $readmeFile
631 - * @return bool
632 - */
633 - public static function restoreReadme($readmeFile = null) {
634 - if ($readmeFile === null) {
635 - $readmeFile = ABSPATH . 'readme.html';
636 - }
637 - $readmePathInfo = pathinfo($readmeFile);
638 - require_once(ABSPATH . WPINC . '/pluggable.php');
639 - $hiddenReadmeFile = $readmePathInfo['dirname'] . '/' . $readmePathInfo['filename'] . '.' . wp_hash('readme') . '.' . $readmePathInfo['extension'];
640 - if (file_exists($hiddenReadmeFile)) {
641 - return @rename($hiddenReadmeFile, $readmeFile);
642 - }
643 -
644 - return false;
645 - }
646 - /**
647 - * Hide WP version
648 - * @return void
649 - */
650 - public static function hideWPVersion() {
651 - global $wp_version;
652 - global $wp_styles;
653 -
654 - if (!($wp_styles instanceof WP_Styles)) {
655 - $wp_styles = new WP_Styles();
656 - }
657 - if ($wp_styles->default_version === $wp_version) {
658 - $wp_styles->default_version = wp_hash($wp_styles->default_version);
659 - }
660 -
661 - foreach ($wp_styles->registered as $key => $val) {
662 - if ($wp_styles->registered[$key]->ver === $wp_version) {
663 - $wp_styles->registered[$key]->ver = wp_hash($wp_styles->registered[$key]->ver);
664 - }
665 - }
666 -
667 - global $wp_scripts;
668 - if (!($wp_scripts instanceof WP_Scripts)) {
669 - $wp_scripts = new WP_Scripts();
670 - }
671 - if ($wp_scripts->default_version === $wp_version) {
672 - $wp_scripts->default_version = wp_hash($wp_scripts->default_version);
673 - }
674 -
675 - foreach ($wp_scripts->registered as $key => $val) {
676 - if ($wp_scripts->registered[$key]->ver === $wp_version) {
677 - $wp_scripts->registered[$key]->ver = wp_hash($wp_scripts->registered[$key]->ver);
678 - }
679 - }
680 - }
681 -
682 - public static function replaceVersion($url) {
683 - return preg_replace_callback("/([&;\?]ver)=(.+?)(&|$)/", "WebTotemOption::replaceVersionCallback", $url);
684 - }
685 -
686 - public static function replaceVersionCallback($matches) {
687 - global $wp_version;
688 - return $matches[1] . '=' . ($wp_version === $matches[2] ? wp_hash($matches[2]) : $matches[2]) . $matches[3];
689 - }
690 -
691 - /**
692 - * Check the nonce comming from any of the settings pages.
693 - *
694 - * @return bool True if the nonce is valid, false otherwise.
695 - */
696 - public static function checkOptionsNonce() {
697 - // Create the option_page value if permalink submission.
698 - if (!isset($_POST['option_page']) && isset($_POST['permalink_structure'])) {
699 - $_POST['option_page'] = 'permalink';
700 - }
701 -
702 - /* check if the option_page has an allowed value */
703 - $option_page = WebTotemRequest::post('option_page');
704 -
705 - if (!$option_page) {
706 - return false;
707 - }
708 -
709 - $action = '';
710 - $nonce = '_wpnonce';
711 -
712 - switch ($option_page) {
713 - case 'general':
714 - case 'writing':
715 - case 'reading':
716 - case 'discussion':
717 - case 'media':
718 - case 'options':
719 - $action = $option_page . '-options';
720 - break;
721 - case 'permalink':
722 - $action = 'update-permalink';
723 - break;
724 - }
725 -
726 - /* check the nonce validity */
727 - return (bool) (
728 - !empty($action)
729 - && isset($_REQUEST[$nonce])
730 - && wp_verify_nonce($_REQUEST[$nonce], $action)
731 - );
732 - }
733 -
734 - /**
735 - * Retrieve all the options stored by Wordpress in the database.
736 - *
737 - * @return array All the options stored by Wordpress in the database.
738 - */
739 - private static function getSiteOptions() {
740 - $settings = array();
741 -
742 - if (array_key_exists('wpdb', $GLOBALS)) {
743 - $results = $GLOBALS['wpdb']->get_results(
744 - 'SELECT * FROM ' . $GLOBALS['wpdb']->options . ' WHERE option_name NOT LIKE "%_transient_%" ORDER BY option_id ASC'
745 - );
746 -
747 - foreach ($results as $row) {
748 - $settings[$row->option_name] = $row->option_value;
749 - }
750 - }
751 -
752 - return $settings;
753 - }
754 -
755 - /**
756 - * Check what Wordpress options were changed comparing the values in the database
757 - * with the values sent through a simple request using a GET or POST method.
758 - *
759 - * @param array $request The content of the global variable GET or POST considering SERVER[REQUEST_METHOD].
760 - * @return array A list of all the options that were changes through this request.
761 - */
762 - public static function whatOptionsWereChanged($request = array())
763 - {
764 - $options_changed = [ 'original' => [], 'changed' => [] ];
765 -
766 - $site_options = self::getSiteOptions();
767 -
768 - foreach ($request as $req_name => $req_value) {
769 - if (array_key_exists($req_name, $site_options) && $site_options[ $req_name ] != $req_value ) {
770 - $options_changed['original'][ $req_name ] = $site_options[ $req_name ];
771 - $options_changed['changed'][ $req_name ] = $req_value;
772 - }
773 - }
774 -
775 - return $options_changed;
776 - }
777 -
778 483
779 484 }