| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
Plugin Name: Password Protected |
| 5 |
Plugin URI: http://www.benhuson.co.uk/ |
| 6 |
A very simple way to quickly password protect your WordPress site with a single password. Integrates seamlessly into your WordPress privacy settings. |
| 7 |
Version: 1.0 |
| 8 |
Author: Ben Huson |
| 9 |
Author URI: http://www.benhuson.co.uk/ |
| 10 |
License: GPLv2 |
| 11 |
*/ |
| 12 |
|
| 13 |
/* |
| 14 |
Copyright 2012 Ben Huson (email : ben@thewhiteroom.net) |
| 15 |
|
| 16 |
This program is free software; you can redistribute it and/or modify |
| 17 |
it under the terms of the GNU General Public License, version 2, as |
| 18 |
published by the Free Software Foundation. |
| 19 |
|
| 20 |
This program is distributed in the hope that it will be useful, |
| 21 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 22 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 23 |
GNU General Public License for more details. |
| 24 |
|
| 25 |
You should have received a copy of the GNU General Public License |
| 26 |
along with this program; if not, write to the Free Software |
| 27 |
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 28 |
*/ |
| 29 |
|
| 30 |
// Start session if not already started... |
| 31 |
if ( session_id() == '' ) |
| 32 |
session_start(); |
| 33 |
|
| 34 |
// Setup Password Protected |
| 35 |
global $Password_Protected; |
| 36 |
$Password_Protected = new Password_Protected(); |
| 37 |
|
| 38 |
class Password_Protected { |
| 39 |
|
| 40 |
var $admin = null; |
| 41 |
var $errors = null; |
| 42 |
|
| 43 |
/** |
| 44 |
* Constructor |
| 45 |
*/ |
| 46 |
function Password_Protected() { |
| 47 |
$this->errors = new WP_Error(); |
| 48 |
add_action( 'init', array( $this, 'maybe_process_login' ), 1 ); |
| 49 |
add_action( 'template_redirect', array( $this, 'maybe_show_login' ), 1 ); |
| 50 |
$this->disable_feeds(); |
| 51 |
if ( is_admin() ) { |
| 52 |
include_once( dirname( __FILE__ ) . '/admin/admin.php' ); |
| 53 |
$this->admin = new Password_Protected_Admin(); |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Is Active? |
| 59 |
*/ |
| 60 |
function is_active() { |
| 61 |
if ( (bool) get_option( 'password_protected_status' ) ) |
| 62 |
return true; |
| 63 |
return false; |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Disable Feeds |
| 68 |
*/ |
| 69 |
function disable_feeds() { |
| 70 |
add_action( 'do_feed', array( $this, 'disable_feed' ), 1 ); |
| 71 |
add_action( 'do_feed_rdf', array( $this, 'disable_feed' ), 1 ); |
| 72 |
add_action( 'do_feed_rss', array( $this, 'disable_feed' ), 1 ); |
| 73 |
add_action( 'do_feed_rss2', array( $this, 'disable_feed' ), 1 ); |
| 74 |
add_action( 'do_feed_atom', array( $this, 'disable_feed' ), 1 ); |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Disable Feed |
| 79 |
* @todo Make Translatable |
| 80 |
*/ |
| 81 |
function disable_feed() { |
| 82 |
wp_die( __( 'Feeds are not available for this site. Please visit the <a href="'. get_bloginfo( 'url' ) .'">website</a>.' ) ); |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Maybe Process Login |
| 87 |
*/ |
| 88 |
function maybe_process_login() { |
| 89 |
if ( $this->is_active() && isset( $_REQUEST['password_protected_pwd'] ) ) { |
| 90 |
$password_protected_pwd = $_REQUEST['password_protected_pwd']; |
| 91 |
$pwd = get_option( 'password_protected_password' ); |
| 92 |
// If correct password... |
| 93 |
if ( $password_protected_pwd == $pwd && $pwd != '' ) { |
| 94 |
$_SESSION[$this->get_site_id() . '_password_protected_auth'] = 1; |
| 95 |
} else { |
| 96 |
// ... otherwise incorrect password |
| 97 |
$this->errors->add( 'incorrect_password', 'Incorrect Password' ); |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
// Log out |
| 102 |
if ( isset( $_REQUEST['action'] ) && $_REQUEST['action'] == 'logout' ) { |
| 103 |
$_SESSION[$this->get_site_id() . '_password_protected_auth'] = 0; |
| 104 |
$this->errors = new WP_Error(); |
| 105 |
$this->errors->add( 'logged_out', __( 'You are now logged out.' ), 'message' ); |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* Maybe Show Login |
| 111 |
*/ |
| 112 |
function maybe_show_login() { |
| 113 |
// Don't show login if not enabled |
| 114 |
if ( ! $this->is_active() ) |
| 115 |
return; |
| 116 |
|
| 117 |
// Logged in |
| 118 |
if ( isset( $_SESSION[$this->get_site_id() . '_password_protected_auth'] ) && $_SESSION[$this->get_site_id() . '_password_protected_auth'] == 1 ) |
| 119 |
return; |
| 120 |
|
| 121 |
// Show login form |
| 122 |
include( dirname( __FILE__ ) . '/theme/login.php' ); |
| 123 |
exit(); |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* Get Site ID |
| 128 |
*/ |
| 129 |
function get_site_id() { |
| 130 |
global $blog_id; |
| 131 |
return apply_filters( 'password_protected_blog_id', $blog_id ); |
| 132 |
} |
| 133 |
|
| 134 |
} |
| 135 |
|
| 136 |
?> |