| 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.1 |
| 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 $version = '1.1'; |
| 41 |
var $admin = null; |
| 42 |
var $errors = null; |
| 43 |
|
| 44 |
/** |
| 45 |
* Constructor |
| 46 |
*/ |
| 47 |
function Password_Protected() { |
| 48 |
$this->errors = new WP_Error(); |
| 49 |
register_activation_hook( __FILE__, array( &$this, 'install' ) ); |
| 50 |
add_action( 'init', array( $this, 'maybe_process_login' ), 1 ); |
| 51 |
add_action( 'template_redirect', array( $this, 'maybe_show_login' ), 1 ); |
| 52 |
$this->disable_feeds(); |
| 53 |
if ( is_admin() ) { |
| 54 |
include_once( dirname( __FILE__ ) . '/admin/admin.php' ); |
| 55 |
$this->admin = new Password_Protected_Admin(); |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Is Active? |
| 61 |
*/ |
| 62 |
function is_active() { |
| 63 |
if ( (bool) get_option( 'password_protected_status' ) ) |
| 64 |
return true; |
| 65 |
return false; |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Disable Feeds |
| 70 |
*/ |
| 71 |
function disable_feeds() { |
| 72 |
add_action( 'do_feed', array( $this, 'disable_feed' ), 1 ); |
| 73 |
add_action( 'do_feed_rdf', array( $this, 'disable_feed' ), 1 ); |
| 74 |
add_action( 'do_feed_rss', array( $this, 'disable_feed' ), 1 ); |
| 75 |
add_action( 'do_feed_rss2', array( $this, 'disable_feed' ), 1 ); |
| 76 |
add_action( 'do_feed_atom', array( $this, 'disable_feed' ), 1 ); |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* Disable Feed |
| 81 |
* @todo Make Translatable |
| 82 |
*/ |
| 83 |
function disable_feed() { |
| 84 |
wp_die( __( 'Feeds are not available for this site. Please visit the <a href="'. get_bloginfo( 'url' ) .'">website</a>.' ) ); |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Maybe Process Login |
| 89 |
*/ |
| 90 |
function maybe_process_login() { |
| 91 |
if ( $this->is_active() && isset( $_REQUEST['password_protected_pwd'] ) ) { |
| 92 |
$password_protected_pwd = $_REQUEST['password_protected_pwd']; |
| 93 |
$pwd = get_option( 'password_protected_password' ); |
| 94 |
// If correct password... |
| 95 |
if ( md5( $password_protected_pwd ) == $pwd && $pwd != '' ) { |
| 96 |
$_SESSION[$this->get_site_id() . '_password_protected_auth'] = 1; |
| 97 |
} else { |
| 98 |
// ... otherwise incorrect password |
| 99 |
$this->errors->add( 'incorrect_password', 'Incorrect Password' ); |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
// Log out |
| 104 |
if ( isset( $_REQUEST['action'] ) && $_REQUEST['action'] == 'logout' ) { |
| 105 |
$_SESSION[$this->get_site_id() . '_password_protected_auth'] = 0; |
| 106 |
$this->errors = new WP_Error(); |
| 107 |
$this->errors->add( 'logged_out', __( 'You are now logged out.' ), 'message' ); |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* Maybe Show Login |
| 113 |
*/ |
| 114 |
function maybe_show_login() { |
| 115 |
// Don't show login if not enabled |
| 116 |
if ( ! $this->is_active() ) |
| 117 |
return; |
| 118 |
|
| 119 |
// Logged in |
| 120 |
if ( isset( $_SESSION[$this->get_site_id() . '_password_protected_auth'] ) && $_SESSION[$this->get_site_id() . '_password_protected_auth'] == 1 ) |
| 121 |
return; |
| 122 |
|
| 123 |
// Show login form |
| 124 |
include( dirname( __FILE__ ) . '/theme/login.php' ); |
| 125 |
exit(); |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* Get Site ID |
| 130 |
*/ |
| 131 |
function get_site_id() { |
| 132 |
global $blog_id; |
| 133 |
return apply_filters( 'password_protected_blog_id', $blog_id ); |
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* Install |
| 138 |
*/ |
| 139 |
function install() { |
| 140 |
$old_version = get_option( 'password_protected_version' ); |
| 141 |
|
| 142 |
// 1.1 - Upgrade to MD5 |
| 143 |
if ( empty( $old_version ) || version_compare( '1.1', $old_version ) ) { |
| 144 |
$pwd = get_option( 'password_protected_password' ); |
| 145 |
if ( ! empty( $pwd ) ) { |
| 146 |
$new_pwd = md5( $pwd ); |
| 147 |
update_option( 'password_protected_password', $new_pwd ); |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
update_option( 'password_protected_version', $this->version ); |
| 152 |
} |
| 153 |
|
| 154 |
} |
| 155 |
|
| 156 |
?> |