=')) {
add_filter('authenticate', array(&$this, 'authenticate'), 10, 3);
} else {
add_action('wp_authenticate', array(&$this, 'authenticate'), 10, 2);
}
add_action('lost_password', array(&$this, 'disable_function'));
add_action('retrieve_password', array(&$this, 'disable_function'));
add_action('password_reset', array(&$this, 'disable_function'));
add_action('admin_print_styles', array(&$this, 'load_styles'));
add_action('check_passwords', array(&$this, 'generate_password'), 10, 3);
add_filter('check_password', array(&$this, 'override_password_check'), 10, 4);
add_filter('show_password_fields', array(&$this, 'disable_password_fields'));
add_filter('contextual_help', array(&$this, 'contextual_help'), 10, 2);
if (!class_exists('adLDAP')) {
require 'ad_ldap/adLDAP.php';
}
}
public function load_styles() {
wp_register_style('adintegration', WP_PLUGIN_URL.'/'.ADINTEGRATION_FOLDER.'/css/adintegration.css',false, '1.7.1', 'screen');
wp_enqueue_style('adintegration');
//die(PLUGINDIR.'/'.ADINTEGRATION_FOLDER.'/css/adintegration.css');
}
/*************************************************************
* Plugin hooks
*************************************************************/
/**
* Add options for this plugin to the database.
*/
public function initialize_options() {
if (current_user_can('manage_options')) {
add_option('AD_Integration_account_suffix', '', 'Account Suffix (will be appended to all usernames created in WordPress, as well as used in the Active Directory authentication process');
add_option('AD_Integration_auto_create_user', false, 'Should a new user be created automatically if not already in the WordPress database?');
add_option('AD_Integration_auto_update_user', false, 'Should the users be updated in the WordPress database everytime they logon? (Works only if automatic user creation is set.)');
add_option('AD_Integration_append_suffix_to_new_users', '', false, 'Should the account suffix be appended to the usernames created in WordPress?');
add_option('AD_Integration_domain_controllers', '', 'Domain Controllers (separate with semicolons)');
add_option('AD_Integration_base_dn', '', 'Base DN');
add_option('AD_Integration_role_equivalent_groups', '', 'Role Equivalent Groups');
add_option('AD_Integration_default_email_domain', '', 'Default Email Domain');
add_option('AD_Integration_port', '389', 'Port on which AD listens (default 389).');
add_option('AD_Integration_bind_user', '', 'Username for non-anonymous requests to AD.');
add_option('AD_Integration_bind_pwd', '', 'Password for non-anonymous requests to AD.');
add_option('AD_Integration_use_tls', false, 'Secure the connection between the Drupal and the LDAP servers using TLS.');
add_option('AD_Integration_authorize_by_group', false, 'Check Login authorization by group membership.');
add_option('AD_Integration_authorization_group', '', 'Group name for authorization.');
add_option('AD_Integration_max_login_attempts', '3', 'Maximum number of failed login attempts before the account is blocked.');
add_option('AD_Integration_block_time', '30', 'Number of seconds an account is blocked after the maximum number of failed login attempts is reached.');
add_option('AD_Integration_user_notification', false, 'Send email to user if his account is blocked.');
add_option('AD_Integration_admin_notification', false, 'Send email to admin if a user account is blocked.');
add_option('AD_Integration_admin_email', '', "Administrator's email address where notifications should be sent to.");
add_option('AD_Integration_display_name', '', "Set user's display_name to an AD attribute or to username if left blank.");
}
}
/**
* Add an options pane for this plugin.
*/
public function add_options_page() {
if (function_exists('add_options_page')) {
add_options_page('Active Directory Integration', 'Active Directory Integration', 9, __FILE__, array(&$this, '_display_options_page'));
}
}
/**
* Wrapper
*
* @param $arg1 WP_User or username
* @param $arg2 username or password
* @param $arg3 passwprd or empty
* @return WP_User
*/
public function authenticate($arg1 = NULL, $arg2 = NULL, $arg3 = NULL) {
global $wp_version;
if (version_compare($wp_version, '2.8', '>=')) {
return $this->ad_authenticate($arg1, $arg2, $arg3);
} else {
return $this->ad_authenticate(NULL, $arg1, $arg2);
}
}
/**
* If the REMOTE_USER evironment is set, use it as the username.
* This assumes that you have externally authenticated the user.
*/
public function ad_authenticate($user = NULL, $username = '', $password = '') {
$user_id = NULL;
$this->_authenticated = false;
// Load options from WordPress-DB.
$this->_load_options();
// Connect to Active Directory
$this->_adldap = new adLDAP(array(
"account_suffix" => $this->_account_suffix,
"base_dn" => $this->_base_dn,
"domain_controllers" => explode(';', $this->_domain_controllers),
"ad_username" => $this->_bind_user, // AD Bind User
"ad_password" => $this->_bind_pwd, // password
"ad_port" => $this->_port, // AD port
"use_tls" => $this->_use_tls // secure?
));
// Check for maximum login attempts
if ($this->_max_login_attempts > 0) {
$failed_logins = $this->_get_failed_logins_within_block_time($username);
if ($failed_logins >= $this->_max_login_attempts) {
$this->_authenticated = false;
// e-mail notfications if user is blocked
if ($this->_user_notification) {
$this->_notify_user($username);
}
if ($this->_admin_notification) {
$this->_notify_admin($username);
}
// Show the blocking page to the user
$this->_display_blocking_page($username);
die(); // important !
}
}
// This is where the action is.
if ( $this->_adldap->authenticate($username, $password) )
{
$this->_authenticated = true;
}
if ( $this->_authenticated == false )
{
$this->_authenticated = false;
$this->_store_failed_login($username);
return false;
}
// Cleanup old database entries
$this->_cleanup_failed_logins($username);
// Check the authorization
if ($this->_authorize_by_group) {
if ($this->_check_authorization_by_group($username)) {
$this->_authenticated = true;
} else {
$this->_authenticated = false;
return false;
}
}
$ad_username = $username;
// should the account suffix be used for the new username?
if ($this->_append_suffix_to_new_users) {
$username .= $this->_account_suffix;
}
// Create new users automatically, if configured
$user = get_userdatabylogin($username);
if (! $user OR ($user->user_login != $username)) {
$user_role = $this->_get_user_role_equiv($ad_username);
if ($this->_auto_create_user || $user_role != '' ) {
// create user
$userinfo = $this->_adldap->user_info($ad_username, array('sn', 'givenname', 'mail', 'displayName', 'description', 'cn'));
$userinfo = $userinfo[0];
$email = $userinfo['mail'][0];
$first_name = $userinfo['givenname'][0];
$last_name = $userinfo['sn'][0];
$display_name = $this->_get_display_name_from_AD($username, $userinfo);
$user_id = $this->_create_user($ad_username, $email, $first_name, $last_name, $display_name, $user_role);
} else {
// Bail out to avoid showing the login form
return new WP_Error('invalid_username', __('ERROR: This user exists in Active Directory, but has not been granted access to this installation of WordPress.'));
}
} else {
// update known users if configured
if ($this->_auto_create_user AND $this->_auto_update_user) {
// Update users role
$user_role = $this->_get_user_role_equiv($ad_username);
$userinfo = $this->_adldap->user_info($ad_username, array('sn', 'givenname', 'mail', 'displayName', 'description', 'cn'));
$userinfo = $userinfo[0];
$email = $userinfo['mail'][0];
$first_name = $userinfo['givenname'][0];
$last_name = $userinfo['sn'][0];
$common_name = $userinfo['cn'][0];
$display_name = $this->_get_display_name_from_AD($username, $userinfo);
$user_id = $this->_update_user($ad_username, $email, $first_name, $last_name, $display_name, $user_role);
}
}
// load user object
if (!$user_id) {
require_once(ABSPATH . WPINC . DIRECTORY_SEPARATOR .'registration.php');
$user_id = username_exists($username);
}
$user = new WP_User($user_id);
return $user;
}
/*
* Skip the password check, since we've externally authenticated.
*/
public function override_password_check($check, $password, $hash, $user_id) {
if ( $this->_authenticated == true )
{
return true;
}
else
{
return $check;
}
}
/*
* Generate a password for the user. This plugin does not
* require the user to enter this value, but we want to set it
* to something nonobvious.
*/
public function generate_password($username, $password1, $password2) {
$password1 = $password2 = $this->_get_password();
}
/*
* Used to disable certain display elements, e.g. password
* fields on profile screen.
*/
public function disable_password_fields($show_password_fields) {
return false;
}
/*
* Used to disable certain login functions, e.g. retrieving a
* user's password.
*/
public function disable_function() {
die('Disabled');
}
/**
* Adding the needed table to database and store the db version in the
* options table on plugin activation.
*/
public static function activate() {
global $wpdb;
//$table_name = $wpdb->prefix . ADIntegrationPlugin::$table_name;
$table_name = $wpdb->prefix . ADIntegrationPlugin::TABLE_NAME;
if (($wpdb->get_var("show tables like '$table_name'") != $table_name) OR (get_option('AD_Integration_db_version') != ADIntegrationPlugin::DB_VERSION)) {
$sql = 'CREATE TABLE ' . $table_name . ' (
id bigint(20) NOT NULL AUTO_INCREMENT,
user_login varchar(60),
failed_login_time bigint(11),
UNIQUE KEY id (id)
);';
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
// store db version in the options
add_option('AD_Integration_db_version', ADIntegrationPlugin::DB_VERSION, 'Version of the table structure');
}
}
/**
* Delete the table from database and delete the db version from the
* options table on plugin deactivation.
*/
public static function deactivate() {
global $wpdb;
//$table_name = $wpdb->prefix . ADIntegrationPlugin::$table_name;
$table_name = $wpdb->prefix . ADIntegrationPlugin::TABLE_NAME;
// drop table
$wpdb->query('DROP TABLE IF EXISTS '.$table_name);
// delete option
delete_option('AD_Integration_db_version');
}
/**
* removes the plugin options from options table.
*/
public static function uninstall($echo=false) {
$options = array(
'AD_Integration_account_suffix','AD_Integration_auto_create_user','AD_Integration_auto_update_user',
'AD_Integration_append_suffix_to_new_users',
'AD_Integration_domain_controllers',
'AD_Integration_base_dn',
'AD_Integration_role_equivalent_groups',
'AD_Integration_default_email_domain',
'AD_Integration_port',
'AD_Integration_bind_user',
'AD_Integration_bind_pwd',
'AD_Integration_use_tls',
'AD_Integration_authorize_by_group',
'AD_Integration_authorization_group',
'AD_Integration_max_login_attempts',
'AD_Integration_block_time',
'AD_Integration_user_notification',
'AD_Integration_admin_notification',
'AD_Integration_admin_email',
'AD_Integration_display_name'
);
foreach($options as $option) {
$delete_setting = delete_option($option);
if ($echo) {
if($delete_setting) {
echo '';
printf(__('Setting Key \'%s\' has been deleted.', 'MiniMetaWidget'), "{$setting}");
echo '
';
} else {
echo '';
printf(__('Error deleting Setting Key \'%s\'.', 'MiniMetaWidget'), "{$setting}");
echo '
';
}
}
}
}
/**
* Shows the contexual help on the options screen.
*
* @param $help
* @param $screen
* @return string help message
*/
function contextual_help ($help, $screen) {
if ($screen == 'settings_page_' . ADINTEGRATION_FOLDER . '/ad-integration') {
$help .= '