1: <?php
2: /**
3: * WordPress session managment.
4: *
5: * Standardizes WordPress session data and uses either database transients or in-memory caching
6: * for storing user session information.
7: *
8: * @package WordPress
9: * @subpackage Session
10: * @since 3.7.0
11: */
12:
13: // Exit if accessed directly.
14: if ( ! defined( 'ABSPATH' ) ) {
15: exit;
16: }
17:
18: if ( ! defined( 'PHP_SESSION_ACTIVE' ) ) {
19: define( 'PHP_SESSION_ACTIVE', 2 );
20: }
21: if ( ! defined( 'PHP_SESSION_NONE' ) ) {
22: define( 'PHP_SESSION_NONE', 1 );
23: }
24:
25: /**
26: * Return the current cache expire setting.
27: *
28: * @return int
29: */
30: function wp_session_cache_expire() {
31: $wp_session = WP_Session::get_instance();
32:
33: return $wp_session->cache_expiration();
34: }
35:
36: /**
37: * Alias of wp_session_write_close()
38: */
39: function wp_session_commit() {
40: wp_session_write_close();
41: }
42:
43: /**
44: * Load a JSON-encoded string into the current session.
45: *
46: * @param string $data Session data.
47: */
48: function wp_session_decode( $data ) {
49: $wp_session = WP_Session::get_instance();
50:
51: return $wp_session->json_in( $data );
52: }
53:
54: /**
55: * Encode the current session's data as a JSON string.
56: *
57: * @return string
58: */
59: function wp_session_encode() {
60: $wp_session = WP_Session::get_instance();
61:
62: return $wp_session->json_out();
63: }
64:
65: /**
66: * Regenerate the session ID.
67: *
68: * @param bool $delete_old_session Delete old session.
69: *
70: * @return bool
71: */
72: function wp_session_regenerate_id( $delete_old_session = false ) {
73: $wp_session = WP_Session::get_instance();
74:
75: $wp_session->regenerate_id( $delete_old_session );
76:
77: return true;
78: }
79:
80: /**
81: * Start new or resume existing session.
82: *
83: * Resumes an existing session based on a value sent by the _wp_session cookie.
84: *
85: * @return bool
86: */
87: function wp_session_start() {
88: $wp_session = WP_Session::get_instance();
89: do_action( 'wp_session_start' );
90:
91: return $wp_session->session_started();
92: }
93: add_action( 'plugins_loaded', 'wp_session_start' );
94:
95: /**
96: * Return the current session status.
97: *
98: * @return int
99: */
100: function wp_session_status() {
101: $wp_session = WP_Session::get_instance();
102:
103: if ( $wp_session->session_started() ) {
104: return defined( 'PHP_SESSION_ACTIVE' ) ? PHP_SESSION_ACTIVE : 2; // phpcs:ignore PHPCompatibility.Constants.NewConstants.php_session_activeFound
105: }
106:
107: return defined( 'PHP_SESSION_NONE' ) ? PHP_SESSION_NONE : 1; // phpcs:ignore PHPCompatibility.Constants.NewConstants.php_session_noneFound
108: }
109:
110: /**
111: * Unset all session variables.
112: */
113: function wp_session_unset() {
114: $wp_session = WP_Session::get_instance();
115:
116: $wp_session->reset();
117: }
118:
119: /**
120: * Write session data and end session
121: */
122: function wp_session_write_close() {
123: $wp_session = WP_Session::get_instance();
124:
125: $wp_session->write_data();
126: do_action( 'wp_session_commit' );
127: }
128: add_action( 'shutdown', 'wp_session_write_close' );
129:
130: /**
131: * Clean up expired sessions by removing data and their expiration entries from
132: * the WordPress options table.
133: *
134: * This method should never be called directly and should instead be triggered as part
135: * of a scheduled task or cron job.
136: */
137: function wp_session_cleanup() {
138: global $wpdb;
139:
140: if ( defined( 'WP_SETUP_CONFIG' ) ) {
141: return;
142: }
143:
144: if ( ! defined( 'WP_INSTALLING' ) ) {
145: $expiration_keys = $wpdb->get_results( "SELECT option_name, option_value FROM $wpdb->options WHERE option_name LIKE '_wp_session_expires_%'" );
146:
147: $now = time();
148: $expired_sessions = array();
149:
150: foreach ( $expiration_keys as $expiration ) {
151:
152: // If the session has expired.
153: if ( $now > intval( $expiration->option_value ) ) {
154:
155: // Get the session ID by parsing the option_name.
156: $session_id = substr( $expiration->option_name, 20 );
157:
158: if ( (int) -1 === (int) $session_id || ! preg_match( '/^[a-f0-9]{32}$/', $session_id ) ) {
159: continue;
160: }
161:
162: $expired_sessions[] = $expiration->option_name;
163: $expired_sessions[] = esc_sql( "_wp_session_$session_id" );
164: }
165: }
166:
167: // Delete all expired sessions in a single query.
168: if ( ! empty( $expired_sessions ) ) {
169: $option_names = implode( "','", $expired_sessions );
170: $wpdb->query( "DELETE FROM $wpdb->options WHERE option_name IN ('$option_names')" ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
171: }
172: }
173:
174: // Allow other plugins to hook in to the garbage collection process.
175: do_action( 'wp_session_cleanup' );
176: }
177: add_action( 'wp_session_garbage_collection', 'wp_session_cleanup' );
178:
179: /**
180: * Register the garbage collector as a twice daily event.
181: */
182: function wp_session_register_garbage_collection() {
183: if ( ! wp_next_scheduled( 'wp_session_garbage_collection' ) ) {
184: wp_schedule_event( time(), 'twicedaily', 'wp_session_garbage_collection' );
185: }
186: }
187: add_action( 'wp', 'wp_session_register_garbage_collection' );
188: