]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-admin/includes/user.php
Wordpress 2.3.2
[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         $errors = new WP_Error();
77
78         /* checking that username has been typed */
79         if ( $user->user_login == '' )
80                 $errors->add( 'user_login', __( '<strong>ERROR</strong>: Please enter a username.' ));
81
82         /* checking the password has been typed twice */
83         do_action_ref_array( 'check_passwords', array ( $user->user_login, & $pass1, & $pass2 ));
84
85         if (!$update ) {
86                 if ( $pass1 == '' || $pass2 == '' )
87                         $errors->add( 'pass', __( '<strong>ERROR</strong>: Please enter your password twice.' ));
88         } else {
89                 if ((empty ( $pass1 ) && !empty ( $pass2 ) ) || (empty ( $pass2 ) && !empty ( $pass1 ) ) )
90                         $errors->add( 'pass', __( "<strong>ERROR</strong>: you typed your new password only once." ));
91         }
92
93         /* Check for "\" in password */
94         if( strpos( " ".$pass1, "\\" ) )
95                 $errors->add( 'pass', __( '<strong>ERROR</strong>: Passwords may not contain the character "\\".' ));
96
97         /* checking the password has been typed twice the same */
98         if ( $pass1 != $pass2 )
99                 $errors->add( 'pass', __( '<strong>ERROR</strong>: Please type the same password in the two password fields.' ));
100
101         if (!empty ( $pass1 ))
102                 $user->user_pass = $pass1;
103
104         if ( !$update && !validate_username( $user->user_login ) )
105                 $errors->add( 'user_login', __( '<strong>ERROR</strong>: This username is invalid.  Please enter a valid username.' ));
106
107         if (!$update && username_exists( $user->user_login ))
108                 $errors->add( 'user_login', __( '<strong>ERROR</strong>: This username is already registered, please choose another one.' ));
109
110         /* checking e-mail address */
111         if ( empty ( $user->user_email ) ) {
112                 $errors->add( 'user_email', __( "<strong>ERROR</strong>: please type an e-mail address" ));
113         } else
114                 if (!is_email( $user->user_email ) ) {
115                         $errors->add( 'user_email', __( "<strong>ERROR</strong>: the email address isn't correct" ));
116                 }
117
118         if ( $errors->get_error_codes() )
119                 return $errors;
120
121         if ( $update ) {
122                 $user_id = wp_update_user( get_object_vars( $user ));
123         } else {
124                 $user_id = wp_insert_user( get_object_vars( $user ));
125                 wp_new_user_notification( $user_id );
126         }
127         return $user_id;
128 }
129
130 function get_author_user_ids() {
131         global $wpdb;
132         $level_key = $wpdb->prefix . 'user_level';
133
134         $query = "SELECT user_id FROM $wpdb->usermeta WHERE meta_key = '$level_key' AND meta_value != '0'";
135
136         return $wpdb->get_col( $query );
137 }
138
139 function get_editable_authors( $user_id ) {
140         global $wpdb;
141
142         $editable = get_editable_user_ids( $user_id );
143
144         if( !$editable ) {
145                 return false;
146         } else {
147                 $editable = join(',', $editable);
148                 $authors = $wpdb->get_results( "SELECT * FROM $wpdb->users WHERE ID IN ($editable) ORDER BY display_name" );
149         }
150
151         return apply_filters('get_editable_authors', $authors);
152 }
153
154 function get_editable_user_ids( $user_id, $exclude_zeros = true ) {
155         global $wpdb;
156
157         $user = new WP_User( $user_id );
158
159         if ( ! $user->has_cap('edit_others_posts') ) {
160                 if ( $user->has_cap('edit_posts') || $exclude_zeros == false )
161                         return array($user->id);
162                 else
163                         return false;
164         }
165
166         $level_key = $wpdb->prefix . 'user_level';
167
168         $query = "SELECT user_id FROM $wpdb->usermeta WHERE meta_key = '$level_key'";
169         if ( $exclude_zeros )
170                 $query .= " AND meta_value != '0'";
171
172         return $wpdb->get_col( $query );
173 }
174
175 function get_nonauthor_user_ids() {
176         global $wpdb;
177         $level_key = $wpdb->prefix . 'user_level';
178
179         $query = "SELECT user_id FROM $wpdb->usermeta WHERE meta_key = '$level_key' AND meta_value = '0'";
180
181         return $wpdb->get_col( $query );
182 }
183
184 function get_others_unpublished_posts($user_id, $type='any') {
185         global $wpdb;
186         $user = get_userdata( $user_id );
187         $level_key = $wpdb->prefix . 'user_level';
188
189         $editable = get_editable_user_ids( $user_id );
190
191         if ( in_array($type, array('draft', 'pending')) )
192                 $type_sql = " post_status = '$type' ";
193         else
194                 $type_sql = " ( post_status = 'draft' OR post_status = 'pending' ) ";
195
196         $dir = ( 'pending' == $type ) ? 'ASC' : 'DESC';
197
198         if( !$editable ) {
199                 $other_unpubs = '';
200         } else {
201                 $editable = join(',', $editable);
202                 $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");
203         }
204
205         return apply_filters('get_others_drafts', $other_unpubs);
206 }
207
208 function get_others_drafts($user_id) {
209         return get_others_unpublished_posts($user_id, 'draft');
210 }
211
212 function get_others_pending($user_id) {
213         return get_others_unpublished_posts($user_id, 'pending');
214 }
215
216 function get_user_to_edit( $user_id ) {
217         $user = new WP_User( $user_id );
218         $user->user_login   = attribute_escape($user->user_login);
219         $user->user_email   = attribute_escape($user->user_email);
220         $user->user_url     = clean_url($user->user_url);
221         $user->first_name   = attribute_escape($user->first_name);
222         $user->last_name    = attribute_escape($user->last_name);
223         $user->display_name = attribute_escape($user->display_name);
224         $user->nickname     = attribute_escape($user->nickname);
225         $user->aim          = attribute_escape($user->aim);
226         $user->yim          = attribute_escape($user->yim);
227         $user->jabber       = attribute_escape($user->jabber);
228         $user->description  =  wp_specialchars($user->description);
229
230         return $user;
231 }
232
233 function get_users_drafts( $user_id ) {
234         global $wpdb;
235         $user_id = (int) $user_id;
236         $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";
237         $query = apply_filters('get_users_drafts', $query);
238         return $wpdb->get_results( $query );
239 }
240
241 function wp_delete_user($id, $reassign = 'novalue') {
242         global $wpdb;
243
244         $id = (int) $id;
245         $user = get_userdata($id);
246
247         if ($reassign == 'novalue') {
248                 $post_ids = $wpdb->get_col("SELECT ID FROM $wpdb->posts WHERE post_author = $id");
249
250                 if ($post_ids) {
251                         foreach ($post_ids as $post_id)
252                                 wp_delete_post($post_id);
253                 }
254
255                 // Clean links
256                 $wpdb->query("DELETE FROM $wpdb->links WHERE link_owner = $id");
257         } else {
258                 $reassign = (int) $reassign;
259                 $wpdb->query("UPDATE $wpdb->posts SET post_author = {$reassign} WHERE post_author = {$id}");
260                 $wpdb->query("UPDATE $wpdb->links SET link_owner = {$reassign} WHERE link_owner = {$id}");
261         }
262
263         // FINALLY, delete user
264         do_action('delete_user', $id);
265
266         $wpdb->query("DELETE FROM $wpdb->users WHERE ID = $id");
267         $wpdb->query("DELETE FROM $wpdb->usermeta WHERE user_id = '$id'");
268
269         wp_cache_delete($id, 'users');
270         wp_cache_delete($user->user_login, 'userlogins');
271
272         return true;
273 }
274
275 function wp_revoke_user($id) {
276         $id = (int) $id;
277
278         $user = new WP_User($id);
279         $user->remove_all_caps();
280 }
281
282 ?>