PageRenderTime 26ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/plugins/buddypress/bp-forums/bbpress/bb-includes/functions.bb-users.php

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