예제 #1
0
 /**
  * WP's wp_authenticate overwrite.
  * Processes the directory login and creates a new user on first access.
  *
  * @param string $username Login form username.
  * @param string $password Login form password
  * @return WP_Error|WP_User WP_User object if login successful, otherwise WP_Error object.
  *
  * @uses wpDirAuth_makeCookieMarker
  * @uses wpDirAuth_auth
  *
  * @see http://codex.wordpress.org/Pluggable_Functions
  */
 function wp_authenticate($username, $password)
 {
     //echo 'authenticating';exit;
     $boolRestoreBlog = false;
     if (defined('WPDIRAUTH_MULTISITE') && WPDIRAUTH_MULTISITE) {
         //echo 'I should switch blogs!';exit;
         global $blog_id;
         $intOriginalBlog = $blog_id;
         switch_to_blog(1);
         //switch to the parent blog
         $boolRestoreBlog = true;
     }
     /**
      * @desc wp-hack for some reason, this function is being called even when a user just goes to the login page. added the next 3 lines so that
      * if the user arrives via $_GET, then we simply tell them to login
      */
     if ($_SERVER['REQUEST_METHOD'] != 'POST') {
         if ($boolRestoreBlog) {
             restore_current_blog();
         }
         return new WP_Error('incorrect_method', __('<strong>Please Login</strong>'));
     }
     if (!$username) {
         if ($boolRestoreBlog) {
             restore_current_blog();
         }
         return new WP_Error('empty_username', __('<strong>Login Error</strong>:
                     The username field is empty.'));
     }
     if (!$password) {
         if ($boolRestoreBlog) {
             restore_current_blog();
         }
         return new WP_Error('empty_password', __('<strong>Login Error</strong>:
                     The password field is empty.'));
     }
     $enable = get_site_option('dirAuthEnable');
     $cookieMarker = get_site_option('dirAuthCookieMarker');
     $boolAutoRegister = (bool) get_site_option('dirAuthAutoRegister');
     if (!$cookieMarker) {
         $cookieMarker = wpDirAuth_makeCookieMarker();
     }
     /**
      * Get the login object. We will use it for first user insertion or when the
      * directory auth option is not activated.
      */
     $login = get_user_by('login', $username);
     $loginUserIsDirUser = false !== $login ? get_user_meta($login->ID, 'wpDirAuthFlag', true) : 0;
     if (!$enable && $loginUserIsDirUser) {
         /*
          * Existing directory user, but directory access has now been disabled.
          */
         if ($boolRestoreBlog) {
             restore_current_blog();
         }
         do_action('wp_login_failed', $username);
         return new WP_Error('login_disabled', __('<strong>Directory Login Error</strong>:
                     Sorry, but the site administrators have disabled
                     directory access in this WordPress install.'));
     } elseif ($enable) {
         /**
          * Directory auth == true
          */
         if (!$login && $boolAutoRegister) {
             /**
              * No existing account record found, autoregister is on, try dir auth
              */
             $userData = wpDirAuth_auth($username, $password);
             if (!is_wp_error($userData)) {
                 /**
                  * Passed directory signin, so create a new WP user
                  */
                 $userLogin = sanitize_user($username);
                 $userEmail = apply_filters('user_registration_email', $userData['email']);
                 if (username_exists($userLogin)) {
                     /*
                      * Username exists.
                      */
                     if ($boolRestoreBlog) {
                         restore_current_blog();
                     }
                     do_action('wp_login_failed', $username);
                     return new WP_Error('username_exists', __('<strong>Directory Login Error</strong>:
                                 Could not create a new WP user account
                                 because the directory username <strong>' . htmlentities($userLogin, ENT_QUOTES, 'UTF-8') . '</strong> is already
                                 registered on this site.'));
                 } elseif (email_exists($userEmail)) {
                     /*
                      * Email exists.
                      */
                     if ($boolRestoreBlog) {
                         restore_current_blog();
                     }
                     do_action('wp_login_failed', $username);
                     return new WP_Error('email_exists', __('<strong>Directory Login Error</strong>:
                                 Could not create a new WP account because
                                 the email <strong>' . htmlentities($userEmail, ENT_QUOTES, 'UTF-8') . '</strong> is
                                 already registered with this site.'));
                 } else {
                     if (defined('WPDIRAUTH_MULTISITE') && WPDIRAUTH_MULTISITE && isset($boolRestoreBlog) && $boolRestoreBlog) {
                         restore_current_blog();
                     }
                     if ($userID = wp_create_user($userLogin, $password, $userEmail)) {
                         $userData['ID'] = $userID;
                         $tmpAr = explode('@', $userData['email']);
                         $userData['nickname'] = str_replace('.', '_', $tmpAr[0]);
                         $userData['display_name'] = $userData['first_name'] . ' ' . $userData['last_name'];
                         unset($userData['email']);
                         wp_update_user($userData);
                         update_user_meta($userID, 'wpDirAuthFlag', 1);
                         wpDirAuth_remove_password_nag($userID);
                         //if($boolRestoreBlog) restore_current_blog();
                         return new WP_User($userID);
                     } else {
                         /*
                          * Unknown error.
                          */
                         //if($boolRestoreBlog) restore_current_blog();
                         do_action('wp_login_failed', $username);
                         return new WP_Error('creation_unknown_error', __('<strong>Directory Login Error</strong>:
                                         Could not create a new user account.
                                         Unknown error. [user: '******'UTF-8') . ', email: ' . htmlentities($userEmail, ENT_QUOTES, 'UTF-8') . ']'));
                     }
                 }
             } else {
                 /*
                  * Did not pass dir auth, and no login present in WP
                  */
                 if ($boolRestoreBlog) {
                     restore_current_blog();
                 }
                 do_action('wp_login_failed', $username);
                 return $userData;
             }
         } else {
             /*
              * Dealing with an existing WP account
              */
             if (!$loginUserIsDirUser) {
                 /*
                  * WP-only user
                  */
                 if (wp_check_password($password, $login->user_pass, $login->ID)) {
                     /*
                      * WP user, password okay.
                      */
                     if ($boolRestoreBlog) {
                         restore_current_blog();
                     }
                     return new WP_User($login->ID);
                 } else {
                     /*
                      * WP user, wrong pass
                      */
                     if ($boolRestoreBlog) {
                         restore_current_blog();
                     }
                     do_action('wp_login_failed', $username);
                     return new WP_Error('incorrect_password', __('<strong>WordPress Login Error</strong>:
                                 Incorrect password.'));
                 }
             } else {
                 /**
                  * Directory user, try ldap binding
                  */
                 $userData = wpDirAuth_auth($username, $password);
                 if (!is_wp_error($userData)) {
                     /*
                      * Directory user, password okay.
                      */
                     wpDirAuth_remove_password_nag($login->ID);
                     if ($boolRestoreBlog) {
                         restore_current_blog();
                     }
                     return new WP_User($login->ID);
                 } else {
                     /*
                      * Directory user, wrong pass
                      */
                     if ($boolRestoreBlog) {
                         restore_current_blog();
                     }
                     do_action('wp_login_failed', $username);
                     return $userData;
                 }
             }
         }
     } else {
         /**
          * Directory auth == false
          */
         if (!$login || $login->user_login != $username) {
             /**
              * No existing account record found
              */
             if ($boolRestoreBlog) {
                 restore_current_blog();
             }
             do_action('wp_login_failed', $username);
             return new WP_Error('failed_login', __('<strong>WordPress Login Error</strong>:
                         Could not authenticate user.
                         Please check your credentials.'));
         } else {
             /*
              * Found an existing WP account.
              */
             if (wp_check_password($password, $login->user_pass, $login->ID)) {
                 /*
                  * WP user, password okay.
                  */
                 if ($boolRestoreBlog) {
                     restore_current_blog();
                 }
                 return new WP_User($login->ID);
             } else {
                 /*
                  * WP user, wrong pass
                  */
                 if ($boolRestoreBlog) {
                     restore_current_blog();
                 }
                 do_action('wp_login_failed', $username);
                 return new WP_Error('incorrect_password', __('<strong>WordPress Login Error</strong>:
                             Incorrect password.'));
             }
         }
     }
 }
예제 #2
0
 /**
  * WP's wp_authenticate overwrite.
  * Processes the directory login and creates a new user on first access.
  *
  * @param string $username Login form username.
  * @param string $password Login form password
  * @return WP_Error|WP_User WP_User object if login successful, otherwise WP_Error object.
  * 
  * @uses wpDirAuth_makeCookieMarker
  * @uses wpDirAuth_auth
  * 
  * @see http://codex.wordpress.org/Pluggable_Functions
  */
 function wp_authenticate($username, $password)
 {
     if (!$username) {
         return new WP_Error('empty_username', __('<strong>Login Error</strong>:
                     The username field is empty.'));
     }
     if (!$password) {
         return new WP_Error('empty_password', __('<strong>Login Error</strong>:
                     The password field is empty.'));
     }
     $enable = get_option('dirAuthEnable');
     $cookieMarker = get_option('dirAuthCookieMarker');
     if (!$cookieMarker) {
         $cookieMarker = wpDirAuth_makeCookieMarker();
     }
     /**
      * Get the login object. We will use it for first user insertion or when the 
      * directory auth option is not activated.
      */
     $login = get_userdatabylogin($username);
     $loginUserIsDirUser = $login ? get_usermeta($login->ID, 'wpDirAuthFlag') : 0;
     if (!$enable && $loginUserIsDirUser) {
         /*
          * Existing directory user, but directory access has now been disabled.
          */
         do_action('wp_login_failed', $username);
         return new WP_Error('login_disabled', __('<strong>Directory Login Error</strong>:
                     Sorry, but the site administrators have disabled
                     directory access in this WordPress install.'));
     } elseif ($enable) {
         /**
          * Directory auth == true
          */
         if (!$login) {
             /**
              * No existing account record found, try dir auth
              */
             $userData = wpDirAuth_auth($username, $password);
             if (!is_wp_error($userData)) {
                 /**
                  * Passed directory signin, so create a new WP user
                  */
                 require_once ABSPATH . WPINC . '/registration.php';
                 $userLogin = sanitize_user($username);
                 $userEmail = apply_filters('user_registration_email', $userData['email']);
                 if (username_exists($userLogin)) {
                     /*
                      * Username exists.
                      */
                     do_action('wp_login_failed', $username);
                     return new WP_Error('username_exists', __('<strong>Directory Login Error</strong>:
                                 Could not create a new WP user account
                                 because the directory username <strong>' . $userLogin . '</strong> is already
                                 registered on this site.'));
                 } elseif (email_exists($userEmail)) {
                     /*
                      * Email exists.
                      */
                     do_action('wp_login_failed', $username);
                     return new WP_Error('email_exists', __('<strong>Directory Login Error</strong>:
                                 Could not create a new WP account because
                                 the email <strong>' . $userEmail . '</strong> is
                                 already registered with this site.'));
                 } elseif ($userID = wp_create_user($userLogin, $password, $userEmail)) {
                     $userData['ID'] = $userID;
                     $tmpAr = split('@', $userData['email']);
                     $userData['nickname'] = str_replace('.', '_', $tmpAr[0]);
                     $userData['display_name'] = $userData['first_name'] . ' ' . $userData['last_name'];
                     unset($userData['email']);
                     wp_update_user($userData);
                     update_usermeta($userID, 'wpDirAuthFlag', 1);
                     return new WP_User($userID);
                 } else {
                     /*
                      * Unknown error.
                      */
                     do_action('wp_login_failed', $username);
                     return new WP_Error('creation_unknown_error', __('<strong>Directory Login Error</strong>:
                                 Could not create a new user account.
                                 Unknown error. [user: '******', email: ' . $userEmail . ']'));
                 }
             } else {
                 /*
                  * Did not pass dir auth, and no login present in WP
                  */
                 do_action('wp_login_failed', $username);
                 return $userData;
             }
         } else {
             /*
              * Dealing with an existing WP account
              */
             if (!$loginUserIsDirUser) {
                 /*
                  * WP-only user
                  */
                 if (wp_check_password($password, $login->user_pass, $login->ID)) {
                     /*
                      * WP user, password okay.
                      */
                     return new WP_User($login->ID);
                 } else {
                     /*
                      * WP user, wrong pass
                      */
                     do_action('wp_login_failed', $username);
                     return new WP_Error('incorrect_password', __('<strong>WordPress Login Error</strong>:
                                 Incorrect password.'));
                 }
             } else {
                 /**
                  * Directory user, try ldap binding
                  */
                 $userData = wpDirAuth_auth($username, $password);
                 if (!is_wp_error($userData)) {
                     /*
                      * Directory user, password okay.
                      */
                     return new WP_User($login->ID);
                 } else {
                     /*
                      * Directory user, wrong pass
                      */
                     do_action('wp_login_failed', $username);
                     return $userData;
                 }
             }
         }
     } else {
         /**
          * Directory auth == false
          */
         if (!$login || $login->user_login != $username) {
             /**
              * No existing account record found
              */
             do_action('wp_login_failed', $username);
             return new WP_Error('failed_login', __('<strong>WordPress Login Error</strong>:
                         Could not authenticate user.
                         Please check your credentials.'));
         } else {
             /*
              * Found an existing WP account.
              */
             if (wp_check_password($password, $login->user_pass, $login->ID)) {
                 /*
                  * WP user, password okay.
                  */
                 return new WP_User($login->ID);
             } else {
                 /*
                  * WP user, wrong pass
                  */
                 do_action('wp_login_failed', $username);
                 return new WP_Error('incorrect_password', __('<strong>WordPress Login Error</strong>:
                             Incorrect password.'));
             }
         }
     }
 }