]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-admin/includes/user.php
Wordpress 2.5.1-scripts
[autoinstalls/wordpress.git] / wp-admin / includes / user.php
1 <?php
2
3 // Creates a new user from the "Users" form using $_POST information.
4 function add_user() {
5         if ( func_num_args() ) { // The hackiest hack that ever did hack
6                 global $current_user, $wp_roles;
7                 $user_id = (int) func_get_arg( 0 );
8
9                 if ( isset( $_POST['role'] ) ) {
10                         if( $user_id != $current_user->id || $wp_roles->role_objects[$_POST['role']]->has_cap( 'edit_users' ) ) {
11                                 $user = new WP_User( $user_id );
12                                 $user->set_role( $_POST['role'] );
13                         }
14                 }
15         } else {
16                 add_action( 'user_register', 'add_user' ); // See above
17                 return edit_user();
18         }
19 }
20
21 function edit_user( $user_id = 0 ) {
22         global $current_user, $wp_roles, $wpdb;
23         if ( $user_id != 0 ) {
24                 $update = true;
25                 $user->ID = (int) $user_id;
26                 $userdata = get_userdata( $user_id );
27                 $user->user_login = $wpdb->escape( $userdata->user_login );
28         } else {
29                 $update = false;
30                 $user = '';
31         }
32
33         if ( isset( $_POST['user_login'] ))
34                 $user->user_login = wp_specialchars( trim( $_POST['user_login'] ));
35
36         $pass1 = $pass2 = '';
37         if ( isset( $_POST['pass1'] ))
38                 $pass1 = $_POST['pass1'];
39         if ( isset( $_POST['pass2'] ))
40                 $pass2 = $_POST['pass2'];
41
42         if ( isset( $_POST['role'] ) && current_user_can( 'edit_users' ) ) {
43                 if( $user_id != $current_user->id || $wp_roles->role_objects[$_POST['role']]->has_cap( 'edit_users' ))
44                         $user->role = $_POST['role'];
45         }
46
47         if ( isset( $_POST['email'] ))
48                 $user->user_email = wp_specialchars( trim( $_POST['email'] ));
49         if ( isset( $_POST['url'] ) ) {
50                 $user->user_url = clean_url( trim( $_POST['url'] ));
51                 $user->user_url = preg_match('/^(https?|ftps?|mailto|news|irc|gopher|nntp|feed|telnet):/is', $user->user_url) ? $user->user_url : 'http://'.$user->user_url;
52         }
53         if ( isset( $_POST['first_name'] ))
54                 $user->first_name = wp_specialchars( trim( $_POST['first_name'] ));
55         if ( isset( $_POST['last_name'] ))
56                 $user->last_name = wp_specialchars( trim( $_POST['last_name'] ));
57         if ( isset( $_POST['nickname'] ))
58                 $user->nickname = wp_specialchars( trim( $_POST['nickname'] ));
59         if ( isset( $_POST['display_name'] ))
60                 $user->display_name = wp_specialchars( trim( $_POST['display_name'] ));
61         if ( isset( $_POST['description'] ))
62                 $user->description = trim( $_POST['description'] );
63         if ( isset( $_POST['jabber'] ))
64                 $user->jabber = wp_specialchars( trim( $_POST['jabber'] ));
65         if ( isset( $_POST['aim'] ))
66                 $user->aim = wp_specialchars( trim( $_POST['aim'] ));
67         if ( isset( $_POST['yim'] ))
68                 $user->yim = wp_specialchars( trim( $_POST['yim'] ));
69         if ( !$update )
70                 $user->rich_editing = 'true';  // Default to true for new users.
71         else if ( isset( $_POST['rich_editing'] ) )
72                 $user->rich_editing = $_POST['rich_editing'];
73         else
74                 $user->rich_editing = 'false';
75
76         if ( !$update )
77                 $user->admin_color = 'fresh';  // Default to fresh for new users.
78         else if ( isset( $_POST['admin_color'] ) )
79                 $user->admin_color = $_POST['admin_color'];
80         else
81                 $user->admin_color = 'fresh';
82
83         $errors = new WP_Error();
84
85         /* checking that username has been typed */
86         if ( $user->user_login == '' )
87                 $errors->add( 'user_login', __( '<strong>ERROR</strong>: Please enter a username.' ));
88
89         /* checking the password has been typed twice */
90         do_action_ref_array( 'check_passwords', array ( $user->user_login, & $pass1, & $pass2 ));
91
92         if ( $update ) {
93                 if ( empty($pass1) && !empty($pass2) )
94                         $errors->add( 'pass', __( '<strong>ERROR</strong>: You entered your new password only once.' ), array( 'form-field' => 'pass1' ) );
95                 elseif ( !empty($pass1) && empty($pass2) )
96                         $errors->add( 'pass', __( '<strong>ERROR</strong>: You entered your new password only once.' ), array( 'form-field' => 'pass2' ) );
97         } else {
98                 if ( empty($pass1) )
99                         $errors->add( 'pass', __( '<strong>ERROR</strong>: Please enter your password.' ), array( 'form-field' => 'pass1' ) );
100                 elseif ( empty($pass2) )
101                         $errors->add( 'pass', __( '<strong>ERROR</strong>: Please enter your password twice.' ), array( 'form-field' => 'pass2' ) );
102         }
103
104         /* Check for "\" in password */
105         if( strpos( " ".$pass1, "\\" ) )
106                 $errors->add( 'pass', __( '<strong>ERROR</strong>: Passwords may not contain the character "\\".' ), array( 'form-field' => 'pass1' ) );
107
108         /* checking the password has been typed twice the same */
109         if ( $pass1 != $pass2 )
110                 $errors->add( 'pass', __( '<strong>ERROR</strong>: Please enter the same password in the two password fields.' ), array( 'form-field' => 'pass1' ) );
111
112         if (!empty ( $pass1 ))
113                 $user->user_pass = $pass1;
114
115         if ( !$update && !validate_username( $user->user_login ) )
116                 $errors->add( 'user_login', __( '<strong>ERROR</strong>: This username is invalid. Please enter a valid username.' ));
117
118         if (!$update && username_exists( $user->user_login ))
119                 $errors->add( 'user_login', __( '<strong>ERROR</strong>: This username is already registered. Please choose another one.' ));
120
121         /* checking e-mail address */
122         if ( empty ( $user->user_email ) ) {
123                 $errors->add( 'user_email', __( '<strong>ERROR</strong>: Please enter an e-mail address.' ), array( 'form-field' => 'email' ) );
124         } else
125                 if (!is_email( $user->user_email ) ) {
126                         $errors->add( 'user_email', __( "<strong>ERROR</strong>: The e-mail address isn't correct." ), array( 'form-field' => 'email' ) );
127                 }
128
129         if ( $errors->get_error_codes() )
130                 return $errors;
131
132         if ( $update ) {
133                 $user_id = wp_update_user( get_object_vars( $user ));
134         } else {
135                 $user_id = wp_insert_user( get_object_vars( $user ));
136                 wp_new_user_notification( $user_id );
137         }
138         return $user_id;
139 }
140
141 function get_author_user_ids() {
142         global $wpdb;
143         $level_key = $wpdb->prefix . 'user_level';
144
145         $query = "SELECT user_id FROM $wpdb->usermeta WHERE meta_key = '$level_key' AND meta_value != '0'";
146
147         return $wpdb->get_col( $query );
148 }
149
150 function get_editable_authors( $user_id ) {
151         global $wpdb;
152
153         $editable = get_editable_user_ids( $user_id );
154
155         if( !$editable ) {
156                 return false;
157         } else {
158                 $editable = join(',', $editable);
159                 $authors = $wpdb->get_results( "SELECT * FROM $wpdb->users WHERE ID IN ($editable) ORDER BY display_name" );
160         }
161
162         return apply_filters('get_editable_authors', $authors);
163 }
164
165 function get_editable_user_ids( $user_id, $exclude_zeros = true ) {
166         global $wpdb;
167
168         $user = new WP_User( $user_id );
169
170         if ( ! $user->has_cap('edit_others_posts') ) {
171                 if ( $user->has_cap('edit_posts') || $exclude_zeros == false )
172                         return array($user->id);
173                 else
174                         return false;
175         }
176
177         $level_key = $wpdb->prefix . 'user_level';
178
179         $query = "SELECT user_id FROM $wpdb->usermeta WHERE meta_key = '$level_key'";
180         if ( $exclude_zeros )
181                 $query .= " AND meta_value != '0'";
182
183         return $wpdb->get_col( $query );
184 }
185
186 function get_nonauthor_user_ids() {
187         global $wpdb;
188         $level_key = $wpdb->prefix . 'user_level';
189
190         $query = "SELECT user_id FROM $wpdb->usermeta WHERE meta_key = '$level_key' AND meta_value = '0'";
191
192         return $wpdb->get_col( $query );
193 }
194
195 function get_others_unpublished_posts($user_id, $type='any') {
196         global $wpdb;
197
198         $editable = get_editable_user_ids( $user_id );
199
200         if ( in_array($type, array('draft', 'pending')) )
201                 $type_sql = " post_status = '$type' ";
202         else
203                 $type_sql = " ( post_status = 'draft' OR post_status = 'pending' ) ";
204
205         $dir = ( 'pending' == $type ) ? 'ASC' : 'DESC';
206
207         if( !$editable ) {
208                 $other_unpubs = '';
209         } else {
210                 $editable = join(',', $editable);
211                 $other_unpubs = $wpdb->get_results("SELECT ID, post_title, post_author FROM $wpdb->posts WHERE post_type = 'post' AND $type_sql AND post_author IN ($editable) AND post_author != '$user_id' ORDER BY post_modified $dir");
212         }
213
214         return apply_filters('get_others_drafts', $other_unpubs);
215 }
216
217 function get_others_drafts($user_id) {
218         return get_others_unpublished_posts($user_id, 'draft');
219 }
220
221 function get_others_pending($user_id) {
222         return get_others_unpublished_posts($user_id, 'pending');
223 }
224
225 function get_user_to_edit( $user_id ) {
226         $user = new WP_User( $user_id );
227         $user->user_login   = attribute_escape($user->user_login);
228         $user->user_email   = attribute_escape($user->user_email);
229         $user->user_url     = clean_url($user->user_url);
230         $user->first_name   = attribute_escape($user->first_name);
231         $user->last_name    = attribute_escape($user->last_name);
232         $user->display_name = attribute_escape($user->display_name);
233         $user->nickname     = attribute_escape($user->nickname);
234         $user->aim          = attribute_escape($user->aim);
235         $user->yim          = attribute_escape($user->yim);
236         $user->jabber       = attribute_escape($user->jabber);
237         $user->description  =  wp_specialchars($user->description);
238
239         return $user;
240 }
241
242 function get_users_drafts( $user_id ) {
243         global $wpdb;
244         $user_id = (int) $user_id;
245         $query = "SELECT ID, post_title FROM $wpdb->posts WHERE post_type = 'post' AND post_status = 'draft' AND post_author = $user_id ORDER BY post_modified DESC";
246         $query = apply_filters('get_users_drafts', $query);
247         return $wpdb->get_results( $query );
248 }
249
250 function wp_delete_user($id, $reassign = 'novalue') {
251         global $wpdb;
252
253         $id = (int) $id;
254
255         if ($reassign == 'novalue') {
256                 $post_ids = $wpdb->get_col("SELECT ID FROM $wpdb->posts WHERE post_author = $id");
257
258                 if ($post_ids) {
259                         foreach ($post_ids as $post_id)
260                                 wp_delete_post($post_id);
261                 }
262
263                 // Clean links
264                 $wpdb->query("DELETE FROM $wpdb->links WHERE link_owner = $id");
265         } else {
266                 $reassign = (int) $reassign;
267                 $wpdb->query("UPDATE $wpdb->posts SET post_author = {$reassign} WHERE post_author = {$id}");
268                 $wpdb->query("UPDATE $wpdb->links SET link_owner = {$reassign} WHERE link_owner = {$id}");
269         }
270
271         // FINALLY, delete user
272         do_action('delete_user', $id);
273
274         $wpdb->query("DELETE FROM $wpdb->users WHERE ID = $id");
275         $wpdb->query("DELETE FROM $wpdb->usermeta WHERE user_id = '$id'");
276
277         wp_cache_delete($id, 'users');
278         wp_cache_delete($user->user_login, 'userlogins');
279         wp_cache_delete($user->user_email, 'useremail');
280
281         return true;
282 }
283
284 function wp_revoke_user($id) {
285         $id = (int) $id;
286
287         $user = new WP_User($id);
288         $user->remove_all_caps();
289 }
290
291 // WP_User_Search class
292 // by Mark Jaquith
293
294 if ( !class_exists('WP_User_Search') ) :
295 class WP_User_Search {
296         var $results;
297         var $search_term;
298         var $page;
299         var $role;
300         var $raw_page;
301         var $users_per_page = 50;
302         var $first_user;
303         var $last_user;
304         var $query_limit;
305         var $query_sort;
306         var $query_from_where;
307         var $total_users_for_query = 0;
308         var $too_many_total_users = false;
309         var $search_errors;
310
311         function WP_User_Search ($search_term = '', $page = '', $role = '') { // constructor
312                 $this->search_term = $search_term;
313                 $this->raw_page = ( '' == $page ) ? false : (int) $page;
314                 $this->page = (int) ( '' == $page ) ? 1 : $page;
315                 $this->role = $role;
316
317                 $this->prepare_query();
318                 $this->query();
319                 $this->prepare_vars_for_template_usage();
320                 $this->do_paging();
321         }
322
323         function prepare_query() {
324                 global $wpdb;
325                 $this->first_user = ($this->page - 1) * $this->users_per_page;
326                 $this->query_limit = ' LIMIT ' . $this->first_user . ',' . $this->users_per_page;
327                 $this->query_sort = ' ORDER BY user_login';
328                 $search_sql = '';
329                 if ( $this->search_term ) {
330                         $searches = array();
331                         $search_sql = 'AND (';
332                         foreach ( array('user_login', 'user_nicename', 'user_email', 'user_url', 'display_name') as $col )
333                                 $searches[] = $col . " LIKE '%$this->search_term%'";
334                         $search_sql .= implode(' OR ', $searches);
335                         $search_sql .= ')';
336                 }
337
338                 $this->query_from_where = "FROM $wpdb->users";
339                 if ( $this->role )
340                         $this->query_from_where .= " INNER JOIN $wpdb->usermeta ON $wpdb->users.ID = $wpdb->usermeta.user_id WHERE $wpdb->usermeta.meta_key = '{$wpdb->prefix}capabilities' AND $wpdb->usermeta.meta_value LIKE '%$this->role%'";
341                 else
342                         $this->query_from_where .= " WHERE 1=1";
343                 $this->query_from_where .= " $search_sql";
344
345         }
346
347         function query() {
348                 global $wpdb;
349                 $this->results = $wpdb->get_col('SELECT ID ' . $this->query_from_where . $this->query_sort . $this->query_limit);
350
351                 if ( $this->results )
352                         $this->total_users_for_query = $wpdb->get_var('SELECT COUNT(ID) ' . $this->query_from_where); // no limit
353                 else
354                         $this->search_errors = new WP_Error('no_matching_users_found', __('No matching users were found!'));
355         }
356
357         function prepare_vars_for_template_usage() {
358                 $this->search_term = stripslashes($this->search_term); // done with DB, from now on we want slashes gone
359         }
360
361         function do_paging() {
362                 if ( $this->total_users_for_query > $this->users_per_page ) { // have to page the results
363                         $this->paging_text = paginate_links( array(
364                                 'total' => ceil($this->total_users_for_query / $this->users_per_page),
365                                 'current' => $this->page,
366                                 'base' => 'users.php?%_%',
367                                 'format' => 'userspage=%#%',
368                                 'add_args' => array( 'usersearch' => urlencode($this->search_term) )
369                         ) );
370                 }
371         }
372
373         function get_results() {
374                 return (array) $this->results;
375         }
376
377         function page_links() {
378                 echo $this->paging_text;
379         }
380
381         function results_are_paged() {
382                 if ( $this->paging_text )
383                         return true;
384                 return false;
385         }
386
387         function is_search() {
388                 if ( $this->search_term )
389                         return true;
390                 return false;
391         }
392 }
393 endif;
394
395 ?>