| 1: | <?php |
| 2: | |
| 3: | |
| 4: | |
| 5: | |
| 6: | |
| 7: | |
| 8: | |
| 9: | |
| 10: | |
| 11: | |
| 12: | |
| 13: | |
| 14: | |
| 15: | if ( ! defined( 'ABSPATH' ) ) { |
| 16: | exit; |
| 17: | } |
| 18: | |
| 19: | |
| 20: | |
| 21: | |
| 22: | |
| 23: | |
| 24: | class EPL_Session { |
| 25: | |
| 26: | |
| 27: | |
| 28: | |
| 29: | |
| 30: | |
| 31: | |
| 32: | |
| 33: | private $session; |
| 34: | |
| 35: | |
| 36: | |
| 37: | |
| 38: | |
| 39: | |
| 40: | |
| 41: | |
| 42: | |
| 43: | private $use_php_sessions = false; |
| 44: | |
| 45: | |
| 46: | |
| 47: | |
| 48: | |
| 49: | |
| 50: | |
| 51: | |
| 52: | private $prefix = ''; |
| 53: | |
| 54: | |
| 55: | |
| 56: | |
| 57: | |
| 58: | |
| 59: | |
| 60: | |
| 61: | private $started = false; |
| 62: | |
| 63: | |
| 64: | |
| 65: | |
| 66: | |
| 67: | |
| 68: | |
| 69: | |
| 70: | private $bundled_wp_session = false; |
| 71: | |
| 72: | |
| 73: | |
| 74: | |
| 75: | |
| 76: | |
| 77: | |
| 78: | |
| 79: | |
| 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: | |
| 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: | |
| 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: | |
| 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: | |
| 136: | |
| 137: | |
| 138: | |
| 139: | |
| 140: | |
| 141: | |
| 142: | |
| 143: | |
| 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: | |
| 165: | |
| 166: | |
| 167: | |
| 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: | |
| 175: | |
| 176: | |
| 177: | |
| 178: | |
| 179: | public function lazy_session() { |
| 180: | |
| 181: | |
| 182: | |
| 183: | |
| 184: | |
| 185: | |
| 186: | |
| 187: | |
| 188: | |
| 189: | return (bool) apply_filters( 'epl_lazy_session', true ); |
| 190: | } |
| 191: | |
| 192: | |
| 193: | |
| 194: | |
| 195: | |
| 196: | |
| 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: | |
| 209: | |
| 210: | |
| 211: | |
| 212: | |
| 213: | |
| 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: | |
| 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: | |
| 270: | |
| 271: | |
| 272: | |
| 273: | |
| 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: | |
| 285: | |
| 286: | |
| 287: | |
| 288: | |
| 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: | |
| 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: | |
| 330: | |
| 331: | |
| 332: | |
| 333: | |
| 334: | |
| 335: | |
| 336: | |
| 337: | |
| 338: | public function set( $key, $value ) { |
| 339: | |
| 340: | $key = sanitize_key( $key ); |
| 341: | |
| 342: | |
| 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: | |
| 364: | |
| 365: | |
| 366: | |
| 367: | |
| 368: | |
| 369: | public function set_expiration_variant_time( $exp ) { |
| 370: | return ( 30 * 60 * 23 ); |
| 371: | } |
| 372: | |
| 373: | |
| 374: | |
| 375: | |
| 376: | |
| 377: | |
| 378: | |
| 379: | |
| 380: | public function set_expiration_time( $exp ) { |
| 381: | return ( 30 * 60 * 24 ); |
| 382: | } |
| 383: | |
| 384: | |
| 385: | |
| 386: | |
| 387: | |
| 388: | |
| 389: | |
| 390: | |
| 391: | |
| 392: | |
| 393: | |
| 394: | |
| 395: | |
| 396: | public function use_php_sessions() { |
| 397: | |
| 398: | $ret = false; |
| 399: | |
| 400: | |
| 401: | $epl_use_php_sessions = (bool) get_option( 'epl_use_php_sessions' ); |
| 402: | |
| 403: | if ( ! $epl_use_php_sessions ) { |
| 404: | |
| 405: | |
| 406: | if ( function_exists( 'session_start' ) ) { |
| 407: | |
| 408: | $ret = true; |
| 409: | |
| 410: | |
| 411: | update_option( 'epl_use_php_sessions', true ); |
| 412: | } |
| 413: | } else { |
| 414: | $ret = $epl_use_php_sessions; |
| 415: | } |
| 416: | |
| 417: | |
| 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: | |
| 429: | |
| 430: | |
| 431: | |
| 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: | |
| 453: | $start_session = false; |
| 454: | } |
| 455: | } |
| 456: | |
| 457: | return apply_filters( 'epl_start_session', $start_session ); |
| 458: | } |
| 459: | |
| 460: | |
| 461: | |
| 462: | |
| 463: | |
| 464: | |
| 465: | |
| 466: | |
| 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: | |
| 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: | |
| 496: | |
| 497: | |
| 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: | |