PageRenderTime 40ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-includes/session.php

https://gitlab.com/haque.mdmanzurul/wp-harpar-carolyn
PHP | 430 lines | 121 code | 40 blank | 269 comment | 12 complexity | 45f983338e09d37b1acd05ef96cc8fbf 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. $token = wp_generate_password( 43, false, false );
  127. $this->update( $token, $session );
  128. return $token;
  129. }
  130. /**
  131. * Update a session token.
  132. *
  133. * @since 4.0.0
  134. * @access public
  135. *
  136. * @param string $token Session token to update.
  137. * @param array $session Session information.
  138. */
  139. final public function update( $token, $session ) {
  140. $verifier = $this->hash_token( $token );
  141. $this->update_session( $verifier, $session );
  142. }
  143. /**
  144. * Destroy a session token.
  145. *
  146. * @since 4.0.0
  147. * @access public
  148. *
  149. * @param string $token Session token to destroy.
  150. */
  151. final public function destroy( $token ) {
  152. $verifier = $this->hash_token( $token );
  153. $this->update_session( $verifier, null );
  154. }
  155. /**
  156. * Destroy all session tokens for this user,
  157. * except a single token, presumably the one in use.
  158. *
  159. * @since 4.0.0
  160. * @access public
  161. *
  162. * @param string $token_to_keep Session token to keep.
  163. */
  164. final public function destroy_others( $token_to_keep ) {
  165. $verifier = $this->hash_token( $token_to_keep );
  166. $session = $this->get_session( $verifier );
  167. if ( $session ) {
  168. $this->destroy_other_sessions( $verifier );
  169. } else {
  170. $this->destroy_all_sessions();
  171. }
  172. }
  173. /**
  174. * Determine whether a session token is still valid,
  175. * based on expiration.
  176. *
  177. * @since 4.0.0
  178. * @access protected
  179. *
  180. * @param array $session Session to check.
  181. * @return bool Whether session is valid.
  182. */
  183. final protected function is_still_valid( $session ) {
  184. return $session['expiration'] >= time();
  185. }
  186. /**
  187. * Destroy all session tokens for a user.
  188. *
  189. * @since 4.0.0
  190. * @access public
  191. */
  192. final public function destroy_all() {
  193. $this->destroy_all_sessions();
  194. }
  195. /**
  196. * Destroy all session tokens for all users.
  197. *
  198. * @since 4.0.0
  199. * @access public
  200. * @static
  201. */
  202. final public static function destroy_all_for_all_users() {
  203. $manager = apply_filters( 'session_token_manager', 'WP_User_Meta_Session_Tokens' );
  204. call_user_func( array( $manager, 'drop_sessions' ) );
  205. }
  206. /**
  207. * Retrieve all sessions of a user.
  208. *
  209. * @since 4.0.0
  210. * @access public
  211. *
  212. * @return array Sessions of a user.
  213. */
  214. final public function get_all() {
  215. return array_values( $this->get_sessions() );
  216. }
  217. /**
  218. * This method should retrieve all sessions of a user, keyed by verifier.
  219. *
  220. * @since 4.0.0
  221. * @access protected
  222. *
  223. * @return array Sessions of a user, keyed by verifier.
  224. */
  225. abstract protected function get_sessions();
  226. /**
  227. * This method should look up a session by its verifier (token hash).
  228. *
  229. * @since 4.0.0
  230. * @access protected
  231. *
  232. * @param string $verifier Verifier of 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. * This method should update a session by its verifier.
  238. *
  239. * Omitting the second argument should destroy the session.
  240. *
  241. * @since 4.0.0
  242. * @access protected
  243. *
  244. * @param string $verifier Verifier of the session to update.
  245. */
  246. abstract protected function update_session( $verifier, $session = null );
  247. /**
  248. * This method should destroy all session tokens for this user,
  249. * except a single session passed.
  250. *
  251. * @since 4.0.0
  252. * @access protected
  253. *
  254. * @param string $verifier Verifier of the session to keep.
  255. */
  256. abstract protected function destroy_other_sessions( $verifier );
  257. /**
  258. * This method should destroy all sessions for a user.
  259. *
  260. * @since 4.0.0
  261. * @access protected
  262. */
  263. abstract protected function destroy_all_sessions();
  264. /**
  265. * This static method should destroy all session tokens for all users.
  266. *
  267. * @since 4.0.0
  268. * @access public
  269. * @static
  270. */
  271. public static function drop_sessions() {}
  272. }
  273. /**
  274. * Meta-based user sessions token manager.
  275. *
  276. * @since 4.0.0
  277. */
  278. class WP_User_Meta_Session_Tokens extends WP_Session_Tokens {
  279. /**
  280. * Get all sessions of a user.
  281. *
  282. * @since 4.0.0
  283. * @access protected
  284. *
  285. * @return array Sessions of a user.
  286. */
  287. protected function get_sessions() {
  288. $sessions = get_user_meta( $this->user_id, 'session_tokens', true );
  289. if ( ! is_array( $sessions ) ) {
  290. return array();
  291. }
  292. $sessions = array_map( array( $this, 'prepare_session' ), $sessions );
  293. return array_filter( $sessions, array( $this, 'is_still_valid' ) );
  294. }
  295. /**
  296. * Converts an expiration to an array of session information.
  297. *
  298. * @param mixed $session Session or expiration.
  299. * @return array Session.
  300. */
  301. protected function prepare_session( $session ) {
  302. if ( is_int( $session ) ) {
  303. return array( 'expiration' => $session );
  304. }
  305. return $session;
  306. }
  307. /**
  308. * Retrieve a session by its verifier (token hash).
  309. *
  310. * @since 4.0.0
  311. * @access protected
  312. *
  313. * @param string $verifier Verifier of the session to retrieve.
  314. * @return array|null The session, or null if it does not exist
  315. */
  316. protected function get_session( $verifier ) {
  317. $sessions = $this->get_sessions();
  318. if ( isset( $sessions[ $verifier ] ) ) {
  319. return $sessions[ $verifier ];
  320. }
  321. return null;
  322. }
  323. /**
  324. * Update a session by its verifier.
  325. *
  326. * @since 4.0.0
  327. * @access protected
  328. *
  329. * @param string $verifier Verifier of the session to update.
  330. * @param array $session Optional. Session. Omitting this argument destroys the session.
  331. */
  332. protected function update_session( $verifier, $session = null ) {
  333. $sessions = $this->get_sessions();
  334. if ( $session ) {
  335. $sessions[ $verifier ] = $session;
  336. } else {
  337. unset( $sessions[ $verifier ] );
  338. }
  339. $this->update_sessions( $sessions );
  340. }
  341. /**
  342. * Update a user's sessions in the usermeta table.
  343. *
  344. * @since 4.0.0
  345. * @access protected
  346. *
  347. * @param array $sessions Sessions.
  348. */
  349. protected function update_sessions( $sessions ) {
  350. if ( ! has_filter( 'attach_session_information' ) ) {
  351. $sessions = wp_list_pluck( $sessions, 'expiration' );
  352. }
  353. if ( $sessions ) {
  354. update_user_meta( $this->user_id, 'session_tokens', $sessions );
  355. } else {
  356. delete_user_meta( $this->user_id, 'session_tokens' );
  357. }
  358. }
  359. /**
  360. * Destroy all session tokens for a user, except a single session passed.
  361. *
  362. * @since 4.0.0
  363. * @access protected
  364. *
  365. * @param string $verifier Verifier of the session to keep.
  366. */
  367. protected function destroy_other_sessions( $verifier ) {
  368. $session = $this->get_session( $verifier );
  369. $this->update_sessions( array( $verifier => $session ) );
  370. }
  371. /**
  372. * Destroy all session tokens for a user.
  373. *
  374. * @since 4.0.0
  375. * @access protected
  376. */
  377. protected function destroy_all_sessions() {
  378. $this->update_sessions( array() );
  379. }
  380. /**
  381. * Destroy all session tokens for all users.
  382. *
  383. * @since 4.0.0
  384. * @access public
  385. * @static
  386. */
  387. public static function drop_sessions() {
  388. delete_metadata( 'user', false, 'session_tokens', false, true );
  389. }
  390. }