/wp-content/plugins/buddypress-old/bp-forums/bbpress/bb-includes/backpress/class.wp-auth.php

https://github.com/pfond/PFOND · PHP · 298 lines · 158 code · 41 blank · 99 comment · 33 complexity · 0d80efdfd135391007d4d6019c03495a MD5 · raw file

  1. <?php
  2. class WP_Auth
  3. {
  4. var $db; // BBPDB object
  5. var $users; // WP_Users object
  6. var $cookies;
  7. var $current = 0;
  8. function WP_Auth( &$db, &$users, $cookies )
  9. {
  10. $this->__construct( $db, $users, $cookies );
  11. }
  12. /**
  13. * @param array $cookies Array indexed by internal name of cookie. Values are arrays of array defining cookie parameters.
  14. * $cookies = array(
  15. * 'auth' => array(
  16. * 0 => array(
  17. * 'domain' => (string) cookie domain
  18. * 'path' => (string) cookie path
  19. * 'name' => (string) cookie name
  20. * 'secure' => (bool) restrict cookie to HTTPS only
  21. * )
  22. * )
  23. * );
  24. *
  25. * At least one cookie is required. Give it an internal name of 'auth'.
  26. *
  27. * Suggested cookie structure:
  28. * logged_in: whether or not a user is logged in. Send everywhere.
  29. * auth: used to authorize user's actions. Send only to domains/paths that need it (e.g. wp-admin/)
  30. * secure_auth: used to authorize user's actions. Send only to domains/paths that need it and only over HTTPS
  31. */
  32. function __construct( &$db, &$users, $cookies )
  33. {
  34. $this->db =& $db;
  35. $this->users =& $users;
  36. $cookies = wp_parse_args( $cookies, array( 'auth' => null ) );
  37. $_cookies = array();
  38. foreach ( $cookies as $_scheme => $_scheme_cookies ) {
  39. foreach ( $_scheme_cookies as $_scheme_cookie ) {
  40. $_cookies[$_scheme][] = wp_parse_args( $_scheme_cookie, array( 'domain' => null, 'path' => null, 'name' => '' ) );
  41. }
  42. unset( $_scheme_cookie );
  43. }
  44. unset( $_scheme, $_scheme_cookies );
  45. $this->cookies = $_cookies;
  46. unset( $_cookies );
  47. }
  48. /**
  49. * Changes the current user by ID or name
  50. *
  51. * Set $id to null and specify a name if you do not know a user's ID
  52. *
  53. * Some WordPress functionality is based on the current user and
  54. * not based on the signed in user. Therefore, it opens the ability
  55. * to edit and perform actions on users who aren't signed in.
  56. *
  57. * @since 2.0.4
  58. * @uses do_action() Calls 'set_current_user' hook after setting the current user.
  59. *
  60. * @param int $id User ID
  61. * @param string $name User's username
  62. * @return BP_User Current user User object
  63. */
  64. function set_current_user( $user_id )
  65. {
  66. $user = $this->users->get_user( $user_id );
  67. if ( !$user || is_wp_error( $user ) ) {
  68. $this->current = 0;
  69. return $this->current;
  70. }
  71. $user_id = $user->ID;
  72. if ( isset( $this->current->ID ) && $user_id == $this->current->ID ) {
  73. return $this->current;
  74. }
  75. if ( class_exists( 'BP_User' ) ) {
  76. $this->current = new BP_User( $user_id );
  77. } else {
  78. $this->current =& $user;
  79. }
  80. // WP add_action( 'set_current_user', 'setup_userdata', 1 );
  81. do_action( 'set_current_user', $user_id );
  82. return $this->current;
  83. }
  84. /**
  85. * Populate variables with information about the currently logged in user
  86. *
  87. * Will set the current user, if the current user is not set. The current
  88. * user will be set to the logged in person. If no user is logged in, then
  89. * it will set the current user to 0, which is invalid and won't have any
  90. * permissions.
  91. *
  92. * @since 0.71
  93. * @uses $current_user Checks if the current user is set
  94. * @uses wp_validate_auth_cookie() Retrieves current logged in user.
  95. *
  96. * @return bool|null False on XMLRPC Request and invalid auth cookie. Null when current user set
  97. */
  98. function get_current_user()
  99. {
  100. if ( !empty( $this->current ) ) {
  101. return $this->current;
  102. }
  103. if ( defined( 'XMLRPC_REQUEST' ) && XMLRPC_REQUEST ) {
  104. $this->set_current_user( 0 );
  105. } elseif ( $user_id = $this->validate_auth_cookie( null, 'logged_in' ) ) {
  106. $this->set_current_user( $user_id );
  107. } else {
  108. $this->set_current_user( 0 );
  109. }
  110. return $this->current;
  111. }
  112. /**
  113. * Validates authentication cookie
  114. *
  115. * The checks include making sure that the authentication cookie
  116. * is set and pulling in the contents (if $cookie is not used).
  117. *
  118. * Makes sure the cookie is not expired. Verifies the hash in
  119. * cookie is what is should be and compares the two.
  120. *
  121. * @since 2.5
  122. *
  123. * @param string $cookie Optional. If used, will validate contents instead of cookie's
  124. * @return bool|int False if invalid cookie, User ID if valid.
  125. */
  126. function validate_auth_cookie( $cookie = null, $scheme = 'auth' )
  127. {
  128. if ( empty( $cookie ) ) {
  129. foreach ( $this->cookies[$scheme] as $_scheme_cookie ) {
  130. // Take the first cookie of type scheme that exists
  131. if ( !empty( $_COOKIE[$_scheme_cookie['name']] ) ) {
  132. $cookie = $_COOKIE[$_scheme_cookie['name']];
  133. break;
  134. }
  135. }
  136. }
  137. if ( !$cookie ) {
  138. return false;
  139. }
  140. $cookie_elements = explode( '|', $cookie );
  141. if ( count( $cookie_elements ) != 3 ) {
  142. do_action( 'auth_cookie_malformed', $cookie, $scheme );
  143. return false;
  144. }
  145. list( $username, $expiration, $hmac ) = $cookie_elements;
  146. $expired = $expiration;
  147. // Allow a grace period for POST and AJAX requests
  148. if ( defined( 'DOING_AJAX' ) || 'POST' == $_SERVER['REQUEST_METHOD'] ) {
  149. $expired += 3600;
  150. }
  151. if ( $expired < time() ) {
  152. do_action( 'auth_cookie_expired', $cookie_elements );
  153. return false;
  154. }
  155. $user = $this->users->get_user( $username, array( 'by' => 'login' ) );
  156. if ( !$user || is_wp_error( $user ) ) {
  157. do_action( 'auth_cookie_bad_username', $cookie_elements );
  158. return $user;
  159. }
  160. $pass_frag = '';
  161. if ( 1 < WP_AUTH_COOKIE_VERSION ) {
  162. $pass_frag = substr( $user->user_pass, 8, 4 );
  163. }
  164. $key = call_user_func( backpress_get_option( 'hash_function_name' ), $username . $pass_frag . '|' . $expiration, $scheme );
  165. $hash = hash_hmac( 'md5', $username . '|' . $expiration, $key );
  166. if ( $hmac != $hash ) {
  167. do_action( 'auth_cookie_bad_hash', $cookie_elements );
  168. return false;
  169. }
  170. do_action( 'auth_cookie_valid', $cookie_elements, $user );
  171. return $user->ID;
  172. }
  173. /**
  174. * Generate authentication cookie contents
  175. *
  176. * @since 2.5
  177. * @uses apply_filters() Calls 'auth_cookie' hook on $cookie contents, User ID
  178. * and expiration of cookie.
  179. *
  180. * @param int $user_id User ID
  181. * @param int $expiration Cookie expiration in seconds
  182. * @return string Authentication cookie contents
  183. */
  184. function generate_auth_cookie( $user_id, $expiration, $scheme = 'auth' )
  185. {
  186. $user = $this->users->get_user( $user_id );
  187. if ( !$user || is_wp_error( $user ) ) {
  188. return $user;
  189. }
  190. $pass_frag = '';
  191. if ( 1 < WP_AUTH_COOKIE_VERSION ) {
  192. $pass_frag = substr( $user->user_pass, 8, 4 );
  193. }
  194. $key = call_user_func( backpress_get_option( 'hash_function_name' ), $user->user_login . $pass_frag . '|' . $expiration, $scheme );
  195. $hash = hash_hmac('md5', $user->user_login . '|' . $expiration, $key);
  196. $cookie = $user->user_login . '|' . $expiration . '|' . $hash;
  197. return apply_filters( 'auth_cookie', $cookie, $user_id, $expiration, $scheme );
  198. }
  199. /**
  200. * Sets the authentication cookies based User ID
  201. *
  202. * The $remember parameter increases the time that the cookie will
  203. * be kept. The default the cookie is kept without remembering is
  204. * two days. When $remember is set, the cookies will be kept for
  205. * 14 days or two weeks.
  206. *
  207. * @since 2.5
  208. *
  209. * @param int $user_id User ID
  210. * @param int $expiration the UNIX time after which the cookie's authentication token is no longer valid
  211. * @param int $expire the UNIX time at which the cookie expires
  212. * @param int $scheme name of the
  213. */
  214. function set_auth_cookie( $user_id, $expiration = 0, $expire = 0, $scheme = 'auth' )
  215. {
  216. if ( !isset( $this->cookies[$scheme] ) ) {
  217. return;
  218. }
  219. if ( !$expiration = absint( $expiration ) ) {
  220. $expiration = time() + 172800; // 2 days
  221. }
  222. $expire = absint( $expire );
  223. foreach ( $this->cookies[$scheme] as $_cookie ) {
  224. $cookie = $this->generate_auth_cookie( $user_id, $expiration, $scheme );
  225. if ( is_wp_error( $cookie ) ) {
  226. return $cookie;
  227. }
  228. do_action( 'set_' . $scheme . '_cookie', $cookie, $expire, $expiration, $user_id, $scheme );
  229. $domain = $_cookie['domain'];
  230. $secure = isset($_cookie['secure']) ? (bool) $_cookie['secure'] : false;
  231. // Set httponly if the php version is >= 5.2.0
  232. if ( version_compare( phpversion(), '5.2.0', 'ge' ) ) {
  233. setcookie( $_cookie['name'], $cookie, $expire, $_cookie['path'], $domain, $secure, true );
  234. } else {
  235. $domain = ( empty( $domain ) ) ? $domain : $domain . '; HttpOnly';
  236. setcookie( $_cookie['name'], $cookie, $expire, $_cookie['path'], $domain, $secure );
  237. }
  238. }
  239. unset( $_cookie );
  240. }
  241. /**
  242. * Deletes all of the cookies associated with authentication
  243. *
  244. * @since 2.5
  245. */
  246. function clear_auth_cookie()
  247. {
  248. do_action( 'clear_auth_cookie' );
  249. foreach ( $this->cookies as $_scheme => $_scheme_cookies ) {
  250. foreach ( $_scheme_cookies as $_cookie ) {
  251. setcookie( $_cookie['name'], ' ', time() - 31536000, $_cookie['path'], $_cookie['domain'] );
  252. }
  253. unset( $_cookie );
  254. }
  255. unset( $_scheme, $_scheme_cookies );
  256. }
  257. }