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

/wp-includes/session.php

https://gitlab.com/webkod3r/tripolis
PHP | 440 lines | 125 code | 42 blank | 273 comment | 13 complexity | bff6c07a8e83ccf4178883fd85cc0cf3 MD5 | raw file
  1. <?php
  2. /**
  3. * Abstract class for managing user session tokens.
  4. *
  5. * @since 4.0.0
  6. */
  7. abstract class WP_Session_Tokens {
  8. /**
  9. * User ID.
  10. *
  11. * @since 4.0.0
  12. * @access protected
  13. * @var int User ID.
  14. */
  15. protected $user_id;
  16. /**
  17. * Protected constructor.
  18. *
  19. * @since 4.0.0
  20. *
  21. * @param int $user_id User whose session to manage.
  22. */
  23. protected function __construct( $user_id ) {
  24. $this->user_id = $user_id;
  25. }
  26. /**
  27. * Get a session token manager instance for a user.
  28. *
  29. * This method contains a filter that allows a plugin to swap out
  30. * the session manager for a subclass of WP_Session_Tokens.
  31. *
  32. * @since 4.0.0
  33. * @access public
  34. * @static
  35. *
  36. * @param int $user_id User whose session to manage.
  37. */
  38. final public static function get_instance( $user_id ) {
  39. /**
  40. * Filter the session token manager used.
  41. *
  42. * @since 4.0.0
  43. *
  44. * @param string $session Name of class to use as the manager.
  45. * Default 'WP_User_Meta_Session_Tokens'.
  46. */
  47. $manager = apply_filters( 'session_token_manager', 'WP_User_Meta_Session_Tokens' );
  48. return new $manager( $user_id );
  49. }
  50. /**
  51. * Hashes a session token for storage.
  52. *
  53. * @since 4.0.0
  54. * @access private
  55. *
  56. * @param string $token Session token to hash.
  57. * @return string A hash of the session token (a verifier).
  58. */
  59. final private function hash_token( $token ) {
  60. // If ext/hash is not present, use sha1() instead.
  61. if ( function_exists( 'hash' ) ) {
  62. return hash( 'sha256', $token );
  63. } else {
  64. return sha1( $token );
  65. }
  66. }
  67. /**
  68. * Get a user's session.
  69. *
  70. * @since 4.0.0
  71. * @access public
  72. *
  73. * @param string $token Session token
  74. * @return array User session
  75. */
  76. final public function get( $token ) {
  77. $verifier = $this->hash_token( $token );
  78. return $this->get_session( $verifier );
  79. }
  80. /**
  81. * Validate a user's session token as authentic.
  82. *
  83. * Checks that the given token is present and hasn't expired.
  84. *
  85. * @since 4.0.0
  86. * @access public
  87. *
  88. * @param string $token Token to verify.
  89. * @return bool Whether the token is valid for the user.
  90. */
  91. final public function verify( $token ) {
  92. $verifier = $this->hash_token( $token );
  93. return (bool) $this->get_session( $verifier );
  94. }
  95. /**
  96. * Generate a session token and attach session information to it.
  97. *
  98. * A session token is a long, random string. It is used in a cookie
  99. * link that cookie to an expiration time and to ensure the cookie
  100. * becomes invalidated upon logout.
  101. *
  102. * This function generates a token and stores it with the associated
  103. * expiration time (and potentially other session information via the
  104. * `attach_session_information` filter).
  105. *
  106. * @since 4.0.0
  107. * @access public
  108. *
  109. * @param int $expiration Session expiration timestamp.
  110. * @return string Session token.
  111. */
  112. final public function create( $expiration ) {
  113. /**
  114. * Filter the information attached to the newly created session.
  115. *
  116. * Could be used in the future to attach information such as
  117. * IP address or user agent to a session.
  118. *
  119. * @since 4.0.0
  120. *
  121. * @param array $session Array of extra data.
  122. * @param int $user_id User ID.
  123. */
  124. $session = apply_filters( 'attach_session_information', array(), $this->user_id );
  125. $session['expiration'] = $expiration;
  126. // IP address.
  127. if ( !empty( $_SERVER['REMOTE_ADDR'] ) ) {
  128. $session['ip'] = $_SERVER['REMOTE_ADDR'];
  129. }
  130. // User-agent.
  131. if ( ! empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
  132. $session['ua'] = wp_unslash( $_SERVER['HTTP_USER_AGENT'] );
  133. }
  134. // Timestamp
  135. $session['login'] = time();
  136. $token = wp_generate_password( 43, false, false );
  137. $this->update( $token, $session );
  138. return $token;
  139. }
  140. /**
  141. * Update a session token.
  142. *
  143. * @since 4.0.0
  144. * @access public
  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. * Destroy a session token.
  155. *
  156. * @since 4.0.0
  157. * @access public
  158. *
  159. * @param string $token Session token to destroy.
  160. */
  161. final public function destroy( $token ) {
  162. $verifier = $this->hash_token( $token );
  163. $this->update_session( $verifier, null );
  164. }
  165. /**
  166. * Destroy all session tokens for this user,
  167. * except a single token, presumably the one in use.
  168. *
  169. * @since 4.0.0
  170. * @access public
  171. *
  172. * @param string $token_to_keep Session token to keep.
  173. */
  174. final public function destroy_others( $token_to_keep ) {
  175. $verifier = $this->hash_token( $token_to_keep );
  176. $session = $this->get_session( $verifier );
  177. if ( $session ) {
  178. $this->destroy_other_sessions( $verifier );
  179. } else {
  180. $this->destroy_all_sessions();
  181. }
  182. }
  183. /**
  184. * Determine whether a session token is still valid,
  185. * based on expiration.
  186. *
  187. * @since 4.0.0
  188. * @access protected
  189. *
  190. * @param array $session Session to check.
  191. * @return bool Whether session is valid.
  192. */
  193. final protected function is_still_valid( $session ) {
  194. return $session['expiration'] >= time();
  195. }
  196. /**
  197. * Destroy all session tokens for a user.
  198. *
  199. * @since 4.0.0
  200. * @access public
  201. */
  202. final public function destroy_all() {
  203. $this->destroy_all_sessions();
  204. }
  205. /**
  206. * Destroy all session tokens for all users.
  207. *
  208. * @since 4.0.0
  209. * @access public
  210. * @static
  211. */
  212. final public static function destroy_all_for_all_users() {
  213. $manager = apply_filters( 'session_token_manager', 'WP_User_Meta_Session_Tokens' );
  214. call_user_func( array( $manager, 'drop_sessions' ) );
  215. }
  216. /**
  217. * Retrieve all sessions of a user.
  218. *
  219. * @since 4.0.0
  220. * @access public
  221. *
  222. * @return array Sessions of a user.
  223. */
  224. final public function get_all() {
  225. return array_values( $this->get_sessions() );
  226. }
  227. /**
  228. * This method should retrieve all sessions of a user, keyed by verifier.
  229. *
  230. * @since 4.0.0
  231. * @access protected
  232. *
  233. * @return array Sessions of a user, keyed by verifier.
  234. */
  235. abstract protected function get_sessions();
  236. /**
  237. * This method should look up a session by its verifier (token hash).
  238. *
  239. * @since 4.0.0
  240. * @access protected
  241. *
  242. * @param string $verifier Verifier of the session to retrieve.
  243. * @return array|null The session, or null if it does not exist.
  244. */
  245. abstract protected function get_session( $verifier );
  246. /**
  247. * This method should update a session by its verifier.
  248. *
  249. * Omitting the second argument should destroy the session.
  250. *
  251. * @since 4.0.0
  252. * @access protected
  253. *
  254. * @param string $verifier Verifier of the session to update.
  255. * @param array $session Optional. Session. Omitting this argument destroys the session.
  256. */
  257. abstract protected function update_session( $verifier, $session = null );
  258. /**
  259. * This method should destroy all session tokens for this user,
  260. * except a single session passed.
  261. *
  262. * @since 4.0.0
  263. * @access protected
  264. *
  265. * @param string $verifier Verifier of the session to keep.
  266. */
  267. abstract protected function destroy_other_sessions( $verifier );
  268. /**
  269. * This method should destroy all sessions for a user.
  270. *
  271. * @since 4.0.0
  272. * @access protected
  273. */
  274. abstract protected function destroy_all_sessions();
  275. /**
  276. * This static method should destroy all session tokens for all users.
  277. *
  278. * @since 4.0.0
  279. * @access public
  280. * @static
  281. */
  282. public static function drop_sessions() {}
  283. }
  284. /**
  285. * Meta-based user sessions token manager.
  286. *
  287. * @since 4.0.0
  288. */
  289. class WP_User_Meta_Session_Tokens extends WP_Session_Tokens {
  290. /**
  291. * Get all sessions of a user.
  292. *
  293. * @since 4.0.0
  294. * @access protected
  295. *
  296. * @return array Sessions of a user.
  297. */
  298. protected function get_sessions() {
  299. $sessions = get_user_meta( $this->user_id, 'session_tokens', true );
  300. if ( ! is_array( $sessions ) ) {
  301. return array();
  302. }
  303. $sessions = array_map( array( $this, 'prepare_session' ), $sessions );
  304. return array_filter( $sessions, array( $this, 'is_still_valid' ) );
  305. }
  306. /**
  307. * Converts an expiration to an array of session information.
  308. *
  309. * @param mixed $session Session or expiration.
  310. * @return array Session.
  311. */
  312. protected function prepare_session( $session ) {
  313. if ( is_int( $session ) ) {
  314. return array( 'expiration' => $session );
  315. }
  316. return $session;
  317. }
  318. /**
  319. * Retrieve a session by its verifier (token hash).
  320. *
  321. * @since 4.0.0
  322. * @access protected
  323. *
  324. * @param string $verifier Verifier of the session to retrieve.
  325. * @return array|null The session, or null if it does not exist
  326. */
  327. protected function get_session( $verifier ) {
  328. $sessions = $this->get_sessions();
  329. if ( isset( $sessions[ $verifier ] ) ) {
  330. return $sessions[ $verifier ];
  331. }
  332. return null;
  333. }
  334. /**
  335. * Update a session by its verifier.
  336. *
  337. * @since 4.0.0
  338. * @access protected
  339. *
  340. * @param string $verifier Verifier of the session to update.
  341. * @param array $session Optional. Session. Omitting this argument destroys the session.
  342. */
  343. protected function update_session( $verifier, $session = null ) {
  344. $sessions = $this->get_sessions();
  345. if ( $session ) {
  346. $sessions[ $verifier ] = $session;
  347. } else {
  348. unset( $sessions[ $verifier ] );
  349. }
  350. $this->update_sessions( $sessions );
  351. }
  352. /**
  353. * Update a user's sessions in the usermeta table.
  354. *
  355. * @since 4.0.0
  356. * @access protected
  357. *
  358. * @param array $sessions Sessions.
  359. */
  360. protected function update_sessions( $sessions ) {
  361. if ( $sessions ) {
  362. update_user_meta( $this->user_id, 'session_tokens', $sessions );
  363. } else {
  364. delete_user_meta( $this->user_id, 'session_tokens' );
  365. }
  366. }
  367. /**
  368. * Destroy all session tokens for a user, except a single session passed.
  369. *
  370. * @since 4.0.0
  371. * @access protected
  372. *
  373. * @param string $verifier Verifier of the session to keep.
  374. */
  375. protected function destroy_other_sessions( $verifier ) {
  376. $session = $this->get_session( $verifier );
  377. $this->update_sessions( array( $verifier => $session ) );
  378. }
  379. /**
  380. * Destroy all session tokens for a user.
  381. *
  382. * @since 4.0.0
  383. * @access protected
  384. */
  385. protected function destroy_all_sessions() {
  386. $this->update_sessions( array() );
  387. }
  388. /**
  389. * Destroy all session tokens for all users.
  390. *
  391. * @since 4.0.0
  392. * @access public
  393. * @static
  394. */
  395. public static function drop_sessions() {
  396. delete_metadata( 'user', 0, 'session_tokens', false, true );
  397. }
  398. }