-
+ 98D3F31B7AE5BD161DCD8AF5E0DAE441A2FFC4F40DD7B9C77D0A7211CF7B3F916046A5B851E59F38A1B2C994E7E1DF980022358666C6392D3F8153C22D32FA1Cmp-wp/wp-includes/user.php(0 . 0)(1 . 559)
155364 <?php
155365 /**
155366 * WordPress User API
155367 *
155368 * @package WordPress
155369 */
155370
155371 /**
155372 * Authenticate user with remember capability.
155373 *
155374 * The credentials is an array that has 'user_login', 'user_password', and
155375 * 'remember' indices. If the credentials is not given, then the log in form
155376 * will be assumed and used if set.
155377 *
155378 * The various authentication cookies will be set by this function and will be
155379 * set for a longer period depending on if the 'remember' credential is set to
155380 * true.
155381 *
155382 * @since 2.5.0
155383 *
155384 * @param array $credentials Optional. User info in order to sign on.
155385 * @param bool $secure_cookie Optional. Whether to use secure cookie.
155386 * @return object Either WP_Error on failure, or WP_User on success.
155387 */
155388 function wp_signon( $credentials = '', $secure_cookie = '' ) {
155389 if ( empty($credentials) ) {
155390 if ( ! empty($_POST['log']) )
155391 $credentials['user_login'] = $_POST['log'];
155392 if ( ! empty($_POST['pwd']) )
155393 $credentials['user_password'] = $_POST['pwd'];
155394 if ( ! empty($_POST['rememberme']) )
155395 $credentials['remember'] = $_POST['rememberme'];
155396 }
155397
155398 if ( !empty($credentials['user_login']) )
155399 $credentials['user_login'] = sanitize_user($credentials['user_login']);
155400 if ( !empty($credentials['user_password']) )
155401 $credentials['user_password'] = trim($credentials['user_password']);
155402 if ( !empty($credentials['remember']) )
155403 $credentials['remember'] = true;
155404 else
155405 $credentials['remember'] = false;
155406
155407 do_action_ref_array('wp_authenticate', array(&$credentials['user_login'], &$credentials['user_password']));
155408
155409 if ( '' === $secure_cookie )
155410 $secure_cookie = is_ssl() ? true : false;
155411
155412 // If no credential info provided, check cookie.
155413 if ( empty($credentials['user_login']) && empty($credentials['user_password']) ) {
155414 $user = wp_validate_auth_cookie();
155415 if ( $user )
155416 return new WP_User($user);
155417
155418 if ( $secure_cookie )
155419 $auth_cookie = SECURE_AUTH_COOKIE;
155420 else
155421 $auth_cookie = AUTH_COOKIE;
155422
155423 if ( !empty($_COOKIE[$auth_cookie]) )
155424 return new WP_Error('expired_session', __('Please log in again.'));
155425
155426 // If the cookie is not set, be silent.
155427 return new WP_Error();
155428 }
155429
155430 if ( empty($credentials['user_login']) || empty($credentials['user_password']) ) {
155431 $error = new WP_Error();
155432
155433 if ( empty($credentials['user_login']) )
155434 $error->add('empty_username', __('<strong>ERROR</strong>: The username field is empty.'));
155435 if ( empty($credentials['user_password']) )
155436 $error->add('empty_password', __('<strong>ERROR</strong>: The password field is empty.'));
155437 return $error;
155438 }
155439
155440 $user = wp_authenticate($credentials['user_login'], $credentials['user_password']);
155441 if ( is_wp_error($user) )
155442 return $user;
155443
155444 wp_set_auth_cookie($user->ID, $credentials['remember'], $secure_cookie);
155445 do_action('wp_login', $credentials['user_login']);
155446 return $user;
155447 }
155448
155449 /**
155450 * Retrieve user data based on field.
155451 *
155452 * Use get_profile() will make a database query to get the value of the table
155453 * column. The value might be cached using the query cache, but care should be
155454 * taken when using the function to not make a lot of queries for retrieving
155455 * user profile information.
155456 *
155457 * If the $user parameter is not used, then the user will be retrieved from a
155458 * cookie of the user. Therefore, if the cookie does not exist, then no value
155459 * might be returned. Sanity checking must be done to ensure that when using
155460 * get_profile() that empty/null/false values are handled and that something is
155461 * at least displayed.
155462 *
155463 * @since 1.5.0
155464 * @uses $wpdb WordPress database object to create queries.
155465 *
155466 * @param string $field User field to retrieve.
155467 * @param string $user Optional. User username.
155468 * @return string The value in the field.
155469 */
155470 function get_profile($field, $user = false) {
155471 global $wpdb;
155472 if ( !$user )
155473 $user = $wpdb->escape($_COOKIE[USER_COOKIE]);
155474 return $wpdb->get_var( $wpdb->prepare("SELECT $field FROM $wpdb->users WHERE user_login = %s", $user) );
155475 }
155476
155477 /**
155478 * Number of posts user has written.
155479 *
155480 * @since 0.71
155481 * @uses $wpdb WordPress database object for queries.
155482 *
155483 * @param int $userid User ID.
155484 * @return int Amount of posts user has written.
155485 */
155486 function get_usernumposts($userid) {
155487 global $wpdb;
155488 $userid = (int) $userid;
155489 $count = $wpdb->get_var( $wpdb->prepare("SELECT COUNT(*) FROM $wpdb->posts WHERE post_author = %d AND post_type = 'post' AND ", $userid) . get_private_posts_cap_sql('post'));
155490 return apply_filters('get_usernumposts', $count, $userid);
155491 }
155492
155493 /**
155494 * Check that the user login name and password is correct.
155495 *
155496 * @since 0.71
155497 * @todo xmlrpc only. Maybe move to xmlrpc.php.
155498 *
155499 * @param string $user_login User name.
155500 * @param string $user_pass User password.
155501 * @return bool False if does not authenticate, true if username and password authenticates.
155502 */
155503 function user_pass_ok($user_login, $user_pass) {
155504 $user = wp_authenticate($user_login, $user_pass);
155505 if ( is_wp_error($user) )
155506 return false;
155507
155508 return true;
155509 }
155510
155511 //
155512 // User option functions
155513 //
155514
155515 /**
155516 * Retrieve user option that can be either global, user, or blog.
155517 *
155518 * If the user ID is not given, then the current user will be used instead. If
155519 * the user ID is given, then the user data will be retrieved. The filter for
155520 * the result, will also pass the original option name and finally the user data
155521 * object as the third parameter.
155522 *
155523 * The option will first check for the non-global name, then the global name,
155524 * and if it still doesn't find it, it will try the blog option. The option can
155525 * either be modified or set by a plugin.
155526 *
155527 * @since 2.0.0
155528 * @uses $wpdb WordPress database object for queries.
155529 * @uses apply_filters() Calls 'get_user_option_$option' hook with result,
155530 * option parameter, and user data object.
155531 *
155532 * @param string $option User option name.
155533 * @param int $user Optional. User ID.
155534 * @param bool $check_blog_options Whether to check for an option in the options table if a per-user option does not exist. Default is true.
155535 * @return mixed
155536 */
155537 function get_user_option( $option, $user = 0, $check_blog_options = true ) {
155538 global $wpdb;
155539
155540 $option = preg_replace('|[^a-z0-9_]|i', '', $option);
155541 if ( empty($user) )
155542 $user = wp_get_current_user();
155543 else
155544 $user = get_userdata($user);
155545
155546 if ( isset( $user->{$wpdb->prefix . $option} ) ) // Blog specific
155547 $result = $user->{$wpdb->prefix . $option};
155548 elseif ( isset( $user->{$option} ) ) // User specific and cross-blog
155549 $result = $user->{$option};
155550 elseif ( $check_blog_options ) // Blog global
155551 $result = get_option( $option );
155552 else
155553 $result = false;
155554
155555 return apply_filters("get_user_option_{$option}", $result, $option, $user);
155556 }
155557
155558 /**
155559 * Update user option with global blog capability.
155560 *
155561 * User options are just like user metadata except that they have support for
155562 * global blog options. If the 'global' parameter is false, which it is by false
155563 * it will prepend the WordPress table prefix to the option name.
155564 *
155565 * @since 2.0.0
155566 * @uses $wpdb WordPress database object for queries
155567 *
155568 * @param int $user_id User ID
155569 * @param string $option_name User option name.
155570 * @param mixed $newvalue User option value.
155571 * @param bool $global Optional. Whether option name is blog specific or not.
155572 * @return unknown
155573 */
155574 function update_user_option( $user_id, $option_name, $newvalue, $global = false ) {
155575 global $wpdb;
155576 if ( !$global )
155577 $option_name = $wpdb->prefix . $option_name;
155578 return update_usermeta( $user_id, $option_name, $newvalue );
155579 }
155580
155581 /**
155582 * Get users for the blog.
155583 *
155584 * For setups that use the multi-blog feature. Can be used outside of the
155585 * multi-blog feature.
155586 *
155587 * @since 2.2.0
155588 * @uses $wpdb WordPress database object for queries
155589 * @uses $blog_id The Blog id of the blog for those that use more than one blog
155590 *
155591 * @param int $id Blog ID.
155592 * @return array List of users that are part of that Blog ID
155593 */
155594 function get_users_of_blog( $id = '' ) {
155595 global $wpdb, $blog_id;
155596 if ( empty($id) )
155597 $id = (int) $blog_id;
155598 $users = $wpdb->get_results( "SELECT user_id, user_login, display_name, user_email, meta_value FROM $wpdb->users, $wpdb->usermeta WHERE " . $wpdb->users . ".ID = " . $wpdb->usermeta . ".user_id AND meta_key = '" . $wpdb->prefix . "capabilities' ORDER BY {$wpdb->usermeta}.user_id" );
155599 return $users;
155600 }
155601
155602 //
155603 // User meta functions
155604 //
155605
155606 /**
155607 * Remove user meta data.
155608 *
155609 * @since 2.0.0
155610 * @uses $wpdb WordPress database object for queries.
155611 *
155612 * @param int $user_id User ID.
155613 * @param string $meta_key Metadata key.
155614 * @param mixed $meta_value Metadata value.
155615 * @return bool True deletion completed and false if user_id is not a number.
155616 */
155617 function delete_usermeta( $user_id, $meta_key, $meta_value = '' ) {
155618 global $wpdb;
155619 if ( !is_numeric( $user_id ) )
155620 return false;
155621 $meta_key = preg_replace('|[^a-z0-9_]|i', '', $meta_key);
155622
155623 if ( is_array($meta_value) || is_object($meta_value) )
155624 $meta_value = serialize($meta_value);
155625 $meta_value = trim( $meta_value );
155626
155627 if ( ! empty($meta_value) )
155628 $wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->usermeta WHERE user_id = %d AND meta_key = %s AND meta_value = %s", $user_id, $meta_key, $meta_value) );
155629 else
155630 $wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->usermeta WHERE user_id = %d AND meta_key = %s", $user_id, $meta_key) );
155631
155632 wp_cache_delete($user_id, 'users');
155633
155634 return true;
155635 }
155636
155637 /**
155638 * Retrieve user metadata.
155639 *
155640 * If $user_id is not a number, then the function will fail over with a 'false'
155641 * boolean return value. Other returned values depend on whether there is only
155642 * one item to be returned, which be that single item type. If there is more
155643 * than one metadata value, then it will be list of metadata values.
155644 *
155645 * @since 2.0.0
155646 * @uses $wpdb WordPress database object for queries.
155647 *
155648 * @param int $user_id User ID
155649 * @param string $meta_key Optional. Metadata key.
155650 * @return mixed
155651 */
155652 function get_usermeta( $user_id, $meta_key = '') {
155653 global $wpdb;
155654 $user_id = (int) $user_id;
155655
155656 if ( !$user_id )
155657 return false;
155658
155659 if ( !empty($meta_key) ) {
155660 $meta_key = preg_replace('|[^a-z0-9_]|i', '', $meta_key);
155661 $user = wp_cache_get($user_id, 'users');
155662 // Check the cached user object
155663 if ( false !== $user && isset($user->$meta_key) )
155664 $metas = array($user->$meta_key);
155665 else
155666 $metas = $wpdb->get_col( $wpdb->prepare("SELECT meta_value FROM $wpdb->usermeta WHERE user_id = %d AND meta_key = %s", $user_id, $meta_key) );
155667 } else {
155668 $metas = $wpdb->get_col( $wpdb->prepare("SELECT meta_value FROM $wpdb->usermeta WHERE user_id = %d", $user_id) );
155669 }
155670
155671 if ( empty($metas) ) {
155672 if ( empty($meta_key) )
155673 return array();
155674 else
155675 return '';
155676 }
155677
155678 $metas = array_map('maybe_unserialize', $metas);
155679
155680 if ( count($metas) == 1 )
155681 return $metas[0];
155682 else
155683 return $metas;
155684 }
155685
155686 /**
155687 * Update metadata of user.
155688 *
155689 * There is no need to serialize values, they will be serialized if it is
155690 * needed. The metadata key can only be a string with underscores. All else will
155691 * be removed.
155692 *
155693 * Will remove the metadata, if the meta value is empty.
155694 *
155695 * @since 2.0.0
155696 * @uses $wpdb WordPress database object for queries
155697 *
155698 * @param int $user_id User ID
155699 * @param string $meta_key Metadata key.
155700 * @param mixed $meta_value Metadata value.
155701 * @return bool True on successful update, false on failure.
155702 */
155703 function update_usermeta( $user_id, $meta_key, $meta_value ) {
155704 global $wpdb;
155705 if ( !is_numeric( $user_id ) )
155706 return false;
155707 $meta_key = preg_replace('|[^a-z0-9_]|i', '', $meta_key);
155708
155709 /** @todo Might need fix because usermeta data is assumed to be already escaped */
155710 if ( is_string($meta_value) )
155711 $meta_value = stripslashes($meta_value);
155712 $meta_value = maybe_serialize($meta_value);
155713
155714 if (empty($meta_value)) {
155715 return delete_usermeta($user_id, $meta_key);
155716 }
155717
155718 $cur = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->usermeta WHERE user_id = %d AND meta_key = %s", $user_id, $meta_key) );
155719 if ( !$cur ) {
155720 $wpdb->query( $wpdb->prepare("INSERT INTO $wpdb->usermeta ( user_id, meta_key, meta_value )
155721 VALUES
155722 ( %d, %s, %s )", $user_id, $meta_key, $meta_value) );
155723 } else if ( $cur->meta_value != $meta_value ) {
155724 $wpdb->query( $wpdb->prepare("UPDATE $wpdb->usermeta SET meta_value = %s WHERE user_id = %d AND meta_key = %s", $meta_value, $user_id, $meta_key) );
155725 } else {
155726 return false;
155727 }
155728
155729 wp_cache_delete($user_id, 'users');
155730
155731 return true;
155732 }
155733
155734 //
155735 // Private helper functions
155736 //
155737
155738 /**
155739 * Setup global user vars.
155740 *
155741 * Used by set_current_user() for back compat. Might be deprecated in the
155742 * future.
155743 *
155744 * @since 2.0.4
155745 * @global string $userdata User description.
155746 * @global string $user_login The user username for logging in
155747 * @global int $user_level The level of the user
155748 * @global int $user_ID The ID of the user
155749 * @global string $user_email The email address of the user
155750 * @global string $user_url The url in the user's profile
155751 * @global string $user_pass_md5 MD5 of the user's password
155752 * @global string $user_identity The display name of the user
155753 *
155754 * @param int $user_id Optional. User ID to setup global data.
155755 */
155756 function setup_userdata($user_id = '') {
155757 global $user_login, $userdata, $user_level, $user_ID, $user_email, $user_url, $user_pass_md5, $user_identity;
155758
155759 if ( '' == $user_id )
155760 $user = wp_get_current_user();
155761 else
155762 $user = new WP_User($user_id);
155763
155764 if ( 0 == $user->ID )
155765 return;
155766
155767 $userdata = $user->data;
155768 $user_login = $user->user_login;
155769 $user_level = (int) isset($user->user_level) ? $user->user_level : 0;
155770 $user_ID = (int) $user->ID;
155771 $user_email = $user->user_email;
155772 $user_url = $user->user_url;
155773 $user_pass_md5 = md5($user->user_pass);
155774 $user_identity = $user->display_name;
155775 }
155776
155777 /**
155778 * Create dropdown HTML content of users.
155779 *
155780 * The content can either be displayed, which it is by default or retrieved by
155781 * setting the 'echo' argument. The 'include' and 'exclude' arguments do not
155782 * need to be used; all users will be displayed in that case. Only one can be
155783 * used, either 'include' or 'exclude', but not both.
155784 *
155785 * The available arguments are as follows:
155786 * <ol>
155787 * <li>show_option_all - Text to show all and whether HTML option exists.</li>
155788 * <li>show_option_none - Text for show none and whether HTML option exists.
155789 * </li>
155790 * <li>orderby - SQL order by clause for what order the users appear. Default is
155791 * 'display_name'.</li>
155792 * <li>order - Default is 'ASC'. Can also be 'DESC'.</li>
155793 * <li>include - User IDs to include.</li>
155794 * <li>exclude - User IDs to exclude.</li>
155795 * <li>multi - Default is 'false'. Whether to skip the ID attribute on the 'select' element.</li>
155796 * <li>show - Default is 'display_name'. User table column to display. If the selected item is empty then the user_login will be displayed in parentesis</li>
155797 * <li>echo - Default is '1'. Whether to display or retrieve content.</li>
155798 * <li>selected - Which User ID is selected.</li>
155799 * <li>name - Default is 'user'. Name attribute of select element.</li>
155800 * <li>class - Class attribute of select element.</li>
155801 * </ol>
155802 *
155803 * @since 2.3.0
155804 * @uses $wpdb WordPress database object for queries
155805 *
155806 * @param string|array $args Optional. Override defaults.
155807 * @return string|null Null on display. String of HTML content on retrieve.
155808 */
155809 function wp_dropdown_users( $args = '' ) {
155810 global $wpdb;
155811 $defaults = array(
155812 'show_option_all' => '', 'show_option_none' => '',
155813 'orderby' => 'display_name', 'order' => 'ASC',
155814 'include' => '', 'exclude' => '', 'multi' => 0,
155815 'show' => 'display_name', 'echo' => 1,
155816 'selected' => 0, 'name' => 'user', 'class' => ''
155817 );
155818
155819 $defaults['selected'] = is_author() ? get_query_var( 'author' ) : 0;
155820
155821 $r = wp_parse_args( $args, $defaults );
155822 extract( $r, EXTR_SKIP );
155823
155824 $query = "SELECT * FROM $wpdb->users";
155825
155826 $query_where = array();
155827
155828 if ( is_array($include) )
155829 $include = join(',', $include);
155830 $include = preg_replace('/[^0-9,]/', '', $include); // (int)
155831 if ( $include )
155832 $query_where[] = "ID IN ($include)";
155833
155834 if ( is_array($exclude) )
155835 $exclude = join(',', $exclude);
155836 $exclude = preg_replace('/[^0-9,]/', '', $exclude); // (int)
155837 if ( $exclude )
155838 $query_where[] = "ID NOT IN ($exclude)";
155839
155840 if ( $query_where )
155841 $query .= " WHERE " . join(' AND', $query_where);
155842
155843 $query .= " ORDER BY $orderby $order";
155844
155845 $users = $wpdb->get_results( $query );
155846
155847 $output = '';
155848 if ( !empty($users) ) {
155849 $id = $multi ? "" : "id='$name'";
155850
155851 $output = "<select name='$name' $id class='$class'>\n";
155852
155853 if ( $show_option_all )
155854 $output .= "\t<option value='0'>$show_option_all</option>\n";
155855
155856 if ( $show_option_none )
155857 $output .= "\t<option value='-1'>$show_option_none</option>\n";
155858
155859 foreach ( (array) $users as $user ) {
155860 $user->ID = (int) $user->ID;
155861 $_selected = $user->ID == $selected ? " selected='selected'" : '';
155862 $display = !empty($user->$show) ? $user->$show : '('. $user->user_login . ')';
155863 $output .= "\t<option value='$user->ID'$_selected>" . wp_specialchars($display) . "</option>\n";
155864 }
155865
155866 $output .= "</select>";
155867 }
155868
155869 $output = apply_filters('wp_dropdown_users', $output);
155870
155871 if ( $echo )
155872 echo $output;
155873
155874 return $output;
155875 }
155876
155877 /**
155878 * Add user meta data as properties to given user object.
155879 *
155880 * The finished user data is cached, but the cache is not used to fill in the
155881 * user data for the given object. Once the function has been used, the cache
155882 * should be used to retrieve user data. The purpose seems then to be to ensure
155883 * that the data in the object is always fresh.
155884 *
155885 * @access private
155886 * @since 2.5.0
155887 * @uses $wpdb WordPress database object for queries
155888 *
155889 * @param object $user The user data object.
155890 */
155891 function _fill_user( &$user ) {
155892 global $wpdb;
155893
155894 $show = $wpdb->hide_errors();
155895 $metavalues = $wpdb->get_results($wpdb->prepare("SELECT meta_key, meta_value FROM $wpdb->usermeta WHERE user_id = %d", $user->ID));
155896 $wpdb->show_errors($show);
155897
155898 if ( $metavalues ) {
155899 foreach ( (array) $metavalues as $meta ) {
155900 $value = maybe_unserialize($meta->meta_value);
155901 $user->{$meta->meta_key} = $value;
155902 }
155903 }
155904
155905 $level = $wpdb->prefix . 'user_level';
155906 if ( isset( $user->{$level} ) )
155907 $user->user_level = $user->{$level};
155908
155909 // For backwards compat.
155910 if ( isset($user->first_name) )
155911 $user->user_firstname = $user->first_name;
155912 if ( isset($user->last_name) )
155913 $user->user_lastname = $user->last_name;
155914 if ( isset($user->description) )
155915 $user->user_description = $user->description;
155916
155917 wp_cache_add($user->ID, $user, 'users');
155918 wp_cache_add($user->user_login, $user->ID, 'userlogins');
155919 wp_cache_add($user->user_email, $user->ID, 'useremail');
155920 }
155921
155922 ?>