-
+ E7176F4843B6CE5DAFE0A3B244D785E698BCCA813763CB960DE3A3C2985BB06040507070B098BC980682B2CEF4EE5E4BB49638B6218FB7F940310BC85E930454mp-wp/wp-includes/registration.php(0 . 0)(1 . 299)
147465 <?php
147466 /**
147467 * User Registration API
147468 *
147469 * @package WordPress
147470 */
147471
147472 /**
147473 * Checks whether the given username exists.
147474 *
147475 * @since 2.0.0
147476 *
147477 * @param string $username Username.
147478 * @return null|int The user's ID on success, and null on failure.
147479 */
147480 function username_exists( $username ) {
147481 if ( $user = get_userdatabylogin( $username ) ) {
147482 return $user->ID;
147483 } else {
147484 return null;
147485 }
147486 }
147487
147488 /**
147489 * Checks whether the given email exists.
147490 *
147491 * @since 2.1.0
147492 * @uses $wpdb
147493 *
147494 * @param string $email Email.
147495 * @return bool|int The user's ID on success, and false on failure.
147496 */
147497 function email_exists( $email ) {
147498 if ( $user = get_user_by_email($email) )
147499 return $user->ID;
147500
147501 return false;
147502 }
147503
147504 /**
147505 * Checks whether an username is valid.
147506 *
147507 * @since 2.0.1
147508 * @uses apply_filters() Calls 'validate_username' hook on $valid check and $username as parameters
147509 *
147510 * @param string $username Username.
147511 * @return bool Whether username given is valid
147512 */
147513 function validate_username( $username ) {
147514 $sanitized = sanitize_user( $username, true );
147515 $valid = ( $sanitized == $username );
147516 return apply_filters( 'validate_username', $valid, $username );
147517 }
147518
147519 /**
147520 * Insert an user into the database.
147521 *
147522 * Can update a current user or insert a new user based on whether the user's ID
147523 * is present.
147524 *
147525 * Can be used to update the user's info (see below), set the user's role, and
147526 * set the user's preference on whether they want the rich editor on.
147527 *
147528 * Most of the $userdata array fields have filters associated with the values.
147529 * The exceptions are 'rich_editing', 'role', 'jabber', 'aim', 'yim',
147530 * 'user_registered', and 'ID'. The filters have the prefix 'pre_user_' followed
147531 * by the field name. An example using 'description' would have the filter
147532 * called, 'pre_user_description' that can be hooked into.
147533 *
147534 * The $userdata array can contain the following fields:
147535 * 'ID' - An integer that will be used for updating an existing user.
147536 * 'user_pass' - A string that contains the plain text password for the user.
147537 * 'user_login' - A string that contains the user's username for logging in.
147538 * 'user_nicename' - A string that contains a nicer looking name for the user.
147539 * The default is the user's username.
147540 * 'user_url' - A string containing the user's URL for the user's web site.
147541 * 'user_email' - A string containing the user's email address.
147542 * 'display_name' - A string that will be shown on the site. Defaults to user's
147543 * username. It is likely that you will want to change this, for both
147544 * appearance and security through obscurity (that is if you don't use and
147545 * delete the default 'admin' user).
147546 * 'nickname' - The user's nickname, defaults to the user's username.
147547 * 'first_name' - The user's first name.
147548 * 'last_name' - The user's last name.
147549 * 'description' - A string containing content about the user.
147550 * 'rich_editing' - A string for whether to enable the rich editor or not. False
147551 * if not empty.
147552 * 'user_registered' - The date the user registered. Format is 'Y-m-d H:i:s'.
147553 * 'role' - A string used to set the user's role.
147554 * 'jabber' - User's Jabber account.
147555 * 'aim' - User's AOL IM account.
147556 * 'yim' - User's Yahoo IM account.
147557 *
147558 * @since 2.0.0
147559 * @uses $wpdb WordPress database layer.
147560 * @uses apply_filters() Calls filters for most of the $userdata fields with the prefix 'pre_user'. See note above.
147561 * @uses do_action() Calls 'profile_update' hook when updating giving the user's ID
147562 * @uses do_action() Calls 'user_register' hook when creating a new user giving the user's ID
147563 *
147564 * @param array $userdata An array of user data.
147565 * @return int The newly created user's ID.
147566 */
147567 function wp_insert_user($userdata) {
147568 global $wpdb;
147569
147570 extract($userdata, EXTR_SKIP);
147571
147572 // Are we updating or creating?
147573 if ( !empty($ID) ) {
147574 $ID = (int) $ID;
147575 $update = true;
147576 $old_user_data = get_userdata($ID);
147577 } else {
147578 $update = false;
147579 // Hash the password
147580 $user_pass = wp_hash_password($user_pass);
147581 }
147582
147583 $user_login = sanitize_user($user_login, true);
147584 $user_login = apply_filters('pre_user_login', $user_login);
147585
147586 if ( empty($user_nicename) )
147587 $user_nicename = sanitize_title( $user_login );
147588 $user_nicename = apply_filters('pre_user_nicename', $user_nicename);
147589
147590 if ( empty($user_url) )
147591 $user_url = '';
147592 $user_url = apply_filters('pre_user_url', $user_url);
147593
147594 if ( empty($user_email) )
147595 $user_email = '';
147596 $user_email = apply_filters('pre_user_email', $user_email);
147597
147598 if ( empty($display_name) )
147599 $display_name = $user_login;
147600 $display_name = apply_filters('pre_user_display_name', $display_name);
147601
147602 if ( empty($nickname) )
147603 $nickname = $user_login;
147604 $nickname = apply_filters('pre_user_nickname', $nickname);
147605
147606 if ( empty($first_name) )
147607 $first_name = '';
147608 $first_name = apply_filters('pre_user_first_name', $first_name);
147609
147610 if ( empty($last_name) )
147611 $last_name = '';
147612 $last_name = apply_filters('pre_user_last_name', $last_name);
147613
147614 if ( empty($description) )
147615 $description = '';
147616 $description = apply_filters('pre_user_description', $description);
147617
147618 if ( empty($rich_editing) )
147619 $rich_editing = 'true';
147620
147621 if ( empty($comment_shortcuts) )
147622 $comment_shortcuts = 'false';
147623
147624 if ( empty($admin_color) )
147625 $admin_color = 'fresh';
147626 $admin_color = preg_replace('|[^a-z0-9 _.\-@]|i', '', $admin_color);
147627
147628 if ( empty($use_ssl) )
147629 $use_ssl = 0;
147630
147631 if ( empty($jabber) )
147632 $jabber = '';
147633
147634 if ( empty($aim) )
147635 $aim = '';
147636
147637 if ( empty($yim) )
147638 $yim = '';
147639
147640 if ( empty($user_registered) )
147641 $user_registered = gmdate('Y-m-d H:i:s');
147642
147643 $data = compact( 'user_pass', 'user_email', 'user_url', 'user_nicename', 'display_name', 'user_registered' );
147644 $data = stripslashes_deep( $data );
147645
147646 if ( $update ) {
147647 $wpdb->update( $wpdb->users, $data, compact( 'ID' ) );
147648 $user_id = (int) $ID;
147649 } else {
147650 $wpdb->insert( $wpdb->users, $data + compact( 'user_login' ) );
147651 $user_id = (int) $wpdb->insert_id;
147652 }
147653
147654 update_usermeta( $user_id, 'first_name', $first_name);
147655 update_usermeta( $user_id, 'last_name', $last_name);
147656 update_usermeta( $user_id, 'nickname', $nickname );
147657 update_usermeta( $user_id, 'description', $description );
147658 update_usermeta( $user_id, 'jabber', $jabber );
147659 update_usermeta( $user_id, 'aim', $aim );
147660 update_usermeta( $user_id, 'yim', $yim );
147661 update_usermeta( $user_id, 'rich_editing', $rich_editing);
147662 update_usermeta( $user_id, 'comment_shortcuts', $comment_shortcuts);
147663 update_usermeta( $user_id, 'admin_color', $admin_color);
147664 update_usermeta( $user_id, 'use_ssl', $use_ssl);
147665
147666 if ( $update && isset($role) ) {
147667 $user = new WP_User($user_id);
147668 $user->set_role($role);
147669 }
147670
147671 if ( !$update ) {
147672 $user = new WP_User($user_id);
147673 $user->set_role(get_option('default_role'));
147674 }
147675
147676 wp_cache_delete($user_id, 'users');
147677 wp_cache_delete($user_login, 'userlogins');
147678
147679 if ( $update )
147680 do_action('profile_update', $user_id, $old_user_data);
147681 else
147682 do_action('user_register', $user_id);
147683
147684 return $user_id;
147685 }
147686
147687 /**
147688 * Update an user in the database.
147689 *
147690 * It is possible to update a user's password by specifying the 'user_pass'
147691 * value in the $userdata parameter array.
147692 *
147693 * If $userdata does not contain an 'ID' key, then a new user will be created
147694 * and the new user's ID will be returned.
147695 *
147696 * If current user's password is being updated, then the cookies will be
147697 * cleared.
147698 *
147699 * @since 2.0.0
147700 * @see wp_insert_user() For what fields can be set in $userdata
147701 * @uses wp_insert_user() Used to update existing user or add new one if user doesn't exist already
147702 *
147703 * @param array $userdata An array of user data.
147704 * @return int The updated user's ID.
147705 */
147706 function wp_update_user($userdata) {
147707 $ID = (int) $userdata['ID'];
147708
147709 // First, get all of the original fields
147710 $user = get_userdata($ID);
147711
147712 // Escape data pulled from DB.
147713 $user = add_magic_quotes(get_object_vars($user));
147714
147715 // If password is changing, hash it now.
147716 if ( ! empty($userdata['user_pass']) ) {
147717 $plaintext_pass = $userdata['user_pass'];
147718 $userdata['user_pass'] = wp_hash_password($userdata['user_pass']);
147719 }
147720
147721 // Merge old and new fields with new fields overwriting old ones.
147722 $userdata = array_merge($user, $userdata);
147723 $user_id = wp_insert_user($userdata);
147724
147725 // Update the cookies if the password changed.
147726 $current_user = wp_get_current_user();
147727 if ( $current_user->id == $ID ) {
147728 if ( isset($plaintext_pass) ) {
147729 wp_clear_auth_cookie();
147730 wp_set_auth_cookie($ID);
147731 }
147732 }
147733
147734 return $user_id;
147735 }
147736
147737 /**
147738 * A simpler way of inserting an user into the database.
147739 *
147740 * Creates a new user with just the username, password, and email. For a more
147741 * detail creation of a user, use wp_insert_user() to specify more infomation.
147742 *
147743 * @since 2.0.0
147744 * @see wp_insert_user() More complete way to create a new user
147745 * @uses $wpdb Escapes $username and $email parameters
147746 *
147747 * @param string $username The user's username.
147748 * @param string $password The user's password.
147749 * @param string $email The user's email (optional).
147750 * @return int The new user's ID.
147751 */
147752 function wp_create_user($username, $password, $email = '') {
147753 global $wpdb;
147754
147755 $user_login = $wpdb->escape($username);
147756 $user_email = $wpdb->escape($email);
147757 $user_pass = $password;
147758
147759 $userdata = compact('user_login', 'user_email', 'user_pass');
147760 return wp_insert_user($userdata);
147761 }
147762
147763 ?>