]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/registration.php
Wordpress 2.5.1-scripts
[autoinstalls/wordpress.git] / wp-includes / registration.php
1 <?php
2 /**
3  * User Registration API
4  *
5  * @package WordPress
6  */
7
8 /**
9  * username_exists() - 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  * email_exists() - 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  * validate_username() - 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  * wp_insert_user() - Insert an user into the database.
57  *
58  * Can update a current user or insert a new user based on whether
59  * the user's ID is present.
60  *
61  * Can be used to update the user's info (see below), set the user's
62  * role, and set the user's preference on whether they want the rich
63  * editor on.
64  *
65  * Most of the $userdata array fields have filters associated with
66  * the values. The exceptions are 'rich_editing', 'role', 'jabber',
67  * 'aim', 'yim', 'user_registered', and 'ID'. The filters have the
68  * prefix 'pre_user_' followed by the field name. An example using
69  * 'description' would have the filter called, 'pre_user_description'
70  * that can be hooked into.
71  *
72  * The $userdata array can contain the following fields:
73  * 'ID' - An integer that will be used for updating an existing user.
74  * 'user_pass' - A string that contains the plain text password for the user.
75  * 'user_login' - A string that contains the user's username for logging in.
76  * 'user_nicename' - A string that contains a nicer looking name for the user.
77  *              The default is the user's username.
78  * 'user_url' - A string containing the user's URL for the user's web site.
79  * 'user_email' - A string containing the user's email address.
80  * 'display_name' - A string that will be shown on the site. Defaults to user's username.
81  *              It is likely that you will want to change this, for both appearance and security
82  *              through obscurity (that is if you don't use and delete the default 'admin' user).
83  * 'nickname' - The user's nickname, defaults to the user's username.
84  * 'first_name' - The user's first name.
85  * 'last_name' - The user's last name.
86  * 'description' - A string containing content about the user.
87  * 'rich_editing' - A string for whether to enable the rich editor or not. False if not
88  *              empty.
89  * 'user_registered' - The date the user registered. Format is 'Y-m-d H:i:s'.
90  * 'role' - A string used to set the user's role.
91  * 'jabber' - User's Jabber account.
92  * 'aim' - User's AOL IM account.
93  * 'yim' - User's Yahoo IM account.
94  *
95  * @since 2.0.0
96  * @uses $wpdb WordPress database layer.
97  * @uses apply_filters() Calls filters for most of the $userdata fields with the prefix 'pre_user'. See note above.
98  * @uses do_action() Calls 'profile_update' hook when updating giving the user's ID
99  * @uses do_action() Calls 'user_register' hook when creating a new user giving the user's ID
100  *
101  * @param array $userdata An array of user data.
102  * @return int The newly created user's ID.
103  */
104 function wp_insert_user($userdata) {
105         global $wpdb;
106
107         extract($userdata, EXTR_SKIP);
108
109         // Are we updating or creating?
110         if ( !empty($ID) ) {
111                 $ID = (int) $ID;
112                 $update = true;
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($admin_color) )
158                 $admin_color = 'fresh';
159         $admin_color = preg_replace('|[^a-z0-9 _.\-@]|i', '', $admin_color);
160
161         if ( empty($user_registered) )
162                 $user_registered = gmdate('Y-m-d H:i:s');
163
164         $data = compact( 'user_pass', 'user_email', 'user_url', 'user_nicename', 'display_name', 'user_registered' );
165         $data = stripslashes_deep( $data );
166
167         if ( $update ) {
168                 $wpdb->update( $wpdb->users, $data, compact( 'ID' ) );
169                 $user_id = (int) $ID;
170         } else {
171                 $wpdb->insert( $wpdb->users, $data + compact( 'user_login' ) );
172                 $user_id = (int) $wpdb->insert_id;
173         }
174
175         update_usermeta( $user_id, 'first_name', $first_name);
176         update_usermeta( $user_id, 'last_name', $last_name);
177         update_usermeta( $user_id, 'nickname', $nickname );
178         update_usermeta( $user_id, 'description', $description );
179         update_usermeta( $user_id, 'jabber', $jabber );
180         update_usermeta( $user_id, 'aim', $aim );
181         update_usermeta( $user_id, 'yim', $yim );
182         update_usermeta( $user_id, 'rich_editing', $rich_editing);
183         update_usermeta( $user_id, 'admin_color', $admin_color);
184
185         if ( $update && isset($role) ) {
186                 $user = new WP_User($user_id);
187                 $user->set_role($role);
188         }
189
190         if ( !$update ) {
191                 $user = new WP_User($user_id);
192                 $user->set_role(get_option('default_role'));
193         }
194
195         wp_cache_delete($user_id, 'users');
196         wp_cache_delete($user_login, 'userlogins');
197
198         if ( $update )
199                 do_action('profile_update', $user_id);
200         else
201                 do_action('user_register', $user_id);
202
203         return $user_id;
204 }
205
206 /**
207  * wp_update_user() - Update an user in the database
208  *
209  * It is possible to update a user's password by specifying the
210  * 'user_pass' value in the $userdata parameter array.
211  *
212  * If $userdata does not contain an 'ID' key, then a new user
213  * will be created and the new user's ID will be returned.
214  *
215  * If current user's password is being updated, then the cookies
216  * will be cleared.
217  *
218  * @since 2.0.0
219  * @see wp_insert_user() For what fields can be set in $userdata
220  * @uses wp_insert_user() Used to update existing user or add new one if user doesn't exist already
221  *
222  * @param array $userdata An array of user data.
223  * @return int The updated user's ID.
224  */
225 function wp_update_user($userdata) {
226         $ID = (int) $userdata['ID'];
227
228         // First, get all of the original fields
229         $user = get_userdata($ID);
230
231         // Escape data pulled from DB.
232         $user = add_magic_quotes(get_object_vars($user));
233
234         // If password is changing, hash it now.
235         if ( ! empty($userdata['user_pass']) ) {
236                 $plaintext_pass = $userdata['user_pass'];
237                 $userdata['user_pass'] = wp_hash_password($userdata['user_pass']);
238         }
239
240         // Merge old and new fields with new fields overwriting old ones.
241         $userdata = array_merge($user, $userdata);
242         $user_id = wp_insert_user($userdata);
243
244         // Update the cookies if the password changed.
245         $current_user = wp_get_current_user();
246         if ( $current_user->id == $ID ) {
247                 if ( isset($plaintext_pass) ) {
248                         wp_clear_auth_cookie();
249                         wp_set_auth_cookie($ID);
250                 }
251         }
252
253         return $user_id;
254 }
255
256 /**
257  * wp_create_user() - A simpler way of inserting an user into the database.
258  *
259  * Creates a new user with just the username, password, and email. For a more
260  * detail creation of a user, use wp_insert_user() to specify more infomation.
261  *
262  * @since 2.0.0
263  * @see wp_insert_user() More complete way to create a new user
264  * @uses $wpdb Escapes $username and $email parameters
265  *
266  * @param string $username The user's username.
267  * @param string $password The user's password.
268  * @param string $email The user's email (optional).
269  * @return int The new user's ID.
270  */
271 function wp_create_user($username, $password, $email = '') {
272         global $wpdb;
273
274         $user_login = $wpdb->escape($username);
275         $user_email = $wpdb->escape($email);
276         $user_pass = $password;
277
278         $userdata = compact('user_login', 'user_email', 'user_pass');
279         return wp_insert_user($userdata);
280 }
281
282 ?>