capabilities = $wpdb->prefix . "capabilities";
$this->role = "unapproved"; //do not change this or bad things will happen to good people
$this->role_ref = "unapproved"; //leave it alone
$this->rolename = "Unapproved User"; //Role name for unapproved users. Change this if you like (will require to deactivate and reactivate the plugin to register)
$this->options = "absolute_privacy"; //name for options array();
$this->default_role = "absolute_privacy_default"; //stores the default role on plugin installation (usually "Subscriber")
}
/**
* createRole function.
* Creates a new role on plugin activation and keeps track of the default role
*
* @access public
* @return void
*/
function createRole(){
global $wp_roles;
$default = get_option('default_role');
/* Let's set the default options if they don't exist */
$options = get_option($this->options);
if(!$options){
/* This section looks a little wonky here, but it has to for proper formatting in the textarea boxes */
$to_update = array( 'members_enabled' => 'yes', // turn on the lockdown
'rss_control' => 'off', //disable the RSS
'pending_welcome_email_subject' => 'Your account with ' . stripslashes(get_option('blogname')) . ' is under review',
'pending_welcome_message' => 'Hi %name%,
Thanks for registering for %blogname%! Your registration is currently being reviewed. You will not be able to login until it has been approved. You will receive an email at that time. Thanks for your patience.
Sincerely,
%blogname%',
'account_approval_email_subject' => 'Your account has been approved!',
'account_approval_message' => 'Your registration with %blogname% has been approved!
Your may login using the following information:
Username: %username%
Password: (hidden)
URL: %blogurl%/wp-login.php',
'admin_approval_email_subject' => 'A new user is waiting approval',
'admin_approval_message' => 'A new user has registered for %blogname% and is waiting your approval. You may approve or delete them here: %approval_url%
This user cannot log in until you approve them.'
);
foreach($to_update as $key => $value){
$options[$key] = $value;
}
update_option($this->options, $options);
}
$role = get_role($this->role);
if(!$role) {
$wp_roles->add_role($this->role, $this->rolename); //create the unapproved role
$role = get_role($this->role);
$role->add_cap('level_0'); //give the unaproved role the 0 capability
update_option($this->default_role, $default); //saves the user's default role preference
$this->_changeDefaultRole($enabled="yes");
return true;
}
else return false;
}
/**
* destroyRole function.
* Deletes role on plugin deactivation
*
* @access public
* @return void
*/
function destroyRole(){
global $wp_roles;
$wp_roles->remove_role($this->role);
$this->_changeDefaultRole($enabled="no");
}
/**
* _changeDefaultRole function.
* Changes the default blog role
*
* @access private
* @param mixed $enabled
* @return void
*/
function _changeDefaultRole($enabled){
$default = get_option($this->default_role);
if($enabled == "yes"){
update_option('default_role', $this->role);
}
else{
update_option('default_role', $default); //change back to default
}
}
/**
* registrationBox function.
* Echos input boxes for first name, last name, and password to
* the registration box.
*
* @access public
* @return void
*/
function registrationBox(){
$options = get_option($this->options);
$output = '
';
$output .= "\n" . '
Your account must be approved before you will be able to login. You will be emailed once it is approved.
';
echo $output;
}
/**
* checkRegErrors function.
* Adds error checks to registration form
*
* @access public
* @param mixed $errors
* @return void
*/
function checkRegErrors($errors){
if(empty($_POST['pswd1']) || empty($_POST['pswd2']) || $_POST['pswd1'] == '' || $_POST['pswd2'] == ''){
$errors->add('password', __('ERROR: Please enter a password in both password boxes.'));
}elseif ($_POST['pswd1'] != $_POST['pswd2']){
$errors->add('password', __('ERROR: Passwords do not match.'));}
if(empty($_POST['first_name']) || empty($_POST['last_name'])){
$errors->add('name', __('ERROR: You must enter a first and last name'));}
return $errors;
}
/**
* regCSS function.
* Adds CSS for registration form
*
* @access public
* @return void
*/
function regCSS(){
echo '';
}
/**
* addNewUser function.
* Adds new registrants name and password
* to the database
*
* @access public
* @param mixed $user_id
* @return void
*/
function addNewUser($user_id){ //adds user meta to the database on registration
global $wpdb;
$options = get_option($this->options);
update_usermeta($user_id, 'first_name', attribute_escape(stripslashes($_POST['first_name'])));
update_usermeta($user_id, 'last_name', attribute_escape(stripslashes($_POST['last_name'])));
$user_role = new WP_User($user_id);
$user_role->set_role($this->role);
if(!empty($_POST['pswd1'])){
$_POST['pswd1'] = wp_set_password(attribute_escape(stripslashes($_POST['pswd1'])), $user_id);
}
$_POST['pswd1'] = '';
$_POST['pswd2'] = '';
unset($_POST['pswd1']);
unset($_POST['pswd2']);
}
/**
* installOptionsMenu function.
*
* @access public
* @return void
*/
function installOptionsMenu() { // install the options menu
if (function_exists('current_user_can')) {
if (!current_user_can('manage_options')) return;
} else {
global $user_level;
get_currentuserinfo();
if ($user_level < 10) return;
}
if (function_exists('add_options_page')) {
add_options_page(__('Absolute Privacy'), __('Absolute Privacy'), 1, __FILE__, array(&$this,'optionsPage'));
}
}
/**
* optionsPage function.
* Displays the settings page
*
* @access public
* @return void
*/
function optionsPage(){
if( isset($_GET['mode']) && ($_GET['mode'] == "moderate") ) {
include('ap_mod_email.php');
return;
}
global $wpdb;
$plugin_path = get_bloginfo('wpurl') . '/wp-content/plugins/' . dirname(plugin_basename(__FILE__));
if (isset($_POST['update_options'])) {
$options['members_enabled'] = trim($_POST['members_enabled'],'{}');
$options['redirect_page'] = trim($_POST['redirect_page'],'{}');
$options['allowed_pages'] = trim($_POST['allowed_pages'],'{}');
$options['admin_block'] = trim($_POST['admin_block'], '{}');
$options['rss_control'] = trim($_POST['rss_control'], '{}');
$options['rss_characters'] = trim($_POST['rss_characters'], '{}');
$options['pending_welcome_email_subject'] = trim(stripslashes($_POST['pending_welcome_email_subject']), '{}');
$options['pending_welcome_message'] = trim(stripslashes($_POST['pending_welcome_message']), '{}');
$options['account_approval_email_subject'] = trim(stripslashes($_POST['account_approval_email_subject']), '{}');
$options['account_approval_message'] = trim(stripslashes($_POST['account_approval_message']), '{}');
$options['admin_approval_email_subject'] = trim(stripslashes($_POST['admin_approval_email_subject']), '{}');
$options['admin_approval_message'] = trim(stripslashes($_POST['admin_approval_message']), '{}');
update_option($this->options, $options);
// Show a message to say we've done something
echo '
options);
$user = get_userdata($user_id); //object with user info
switch($type){
case('pending_welcome'):
$to_email = $user->user_email;
$subject = $options['pending_welcome_email_subject'];
$message = $options['pending_welcome_message'];
break;
case('account_approved'):
$to_email = $user->user_email;
$subject = $options['account_approval_email_subject'];
$message = $options['account_approval_message'];
break;
case('admin_notification'):
$to_email = get_bloginfo('admin_email');
$subject = $options['admin_approval_email_subject'];
$message = $options['admin_approval_message'];
break;
}
$replace = array('%username%' => $user->user_login,
'%name%' => $user->display_name,
'%blogname%' => get_bloginfo('name'),
'%blogurl%' => get_bloginfo('url'),
'%approval_url%' => get_bloginfo('url') . '/wp-admin/options-general.php?page=' . dirname(plugin_basename(__FILE__)) . '/absolute_privacy.php&mode=moderate&id='.$user_id
);
$email_body = strtr(stripslashes($message), $replace); //get email body and replace variables
$headers = "MIME-Version: 1.0\n" .
"From: " . get_option('blogname') . " <" . get_option('admin_email') . ">";
wp_mail( $to_email, $subject, $email_body, $headers);
return;
}
/**
* moderateMenu function.
* installes the "Moderate Users" page, which displays all users currently not approved on the blog
* @access public
* @return void
*/
function moderateMenu(){
if (function_exists('current_user_can')) {
if (!current_user_can('manage_options')) return;
} else {
global $user_level;
get_currentuserinfo();
if ($user_level < 10) return;
}
add_submenu_page('users.php', 'Moderate Users', 'Moderate Users', 'edit_themes', basename(__FILE__), array(&$this,'moderateUsers'));
}
/**
* moderateUsers function.
* handles the moderate users function
*
* @access public
* @return void
*/
function moderateUsers(){
global $wpdb;
$options = get_option($this->options);
if (function_exists('current_user_can')) {
if (!current_user_can('manage_options')) wp_die('You are not able to do that');
} else {
global $user_level;
get_currentuserinfo();
if ($user_level < 10) wp_die('You are not able to do that');
}
//get all users who are unapproved
$query = "SELECT user_id FROM ".$wpdb->usermeta." WHERE meta_key = '" . $this->capabilities . "' AND meta_value LIKE '%" . $this->role_ref . "%';";
$unapproved = $wpdb->get_col($query);
if (isset($_POST['update_options'])) {
if ($_POST['update_options'] == "Delete Selected Users"){
foreach($_POST['users'] as $user){
if (!current_user_can('delete_user', $user)){
wp_die(__('You can’t delete that user.'));
}
if($user == $current_user->ID) {
wp_die('You cannot delete yourself.');
}
wp_delete_user($user);
}
// Show a message to say we've done something
echo '
' . __('User(s) deleted') . '
';
return;
}
if ($_POST['update_options'] == "Approve Selected Users"){
foreach($_POST['users'] as $user){
$user = get_userdata($user);
$user_role = new WP_User($user->ID);
$user_role->set_role("subscriber");
$this->handleEmail($user->ID, $type= 'account_approved');
}
// Show a message to say we've done something
echo '
' . __('User(s) Approved. Notifications sent via email.') . '
';
return;
}
}
$output = '
Absolute Privacy: Moderate Users
";
return;
}
$output = '
Approved users will receive an email notification of their approval.
';
echo $output;
}
function check_is_feed($content){
$options = get_option($this->options);
if(is_feed()) :
switch($options['rss_control']) {
case "on":
//allow full RSS
break;
case "headline":
$content = '';
break;
case "excerpt":
$content = substr(strip_tags(get_the_content()), 0, $options['rss_characters']) . "...";
break;
}
endif;
return $content;
}
/**
* lockDown function.
* redirects non-logged users if setting is enabled
*
* @access public
* @return void
*/
function lockDown(){
global $wp_version;
$options = get_option($this->options);
if(is_feed() && $options['rss_control'] != "off") return; //allow RSS feed to be handled by check_is_feed() function unless the RSS feed is disabled.
if(($options['members_enabled'] == "yes") && (!is_user_logged_in()) ){
if( isset($options['allowed_pages']) && $options['allowed_pages'] != '' ){
$allowed_pages = explode(',', $options['allowed_pages']);
if(is_page($allowed_pages) || is_single($allowed_pages) ) return; //let them visit the allowed pages
}
if( (isset($options['redirect_page'])) && ($options['redirect_page'] != '') ){
if(is_single($options['redirect_page']) || is_page($options['redirect_page'])) return; //end the function is the visitor is already on the redirect_page page
$requested_url = get_permalink($options['redirect_page']);
if($wp_version < 2.8){
$requested_url = urlencode($requested_url); //WP 2.8+ encodes the URL
}
$url = $requested_url;
}else{
$requested_url = (!empty($_SERVER['HTTPS'])) ? "https://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'] : "http://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
if($wp_version < 2.8){
$requested_url = urlencode($requested_url); //WP 2.8+ encodes the URL
}
$url = wp_login_url($requested_url);
}
wp_redirect($url, 302);
exit();
}
return;
}
function adminLockDown(){
global $userdata, $userlevel;
if(!is_admin() || !(is_user_logged_in()) ) return;
//if it's not an admin page or the user isn't logged in at all, we don't need this
$options= get_option($this->options);
$user_role = new WP_User($userdata->ID);
$capabilities = $this->capabilities;
if ($options['admin_block'] == "yes" && array_key_exists('subscriber', $user_role->$capabilities)){
$url = get_bloginfo('url');
wp_redirect($url, 302);
exit();
}
}
} // end class declaration
} // end !class_exists check
if (class_exists("absolutePrivacy")) {
$absolutePrivacy = new absolutePrivacy();
}
//Actions and Filters
if (isset($absolutePrivacy)) {
register_activation_hook(__FILE__, array(&$absolutePrivacy, 'createRole')); //adds role on activation
register_deactivation_hook(__FILE__, array(&$absolutePrivacy, 'destroyRole')); //removes role on deactivation
if( isset($_GET['action']) && ($_GET['action'] == 'register') ) add_action( 'login_head', array(&$absolutePrivacy, 'regCSS')); //adds registration form CSS
add_action( 'register_form', array(&$absolutePrivacy, 'registrationBox')); //adds password field to registration box
add_filter( 'registration_errors', array(&$absolutePrivacy, 'checkRegErrors')); //adds registration form error checks
add_action('user_register', array(&$absolutePrivacy, 'addNewUser')); //adds registration info to database
add_action('admin_menu', array(&$absolutePrivacy, 'installOptionsMenu')); //install the options menu
add_action('admin_menu', array(&$absolutePrivacy, 'moderateMenu'));
add_action('template_redirect', array(&$absolutePrivacy, 'lockDown'));
add_filter('the_content', array(&$absolutePrivacy, 'check_is_feed'));
add_action('init', array(&$absolutePrivacy, 'adminLockDown'), 0);
add_action('login_head', 'rsd_link');
if(!function_exists('wp_authenticate')) {
function wp_authenticate($username, $password) {
global $wpdb, $error, $absolutePrivacy;
$username = sanitize_user($username);
$password = trim($password);
$user = apply_filters('authenticate', null, $username, $password);
if(is_wp_error($user)) {
return new WP_Error(403, __('You must login to view this site.'));
}
if ( defined('XMLRPC_REQUEST') && XMLRPC_REQUEST ) return $user; //allows the XML-RPC protocol for remote publishing
if ( '' == $username ) return new WP_Error('empty_username', __('ERROR: The username field is empty.'));
if ( '' == $password ) return new WP_Error('empty_password', __('ERROR: The password field is empty.'));
$user = get_userdatabylogin($username);
if ( !$user || ($user->user_login != $username) ) {
do_action( 'wp_login_failed', $username );
return new WP_Error('invalid_username', __('ERROR: Invalid login info.'));
}
$user_role = new WP_User($user->ID);
$capabilities = $absolutePrivacy->capabilities;
if (array_key_exists($absolutePrivacy->role, $user_role->$capabilities)) { //if the user's role is listed as "unapproved"
return new WP_Error('unapproved', __("ERROR: The administrator of this site must approve your account before you can login. You will be notified via email when it has been approved."));
}
$user = apply_filters('wp_authenticate_user', $user, $password);
if (is_wp_error($user)) {
do_action( 'wp_login_failed', $username );
return $user;
}
if (!wp_check_password($password, $user->user_pass, $user->ID)) {
do_action( 'wp_login_failed', $username );
return new WP_Error('incorrect_password', __('ERROR: Invalid login info.'));
}
return new WP_User($user->ID);
}
}
if ( !function_exists('wp_new_user_notification') ) {
function wp_new_user_notification($user_id, $plaintext_pass = '') {
global $absolutePrivacy;
$user = get_userdata($user_id); //object with user info
$absolutePrivacy->handleEmail($user_id, $type='admin_notification'); //send admin email
if ( empty($plaintext_pass) )
return;
$absolutePrivacy->handleEmail($user_id, $type='pending_welcome'); //send new user pending message email
}
}
} //end class_exists check
//quick script to get users IP address. Taken from http://www.phpbuilder.com/board/showpost.php?s=54f0e5d7127dac39a80f088ba1c4def1&p=10748983&postcount=8
/*
function ap_getUserIP(){
if ( isset($_SERVER["REMOTE_ADDR"]) ) {
$ip = $_SERVER["REMOTE_ADDR"] . ' ';
}elseif ( isset($_SERVER["HTTP_X_FORWARDED_FOR"]) ) {
$ip = $_SERVER["HTTP_X_FORWARDED_FOR"] . ' ';
} elseif ( isset($_SERVER["HTTP_CLIENT_IP"]) ) {
$ip = $_SERVER["HTTP_CLIENT_IP"] . ' ';
}
return $ip;
}
// Working on this for a future version
*/
?>