1: <?php
2: /**
3: * EPL Session
4: *
5: * This is a wrapper class for WP_Session / PHP $_SESSION
6: *
7: * @package EPL
8: * @subpackage Classes/Session
9: * @copyright Copyright (c) 2020, Merv Barrett
10: * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
11: * @since 3.4.15
12: */
13:
14: // Exit if accessed directly.
15: if ( ! defined( 'ABSPATH' ) ) {
16: exit;
17: }
18:
19: /**
20: * EPL_Session Class
21: *
22: * @since 3.4.15
23: */
24: class EPL_Session {
25:
26: /**
27: * Holds our session data
28: *
29: * @var array
30: * @access private
31: * @since 3.4.15
32: */
33: private $session;
34:
35:
36: /**
37: * Whether to use PHP $_SESSION or WP_Session
38: *
39: * @var bool
40: * @access private
41: * @since 3.4.15
42: */
43: private $use_php_sessions = false;
44:
45: /**
46: * Session index prefix
47: *
48: * @var string
49: * @access private
50: * @since 3.4.15
51: */
52: private $prefix = '';
53:
54: /**
55: * Whether the session has been started for this request
56: *
57: * @var bool
58: * @access private
59: * @since 3.6
60: */
61: private $started = false;
62:
63: /**
64: * Whether EPL loaded the bundled WP_Session library
65: *
66: * @var bool
67: * @access private
68: * @since 3.6
69: */
70: private $bundled_wp_session = false;
71:
72: /**
73: * Get things started
74: *
75: * Defines our WP_Session constants, includes the necessary libraries and
76: * retrieves the WP Session instance
77: *
78: * @since 3.4.15
79: * @since 3.6 The bundled WP_Session library no longer starts a session on every request.
80: */
81: public function __construct() {
82:
83: $this->use_php_sessions = $this->use_php_sessions();
84:
85: if ( $this->use_php_sessions ) {
86:
87: if ( is_multisite() ) {
88:
89: $this->prefix = '_' . get_current_blog_id();
90:
91: }
92:
93: // Use PHP SESSION (must be enabled via the EPL_USE_PHP_SESSIONS constant).
94: add_action( 'init', array( $this, 'maybe_start_session' ), -2 );
95:
96: } else {
97:
98: if ( ! $this->should_start_session() ) {
99: return;
100: }
101:
102: // Use WP_Session (default).
103: if ( ! defined( 'WP_SESSION_COOKIE' ) ) {
104: define( 'WP_SESSION_COOKIE', 'epl_wp_session' );
105: }
106:
107: if ( ! class_exists( 'Recursive_ArrayAccess' ) ) {
108:
109: require_once EPL_PATH_LIB . 'includes/libraries/class-recursive-arrayaccess.php';
110: }
111:
112: if ( ! class_exists( 'WP_Session' ) ) {
113: require_once EPL_PATH_LIB . 'includes/libraries/class-wp-session.php';
114: require_once EPL_PATH_LIB . 'includes/libraries/wp-session.php';
115:
116: // The library starts a session, and sends its cookie, on every request. EPL starts it only when needed.
117: remove_action( 'plugins_loaded', 'wp_session_start' );
118: remove_action( 'shutdown', 'wp_session_write_close' );
119: $this->bundled_wp_session = true;
120: }
121:
122: add_filter( 'wp_session_expiration_variant', array( $this, 'set_expiration_variant_time' ), 99999 );
123: add_filter( 'wp_session_expiration', array( $this, 'set_expiration_time' ), 99999 );
124:
125: }
126:
127: if ( empty( $this->session ) && ! $this->use_php_sessions ) {
128: add_action( 'plugins_loaded', array( $this, 'init' ), -1 );
129: } else {
130: add_action( 'init', array( $this, 'init' ), -1 );
131: }
132: }
133:
134: /**
135: * Setup the WP_Session instance
136: *
137: * Loads the visitor's existing session. A new session is only started when something
138: * is stored in it (see set()), so pages for visitors without a session send no session
139: * cookie and can be cached.
140: *
141: * @since 3.4.15
142: * @since 3.6 Starts a session only when the visitor already has one, or when lazy sessions are off.
143: * @return array|WP_Session
144: */
145: public function init() {
146:
147: if ( $this->use_php_sessions ) {
148: if ( session_id() ) {
149: $this->started = true;
150: $this->session = $this->get_php_session_data();
151: } else {
152: $this->session = array();
153: }
154: } elseif ( ! $this->lazy_session() || $this->has_session_cookie() ) {
155: $this->start_session();
156: } else {
157: $this->session = array();
158: }
159:
160: return $this->session;
161: }
162:
163: /**
164: * EPL's data in the PHP session
165: *
166: * @since 3.6
167: * @return array
168: */
169: private function get_php_session_data() {
170: return isset( $_SESSION[ 'epl' . $this->prefix ] ) && is_array( $_SESSION[ 'epl' . $this->prefix ] ) ? $_SESSION[ 'epl' . $this->prefix ] : array();
171: }
172:
173: /**
174: * Start the session only when something is stored in it?
175: *
176: * @since 3.6
177: * @return bool
178: */
179: public function lazy_session() {
180: /**
181: * Filters whether EPL starts the session only when something is stored in it.
182: *
183: * Return false to start a session, and send its cookie, on every request as before 3.6.
184: *
185: * @since 3.6
186: *
187: * @param bool $lazy Default true.
188: */
189: return (bool) apply_filters( 'epl_lazy_session', true );
190: }
191:
192: /**
193: * Does the request carry a session cookie?
194: *
195: * @since 3.6
196: * @return bool
197: */
198: public function has_session_cookie() {
199: if ( $this->use_php_sessions ) {
200: $name = session_name();
201: } else {
202: $name = defined( 'WP_SESSION_COOKIE' ) ? WP_SESSION_COOKIE : 'epl_wp_session';
203: }
204: return ! empty( $_COOKIE[ $name ] );
205: }
206:
207: /**
208: * Start the session and send its cookie
209: *
210: * Values stored before the session started are kept.
211: *
212: * @since 3.6
213: * @return bool True when the session is started.
214: */
215: public function start_session() {
216:
217: if ( $this->started ) {
218: return true;
219: }
220:
221: if ( ! $this->should_start_session() ) {
222: return false;
223: }
224:
225: $pending = is_array( $this->session ) ? $this->session : array();
226:
227: if ( $this->use_php_sessions ) {
228:
229: if ( ! session_id() ) {
230: if ( headers_sent() ) {
231: return false;
232: }
233: session_start();
234: }
235:
236: $this->session = array_merge( $this->get_php_session_data(), $pending );
237:
238: $_SESSION[ 'epl' . $this->prefix ] = $this->session;
239:
240: } else {
241:
242: // The cookie can't be sent once output has started.
243: if ( ! class_exists( 'WP_Session' ) || headers_sent() ) {
244: return false;
245: }
246:
247: if ( $this->bundled_wp_session ) {
248: wp_session_start();
249: }
250:
251: $this->session = WP_Session::get_instance();
252:
253: foreach ( $pending as $key => $value ) {
254: $this->session[ $key ] = $value;
255: }
256:
257: if ( $this->bundled_wp_session && ! has_action( 'shutdown', 'wp_session_write_close' ) ) {
258: add_action( 'shutdown', 'wp_session_write_close' );
259: }
260: }
261:
262: $this->started = true;
263:
264: return true;
265: }
266:
267:
268: /**
269: * Retrieve session ID
270: *
271: * @since 1.6
272: * @since 3.6 Returns an empty string when no session is started.
273: * @return string Session ID
274: */
275: public function get_id() {
276: if ( $this->use_php_sessions ) {
277: return session_id();
278: }
279: return is_object( $this->session ) && isset( $this->session->session_id ) ? $this->session->session_id : '';
280: }
281:
282:
283: /**
284: * Retrieve a session variable
285: *
286: * @since 3.4.15
287: * @param string $key Session key.
288: * @return mixed Session variable
289: */
290: public function get( $key ) {
291:
292: $key = sanitize_key( $key );
293: $return = false;
294:
295: if ( isset( $this->session[ $key ] ) && ! empty( $this->session[ $key ] ) ) {
296:
297: preg_match( '/[oO]\s*:\s*\d+\s*:\s*"\s*(?!(?i)(stdClass))/', $this->session[ $key ], $matches );
298: if ( ! empty( $matches ) ) {
299: $this->set( $key, null );
300: return false;
301: }
302:
303: if ( is_numeric( $this->session[ $key ] ) ) {
304: $return = $this->session[ $key ];
305: } else {
306:
307: $maybe_json = json_decode( $this->session[ $key ] );
308:
309: // Since json_last_error is PHP 5.3+, we have to rely on a `null` value for failing to parse JSON.
310: if ( is_null( $maybe_json ) ) {
311: $is_serialized = is_serialized( $this->session[ $key ] );
312: if ( $is_serialized ) {
313: $value = @unserialize( $this->session[ $key ] );
314: $this->set( $key, (array) $value );
315: $return = $value;
316: } else {
317: $return = $this->session[ $key ];
318: }
319: } else {
320: $return = json_decode( $this->session[ $key ], true );
321: }
322: }
323: }
324:
325: return $return;
326: }
327:
328: /**
329: * Set a session variable
330: *
331: * @since 3.4.15
332: * @since 3.6 Starts the session, and sends its cookie, the first time a value is stored.
333: *
334: * @param string $key Session key.
335: * @param int|string|array $value Session variable.
336: * @return mixed Session variable
337: */
338: public function set( $key, $value ) {
339:
340: $key = sanitize_key( $key );
341:
342: // Clearing a value that was never stored needs no session; storing one starts it.
343: $clearing = null === $value || '' === $value || array() === $value;
344: if ( ! $this->started && ! ( $clearing && empty( $this->session[ $key ] ) ) ) {
345: $this->start_session();
346: }
347:
348: if ( is_array( $value ) ) {
349: $this->session[ $key ] = wp_json_encode( $value );
350: } else {
351: $this->session[ $key ] = esc_attr( $value );
352: }
353:
354: if ( $this->use_php_sessions && $this->started ) {
355:
356: $_SESSION[ 'epl' . $this->prefix ] = $this->session;
357: }
358:
359: return $this->session[ $key ];
360: }
361:
362: /**
363: * Force the cookie expiration variant time to 23 hours
364: *
365: * @since 3.4.15
366: * @param int $exp Default expiration (1 hour).
367: * @return int
368: */
369: public function set_expiration_variant_time( $exp ) {
370: return ( 30 * 60 * 23 );
371: }
372:
373: /**
374: * Force the cookie expiration time to 24 hours
375: *
376: * @since 3.4.15
377: * @param int $exp Default expiration (1 hour).
378: * @return int Cookie expiration time
379: */
380: public function set_expiration_time( $exp ) {
381: return ( 30 * 60 * 24 );
382: }
383:
384: /**
385: * Starts a new session if one hasn't started yet.
386: *
387: * Checks to see if the server supports PHP sessions
388: * or if the EPL_USE_PHP_SESSIONS constant is defined
389: *
390: * @since 3.4.15
391: * @since 3.6 Detecting PHP session support no longer writes to the session, so it no longer starts one on every request.
392: * @author Daniel J Griffiths
393: * @author Daniel J Griffiths
394: * @return boolean $ret True if we are using PHP sessions, false otherwise.
395: */
396: public function use_php_sessions() {
397:
398: $ret = false;
399:
400: // If the database variable is already set, no need to run autodetection.
401: $epl_use_php_sessions = (bool) get_option( 'epl_use_php_sessions' );
402:
403: if ( ! $epl_use_php_sessions ) {
404:
405: // Attempt to detect if the server supports PHP sessions. Doesn't store anything, so no session is started.
406: if ( function_exists( 'session_start' ) ) {
407:
408: $ret = true;
409:
410: // Set the database option.
411: update_option( 'epl_use_php_sessions', true );
412: }
413: } else {
414: $ret = $epl_use_php_sessions;
415: }
416:
417: // Enable or disable PHP Sessions based on the EPL_USE_PHP_SESSIONS constant.
418: if ( defined( 'EPL_USE_PHP_SESSIONS' ) && EPL_USE_PHP_SESSIONS ) {
419: $ret = true;
420: } elseif ( defined( 'EPL_USE_PHP_SESSIONS' ) && ! EPL_USE_PHP_SESSIONS ) {
421: $ret = false;
422: }
423:
424: return (bool) apply_filters( 'epl_use_php_sessions', $ret );
425: }
426:
427: /**
428: * Determines if we should start sessions
429: *
430: * @since 2.5.11
431: * @return bool
432: */
433: public function should_start_session() {
434:
435: $start_session = true;
436:
437: if ( ! empty( $_SERVER['REQUEST_URI'] ) ) {
438:
439: $blacklist = $this->get_blacklist();
440: $uri = ltrim( $_SERVER['REQUEST_URI'], '/' );
441: $uri = untrailingslashit( $uri );
442:
443: if ( in_array( $uri, $blacklist, true ) ) {
444: $start_session = false;
445: }
446:
447: if ( false !== strpos( $uri, 'feed=' ) ) {
448: $start_session = false;
449: }
450:
451: if ( false !== strpos( $uri, 'wp_scrape_key' ) ) {
452: // Starting sessions while saving the file editor can break the save process, so don't start.
453: $start_session = false;
454: }
455: }
456:
457: return apply_filters( 'epl_start_session', $start_session );
458: }
459:
460: /**
461: * Retrieve the URI blacklist
462: *
463: * These are the URIs where we never start sessions
464: *
465: * @since 2.5.11
466: * @return array
467: */
468: public function get_blacklist() {
469:
470: $blacklist = apply_filters(
471: 'epl_session_start_uri_blacklist',
472: array(
473: 'feed',
474: 'feed/rss',
475: 'feed/rss2',
476: 'feed/rdf',
477: 'feed/atom',
478: 'comments/feed',
479: )
480: );
481:
482: // Look to see if WordPress is in a sub folder or this is a network site that uses sub folders.
483: $folder = str_replace( network_home_url(), '', get_site_url() );
484:
485: if ( ! empty( $folder ) ) {
486: foreach ( $blacklist as $path ) {
487: $blacklist[] = $folder . '/' . $path;
488: }
489: }
490:
491: return $blacklist;
492: }
493:
494: /**
495: * Starts a new session if one hasn't started yet.
496: *
497: * @since 3.6 Only resumes an existing session; a new one starts when something is stored.
498: */
499: public function maybe_start_session() {
500:
501: if ( ! $this->should_start_session() ) {
502: return;
503: }
504:
505: if ( $this->lazy_session() && ! $this->has_session_cookie() ) {
506: return;
507: }
508:
509: if ( ! session_id() && ! headers_sent() ) {
510: session_start();
511: }
512: }
513: }
514: