示例#1
0
 /**
  * Constructor
  *
  * Retrieves the userdata and passes it to {@link HQ_User::init()}.
  *
  * @since 0.0.1
  * @access public
  *
  * @global hqdb $hqdb
  *
  * @param int|string|stdClass|HQ_User $id User's ID, a HQ_User object, or a user object from the DB.
  * @param string $name Optional. User's username
  * @param int $blog_id Optional Blog ID, defaults to current blog.
  */
 public function __construct($id = 0, $name = '', $blog_id = '')
 {
     if (!isset(self::$back_compat_keys)) {
         $prefix = $GLOBALS['hqdb']->prefix;
         self::$back_compat_keys = array('user_firstname' => 'first_name', 'user_lastname' => 'last_name', 'user_description' => 'description', 'user_level' => $prefix . 'user_level', $prefix . 'usersettings' => $prefix . 'user-settings', $prefix . 'usersettingstime' => $prefix . 'user-settings-time');
     }
     if ($id instanceof HQ_User) {
         $this->init($id->data, $blog_id);
         return;
     } elseif (is_object($id)) {
         $this->init($id, $blog_id);
         return;
     }
     if (!empty($id) && !is_numeric($id)) {
         $name = $id;
         $id = 0;
     }
     if ($id) {
         $data = self::get_data_by('id', $id);
     } else {
         $data = self::get_data_by('login', $name);
     }
     if ($data) {
         $this->init($data, $blog_id);
     } else {
         $this->data = new stdClass();
     }
 }
示例#2
0
 /**
  * Retrieve user info by a given field
  *
  * @since 0.0.1
  *
  * @param string     $field The field to retrieve the user with. id | slug | email | login
  * @param int|string $value A value for $field. A user ID, slug, email address, or login name.
  * @return HQ_User|false HQ_User object on success, false on failure.
  */
 function get_user_by($field, $value)
 {
     $userdata = HQ_User::get_data_by($field, $value);
     if (!$userdata) {
         return false;
     }
     $user = new HQ_User();
     $user->init($userdata);
     return $user;
 }
示例#3
0
 /**
  Installs the site.
 *
 * Runs the required functions to set up and populate the database,
 * including primary admin user and initial options.
 *
 * @since 0.0.1
 *
 * @param string $blog_title    Blog title.
 * @param string $user_name     User's username.
 * @param string $user_email    User's email.
 * @param bool   $public        Whether blog is public.
 * @param string $deprecated    Optional. Not used.
 * @param string $user_password Optional. User's chosen password. Default empty (random password).
 * @param string $language      Optional. Language chosen. Default empty.
 * @return array Array keys 'url', 'user_id', 'password', and 'password_message'.
 */
 function hq_install($blog_title, $user_name, $user_email, $public, $deprecated = '', $user_password = '', $language = '')
 {
     if (!empty($deprecated)) {
         _deprecated_argument(__FUNCTION__, '2.6');
     }
     hq_check_mysql_version();
     //TODO: no cache
     //hq_cache_flush();
     //TODO: Debug
     //make_db_current_silent();
     make_db_current();
     populate_options();
     populate_roles();
     update_option('blogname', $blog_title);
     update_option('admin_email', $user_email);
     update_option('blog_public', $public);
     if ($language) {
         update_option('HQLANG', $language);
     }
     $guessurl = hq_guess_url();
     update_option('siteurl', $guessurl);
     // If not a public blog, don't ping.
     if (!$public) {
         update_option('default_pingback_flag', 0);
     }
     /*
      * Create default user. If the user already exists, the user tables are
      * being shared among blogs. Just set the role in that case.
      */
     $user_id = username_exists($user_name);
     $user_password = trim($user_password);
     $email_password = false;
     if (!$user_id && empty($user_password)) {
         $user_password = hq_generate_password(12, false);
         $message = __('<strong><em>Note that password</em></strong> carefully! It is a <em>random</em> password that was generated just for you.');
         $user_id = hq_create_user($user_name, $user_password, $user_email);
         update_user_option($user_id, 'default_password_nag', true, true);
         $email_password = true;
     } elseif (!$user_id) {
         // Password has been provided
         $message = '<em>' . __('Your chosen password.') . '</em>';
         $user_id = hq_create_user($user_name, $user_password, $user_email);
     } else {
         $message = __('User already exists. Password inherited.');
     }
     $user = new HQ_User($user_id);
     $user->set_role('administrator');
     hq_install_defaults($user_id);
     hq_install_maybe_enable_pretty_permalinks();
     flush_rewrite_rules();
     hq_new_blog_notification($blog_title, $guessurl, $user_id, $email_password ? $user_password : __('The password you chose during the install.'));
     hq_cache_flush();
     /**
      * Fires after a site is fully installed.
      *
      * @since 0.0.1
      *
      * @param HQ_User $user The site owner.
      */
     do_action('hq_install', $user);
     return array('url' => $guessurl, 'user_id' => $user_id, 'password' => $user_password, 'password_message' => $message);
 }
示例#4
0
/**
 * Insert a user into the database.
 *
 * Most of the `$userdata` array fields have filters associated with the values. Exceptions are
 * 'ID', 'rich_editing', 'comment_shortcuts', 'admin_color', 'use_ssl',
 * 'user_registered', and 'role'. The filters have the prefix 'pre_user_' followed by the field
 * name. An example using 'description' would have the filter called, 'pre_user_description' that
 * can be hooked into.
 *
 * @since 0.0.1
 *
 * @global hqdb $hqdb HiveQueen database object for queries.
 *
 * @param array|object|HQ_User $userdata {
 *     An array, object, or HQ_User object of user data arguments.
 *
 *     @type int         $ID                   User ID. If supplied, the user will be updated.
 *     @type string      $user_pass            The plain-text user password.
 *     @type string      $user_login           The user's login username.
 *     @type string      $user_nicename        The URL-friendly user name.
 *     @type string      $user_url             The user URL.
 *     @type string      $user_email           The user email address.
 *     @type string      $display_name         The user's display name.
 *                                             Default is the the user's username.
 *     @type string      $nickname             The user's nickname.
 *                                             Default is the the user's username.
 *     @type string      $first_name           The user's first name. For new users, will be used
 *                                             to build the first part of the user's display name
 *                                             if `$display_name` is not specified.
 *     @type string      $last_name            The user's last name. For new users, will be used
 *                                             to build the second part of the user's display name
 *                                             if `$display_name` is not specified.
 *     @type string      $description          The user's biographical description.
 *     @type string|bool $rich_editing         Whether to enable the rich-editor for the user.
 *                                             False if not empty.
 *     @type string|bool $comment_shortcuts    Whether to enable comment moderation keyboard
 *                                             shortcuts for the user. Default false.
 *     @type string      $admin_color          Admin color scheme for the user. Default 'fresh'.
 *     @type bool        $use_ssl              Whether the user should always access the admin over
 *                                             https. Default false.
 *     @type string      $user_registered      Date the user registered. Format is 'Y-m-d H:i:s'.
 *     @type string|bool $show_admin_bar_front Whether to display the Admin Bar for the user on the
 *                                             site's frontend. Default true.
 *     @type string      $role                 User's role.
 * }
 * @return int|HQ_Error The newly created user's ID or a HQ_Error object if the user could not
 *                      be created.
 */
function hq_insert_user($userdata)
{
    global $hqdb;
    if ($userdata instanceof stdClass) {
        $userdata = get_object_vars($userdata);
    } elseif ($userdata instanceof HQ_User) {
        $userdata = $userdata->to_array();
    }
    // Are we updating or creating?
    if (!empty($userdata['ID'])) {
        $ID = (int) $userdata['ID'];
        $update = true;
        $old_user_data = HQ_User::get_data_by('id', $ID);
        // hashed in hq_update_user(), plaintext if called directly
        $user_pass = $userdata['user_pass'];
    } else {
        $update = false;
        // Hash the password
        $user_pass = hq_hash_password($userdata['user_pass']);
    }
    $sanitized_user_login = sanitize_user($userdata['user_login'], true);
    /**
     * Filter a username after it has been sanitized.
     *
     * This filter is called before the user is created or updated.
     *
     * @since 0.0.1
     *
     * @param string $sanitized_user_login Username after it has been sanitized.
     */
    $pre_user_login = apply_filters('pre_user_login', $sanitized_user_login);
    //Remove any non-printable chars from the login string to see if we have ended up with an empty username
    $user_login = trim($pre_user_login);
    if (empty($user_login)) {
        return new HQ_Error('empty_user_login', __('Cannot create a user with an empty login name.'));
    }
    if (!$update && username_exists($user_login)) {
        return new HQ_Error('existing_user_login', __('Sorry, that username already exists!'));
    }
    // If a nicename is provided, remove unsafe user characters before
    // using it. Otherwise build a nicename from the user_login.
    if (!empty($userdata['user_nicename'])) {
        $user_nicename = sanitize_user($userdata['user_nicename'], true);
    } else {
        $user_nicename = $user_login;
    }
    $user_nicename = sanitize_title($user_nicename);
    // Store values to save in user meta.
    $meta = array();
    /**
     * Filter a user's nicename before the user is created or updated.
     *
     * @since 0.0.1
     *
     * @param string $user_nicename The user's nicename.
     */
    $user_nicename = apply_filters('pre_user_nicename', $user_nicename);
    $raw_user_url = empty($userdata['user_url']) ? '' : $userdata['user_url'];
    /**
     * Filter a user's URL before the user is created or updated.
     *
     * @since 0.0.1
     *
     * @param string $raw_user_url The user's URL.
     */
    $user_url = apply_filters('pre_user_url', $raw_user_url);
    $raw_user_email = empty($userdata['user_email']) ? '' : $userdata['user_email'];
    /**
     * Filter a user's email before the user is created or updated.
     *
     * @since 0.0.1
     *
     * @param string $raw_user_email The user's email.
     */
    $user_email = apply_filters('pre_user_email', $raw_user_email);
    /*
     * If there is no update, just check for `email_exists`. If there is an update,
     * check if current email and new email are the same, or not, and check `email_exists`
     * accordingly.
     */
    if ((!$update || !empty($old_user_data) && 0 !== strcasecmp($user_email, $old_user_data->user_email)) && !defined('HQ_IMPORTING') && email_exists($user_email)) {
        return new HQ_Error('existing_user_email', __('Sorry, that email address is already used!'));
    }
    $nickname = empty($userdata['nickname']) ? $user_login : $userdata['nickname'];
    /**
     * Filter a user's nickname before the user is created or updated.
     *
     * @since 0.0.1
     *
     * @param string $nickname The user's nickname.
     */
    $meta['nickname'] = apply_filters('pre_user_nickname', $nickname);
    $first_name = empty($userdata['first_name']) ? '' : $userdata['first_name'];
    /**
     * Filter a user's first name before the user is created or updated.
     *
     * @since 0.0.1
     *
     * @param string $first_name The user's first name.
     */
    $meta['first_name'] = apply_filters('pre_user_first_name', $first_name);
    $last_name = empty($userdata['last_name']) ? '' : $userdata['last_name'];
    /**
     * Filter a user's last name before the user is created or updated.
     *
     * @since 0.0.1
     *
     * @param string $last_name The user's last name.
     */
    $meta['last_name'] = apply_filters('pre_user_last_name', $last_name);
    if (empty($userdata['display_name'])) {
        if ($update) {
            $display_name = $user_login;
        } elseif ($meta['first_name'] && $meta['last_name']) {
            /* translators: 1: first name, 2: last name */
            $display_name = sprintf(_x('%1$s %2$s', 'Display name based on first name and last name'), $meta['first_name'], $meta['last_name']);
        } elseif ($meta['first_name']) {
            $display_name = $meta['first_name'];
        } elseif ($meta['last_name']) {
            $display_name = $meta['last_name'];
        } else {
            $display_name = $user_login;
        }
    } else {
        $display_name = $userdata['display_name'];
    }
    /**
     * Filter a user's display name before the user is created or updated.
     *
     * @since 0.0.1
     *
     * @param string $display_name The user's display name.
     */
    $display_name = apply_filters('pre_user_display_name', $display_name);
    $description = empty($userdata['description']) ? '' : $userdata['description'];
    /**
     * Filter a user's description before the user is created or updated.
     *
     * @since 0.0.1
     *
     * @param string $description The user's description.
     */
    $meta['description'] = apply_filters('pre_user_description', $description);
    $meta['rich_editing'] = empty($userdata['rich_editing']) ? 'true' : $userdata['rich_editing'];
    $meta['comment_shortcuts'] = empty($userdata['comment_shortcuts']) || 'false' === $userdata['comment_shortcuts'] ? 'false' : 'true';
    $admin_color = empty($userdata['admin_color']) ? 'fresh' : $userdata['admin_color'];
    $meta['admin_color'] = preg_replace('|[^a-z0-9 _.\\-@]|i', '', $admin_color);
    $meta['use_ssl'] = empty($userdata['use_ssl']) ? 0 : $userdata['use_ssl'];
    $user_registered = empty($userdata['user_registered']) ? gmdate('Y-m-d H:i:s') : $userdata['user_registered'];
    $meta['show_admin_bar_front'] = empty($userdata['show_admin_bar_front']) ? 'true' : $userdata['show_admin_bar_front'];
    $user_nicename_check = $hqdb->get_var($hqdb->prepare("SELECT ID FROM {$hqdb->users} WHERE user_nicename = %s AND user_login != %s LIMIT 1", $user_nicename, $user_login));
    if ($user_nicename_check) {
        $suffix = 2;
        while ($user_nicename_check) {
            $alt_user_nicename = $user_nicename . "-{$suffix}";
            $user_nicename_check = $hqdb->get_var($hqdb->prepare("SELECT ID FROM {$hqdb->users} WHERE user_nicename = %s AND user_login != %s LIMIT 1", $alt_user_nicename, $user_login));
            $suffix++;
        }
        $user_nicename = $alt_user_nicename;
    }
    $compacted = compact('user_pass', 'user_email', 'user_url', 'user_nicename', 'display_name', 'user_registered');
    $data = hq_unslash($compacted);
    if ($update) {
        if ($user_email !== $old_user_data->user_email) {
            $data['user_activation_key'] = '';
        }
        $hqdb->update($hqdb->users, $data, compact('ID'));
        $user_id = (int) $ID;
    } else {
        $hqdb->insert($hqdb->users, $data + compact('user_login'));
        $user_id = (int) $hqdb->insert_id;
    }
    $user = new HQ_User($user_id);
    // Update user meta.
    foreach ($meta as $key => $value) {
        update_user_meta($user_id, $key, $value);
    }
    foreach (hq_get_user_contact_methods($user) as $key => $value) {
        if (isset($userdata[$key])) {
            update_user_meta($user_id, $key, $userdata[$key]);
        }
    }
    if (isset($userdata['role'])) {
        $user->set_role($userdata['role']);
    } elseif (!$update) {
        $user->set_role(get_option('default_role'));
    }
    //TODO: Goyo no cache
    //hq_cache_delete( $user_id, 'users' );
    //hq_cache_delete( $user_login, 'userlogins' );
    if ($update) {
        /**
         * Fires immediately after an existing user is updated.
         *
         * @since 0.0.1
         *
         * @param int    $user_id       User ID.
         * @param object $old_user_data Object containing user's data prior to update.
         */
        do_action('profile_update', $user_id, $old_user_data);
    } else {
        /**
         * Fires immediately after a new user is registered.
         *
         * @since 0.0.1
         *
         * @param int $user_id User ID.
         */
        do_action('user_register', $user_id);
    }
    return $user_id;
}
示例#5
0
/**
 * Remove all capabilities from user.
 *
 * @since 0.0.1
 *
 * @param int $id User ID.
 */
function hq_revoke_user($id)
{
    $id = (int) $id;
    $user = new HQ_User($id);
    $user->remove_all_caps();
}