PageRenderTime 44ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/htdocs/wp-includes/class-wp-session-tokens.php

https://gitlab.com/VTTE/sitios-vtte
PHP | 294 lines | 76 code | 27 blank | 191 comment | 6 complexity | 5465558fc9cc88c629fb75369304f8e6 MD5 | raw file
  1. <?php
  2. /**
  3. * Session API: WP_Session_Tokens class
  4. *
  5. * @package WordPress
  6. * @subpackage Session
  7. * @since 4.7.0
  8. */
  9. /**
  10. * Abstract class for managing user session tokens.
  11. *
  12. * @since 4.0.0
  13. */
  14. abstract class WP_Session_Tokens {
  15. /**
  16. * User ID.
  17. *
  18. * @since 4.0.0
  19. * @var int User ID.
  20. */
  21. protected $user_id;
  22. /**
  23. * Protected constructor. Use the `get_instance()` method to get the instance.
  24. *
  25. * @since 4.0.0
  26. *
  27. * @param int $user_id User whose session to manage.
  28. */
  29. protected function __construct( $user_id ) {
  30. $this->user_id = $user_id;
  31. }
  32. /**
  33. * Retrieves a session manager instance for a user.
  34. *
  35. * This method contains a {@see 'session_token_manager'} filter, allowing a plugin to swap out
  36. * the session manager for a subclass of `WP_Session_Tokens`.
  37. *
  38. * @since 4.0.0
  39. *
  40. * @param int $user_id User whose session to manage.
  41. * @return WP_Session_Tokens The session object, which is by default an instance of
  42. * the `WP_User_Meta_Session_Tokens` class.
  43. */
  44. final public static function get_instance( $user_id ) {
  45. /**
  46. * Filters the class name for the session token manager.
  47. *
  48. * @since 4.0.0
  49. *
  50. * @param string $session Name of class to use as the manager.
  51. * Default 'WP_User_Meta_Session_Tokens'.
  52. */
  53. $manager = apply_filters( 'session_token_manager', 'WP_User_Meta_Session_Tokens' );
  54. return new $manager( $user_id );
  55. }
  56. /**
  57. * Hashes the given session token for storage.
  58. *
  59. * @since 4.0.0
  60. *
  61. * @param string $token Session token to hash.
  62. * @return string A hash of the session token (a verifier).
  63. */
  64. final private function hash_token( $token ) {
  65. // If ext/hash is not present, use sha1() instead.
  66. if ( function_exists( 'hash' ) ) {
  67. return hash( 'sha256', $token );
  68. } else {
  69. return sha1( $token );
  70. }
  71. }
  72. /**
  73. * Retrieves a user's session for the given token.
  74. *
  75. * @since 4.0.0
  76. *
  77. * @param string $token Session token.
  78. * @return array|null The session, or null if it does not exist.
  79. */
  80. final public function get( $token ) {
  81. $verifier = $this->hash_token( $token );
  82. return $this->get_session( $verifier );
  83. }
  84. /**
  85. * Validates the given session token for authenticity and validity.
  86. *
  87. * Checks that the given token is present and hasn't expired.
  88. *
  89. * @since 4.0.0
  90. *
  91. * @param string $token Token to verify.
  92. * @return bool Whether the token is valid for the user.
  93. */
  94. final public function verify( $token ) {
  95. $verifier = $this->hash_token( $token );
  96. return (bool) $this->get_session( $verifier );
  97. }
  98. /**
  99. * Generates a session token and attaches session information to it.
  100. *
  101. * A session token is a long, random string. It is used in a cookie
  102. * to link that cookie to an expiration time and to ensure the cookie
  103. * becomes invalidated when the user logs out.
  104. *
  105. * This function generates a token and stores it with the associated
  106. * expiration time (and potentially other session information via the
  107. * {@see 'attach_session_information'} filter).
  108. *
  109. * @since 4.0.0
  110. *
  111. * @param int $expiration Session expiration timestamp.
  112. * @return string Session token.
  113. */
  114. final public function create( $expiration ) {
  115. /**
  116. * Filters the information attached to the newly created session.
  117. *
  118. * Can be used to attach further information to a session.
  119. *
  120. * @since 4.0.0
  121. *
  122. * @param array $session Array of extra data.
  123. * @param int $user_id User ID.
  124. */
  125. $session = apply_filters( 'attach_session_information', array(), $this->user_id );
  126. $session['expiration'] = $expiration;
  127. // IP address.
  128. if ( ! empty( $_SERVER['REMOTE_ADDR'] ) ) {
  129. $session['ip'] = $_SERVER['REMOTE_ADDR'];
  130. }
  131. // User-agent.
  132. if ( ! empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
  133. $session['ua'] = wp_unslash( $_SERVER['HTTP_USER_AGENT'] );
  134. }
  135. // Timestamp.
  136. $session['login'] = time();
  137. $token = wp_generate_password( 43, false, false );
  138. $this->update( $token, $session );
  139. return $token;
  140. }
  141. /**
  142. * Updates the data for the session with the given token.
  143. *
  144. * @since 4.0.0
  145. *
  146. * @param string $token Session token to update.
  147. * @param array $session Session information.
  148. */
  149. final public function update( $token, $session ) {
  150. $verifier = $this->hash_token( $token );
  151. $this->update_session( $verifier, $session );
  152. }
  153. /**
  154. * Destroys the session with the given token.
  155. *
  156. * @since 4.0.0
  157. *
  158. * @param string $token Session token to destroy.
  159. */
  160. final public function destroy( $token ) {
  161. $verifier = $this->hash_token( $token );
  162. $this->update_session( $verifier, null );
  163. }
  164. /**
  165. * Destroys all sessions for this user except the one with the given token (presumably the one in use).
  166. *
  167. * @since 4.0.0
  168. *
  169. * @param string $token_to_keep Session token to keep.
  170. */
  171. final public function destroy_others( $token_to_keep ) {
  172. $verifier = $this->hash_token( $token_to_keep );
  173. $session = $this->get_session( $verifier );
  174. if ( $session ) {
  175. $this->destroy_other_sessions( $verifier );
  176. } else {
  177. $this->destroy_all_sessions();
  178. }
  179. }
  180. /**
  181. * Determines whether a session is still valid, based on its expiration timestamp.
  182. *
  183. * @since 4.0.0
  184. *
  185. * @param array $session Session to check.
  186. * @return bool Whether session is valid.
  187. */
  188. final protected function is_still_valid( $session ) {
  189. return $session['expiration'] >= time();
  190. }
  191. /**
  192. * Destroys all sessions for a user.
  193. *
  194. * @since 4.0.0
  195. */
  196. final public function destroy_all() {
  197. $this->destroy_all_sessions();
  198. }
  199. /**
  200. * Destroys all sessions for all users.
  201. *
  202. * @since 4.0.0
  203. */
  204. final public static function destroy_all_for_all_users() {
  205. /** This filter is documented in wp-includes/class-wp-session-tokens.php */
  206. $manager = apply_filters( 'session_token_manager', 'WP_User_Meta_Session_Tokens' );
  207. call_user_func( array( $manager, 'drop_sessions' ) );
  208. }
  209. /**
  210. * Retrieves all sessions for a user.
  211. *
  212. * @since 4.0.0
  213. *
  214. * @return array Sessions for a user.
  215. */
  216. final public function get_all() {
  217. return array_values( $this->get_sessions() );
  218. }
  219. /**
  220. * Retrieves all sessions of the user.
  221. *
  222. * @since 4.0.0
  223. *
  224. * @return array Sessions of the user.
  225. */
  226. abstract protected function get_sessions();
  227. /**
  228. * Retrieves a session based on its verifier (token hash).
  229. *
  230. * @since 4.0.0
  231. *
  232. * @param string $verifier Verifier for the session to retrieve.
  233. * @return array|null The session, or null if it does not exist.
  234. */
  235. abstract protected function get_session( $verifier );
  236. /**
  237. * Updates a session based on its verifier (token hash).
  238. *
  239. * Omitting the second argument destroys the session.
  240. *
  241. * @since 4.0.0
  242. *
  243. * @param string $verifier Verifier for the session to update.
  244. * @param array $session Optional. Session. Omitting this argument destroys the session.
  245. */
  246. abstract protected function update_session( $verifier, $session = null );
  247. /**
  248. * Destroys all sessions for this user, except the single session with the given verifier.
  249. *
  250. * @since 4.0.0
  251. *
  252. * @param string $verifier Verifier of the session to keep.
  253. */
  254. abstract protected function destroy_other_sessions( $verifier );
  255. /**
  256. * Destroys all sessions for the user.
  257. *
  258. * @since 4.0.0
  259. */
  260. abstract protected function destroy_all_sessions();
  261. /**
  262. * Destroys all sessions for all users.
  263. *
  264. * @since 4.0.0
  265. */
  266. public static function drop_sessions() {}
  267. }