web/wp-includes/registration.php
branchwordpress
changeset 109 03b0d1493584
child 132 4d4862461b8d
equal deleted inserted replaced
-1:000000000000 109:03b0d1493584
       
     1 <?php
       
     2 /**
       
     3  * User Registration API
       
     4  *
       
     5  * @package WordPress
       
     6  */
       
     7 
       
     8 /**
       
     9  * Checks whether the given username exists.
       
    10  *
       
    11  * @since 2.0.0
       
    12  *
       
    13  * @param string $username Username.
       
    14  * @return null|int The user's ID on success, and null on failure.
       
    15  */
       
    16 function username_exists( $username ) {
       
    17 	if ( $user = get_userdatabylogin( $username ) ) {
       
    18 		return $user->ID;
       
    19 	} else {
       
    20 		return null;
       
    21 	}
       
    22 }
       
    23 
       
    24 /**
       
    25  * Checks whether the given email exists.
       
    26  *
       
    27  * @since 2.1.0
       
    28  * @uses $wpdb
       
    29  *
       
    30  * @param string $email Email.
       
    31  * @return bool|int The user's ID on success, and false on failure.
       
    32  */
       
    33 function email_exists( $email ) {
       
    34 	if ( $user = get_user_by_email($email) )
       
    35 		return $user->ID;
       
    36 
       
    37 	return false;
       
    38 }
       
    39 
       
    40 /**
       
    41  * Checks whether an username is valid.
       
    42  *
       
    43  * @since 2.0.1
       
    44  * @uses apply_filters() Calls 'validate_username' hook on $valid check and $username as parameters
       
    45  *
       
    46  * @param string $username Username.
       
    47  * @return bool Whether username given is valid
       
    48  */
       
    49 function validate_username( $username ) {
       
    50 	$sanitized = sanitize_user( $username, true );
       
    51 	$valid = ( $sanitized == $username );
       
    52 	return apply_filters( 'validate_username', $valid, $username );
       
    53 }
       
    54 
       
    55 /**
       
    56  * Insert an user into the database.
       
    57  *
       
    58  * Can update a current user or insert a new user based on whether the user's ID
       
    59  * is present.
       
    60  *
       
    61  * Can be used to update the user's info (see below), set the user's role, and
       
    62  * set the user's preference on whether they want the rich editor on.
       
    63  *
       
    64  * Most of the $userdata array fields have filters associated with the values.
       
    65  * The exceptions are 'rich_editing', 'role', 'jabber', 'aim', 'yim',
       
    66  * 'user_registered', and 'ID'. The filters have the prefix 'pre_user_' followed
       
    67  * by the field name. An example using 'description' would have the filter
       
    68  * called, 'pre_user_description' that can be hooked into.
       
    69  *
       
    70  * The $userdata array can contain the following fields:
       
    71  * 'ID' - An integer that will be used for updating an existing user.
       
    72  * 'user_pass' - A string that contains the plain text password for the user.
       
    73  * 'user_login' - A string that contains the user's username for logging in.
       
    74  * 'user_nicename' - A string that contains a nicer looking name for the user.
       
    75  *		The default is the user's username.
       
    76  * 'user_url' - A string containing the user's URL for the user's web site.
       
    77  * 'user_email' - A string containing the user's email address.
       
    78  * 'display_name' - A string that will be shown on the site. Defaults to user's
       
    79  *		username. It is likely that you will want to change this, for both
       
    80  *		appearance and security through obscurity (that is if you don't use and
       
    81  *		delete the default 'admin' user).
       
    82  * 'nickname' - The user's nickname, defaults to the user's username.
       
    83  * 'first_name' - The user's first name.
       
    84  * 'last_name' - The user's last name.
       
    85  * 'description' - A string containing content about the user.
       
    86  * 'rich_editing' - A string for whether to enable the rich editor or not. False
       
    87  *		if not empty.
       
    88  * 'user_registered' - The date the user registered. Format is 'Y-m-d H:i:s'.
       
    89  * 'role' - A string used to set the user's role.
       
    90  * 'jabber' - User's Jabber account.
       
    91  * 'aim' - User's AOL IM account.
       
    92  * 'yim' - User's Yahoo IM account.
       
    93  *
       
    94  * @since 2.0.0
       
    95  * @uses $wpdb WordPress database layer.
       
    96  * @uses apply_filters() Calls filters for most of the $userdata fields with the prefix 'pre_user'. See note above.
       
    97  * @uses do_action() Calls 'profile_update' hook when updating giving the user's ID
       
    98  * @uses do_action() Calls 'user_register' hook when creating a new user giving the user's ID
       
    99  *
       
   100  * @param array $userdata An array of user data.
       
   101  * @return int The newly created user's ID.
       
   102  */
       
   103 function wp_insert_user($userdata) {
       
   104 	global $wpdb;
       
   105 
       
   106 	extract($userdata, EXTR_SKIP);
       
   107 
       
   108 	// Are we updating or creating?
       
   109 	if ( !empty($ID) ) {
       
   110 		$ID = (int) $ID;
       
   111 		$update = true;
       
   112 		$old_user_data = get_userdata($ID);
       
   113 	} else {
       
   114 		$update = false;
       
   115 		// Hash the password
       
   116 		$user_pass = wp_hash_password($user_pass);
       
   117 	}
       
   118 
       
   119 	$user_login = sanitize_user($user_login, true);
       
   120 	$user_login = apply_filters('pre_user_login', $user_login);
       
   121 
       
   122 	if ( empty($user_nicename) )
       
   123 		$user_nicename = sanitize_title( $user_login );
       
   124 	$user_nicename = apply_filters('pre_user_nicename', $user_nicename);
       
   125 
       
   126 	if ( empty($user_url) )
       
   127 		$user_url = '';
       
   128 	$user_url = apply_filters('pre_user_url', $user_url);
       
   129 
       
   130 	if ( empty($user_email) )
       
   131 		$user_email = '';
       
   132 	$user_email = apply_filters('pre_user_email', $user_email);
       
   133 
       
   134 	if ( empty($display_name) )
       
   135 		$display_name = $user_login;
       
   136 	$display_name = apply_filters('pre_user_display_name', $display_name);
       
   137 
       
   138 	if ( empty($nickname) )
       
   139 		$nickname = $user_login;
       
   140 	$nickname = apply_filters('pre_user_nickname', $nickname);
       
   141 
       
   142 	if ( empty($first_name) )
       
   143 		$first_name = '';
       
   144 	$first_name = apply_filters('pre_user_first_name', $first_name);
       
   145 
       
   146 	if ( empty($last_name) )
       
   147 		$last_name = '';
       
   148 	$last_name = apply_filters('pre_user_last_name', $last_name);
       
   149 
       
   150 	if ( empty($description) )
       
   151 		$description = '';
       
   152 	$description = apply_filters('pre_user_description', $description);
       
   153 
       
   154 	if ( empty($rich_editing) )
       
   155 		$rich_editing = 'true';
       
   156 
       
   157 	if ( empty($comment_shortcuts) )
       
   158 		$comment_shortcuts = 'false';
       
   159 
       
   160 	if ( empty($admin_color) )
       
   161 		$admin_color = 'fresh';
       
   162 	$admin_color = preg_replace('|[^a-z0-9 _.\-@]|i', '', $admin_color);
       
   163 
       
   164 	if ( empty($use_ssl) )
       
   165 		$use_ssl = 0;
       
   166 
       
   167 	if ( empty($jabber) )
       
   168 		$jabber = '';
       
   169 
       
   170 	if ( empty($aim) )
       
   171 		$aim = '';
       
   172 
       
   173 	if ( empty($yim) )
       
   174 		$yim = '';
       
   175 
       
   176 	if ( empty($user_registered) )
       
   177 		$user_registered = gmdate('Y-m-d H:i:s');
       
   178 
       
   179 	$user_nicename_check = $wpdb->get_var( $wpdb->prepare("SELECT ID FROM $wpdb->users WHERE user_nicename = %s AND user_login != %s LIMIT 1" , $user_nicename, $user_login));
       
   180 
       
   181 	if ($user_nicename_check) {
       
   182 		$suffix = 2;
       
   183 		while ($user_nicename_check) {
       
   184 			$alt_user_nicename = $user_nicename . "-$suffix";
       
   185 			$user_nicename_check = $wpdb->get_var( $wpdb->prepare("SELECT ID FROM $wpdb->users WHERE user_nicename = %s AND user_login != %s LIMIT 1" , $alt_user_nicename, $user_login));
       
   186 			$suffix++;
       
   187 		}
       
   188 		$user_nicename = $alt_user_nicename;
       
   189 	}
       
   190 
       
   191 	$data = compact( 'user_pass', 'user_email', 'user_url', 'user_nicename', 'display_name', 'user_registered' );
       
   192 	$data = stripslashes_deep( $data );
       
   193 
       
   194 	if ( $update ) {
       
   195 		$wpdb->update( $wpdb->users, $data, compact( 'ID' ) );
       
   196 		$user_id = (int) $ID;
       
   197 	} else {
       
   198 		$wpdb->insert( $wpdb->users, $data + compact( 'user_login' ) );
       
   199 		$user_id = (int) $wpdb->insert_id;
       
   200 	}
       
   201 
       
   202 	update_usermeta( $user_id, 'first_name', $first_name);
       
   203 	update_usermeta( $user_id, 'last_name', $last_name);
       
   204 	update_usermeta( $user_id, 'nickname', $nickname );
       
   205 	update_usermeta( $user_id, 'description', $description );
       
   206 	update_usermeta( $user_id, 'jabber', $jabber );
       
   207 	update_usermeta( $user_id, 'aim', $aim );
       
   208 	update_usermeta( $user_id, 'yim', $yim );
       
   209 	update_usermeta( $user_id, 'rich_editing', $rich_editing);
       
   210 	update_usermeta( $user_id, 'comment_shortcuts', $comment_shortcuts);
       
   211 	update_usermeta( $user_id, 'admin_color', $admin_color);
       
   212 	update_usermeta( $user_id, 'use_ssl', $use_ssl);
       
   213 
       
   214 	if ( isset($role) ) {
       
   215 		$user = new WP_User($user_id);
       
   216 		$user->set_role($role);
       
   217 	} elseif ( !$update ) {
       
   218 		$user = new WP_User($user_id);
       
   219 		$user->set_role(get_option('default_role'));
       
   220 	}
       
   221 
       
   222 	wp_cache_delete($user_id, 'users');
       
   223 	wp_cache_delete($user_login, 'userlogins');
       
   224 
       
   225 	if ( $update )
       
   226 		do_action('profile_update', $user_id, $old_user_data);
       
   227 	else
       
   228 		do_action('user_register', $user_id);
       
   229 
       
   230 	return $user_id;
       
   231 }
       
   232 
       
   233 /**
       
   234  * Update an user in the database.
       
   235  *
       
   236  * It is possible to update a user's password by specifying the 'user_pass'
       
   237  * value in the $userdata parameter array.
       
   238  *
       
   239  * If $userdata does not contain an 'ID' key, then a new user will be created
       
   240  * and the new user's ID will be returned.
       
   241  *
       
   242  * If current user's password is being updated, then the cookies will be
       
   243  * cleared.
       
   244  *
       
   245  * @since 2.0.0
       
   246  * @see wp_insert_user() For what fields can be set in $userdata
       
   247  * @uses wp_insert_user() Used to update existing user or add new one if user doesn't exist already
       
   248  *
       
   249  * @param array $userdata An array of user data.
       
   250  * @return int The updated user's ID.
       
   251  */
       
   252 function wp_update_user($userdata) {
       
   253 	$ID = (int) $userdata['ID'];
       
   254 
       
   255 	// First, get all of the original fields
       
   256 	$user = get_userdata($ID);
       
   257 
       
   258 	// Escape data pulled from DB.
       
   259 	$user = add_magic_quotes(get_object_vars($user));
       
   260 
       
   261 	// If password is changing, hash it now.
       
   262 	if ( ! empty($userdata['user_pass']) ) {
       
   263 		$plaintext_pass = $userdata['user_pass'];
       
   264 		$userdata['user_pass'] = wp_hash_password($userdata['user_pass']);
       
   265 	}
       
   266 
       
   267 	// Merge old and new fields with new fields overwriting old ones.
       
   268 	$userdata = array_merge($user, $userdata);
       
   269 	$user_id = wp_insert_user($userdata);
       
   270 
       
   271 	// Update the cookies if the password changed.
       
   272 	$current_user = wp_get_current_user();
       
   273 	if ( $current_user->id == $ID ) {
       
   274 		if ( isset($plaintext_pass) ) {
       
   275 			wp_clear_auth_cookie();
       
   276 			wp_set_auth_cookie($ID);
       
   277 		}
       
   278 	}
       
   279 
       
   280 	return $user_id;
       
   281 }
       
   282 
       
   283 /**
       
   284  * A simpler way of inserting an user into the database.
       
   285  *
       
   286  * Creates a new user with just the username, password, and email. For a more
       
   287  * detail creation of a user, use wp_insert_user() to specify more infomation.
       
   288  *
       
   289  * @since 2.0.0
       
   290  * @see wp_insert_user() More complete way to create a new user
       
   291  * @uses $wpdb Escapes $username and $email parameters
       
   292  *
       
   293  * @param string $username The user's username.
       
   294  * @param string $password The user's password.
       
   295  * @param string $email The user's email (optional).
       
   296  * @return int The new user's ID.
       
   297  */
       
   298 function wp_create_user($username, $password, $email = '') {
       
   299 	global $wpdb;
       
   300 
       
   301 	$user_login = $wpdb->escape($username);
       
   302 	$user_email = $wpdb->escape($email);
       
   303 	$user_pass = $password;
       
   304 
       
   305 	$userdata = compact('user_login', 'user_email', 'user_pass');
       
   306 	return wp_insert_user($userdata);
       
   307 }
       
   308 
       
   309 ?>