PluginProbe
Password Protected — Lock Entire Site, Pages, Posts, Categories, and Partial Content / 2.6.6
Password Protected — Lock Entire Site, Pages, Posts, Categories, and Partial Content v2.6.6
2.8.4 2.8.3 2.8.2 2.8.1 trunk 1.0 1.1 1.2 1.2.1 1.2.2 1.3 1.4 1.5 1.6 1.6.1 1.6.2 1.7 1.7.1 1.7.2 1.8 1.9 2.0 2.0.1 2.0.2 2.0.3 All 63 releases
password-protected / password-protected.php

password-protected.php in Password Protected — Lock Entire Site, Pages, Posts, Categories, and Partial Content 2.6.6, at password-protected.php

965 lines 25.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /*
3 Plugin Name: Password Protected
4 Plugin URI: https://wordpress.org/plugins/password-protected/
5 Description: A very simple way to quickly password protect your WordPress site with a single password. Please note: This plugin does not restrict access to uploaded files and images and does not work with some caching setups.
6 Version: 2.6.6
7 Author: Password Protected
8 Text Domain: password-protected
9 Author URI: https://passwordprotectedwp.com/
10 License: GPLv2
11 */
12 /*
13 This program is free software; you can redistribute it and/or modify
14 it under the terms of the GNU General Public License, version 2, as
15 published by the Free Software Foundation.
16
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
21
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 */
26
27 /**
28 * @todo Use wp_hash_password() ?
29 * @todo Remember me
30 */
31
32 define( 'PASSWORD_PROTECTED_SUBDIR', '/' . str_replace( basename( __FILE__ ), '', plugin_basename( __FILE__ ) ) );
33 define( 'PASSWORD_PROTECTED_URL', plugins_url( PASSWORD_PROTECTED_SUBDIR ) );
34 define( 'PASSWORD_PROTECTED_DIR', plugin_dir_path( __FILE__ ) );
35
36 require_once PASSWORD_PROTECTED_DIR . 'includes/freemius.php';
37
38 global $Password_Protected;
39 $Password_Protected = new Password_Protected();
40
41 class Password_Protected {
42
43 var $version = '2.6.6';
44 var $admin = null;
45 var $errors = null;
46 var $admin_caching = null;
47
48 /**
49 * Constructor
50 */
51 public function __construct() {
52
53 $this->errors = new WP_Error();
54
55 register_activation_hook( __FILE__, array( &$this, 'install' ) );
56
57 add_action( 'plugins_loaded', array( $this, 'load_plugin_textdomain' ) );
58
59 add_filter( 'password_protected_is_active', array( $this, 'allow_ip_addresses' ) );
60
61 add_action( 'init', array( $this, 'disable_caching' ), 1 );
62 add_action( 'init', array( $this, 'maybe_process_logout' ), 1 );
63 add_action( 'init', array( $this, 'maybe_process_login' ), 1 );
64 add_action( 'wp', array( $this, 'disable_feeds' ) );
65 add_action( 'template_redirect', array( $this, 'maybe_show_login' ), -10 );
66 add_filter( 'pre_option_password_protected_status', array( $this, 'allow_feeds' ) );
67 add_filter( 'pre_option_password_protected_status', array( $this, 'allow_administrators' ) );
68 add_filter( 'pre_option_password_protected_status', array( $this, 'allow_users' ) );
69 add_filter( 'rest_authentication_errors', array( $this, 'only_allow_logged_in_rest_access' ) );
70 add_action( 'init', array( $this, 'compat' ) );
71 add_action( 'password_protected_login_messages', array( $this, 'login_messages' ) );
72 add_action( 'login_enqueue_scripts', array( $this, 'load_theme_stylesheet' ), 5 );
73
74 add_action('password_protected_above_password_field', array( $this, 'password_protected_above_password_field' ));
75 add_action('password_protected_below_password_field', array( $this, 'password_protected_below_password_field' ));
76
77
78 // Available from WordPress 4.3+
79 if ( function_exists( 'wp_site_icon' ) ) {
80 add_action( 'password_protected_login_head', 'wp_site_icon' );
81 }
82
83 add_shortcode( 'password_protected_logout_link', array( $this, 'logout_link_shortcode' ) );
84
85 include_once dirname( __FILE__ ) . '/admin/admin-bar.php';
86
87 if ( is_admin() ) {
88
89
90 include_once dirname( __FILE__ ) . '/admin/admin-caching.php';
91 include_once dirname( __FILE__ ) . '/admin/admin.php';
92
93 $this->admin_caching = new Password_Protected_Admin_Caching( $this );
94 $this->admin = new Password_Protected_Admin();
95
96
97 }
98 include_once dirname( __FILE__ ) . '/admin/class-recaptcha.php';
99 new Password_Protected_reCAPTCHA();
100
101 include_once dirname( __FILE__ ) . '/includes/transient-functions.php';
102 }
103
104 /**
105 * I18n
106 */
107 public function load_plugin_textdomain() {
108
109 load_plugin_textdomain( 'password-protected', false, basename( dirname( __FILE__ ) ) . '/languages' );
110
111 }
112
113 /**
114 * Disable Page Caching
115 */
116 public function disable_caching() {
117
118 if ( $this->is_active() && ! defined( 'DONOTCACHEPAGE' ) ) {
119 define( 'DONOTCACHEPAGE', true );
120 }
121
122 }
123
124 /**
125 * Is Active?
126 *
127 * @return boolean Is password protection active?
128 */
129 public function is_active() {
130
131 global $wp_query;
132
133 // Always allow access to robots.txt
134 if ( isset( $wp_query ) && is_robots() ) {
135 return false;
136 }
137
138 if ( (bool) get_option( 'password_protected_status' ) ) {
139 $is_active = true;
140 } else {
141 $is_active = false;
142 }
143
144 $is_active = apply_filters( 'password_protected_is_active', $is_active );
145
146 if ( isset( $_GET['password-protected'] ) ) {
147 $is_active = true;
148 }
149
150 return $is_active;
151
152 }
153
154 /**
155 * Disable Feeds
156 *
157 * @todo An option/filter to prevent disabling of feeds.
158 */
159 public function disable_feeds() {
160
161 if ( $this->is_active() ) {
162 add_action( 'do_feed', array( $this, 'disable_feed' ), 1 );
163 add_action( 'do_feed_rdf', array( $this, 'disable_feed' ), 1 );
164 add_action( 'do_feed_rss', array( $this, 'disable_feed' ), 1 );
165 add_action( 'do_feed_rss2', array( $this, 'disable_feed' ), 1 );
166 add_action( 'do_feed_atom', array( $this, 'disable_feed' ), 1 );
167 }
168
169 }
170
171 /**
172 * Disable Feed
173 *
174 * @todo Make Translatable
175 */
176 public function disable_feed() {
177
178 wp_die( sprintf( __( 'Feeds are not available for this site. Please visit the <a href="%s">website</a>.', 'password-protected' ), get_bloginfo( 'url' ) ) );
179
180 }
181
182 /**
183 * Allow Feeds
184 *
185 * @param boolean $bool Allow feeds.
186 * @return boolean True/false.
187 */
188 public function allow_feeds( $bool ) {
189
190 if ( is_feed() && (bool) get_option( 'password_protected_feeds' ) ) {
191 return 0;
192 }
193
194 return $bool;
195
196 }
197
198 /**
199 * Allow Administrators
200 *
201 * @param boolean $bool Allow administrators.
202 * @return boolean True/false.
203 */
204 public function allow_administrators( $bool ) {
205
206 if ( ! is_admin() && current_user_can( 'manage_options' ) && (bool) get_option( 'password_protected_administrators' ) ) {
207 return 0;
208 }
209
210 return $bool;
211
212 }
213
214 /**
215 * Allow Users
216 *
217 * @param boolean $bool Allow administrators.
218 * @return boolean True/false.
219 */
220 public function allow_users( $bool ) {
221
222 if ( ! is_admin() && is_user_logged_in() && (bool) get_option( 'password_protected_users' ) ) {
223 return 0;
224 }
225
226 return $bool;
227
228 }
229
230 /**
231 * Allow IP Addresses
232 *
233 * If user has a valid email address, return false to disable password protection.
234 *
235 * @param boolean $bool Allow IP addresses.
236 * @return boolean True/false.
237 */
238 public function allow_ip_addresses( $bool ) {
239
240 $ip_addresses = $this->get_allowed_ip_addresses();
241
242 if ( isset( $_SERVER['REMOTE_ADDR'] ) && in_array( $_SERVER['REMOTE_ADDR'], $ip_addresses ) ) {
243 $bool = false;
244 }
245
246 return $bool;
247
248 }
249
250 /**
251 * Get Allowed IP Addresses
252 *
253 * @return array IP addresses.
254 */
255 public function get_allowed_ip_addresses() {
256
257 return explode( "\n", get_option( 'password_protected_allowed_ip_addresses' ) );
258
259 }
260
261 /**
262 * Allow the remember me function
263 *
264 * @return. boolean
265 */
266 public function allow_remember_me() {
267
268 return (bool) get_option( 'password_protected_remember_me' );
269
270 }
271
272 /**
273 * Encrypt Password
274 *
275 * @param string $password Password.
276 * @return string Encrypted password.
277 */
278 public function encrypt_password( $password ) {
279
280 return md5( $password );
281
282 }
283
284 /**
285 * Maybe Process Logout
286 */
287 public function maybe_process_logout() {
288
289 if ( isset( $_REQUEST['password-protected'] ) && sanitize_text_field( $_REQUEST['password-protected'] ) == 'logout' ) {
290
291 $this->logout();
292
293 if ( isset( $_REQUEST['redirect_to'] ) ) {
294 $redirect_to = remove_query_arg( 'password-protected', esc_url_raw( $_REQUEST['redirect_to'], array( 'http', 'https' ) ) );
295 } else {
296 $redirect_to = home_url( '/' );
297 }
298
299 $this->safe_redirect( $redirect_to );
300 exit();
301
302 }
303
304 }
305
306 /**
307 * Maybe Process Login
308 */
309 public function maybe_process_login() {
310
311 if ( $this->is_active() && isset( $_REQUEST['password_protected_pwd'] ) ) {
312
313 $password_protected_pwd = sanitize_text_field( $_REQUEST['password_protected_pwd'] );
314 $default_password = get_option( 'password_protected_password' );
315
316 $auth = false;
317 $p_id = 0;
318
319 if ( empty( $default_password ) ) {
320
321 $authentication = $this->password_protected_check_pro_password( $password_protected_pwd );
322 $auth = $authentication['auth'];
323 $p_id = $authentication['p_id'];
324
325 } else {
326
327 if ( ( hash_equals( $default_password, $this->encrypt_password( $password_protected_pwd ) ) && $default_password != '' ) || apply_filters( 'password_protected_process_login', false, $password_protected_pwd ) ) {
328 $auth = true;
329 }
330
331 if ( ! $auth ) {
332
333 $authentication = $this->password_protected_check_pro_password( $password_protected_pwd );
334 $auth = $authentication['auth'];
335 $p_id = $authentication['p_id'];
336 }
337
338 }
339
340 $this->errors = apply_filters( 'password_protected_verify_recaptcha', $this->errors );
341
342 if( count( @$this->errors->errors ) > 0 ) return;
343
344 $this->password_protected_process_login( $auth, $password_protected_pwd, $p_id );
345
346 }
347
348 }
349
350 public function password_protected_process_login( bool $auth, $requested_password, $password_id ) {
351
352 if( $auth )
353 $throttle = apply_filters( 'password_protected_check_for_throttling', true );
354
355
356 if( $auth && $throttle ) {
357
358 do_action( 'password_protected_success_login_attempt', 'global', $requested_password, $password_id );
359 $remember = isset( $_REQUEST['password_protected_rememberme'] ) ? boolval( $_REQUEST['password_protected_rememberme'] ) : false;
360
361 if ( ! $this->allow_remember_me() ) {
362 $remember = false;
363 }
364 $this->set_auth_cookie( $remember );
365
366 $redirect_to = isset( $_REQUEST['redirect_to'] ) ? esc_url($_REQUEST['redirect_to']) : '';
367 $redirect_to = apply_filters( 'password_protected_login_redirect', $redirect_to, $requested_password );
368
369 if ( ! empty( $redirect_to ) ) {
370 $this->safe_redirect( remove_query_arg( 'password-protected', $redirect_to ) );
371 exit;
372 } elseif ( isset( $_GET['password_protected_pwd'] ) ) {
373 $this->safe_redirect( remove_query_arg( 'password-protected' ) );
374 exit;
375 } else {
376 $this->safe_redirect( site_url() );
377 exit;
378 }
379 } else {
380 do_action( 'password_protected_failure_login_attempt', 'global', $requested_password, $password_id );
381
382 // ... otherwise incorrect password
383 $this->clear_auth_cookie();
384
385 $show_default_error = apply_filters( 'password_protected_throttling_error_messages', true );
386
387 if( $show_default_error )
388 $this->errors->add( 'incorrect_password', __( 'Incorrect Password', 'password-protected' ) );
389 }
390 }
391
392 /**
393 * password_protected_check_pro_password
394 *
395 * @param mixed $requested_password
396 * @return void
397 */
398 public function password_protected_check_pro_password( $requested_password ) {
399
400 $pro_passwords = apply_filters( 'password_protected_passwords', array() );
401 $pro_passwords = array_filter( $pro_passwords );
402 $auth = false;
403 $p_id = 0;
404
405 if( is_array( $pro_passwords ) && count( $pro_passwords ) > 0 ) {
406
407 foreach( $pro_passwords as $i => $p ) {
408
409 if ( ( hash_equals( $p, $this->encrypt_password( $requested_password ) ) && $pro_passwords != '' ) || apply_filters( 'password_protected_process_login', false, $requested_password ) ) {
410
411 $auth = apply_filters( 'password_protected_login_password_matched', $p, $this->errors );
412 $p_id = $i;
413 break;
414
415 }
416
417 }
418
419 } else {
420
421 $auth = false;
422
423 }
424
425 return array(
426 'auth' => $auth,
427 'p_id' => $p_id,
428 );
429 }
430
431 /**
432 * Is User Logged In?
433 *
434 * @return boolean
435 */
436 public function is_user_logged_in() {
437
438 return $this->is_active() && $this->validate_auth_cookie();
439
440 }
441
442 /**
443 * Maybe Show Login
444 */
445 public function maybe_show_login() {
446
447 if ( class_exists( 'Login_designer' ) ) {
448 if ( is_customize_preview() ) {
449 return 1;
450 }
451 }
452
453 // Filter for adding exceptions.
454 $show_login = apply_filters( 'password_protected_show_login', $this->is_active() );
455
456 // Logged in
457 if ( $this->is_user_logged_in() ) {
458 $show_login = false;
459 }
460
461 if ( ! $show_login ) {
462 return 1;
463 }
464
465 // Show login form
466 if ( isset( $_REQUEST['password-protected'] ) && 'login' == sanitize_text_field( $_REQUEST['password-protected'] ) ) {
467
468 $default_theme_file = locate_template( array( 'password-protected-login.php' ) );
469
470 if ( empty( $default_theme_file ) ) {
471 $default_theme_file = dirname( __FILE__ ) . '/theme/password-protected-login.php';
472 }
473
474 $theme_file = apply_filters( 'password_protected_theme_file', $default_theme_file );
475 if ( ! file_exists( $theme_file ) ) {
476 $theme_file = $default_theme_file;
477 }
478
479 load_template( $theme_file );
480 exit();
481
482 } else {
483 global $wp;
484
485 $redirect_to = add_query_arg( 'password-protected', 'login', home_url( $wp->request . '?' . $_SERVER['QUERY_STRING'] ) );
486
487 // URL to redirect back to after login
488 $redirect_to_url = apply_filters( 'password_protected_login_redirect_url', ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] );
489 if ( ! empty( $redirect_to_url ) ) {
490 $redirect_to = add_query_arg( 'redirect_to', urlencode( $redirect_to_url ), $redirect_to );
491 }
492
493 nocache_headers();
494 wp_redirect( $redirect_to );
495 exit();
496
497 }
498 }
499
500 /**
501 * Get Site ID
502 *
503 * @return string Site ID.
504 */
505 public function get_site_id() {
506
507 global $blog_id;
508 return 'bid_' . apply_filters( 'password_protected_blog_id', $blog_id );
509
510 }
511
512 /**
513 * Login URL
514 *
515 * @return string Login URL.
516 */
517 public function login_url() {
518 global $wp;
519 return add_query_arg( 'password-protected', 'login', home_url( $wp->request . '?' . $_SERVER['QUERY_STRING'] ) );
520
521 }
522
523 /**
524 * Logout
525 */
526 public function logout() {
527
528 $this->clear_auth_cookie();
529 do_action( 'password_protected_logout' );
530
531 }
532
533 /**
534 * Logout URL
535 *
536 * @param string $redirect_to Optional. Redirect URL.
537 * @return string Logout URL.
538 */
539 public function logout_url( $redirect_to = '' ) {
540
541 $query = array(
542 'password-protected' => 'logout',
543 'redirect_to' => esc_url_raw( $redirect_to ),
544 );
545
546 if ( empty( $query['redirect_to'] ) ) {
547 unset( $query['redirect_to'] );
548 }
549
550 return add_query_arg( $query, home_url() );
551
552 }
553
554 /**
555 * Logout Link
556 *
557 * @param array $args Link args.
558 * @return string HTML link tag.
559 */
560 public function logout_link( $args = null ) {
561
562 // Only show if user is logged in
563 if ( ! $this->is_user_logged_in() ) {
564 return '';
565 }
566
567 $args = wp_parse_args(
568 $args,
569 array(
570 'redirect_to' => '',
571 'text' => __( 'Logout', 'password-protected' ),
572 )
573 );
574
575 if ( empty( $args['text'] ) ) {
576 $args['text'] = __( 'Logout', 'password-protected' );
577 }
578
579 return sprintf( '<a href="%s">%s</a>', esc_url( $this->logout_url( $args['redirect_to'] ) ), esc_html( $args['text'] ) );
580
581 }
582
583 /**
584 * Logout Link Shortcode
585 *
586 * @param array $args Link args.
587 * @return string HTML link tag.
588 */
589 public function logout_link_shortcode( $atts, $content = null ) {
590
591 $atts = shortcode_atts(
592 array(
593 'redirect_to' => '',
594 'text' => $content,
595 ),
596 $atts,
597 'logout_link_shortcode'
598 );
599
600 return $this->logout_link( $atts );
601
602 }
603
604 /**
605 * Get Hashed Password
606 *
607 * @return string Hashed password.
608 */
609 public function get_hashed_password() {
610
611 return md5( get_option( 'password_protected_password' ) . wp_salt() );
612
613 }
614
615 /**
616 * Validate Auth Cookie
617 *
618 * @param string $cookie Cookie string.
619 * @param string $scheme Cookie scheme.
620 * @return boolean Validation successful?
621 */
622 public function validate_auth_cookie( $cookie = '', $scheme = '' ) {
623
624 if ( ! $cookie_elements = $this->parse_auth_cookie( $cookie, $scheme ) ) {
625 do_action( 'password_protected_auth_cookie_malformed', $cookie, $scheme );
626 return false;
627 }
628
629 extract( $cookie_elements, EXTR_OVERWRITE );
630
631 $expired = $expiration;
632
633 // Allow a grace period for POST and AJAX requests
634 if ( defined( 'DOING_AJAX' ) || 'POST' == $_SERVER['REQUEST_METHOD'] ) {
635 $expired += 3600;
636 }
637
638 // Quick check to see if an honest cookie has expired
639 if ( $expired < current_time( 'timestamp' ) ) {
640 do_action( 'password_protected_auth_cookie_expired', $cookie_elements );
641 return false;
642 }
643
644 $key = md5( $this->get_site_id() . $this->get_hashed_password() . '|' . $expiration );
645 $hash = hash_hmac( 'md5', $this->get_site_id() . '|' . $expiration, $key );
646
647 if ( $hmac != $hash ) {
648 do_action( 'password_protected_auth_cookie_bad_hash', $cookie_elements );
649 return false;
650 }
651
652 if ( $expiration < current_time( 'timestamp' ) ) { // AJAX/POST grace period set above
653 $GLOBALS['login_grace_period'] = 1;
654 }
655
656 return true;
657
658 }
659
660 /**
661 * Generate Auth Cookie
662 *
663 * @param int $expiration Expiration time in seconds.
664 * @param string $scheme Cookie scheme.
665 * @return string Cookie.
666 */
667 public function generate_auth_cookie( $expiration, $scheme = 'auth' ) {
668
669 $key = md5( $this->get_site_id() . $this->get_hashed_password() . '|' . $expiration );
670 $hash = hash_hmac( 'md5', $this->get_site_id() . '|' . $expiration, $key );
671 $cookie = $this->get_site_id() . '|' . $expiration . '|' . $hash;
672
673 return $cookie;
674
675 }
676
677 /**
678 * Parse Auth Cookie
679 *
680 * @param string $cookie Cookie string.
681 * @param string $scheme Cookie scheme.
682 * @return string Cookie string.
683 */
684 public function parse_auth_cookie( $cookie = '', $scheme = '' ) {
685 if ( empty( $cookie ) ) {
686
687 $cookie_name = $this->cookie_name();
688 $use_transient = get_option( 'password_protected_use_transient', '' );
689
690 if ( empty( $use_transient ) ) {
691 if ( empty( $_COOKIE[ $cookie_name ] ) ) {
692 return false;
693 }
694
695 $cookie = $_COOKIE[ $cookie_name ];
696 } else {
697 $cookie = pp_get_transient( $cookie_name );
698 }
699 }
700
701 $cookie_elements = explode( '|', $cookie );
702
703 if ( count( $cookie_elements ) != 3 ) {
704 return false;
705 }
706
707 list( $site_id, $expiration, $hmac ) = $cookie_elements;
708
709 return compact( 'site_id', 'expiration', 'hmac', 'scheme' );
710
711 }
712
713 /**
714 * Set Auth Cookie
715 *
716 * @todo
717 *
718 * @param boolean $remember Remember logged in.
719 * @param string $secure Secure cookie.
720 */
721 public function set_auth_cookie( $remember = false, $secure = '' ) {
722
723 if ( $remember ) {
724 $expiration_time = apply_filters( 'password_protected_auth_cookie_expiration', get_option( 'password_protected_remember_me_lifetime', 14 ) * DAY_IN_SECONDS, $remember );
725 $expiration = $expire = current_time( 'timestamp' ) + $expiration_time;
726 } else {
727 $expiration_time = apply_filters( 'password_protected_auth_cookie_expiration', DAY_IN_SECONDS * 20, $remember );
728 $expiration = current_time( 'timestamp' ) + $expiration_time;
729 $expire = 0;
730 }
731
732 if ( '' === $secure ) {
733 $secure = is_ssl();
734 }
735
736 $secure_password_protected_cookie = apply_filters( 'password_protected_secure_password_protected_cookie', false, $secure );
737 $password_protected_cookie = $this->generate_auth_cookie( $expiration, 'password_protected' );
738
739 $use_transient = get_option( 'password_protected_use_transient', '' );
740 if ( empty( $use_transient ) ) {
741 setcookie( $this->cookie_name(), $password_protected_cookie, $expire, COOKIEPATH, COOKIE_DOMAIN, $secure_password_protected_cookie, true );
742 if ( COOKIEPATH != SITECOOKIEPATH ) {
743 setcookie( $this->cookie_name(), $password_protected_cookie, $expire, SITECOOKIEPATH, COOKIE_DOMAIN, $secure_password_protected_cookie, true );
744 }
745 } else {
746 pp_set_transient( $this->cookie_name(), $password_protected_cookie, $expiration_time );
747 }
748
749 }
750
751 /**
752 * Clear Auth Cookie
753 */
754 public function clear_auth_cookie() {
755 $use_transient = get_option( 'password_protected_use_transient', '' );
756 if ( empty( $use_transient ) ) {
757 setcookie( $this->cookie_name(), ' ', current_time( 'timestamp' ) - 31536000, COOKIEPATH, COOKIE_DOMAIN );
758 setcookie( $this->cookie_name(), ' ', current_time( 'timestamp' ) - 31536000, SITECOOKIEPATH, COOKIE_DOMAIN );
759 } else {
760 pp_delete_transient( $this->cookie_name() );
761 }
762
763 }
764
765 /**
766 * Cookie Name
767 *
768 * @return string Cookie name.
769 */
770 public function cookie_name() {
771
772 /**
773 * Filters the cookie name
774 */
775 return apply_filters( 'password_protected_cookie_name', $this->get_site_id() . '_password_protected_auth', $this );
776
777 }
778
779 /**
780 * Install
781 */
782 public function install() {
783
784 $old_version = get_option( 'password_protected_version' );
785
786 // 1.1 - Upgrade to MD5
787 if ( empty( $old_version ) || $old_version == '1.1' ) {
788 $pwd = get_option( 'password_protected_password' );
789 if ( ! empty( $pwd ) ) {
790 $new_pwd = $this->encrypt_password( $pwd );
791 update_option( 'password_protected_password', $new_pwd );
792 }
793 }
794
795 update_option( 'password_protected_version', $this->version );
796
797 }
798
799 /**
800 * Compat
801 *
802 * Support for 3rd party plugins:
803 *
804 * - Login Logo https://wordpress.org/plugins/login-logo/
805 * - Uber Login Logo https://wordpress.org/plugins/uber-login-logo/
806 */
807 public function compat() {
808
809 if ( class_exists( 'CWS_Login_Logo_Plugin' ) ) {
810
811 // Add support for Mark Jaquith's Login Logo plugin
812 add_action( 'password_protected_login_head', array( new CWS_Login_Logo_Plugin(), 'login_head' ) );
813
814 } elseif ( class_exists( 'UberLoginLogo' ) ) {
815
816 // Add support for Uber Login Logo plugin
817 add_action( 'password_protected_login_head', array( 'UberLoginLogo', 'replaceLoginLogo' ) );
818
819 }
820
821 }
822
823 /**
824 * Login Messages
825 * Outputs messages and errors in the login template.
826 */
827 public function login_messages() {
828
829 // Add message
830 $message = apply_filters( 'password_protected_login_message', '' );
831 if ( ! empty( $message ) ) {
832 echo $message . "\n";
833 }
834
835 if ( $this->errors->get_error_code() ) {
836
837 $errors = '';
838 $messages = '';
839
840 foreach ( $this->errors->get_error_codes() as $code ) {
841 $severity = $this->errors->get_error_data( $code );
842 foreach ( $this->errors->get_error_messages( $code ) as $error ) {
843 if ( 'message' == $severity ) {
844 $messages .= $error . '<br />';
845 } else {
846 $errors .= $error . '<br />';
847 }
848 }
849 }
850
851 if ( ! empty( $errors ) ) {
852 echo '<div id="login_error" class="notice notice-error">' . apply_filters( 'password_protected_login_errors', $errors ) . "</div>\n";
853 }
854 if ( ! empty( $messages ) ) {
855 echo '<p class="message">' . apply_filters( 'password_protected_login_messages', $messages ) . "</p>\n";
856 }
857 }
858
859 }
860
861 /**
862 * Load Theme Stylesheet
863 *
864 * Check wether a 'password-protected-login.css' stylesheet exists in your theme
865 * and if so loads it.
866 *
867 * Works with child themes.
868 *
869 * Possible to specify a different file in the theme folder via the
870 * 'password_protected_stylesheet_file' filter (allows for theme subfolders).
871 */
872 public function load_theme_stylesheet() {
873
874 $filename = apply_filters( 'password_protected_stylesheet_file', 'password-protected-login.css' );
875
876 $located = locate_template( $filename );
877
878 if ( ! empty( $located ) ) {
879
880 $stylesheet_directory = trailingslashit( get_stylesheet_directory() );
881 $template_directory = trailingslashit( get_template_directory() );
882
883 if ( $stylesheet_directory == substr( $located, 0, strlen( $stylesheet_directory ) ) ) {
884 wp_enqueue_style( 'password-protected-login', get_stylesheet_directory_uri() . '/' . $filename );
885 } elseif ( $template_directory == substr( $located, 0, strlen( $template_directory ) ) ) {
886 wp_enqueue_style( 'password-protected-login', get_template_directory_uri() . '/' . $filename );
887 }
888 }
889
890 }
891
892 /**
893 * Safe Redirect
894 *
895 * Ensure the redirect is to the same site or pluggable list of allowed domains.
896 * If invalid will redirect to ...
897 * Based on the WordPress wp_safe_redirect() function.
898 */
899 public function safe_redirect( $location, $status = 302 ) {
900
901 $location = wp_sanitize_redirect( $location );
902 $location = wp_validate_redirect( $location, home_url() );
903
904 wp_redirect( $location, $status );
905
906 }
907
908 /**
909 * Is Plugin Supported?
910 *
911 * Check to see if there are any known reasons why this plugin may not work in
912 * the user's hosting environment.
913 *
914 * @return boolean
915 */
916 static function is_plugin_supported() {
917
918 return true;
919
920 }
921
922 /**
923 * Check whether a given request has permissions
924 *
925 * Always allow logged in users who require REST API for Gutenberg
926 * and other admin/plugin compatibility.
927 *
928 * @param WP_REST_Request $access Full details about the request.
929 * @return WP_Error|boolean
930 */
931 public function only_allow_logged_in_rest_access( $access ) {
932
933 // If user is not logged in
934 if ( $this->is_active() && ! $this->is_user_logged_in() && ! is_user_logged_in() && ! (bool) get_option( 'password_protected_rest' ) ) {
935 return new WP_Error( 'rest_cannot_access', __( 'Only authenticated users can access the REST API.', 'password-protected' ), array( 'status' => rest_authorization_required_code() ) );
936 }
937
938 return $access;
939
940 }
941
942 /**
943 * Print text above password field
944 * @return void.
945 */
946 public function password_protected_above_password_field() {
947 $text = get_option('password_protected_text_above_password');
948 if( ! empty( $text ) ) {
949 echo '<div class="password-protected-text-above" style="width:100%;">' . wp_kses_post( $text ) . '</div>';
950 }
951 }
952
953 /**
954 * Print text below password field
955 * @return void.
956 */
957 public function password_protected_below_password_field() {
958 $text = get_option('password_protected_text_below_password');
959 if( ! empty( $text ) ) {
960 echo '<div class="password-protected-text-below" style="width:100%">' . wp_kses_post( $text ) . '</div>';
961 }
962 }
963
964 }
965