home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Blogs / wordpress2.6.exe / wordpress2.6 / wp-admin / includes / user.php < prev    next >
Encoding:
PHP Script  |  2008-07-27  |  12.9 KB  |  396 lines

  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.     return $wpdb->get_col( $wpdb->prepare("SELECT user_id FROM $wpdb->usermeta WHERE meta_key = %s AND meta_value != '0'", $level_key) );
  145. }
  146.  
  147. function get_editable_authors( $user_id ) {
  148.     global $wpdb;
  149.  
  150.     $editable = get_editable_user_ids( $user_id );
  151.  
  152.     if( !$editable ) {
  153.         return false;
  154.     } else {
  155.         $editable = join(',', $editable);
  156.         $authors = $wpdb->get_results( "SELECT * FROM $wpdb->users WHERE ID IN ($editable) ORDER BY display_name" );
  157.     }
  158.  
  159.     return apply_filters('get_editable_authors', $authors);
  160. }
  161.  
  162. function get_editable_user_ids( $user_id, $exclude_zeros = true ) {
  163.     global $wpdb;
  164.  
  165.     $user = new WP_User( $user_id );
  166.  
  167.     if ( ! $user->has_cap('edit_others_posts') ) {
  168.         if ( $user->has_cap('edit_posts') || $exclude_zeros == false )
  169.             return array($user->id);
  170.         else
  171.             return false;
  172.     }
  173.  
  174.     $level_key = $wpdb->prefix . 'user_level';
  175.  
  176.     $query = $wpdb->prepare("SELECT user_id FROM $wpdb->usermeta WHERE meta_key = %s", $level_key);
  177.     if ( $exclude_zeros )
  178.         $query .= " AND meta_value != '0'";
  179.  
  180.     return $wpdb->get_col( $query );
  181. }
  182.  
  183. function get_nonauthor_user_ids() {
  184.     global $wpdb;
  185.     $level_key = $wpdb->prefix . 'user_level';
  186.  
  187.     return $wpdb->get_col( $wpdb->prepare("SELECT user_id FROM $wpdb->usermeta WHERE meta_key = %s AND meta_value = '0'", $level_key) );
  188. }
  189.  
  190. function get_others_unpublished_posts($user_id, $type='any') {
  191.     global $wpdb;
  192.  
  193.     $editable = get_editable_user_ids( $user_id );
  194.  
  195.     if ( in_array($type, array('draft', 'pending')) )
  196.         $type_sql = " post_status = '$type' ";
  197.     else
  198.         $type_sql = " ( post_status = 'draft' OR post_status = 'pending' ) ";
  199.  
  200.     $dir = ( 'pending' == $type ) ? 'ASC' : 'DESC';
  201.  
  202.     if( !$editable ) {
  203.         $other_unpubs = '';
  204.     } else {
  205.         $editable = join(',', $editable);
  206.         $other_unpubs = $wpdb->get_results( $wpdb->prepare("SELECT ID, post_title, post_author FROM $wpdb->posts WHERE post_type = 'post' AND $type_sql AND post_author IN ($editable) AND post_author != %d ORDER BY post_modified $dir", $user_id) );
  207.     }
  208.  
  209.     return apply_filters('get_others_drafts', $other_unpubs);
  210. }
  211.  
  212. function get_others_drafts($user_id) {
  213.     return get_others_unpublished_posts($user_id, 'draft');
  214. }
  215.  
  216. function get_others_pending($user_id) {
  217.     return get_others_unpublished_posts($user_id, 'pending');
  218. }
  219.  
  220. function get_user_to_edit( $user_id ) {
  221.     $user = new WP_User( $user_id );
  222.     $user->user_login   = attribute_escape($user->user_login);
  223.     $user->user_email   = attribute_escape($user->user_email);
  224.     $user->user_url     = clean_url($user->user_url);
  225.     $user->first_name   = attribute_escape($user->first_name);
  226.     $user->last_name    = attribute_escape($user->last_name);
  227.     $user->display_name = attribute_escape($user->display_name);
  228.     $user->nickname     = attribute_escape($user->nickname);
  229.     $user->aim          = attribute_escape($user->aim);
  230.     $user->yim          = attribute_escape($user->yim);
  231.     $user->jabber       = attribute_escape($user->jabber);
  232.     $user->description  =  wp_specialchars($user->description);
  233.  
  234.     return $user;
  235. }
  236.  
  237. function get_users_drafts( $user_id ) {
  238.     global $wpdb;
  239.     $query = $wpdb->prepare("SELECT ID, post_title FROM $wpdb->posts WHERE post_type = 'post' AND post_status = 'draft' AND post_author = %d ORDER BY post_modified DESC", $user_id);
  240.     $query = apply_filters('get_users_drafts', $query);
  241.     return $wpdb->get_results( $query );
  242. }
  243.  
  244. function wp_delete_user($id, $reassign = 'novalue') {
  245.     global $wpdb;
  246.  
  247.     $id = (int) $id;
  248.  
  249.     if ($reassign == 'novalue') {
  250.         $post_ids = $wpdb->get_col( $wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE post_author = %d", $id) );
  251.  
  252.         if ($post_ids) {
  253.             foreach ($post_ids as $post_id)
  254.                 wp_delete_post($post_id);
  255.         }
  256.  
  257.         // Clean links
  258.         $wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->links WHERE link_owner = %d", $id) );
  259.     } else {
  260.         $reassign = (int) $reassign;
  261.         $wpdb->query( $wpdb->prepare("UPDATE $wpdb->posts SET post_author = %d WHERE post_author = %d", $reassign, $id) );
  262.         $wpdb->query( $wpdb->prepare("UPDATE $wpdb->links SET link_owner = %d WHERE link_owner = %d", $reassign, $id) );
  263.     }
  264.  
  265.     // FINALLY, delete user
  266.     do_action('delete_user', $id);
  267.  
  268.     $wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->users WHERE ID = %d", $id) );
  269.     $wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->usermeta WHERE user_id = %d", $id) );
  270.  
  271.     wp_cache_delete($id, 'users');
  272.     wp_cache_delete($user->user_login, 'userlogins');
  273.     wp_cache_delete($user->user_email, 'useremail');
  274.  
  275.     return true;
  276. }
  277.  
  278. function wp_revoke_user($id) {
  279.     $id = (int) $id;
  280.  
  281.     $user = new WP_User($id);
  282.     $user->remove_all_caps();
  283. }
  284.  
  285. // WP_User_Search class
  286. // by Mark Jaquith
  287.  
  288. if ( !class_exists('WP_User_Search') ) :
  289. class WP_User_Search {
  290.     var $results;
  291.     var $search_term;
  292.     var $page;
  293.     var $role;
  294.     var $raw_page;
  295.     var $users_per_page = 50;
  296.     var $first_user;
  297.     var $last_user;
  298.     var $query_limit;
  299.     var $query_sort;
  300.     var $query_from_where;
  301.     var $total_users_for_query = 0;
  302.     var $too_many_total_users = false;
  303.     var $search_errors;
  304.  
  305.     function WP_User_Search ($search_term = '', $page = '', $role = '') { // constructor
  306.         $this->search_term = $search_term;
  307.         $this->raw_page = ( '' == $page ) ? false : (int) $page;
  308.         $this->page = (int) ( '' == $page ) ? 1 : $page;
  309.         $this->role = $role;
  310.  
  311.         $this->prepare_query();
  312.         $this->query();
  313.         $this->prepare_vars_for_template_usage();
  314.         $this->do_paging();
  315.     }
  316.  
  317.     function prepare_query() {
  318.         global $wpdb;
  319.         $this->first_user = ($this->page - 1) * $this->users_per_page;
  320.         $this->query_limit = $wpdb->prepare(" LIMIT %d, %d", $this->first_user, $this->users_per_page);
  321.         $this->query_sort = ' ORDER BY user_login';
  322.         $search_sql = '';
  323.         if ( $this->search_term ) {
  324.             $searches = array();
  325.             $search_sql = 'AND (';
  326.             foreach ( array('user_login', 'user_nicename', 'user_email', 'user_url', 'display_name') as $col )
  327.                 $searches[] = $col . " LIKE '%$this->search_term%'";
  328.             $search_sql .= implode(' OR ', $searches);
  329.             $search_sql .= ')';
  330.         }
  331.  
  332.         $this->query_from_where = "FROM $wpdb->users";
  333.         if ( $this->role )
  334.             $this->query_from_where .= $wpdb->prepare(" 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 %s", '%' . $this->role . '%');
  335.         else
  336.             $this->query_from_where .= " WHERE 1=1";
  337.         $this->query_from_where .= " $search_sql";
  338.  
  339.     }
  340.  
  341.     function query() {
  342.         global $wpdb;
  343.         $this->results = $wpdb->get_col('SELECT ID ' . $this->query_from_where . $this->query_sort . $this->query_limit);
  344.  
  345.         if ( $this->results )
  346.             $this->total_users_for_query = $wpdb->get_var('SELECT COUNT(ID) ' . $this->query_from_where); // no limit
  347.         else
  348.             $this->search_errors = new WP_Error('no_matching_users_found', __('No matching users were found!'));
  349.     }
  350.  
  351.     function prepare_vars_for_template_usage() {
  352.         $this->search_term = stripslashes($this->search_term); // done with DB, from now on we want slashes gone
  353.     }
  354.  
  355.     function do_paging() {
  356.         if ( $this->total_users_for_query > $this->users_per_page ) { // have to page the results
  357.             $args = array();
  358.             if( ! empty($this->search_term) )
  359.                 $args['usersearch'] = urlencode($this->search_term);
  360.             if( ! empty($this->role) )
  361.                 $args['role'] = urlencode($this->role);
  362.  
  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' => $args
  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. ?>
  396.