[ Root ] [ Search ] [ Index ]

PHP Cross Reference of bbPress Trunk

Provided by Yoast

title

Body

[close]

/bb-includes/ -> functions.bb-users.php (source)

   1  <?php
   2  
   3  /* Users */
   4  
   5  function bb_block_current_user() {
   6      global $bbdb;
   7      if ( $id = bb_get_current_user_info( 'id' ) )
   8          bb_update_usermeta( $id, $bbdb->prefix . 'been_blocked', 1 ); // Just for logging.
   9      bb_logout();
  10      bb_die(__("You've been blocked.  If you think a mistake has been made, contact this site's administrator."));
  11  }
  12  
  13  function bb_get_user( $user_id, $args = null ) {
  14      global $wp_users_object;
  15      $user = $wp_users_object->get_user( $user_id, $args );
  16      if ( is_wp_error($user) )
  17          return false;
  18      return $user;
  19  }
  20  
  21  function bb_cache_users( $ids ) {
  22      global $wp_users_object;
  23      $wp_users_object->get_user( $ids );
  24  }
  25  
  26  function bb_get_user_by_nicename( $nicename ) {
  27      global $wp_users_object;
  28      $user = $wp_users_object->get_user( $nicename, array( 'by' => 'nicename' ) );
  29      if ( is_wp_error($user) )
  30          return false;
  31      return $user;
  32  }
  33  
  34  function bb_delete_user( $user_id, $reassign = 0 ) {
  35      global $wp_users_object, $bbdb;
  36  
  37      if ( !$user = bb_get_user( $user_id ) )
  38          return false;
  39  
  40      if ( $reassign ) {
  41          if ( !$new_user = bb_get_user( $reassign ) )
  42              return false;
  43          $bbdb->update( $bbdb->posts, array( 'poster_id' => $new_user->ID ), array( 'poster_id' => $user->ID ) );
  44          $bbdb->update( $bbdb->term_relationships, array( 'user_id' => $new_user->ID ), array( 'user_id' => $user->ID ) );
  45          $bbdb->update( $bbdb->topics, array( 'topic_poster' => $new_user->ID, 'topic_poster_name' => $new_user->user_login), array( 'topic_poster' => $user->ID ) );
  46          $bbdb->update( $bbdb->topics, array( 'topic_last_poster' => $new_user->ID, 'topic_last_poster_name' => $new_user->user_login ), array( 'topic_last_poster' => $user->ID ) );
  47          bb_update_topics_replied( $new_user->ID );
  48          wp_cache_flush( 'bb_post' );
  49          wp_cache_flush( 'bb_thread' );
  50          wp_cache_flush( 'bb_topic_tag' );
  51          wp_cache_flush( 'bb_topic' );
  52      }
  53  
  54      do_action( 'bb_delete_user', $user->ID, $reassign );
  55  
  56      $wp_users_object->delete_user( $user->ID );
  57  
  58      return true;
  59  }
  60  
  61  function bb_update_topics_replied( $user_id ) {
  62      global $bbdb;
  63  
  64      $user_id = (int) $user_id;
  65  
  66      if ( !$user = bb_get_user( $user_id ) )
  67          return false;
  68  
  69      $topics_replied = (int) $bbdb->get_var( $bbdb->prepare( "SELECT COUNT(DISTINCT topic_id) FROM $bbdb->posts WHERE post_status = '0' AND poster_id = %d", $user_id ) );
  70      return bb_update_usermeta( $user_id, $bbdb->prefix . 'topics_replied', $topics_replied );
  71  }
  72  
  73  function bb_update_user_status( $user_id, $user_status = 0 ) {
  74      global $wp_users_object;
  75      $user = bb_get_user( $user_id );
  76      $user_status = (int) $user_status;
  77      $wp_users_object->update_user( $user->ID, compact( 'user_status' ) );
  78  }
  79  
  80  function bb_trusted_roles() {
  81      return apply_filters( 'bb_trusted_roles', array('moderator', 'administrator', 'keymaster') );
  82  }
  83  
  84  function bb_is_trusted_user( $user ) { // ID, user_login, WP_User, DB user obj
  85      if ( is_numeric($user) || is_string($user) )
  86          $user = new BP_User( $user );
  87      elseif ( is_object($user) && is_a($user, 'BP_User') ); // Intentional
  88      elseif ( is_object($user) && isset($user->ID) && isset($user->user_login) ) // Make sure it's actually a user object
  89          $user = new BP_User( $user->ID );
  90      else
  91          return;
  92  
  93      if ( !$user->ID )
  94          return;
  95  
  96      return apply_filters( 'bb_is_trusted_user', (bool) array_intersect(bb_trusted_roles(), $user->roles), $user->ID );
  97  }
  98  
  99  function bb_apply_wp_role_map_to_user( $user, $reload = true ) {
 100      // Expects only user ids
 101      if ( !is_numeric( $user ) ) {
 102          return;
 103      }
 104  
 105      $user = (int) $user;
 106  
 107      if ( !$wordpress_table_prefix = bb_get_option('wp_table_prefix') ) {
 108          return;
 109      }
 110  
 111      if ( $wordpress_mu_primary_blog_id = bb_get_option( 'wordpress_mu_primary_blog_id' ) ) {
 112          $wordpress_table_prefix .= $wordpress_mu_primary_blog_id . '_';
 113      }
 114  
 115      if ( !$wordpress_roles_map = bb_get_option( 'wp_roles_map' ) ) {
 116          return;
 117      }
 118  
 119      global $bbdb;
 120      global $wp_roles;
 121      global $bb;
 122  
 123      static $bbpress_roles_map = false;
 124  
 125      if ( !$bbpress_roles_map ) {
 126          $bbpress_roles_map = array();
 127          foreach ( $wp_roles->get_names() as $_bbpress_role => $_bbpress_rolename ) {
 128              $bbpress_roles_map[$_bbpress_role] = 'subscriber';
 129          }
 130          unset( $_bbpress_role, $_bbpress_rolename );
 131          $bbpress_roles_map = array_merge( $bbpress_roles_map, array_flip( $wordpress_roles_map ) );
 132          unset( $bbpress_roles_map['inactive'], $bbpress_roles_map['blocked'] );
 133      }
 134  
 135      static $wordpress_userlevel_map = array(
 136          'administrator' => 10,
 137          'editor' => 7,
 138          'author' => 2,
 139          'contributor' => 1,
 140          'subscriber' => 0
 141      );
 142  
 143      $bbpress_roles = bb_get_usermeta( $user, $bbdb->prefix . 'capabilities' );
 144      $wordpress_roles = bb_get_usermeta( $user, $wordpress_table_prefix . 'capabilities' );
 145  
 146      if ( !$bbpress_roles && is_array( $wordpress_roles ) ) {
 147          $bbpress_roles_new = array();
 148  
 149          foreach ( $wordpress_roles as $wordpress_role => $wordpress_role_value ) {
 150              if ( $wordpress_roles_map[strtolower( $wordpress_role )] && $wordpress_role_value ) {
 151                  $bbpress_roles_new[$wordpress_roles_map[strtolower( $wordpress_role )]] = true;
 152              }
 153          }
 154  
 155          if ( count( $bbpress_roles_new ) ) {
 156              bb_update_usermeta( $user, $bbdb->prefix . 'capabilities', $bbpress_roles_new );
 157              if ( $reload ) {
 158                  header( 'Location: ' . bb_get_uri( null, null, BB_URI_CONTEXT_HEADER ) );
 159                  exit;
 160              }
 161          }
 162      } elseif ( !$wordpress_roles && is_array( $bbpress_roles ) ) {
 163          $wordpress_roles_new = array();
 164  
 165          foreach ( $bbpress_roles as $bbpress_role => $bbpress_role_value ) {
 166              if ( $bbpress_roles_map[strtolower( $bbpress_role )] && $bbpress_role_value ) {
 167                  $wordpress_roles_new[$bbpress_roles_map[strtolower( $bbpress_role )]] = true;
 168                  $wordpress_userlevels_new[] = $wordpress_userlevel_map[$bbpress_roles_map[strtolower( $bbpress_role )]];
 169              }
 170          }
 171  
 172          if ( count( $wordpress_roles_new ) ) {
 173              bb_update_usermeta( $user, $wordpress_table_prefix . 'capabilities', $wordpress_roles_new );
 174              bb_update_usermeta( $user, $wordpress_table_prefix . 'user_level', max( $wordpress_userlevels_new ) );
 175          }
 176      }
 177  }
 178  
 179  function bb_apply_wp_role_map_to_orphans() {
 180      if ( !$wordpress_table_prefix = bb_get_option('wp_table_prefix') ) {
 181          return;
 182      }
 183  
 184      if ( $wordpress_mu_primary_blog_id = bb_get_option( 'wordpress_mu_primary_blog_id' ) ) {
 185          $wordpress_table_prefix .= $wordpress_mu_primary_blog_id . '_';
 186      }
 187  
 188      $role_query = <<<EOQ
 189          SELECT
 190              ID
 191          FROM
 192              `%1\$s`
 193          LEFT JOIN `%2\$s` AS bbrole
 194              ON ID = bbrole.user_id
 195              AND bbrole.meta_key = '%3\$scapabilities'
 196          LEFT JOIN `%2\$s` AS wprole
 197              ON ID = wprole.user_id
 198              AND wprole.meta_key = '%4\$scapabilities'
 199          WHERE
 200              bbrole.meta_key IS NULL OR
 201              bbrole.meta_value IS NULL OR
 202              wprole.meta_key IS NULL OR
 203              wprole.meta_value IS NULL
 204          ORDER BY
 205              ID
 206  EOQ;
 207  
 208      global $bbdb;
 209  
 210      $role_query = $bbdb->prepare( $role_query, $bbdb->users, $bbdb->usermeta, $bbdb->prefix, $wordpress_table_prefix );
 211  
 212      if ( $user_ids = $bbdb->get_col( $role_query ) ) {
 213          foreach ( $user_ids as $user_id ) {
 214              bb_apply_wp_role_map_to_user( $user_id, false );
 215          }
 216      }
 217  }
 218  
 219  /**
 220   * Updates a user's details in the database
 221   *
 222   * {@internal Missing Long Description}}
 223   *
 224   * @since 0.7.2
 225   * @global bbdb $bbdb
 226   *
 227   * @param int $user_id
 228   * @param string $user_email
 229   * @param string $user_url
 230   * @return int
 231   */
 232  function bb_update_user( $user_id, $user_email, $user_url, $display_name ) {
 233      global $wp_users_object;
 234  
 235      $user_id = (int) $user_id;
 236      $user_url = bb_fix_link( $user_url );
 237  
 238      $wp_users_object->update_user( $user_id, compact( 'user_email', 'user_url', 'display_name' ) );
 239  
 240      do_action('bb_update_user', $user_id);
 241      return $user_id;
 242  }
 243  
 244  /**
 245   * Sends a reset password email
 246   *
 247   * Sends an email to the email address specified in the user's profile
 248   * prompting them to change their password.
 249   *
 250   * @since 0.7.2
 251   * @global bbdb $bbdb
 252   *
 253   * @param string $user_login
 254   * @return bool
 255   */
 256  function bb_reset_email( $user_login )
 257  {
 258      global $bbdb;
 259  
 260      $user_login = sanitize_user( $user_login, true );
 261  
 262      if ( !$user = $bbdb->get_row( $bbdb->prepare( "SELECT * FROM $bbdb->users WHERE user_login = %s", $user_login ) ) ) {
 263          return new WP_Error( 'user_does_not_exist', __( 'The specified user does not exist.' ) );
 264      }
 265  
 266      $resetkey = substr( md5( bb_generate_password() ), 0, 15 );
 267      bb_update_usermeta( $user->ID, 'newpwdkey', $resetkey );
 268  
 269      $reseturi = bb_get_uri(
 270          'bb-reset-password.php',
 271          array( 'key' => $resetkey ),
 272          BB_URI_CONTEXT_TEXT + BB_URI_CONTEXT_BB_USER_FORMS
 273      );
 274  
 275      $message = sprintf(
 276          __( "If you wanted to reset your password, you may do so by visiting the following address:\n\n%s\n\nIf you don't want to reset your password, just ignore this email. Thanks!" ),
 277          $reseturi
 278      );
 279      $message = apply_filters( 'bb_reset_email_message', $message, $user, $reseturi, $resetkey );
 280  
 281      $subject = sprintf(
 282          __( '%s: Password Reset' ),
 283          bb_get_option( 'name' )
 284      );
 285      $subject = apply_filters( 'bb_reset_email_subject', $subject, $user );
 286  
 287      $mail_result = bb_mail(
 288          bb_get_user_email( $user->ID ),
 289          $subject,
 290          $message
 291      );
 292  
 293      if ( !$mail_result ) {
 294          return new WP_Error( 'sending_mail_failed', __( 'The email containing the password reset link could not be sent.' ) );
 295      }
 296  
 297      return true;
 298  }
 299  
 300  /**
 301   * Handles the resetting of users' passwords
 302   *
 303   * Handles resetting a user's password, prompted by an email sent by
 304   * {@see bb_reset_email()}
 305   *
 306   * @since 0.7.2
 307   * @global bbdb $bbdb
 308   *
 309   * @param string $key
 310   * @return unknown
 311   */
 312  function bb_reset_password( $key )
 313  {
 314      global $bbdb;
 315  
 316      $key = sanitize_user( $key, true );
 317  
 318      if ( empty( $key ) || !is_string( $key ) ) {
 319          return new WP_Error( 'invalid_key', __( 'Invalid key' ) );
 320      }
 321  
 322      if ( !$user_id = $bbdb->get_var( $bbdb->prepare( "SELECT user_id FROM $bbdb->usermeta WHERE meta_key = 'newpwdkey' AND meta_value = %s", $key ) ) ) {
 323          return new WP_Error( 'invalid_key', __( 'Invalid key' ) );
 324      }
 325  
 326      $user = new BP_User( $user_id );
 327  
 328      if ( !$user || is_wp_error( $user ) ) {
 329          return new WP_Error( 'invalid_key', __( 'Invalid key' ) );
 330      }
 331  
 332      if ( bb_has_broken_pass( $user->ID ) ) {
 333          bb_block_current_user();
 334      }
 335  
 336      if ( !$user->has_cap( 'change_user_password', $user->ID ) ) {
 337          return new WP_Error( 'permission_denied', __( 'You are not allowed to change your password.' ) );
 338      }
 339  
 340      $newpass = bb_generate_password();
 341      bb_update_user_password( $user->ID, $newpass );
 342      if ( !bb_send_pass( $user->ID, $newpass ) ) {
 343          return new WP_Error( 'sending_mail_failed', __( 'The email containing the new password could not be sent.' ) );
 344      }
 345  
 346      bb_update_usermeta( $user->ID, 'newpwdkey', '' );
 347      return true;
 348  }
 349  
 350  /**
 351   * Updates a user's password in the database
 352   *
 353   * {@internal Missing Long Description}}
 354   *
 355   * @since 0.7.2
 356   * @global bbdb $bbdb
 357   *
 358   * @param int $user_id
 359   * @param string $password
 360   * @return int
 361   */
 362  function bb_update_user_password( $user_id, $password ) {
 363      global $wp_users_object;
 364  
 365      $user_id = (int) $user_id;
 366  
 367      $wp_users_object->set_password( $password, $user_id );
 368  
 369      do_action('bb_update_user_password', $user_id);
 370      return $user_id;
 371  }
 372  
 373  /**
 374   * Sends an email with the user's new password
 375   *
 376   * {@internal Missing Long Description}}
 377   *
 378   * @since 0.7.2
 379   * @global bbdb $bbdb {@internal Not used}}
 380   *
 381   * @param int|string $user
 382   * @param string $pass
 383   * @return bool
 384   */
 385  function bb_send_pass( $user, $pass )
 386  {
 387      if ( !$user = bb_get_user( $user ) ) {
 388          return false;
 389      }
 390  
 391      $message = sprintf(
 392          __( "Your username is: %1\$s \nYour password is: %2\$s \nYou can now log in: %3\$s \n\nEnjoy!" ),
 393          $user->user_login,
 394          $pass,
 395          bb_get_uri( null, null, BB_URI_CONTEXT_TEXT )
 396      );
 397      $message = apply_filters( 'bb_send_pass_message', $message, $user, $pass );
 398  
 399      $subject = sprintf(
 400          __( '%s: Password' ),
 401          bb_get_option( 'name' )
 402      );
 403      $subject = apply_filters( 'bb_send_pass_subject', $subject, $user );
 404  
 405      return bb_mail(
 406          bb_get_user_email( $user->ID ),
 407          $subject,
 408          $message
 409      );
 410  }
 411  
 412  
 413  
 414  /* Favorites */
 415  
 416  function get_user_favorites( $user_id, $topics = false ) {
 417      $user = bb_get_user( $user_id );
 418      if ( !empty($user->favorites) ) {
 419          if ( $topics )
 420              $query = new BB_Query( 'topic', array('favorites' => $user_id, 'index_hint' => 'USE INDEX (`forum_time`)'), 'get_user_favorites' );
 421          else
 422              $query = new BB_Query( 'post', array('favorites' => $user_id), 'get_user_favorites' );
 423          return $query->results;
 424      }
 425  }
 426  
 427  function is_user_favorite( $user_id = 0, $topic_id = 0 ) {
 428      if ( $user_id )
 429          $user = bb_get_user( $user_id );
 430      else
 431           global $user;
 432      if ( $topic_id )
 433          $topic = get_topic( $topic_id );
 434      else
 435          global $topic;
 436      if ( !$user || !$topic )
 437          return;
 438  
 439      if ( isset($user->favorites) )
 440              return in_array($topic->topic_id, explode(',', $user->favorites));
 441      return false;
 442  }
 443  
 444  function bb_add_user_favorite( $user_id, $topic_id ) {
 445      global $bbdb;
 446      $user_id = (int) $user_id;
 447      $topic_id = (int) $topic_id;
 448      $user = bb_get_user( $user_id );
 449      $topic = get_topic( $topic_id );
 450      if ( !$user || !$topic )
 451          return false;
 452  
 453      $fav = $user->favorites ? explode(',', $user->favorites) : array();
 454      if ( ! in_array( $topic_id, $fav ) ) {
 455          $fav[] = $topic_id;
 456          $fav = implode(',', $fav);
 457          bb_update_usermeta( $user->ID, $bbdb->prefix . 'favorites', $fav);
 458      }
 459      do_action('bb_add_user_favorite', $user_id, $topic_id);
 460      return true;
 461  }
 462  
 463  function bb_remove_user_favorite( $user_id, $topic_id ) {
 464      global $bbdb;
 465      $user_id = (int) $user_id;
 466      $topic_id = (int) $topic_id;
 467      $user = bb_get_user( $user_id );
 468      if ( !$user )
 469          return false;
 470  
 471      $fav = explode(',', $user->favorites);
 472      if ( is_int( $pos = array_search($topic_id, $fav) ) ) {
 473          array_splice($fav, $pos, 1);
 474          $fav = implode(',', $fav);
 475          bb_update_usermeta( $user->ID, $bbdb->prefix . 'favorites', $fav);
 476      }
 477      do_action('bb_remove_user_favorite', $user_id, $topic_id);
 478      return true;
 479  }


Generated: Mon Nov 15 04:45:27 2010 Cross-referenced by PHPXref 0.7