PageRenderTime 28ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/tags/Doduo_1.1/WebKitSite/blog/wp-admin/includes/user.php

https://github.com/weissms/owb-mirror
PHP | 282 lines | 259 code | 17 blank | 6 comment | 52 complexity | 3057928d1202d2324e772650d9f8f6f0 MD5 | raw file
  1. <?php
  2. // Creates a new user from the "Users" form using $_POST information.
  3. function add_user() {
  4. if ( func_num_args() ) { // The hackiest hack that ever did hack
  5. global $current_user, $wp_roles;
  6. $user_id = (int) func_get_arg( 0 );
  7. if ( isset( $_POST['role'] ) ) {
  8. if( $user_id != $current_user->id || $wp_roles->role_objects[$_POST['role']]->has_cap( 'edit_users' ) ) {
  9. $user = new WP_User( $user_id );
  10. $user->set_role( $_POST['role'] );
  11. }
  12. }
  13. } else {
  14. add_action( 'user_register', 'add_user' ); // See above
  15. return edit_user();
  16. }
  17. }
  18. function edit_user( $user_id = 0 ) {
  19. global $current_user, $wp_roles, $wpdb;
  20. if ( $user_id != 0 ) {
  21. $update = true;
  22. $user->ID = (int) $user_id;
  23. $userdata = get_userdata( $user_id );
  24. $user->user_login = $wpdb->escape( $userdata->user_login );
  25. } else {
  26. $update = false;
  27. $user = '';
  28. }
  29. if ( isset( $_POST['user_login'] ))
  30. $user->user_login = wp_specialchars( trim( $_POST['user_login'] ));
  31. $pass1 = $pass2 = '';
  32. if ( isset( $_POST['pass1'] ))
  33. $pass1 = $_POST['pass1'];
  34. if ( isset( $_POST['pass2'] ))
  35. $pass2 = $_POST['pass2'];
  36. if ( isset( $_POST['role'] ) && current_user_can( 'edit_users' ) ) {
  37. if( $user_id != $current_user->id || $wp_roles->role_objects[$_POST['role']]->has_cap( 'edit_users' ))
  38. $user->role = $_POST['role'];
  39. }
  40. if ( isset( $_POST['email'] ))
  41. $user->user_email = wp_specialchars( trim( $_POST['email'] ));
  42. if ( isset( $_POST['url'] ) ) {
  43. $user->user_url = clean_url( trim( $_POST['url'] ));
  44. $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;
  45. }
  46. if ( isset( $_POST['first_name'] ))
  47. $user->first_name = wp_specialchars( trim( $_POST['first_name'] ));
  48. if ( isset( $_POST['last_name'] ))
  49. $user->last_name = wp_specialchars( trim( $_POST['last_name'] ));
  50. if ( isset( $_POST['nickname'] ))
  51. $user->nickname = wp_specialchars( trim( $_POST['nickname'] ));
  52. if ( isset( $_POST['display_name'] ))
  53. $user->display_name = wp_specialchars( trim( $_POST['display_name'] ));
  54. if ( isset( $_POST['description'] ))
  55. $user->description = trim( $_POST['description'] );
  56. if ( isset( $_POST['jabber'] ))
  57. $user->jabber = wp_specialchars( trim( $_POST['jabber'] ));
  58. if ( isset( $_POST['aim'] ))
  59. $user->aim = wp_specialchars( trim( $_POST['aim'] ));
  60. if ( isset( $_POST['yim'] ))
  61. $user->yim = wp_specialchars( trim( $_POST['yim'] ));
  62. if ( !$update )
  63. $user->rich_editing = 'true'; // Default to true for new users.
  64. else if ( isset( $_POST['rich_editing'] ) )
  65. $user->rich_editing = $_POST['rich_editing'];
  66. else
  67. $user->rich_editing = 'false';
  68. $errors = new WP_Error();
  69. /* checking that username has been typed */
  70. if ( $user->user_login == '' )
  71. $errors->add( 'user_login', __( '<strong>ERROR</strong>: Please enter a username.' ));
  72. /* checking the password has been typed twice */
  73. do_action_ref_array( 'check_passwords', array ( $user->user_login, & $pass1, & $pass2 ));
  74. if (!$update ) {
  75. if ( $pass1 == '' || $pass2 == '' )
  76. $errors->add( 'pass', __( '<strong>ERROR</strong>: Please enter your password twice.' ));
  77. } else {
  78. if ((empty ( $pass1 ) && !empty ( $pass2 ) ) || (empty ( $pass2 ) && !empty ( $pass1 ) ) )
  79. $errors->add( 'pass', __( "<strong>ERROR</strong>: you typed your new password only once." ));
  80. }
  81. /* Check for "\" in password */
  82. if( strpos( " ".$pass1, "\\" ) )
  83. $errors->add( 'pass', __( '<strong>ERROR</strong>: Passwords may not contain the character "\\".' ));
  84. /* checking the password has been typed twice the same */
  85. if ( $pass1 != $pass2 )
  86. $errors->add( 'pass', __( '<strong>ERROR</strong>: Please type the same password in the two password fields.' ));
  87. if (!empty ( $pass1 ))
  88. $user->user_pass = $pass1;
  89. if ( !$update && !validate_username( $user->user_login ) )
  90. $errors->add( 'user_login', __( '<strong>ERROR</strong>: This username is invalid. Please enter a valid username.' ));
  91. if (!$update && username_exists( $user->user_login ))
  92. $errors->add( 'user_login', __( '<strong>ERROR</strong>: This username is already registered, please choose another one.' ));
  93. /* checking e-mail address */
  94. if ( empty ( $user->user_email ) ) {
  95. $errors->add( 'user_email', __( "<strong>ERROR</strong>: please type an e-mail address" ));
  96. } else
  97. if (!is_email( $user->user_email ) ) {
  98. $errors->add( 'user_email', __( "<strong>ERROR</strong>: the email address isn't correct" ));
  99. }
  100. if ( $errors->get_error_codes() )
  101. return $errors;
  102. if ( $update ) {
  103. $user_id = wp_update_user( get_object_vars( $user ));
  104. } else {
  105. $user_id = wp_insert_user( get_object_vars( $user ));
  106. wp_new_user_notification( $user_id );
  107. }
  108. return $user_id;
  109. }
  110. function get_author_user_ids() {
  111. global $wpdb;
  112. $level_key = $wpdb->prefix . 'user_level';
  113. $query = "SELECT user_id FROM $wpdb->usermeta WHERE meta_key = '$level_key' AND meta_value != '0'";
  114. return $wpdb->get_col( $query );
  115. }
  116. function get_editable_authors( $user_id ) {
  117. global $wpdb;
  118. $editable = get_editable_user_ids( $user_id );
  119. if( !$editable ) {
  120. return false;
  121. } else {
  122. $editable = join(',', $editable);
  123. $authors = $wpdb->get_results( "SELECT * FROM $wpdb->users WHERE ID IN ($editable) ORDER BY display_name" );
  124. }
  125. return apply_filters('get_editable_authors', $authors);
  126. }
  127. function get_editable_user_ids( $user_id, $exclude_zeros = true ) {
  128. global $wpdb;
  129. $user = new WP_User( $user_id );
  130. if ( ! $user->has_cap('edit_others_posts') ) {
  131. if ( $user->has_cap('edit_posts') || $exclude_zeros == false )
  132. return array($user->id);
  133. else
  134. return false;
  135. }
  136. $level_key = $wpdb->prefix . 'user_level';
  137. $query = "SELECT user_id FROM $wpdb->usermeta WHERE meta_key = '$level_key'";
  138. if ( $exclude_zeros )
  139. $query .= " AND meta_value != '0'";
  140. return $wpdb->get_col( $query );
  141. }
  142. function get_nonauthor_user_ids() {
  143. global $wpdb;
  144. $level_key = $wpdb->prefix . 'user_level';
  145. $query = "SELECT user_id FROM $wpdb->usermeta WHERE meta_key = '$level_key' AND meta_value = '0'";
  146. return $wpdb->get_col( $query );
  147. }
  148. function get_others_unpublished_posts($user_id, $type='any') {
  149. global $wpdb;
  150. $user = get_userdata( $user_id );
  151. $level_key = $wpdb->prefix . 'user_level';
  152. $editable = get_editable_user_ids( $user_id );
  153. if ( in_array($type, array('draft', 'pending')) )
  154. $type_sql = " post_status = '$type' ";
  155. else
  156. $type_sql = " ( post_status = 'draft' OR post_status = 'pending' ) ";
  157. $dir = ( 'pending' == $type ) ? 'ASC' : 'DESC';
  158. if( !$editable ) {
  159. $other_unpubs = '';
  160. } else {
  161. $editable = join(',', $editable);
  162. $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");
  163. }
  164. return apply_filters('get_others_drafts', $other_unpubs);
  165. }
  166. function get_others_drafts($user_id) {
  167. return get_others_unpublished_posts($user_id, 'draft');
  168. }
  169. function get_others_pending($user_id) {
  170. return get_others_unpublished_posts($user_id, 'pending');
  171. }
  172. function get_user_to_edit( $user_id ) {
  173. $user = new WP_User( $user_id );
  174. $user->user_login = attribute_escape($user->user_login);
  175. $user->user_email = attribute_escape($user->user_email);
  176. $user->user_url = clean_url($user->user_url);
  177. $user->first_name = attribute_escape($user->first_name);
  178. $user->last_name = attribute_escape($user->last_name);
  179. $user->display_name = attribute_escape($user->display_name);
  180. $user->nickname = attribute_escape($user->nickname);
  181. $user->aim = attribute_escape($user->aim);
  182. $user->yim = attribute_escape($user->yim);
  183. $user->jabber = attribute_escape($user->jabber);
  184. $user->description = wp_specialchars($user->description);
  185. return $user;
  186. }
  187. function get_users_drafts( $user_id ) {
  188. global $wpdb;
  189. $user_id = (int) $user_id;
  190. $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";
  191. $query = apply_filters('get_users_drafts', $query);
  192. return $wpdb->get_results( $query );
  193. }
  194. function wp_delete_user($id, $reassign = 'novalue') {
  195. global $wpdb;
  196. $id = (int) $id;
  197. $user = get_userdata($id);
  198. if ($reassign == 'novalue') {
  199. $post_ids = $wpdb->get_col("SELECT ID FROM $wpdb->posts WHERE post_author = $id");
  200. if ($post_ids) {
  201. foreach ($post_ids as $post_id)
  202. wp_delete_post($post_id);
  203. }
  204. // Clean links
  205. $wpdb->query("DELETE FROM $wpdb->links WHERE link_owner = $id");
  206. } else {
  207. $reassign = (int) $reassign;
  208. $wpdb->query("UPDATE $wpdb->posts SET post_author = {$reassign} WHERE post_author = {$id}");
  209. $wpdb->query("UPDATE $wpdb->links SET link_owner = {$reassign} WHERE link_owner = {$id}");
  210. }
  211. // FINALLY, delete user
  212. do_action('delete_user', $id);
  213. $wpdb->query("DELETE FROM $wpdb->users WHERE ID = $id");
  214. $wpdb->query("DELETE FROM $wpdb->usermeta WHERE user_id = '$id'");
  215. wp_cache_delete($id, 'users');
  216. wp_cache_delete($user->user_login, 'userlogins');
  217. return true;
  218. }
  219. function wp_revoke_user($id) {
  220. $id = (int) $id;
  221. $user = new WP_User($id);
  222. $user->remove_all_caps();
  223. }
  224. ?>