class-recursive-arrayaccess.php
3 years ago
class-wp-session-utils.php
3 years ago
class-wp-session.php
3 years ago
wp-session.php
3 years ago
class-wp-session.php
255 lines
| 1 | <?php |
| 2 | /** |
| 3 | * WordPress session management. |
| 4 | * |
| 5 | * Standardizes WordPress session data using database-backed options for storage. |
| 6 | * for storing user session information. |
| 7 | * |
| 8 | * @package WordPress |
| 9 | * @subpackage Session |
| 10 | */ |
| 11 | |
| 12 | /** |
| 13 | * WordPress Session class for managing user session data. |
| 14 | * |
| 15 | * @package WordPress |
| 16 | */ |
| 17 | final class WP_PPress_Session extends PPRESS_Recursive_ArrayAccess |
| 18 | { |
| 19 | /** |
| 20 | * ID of the current session. |
| 21 | * |
| 22 | * @var string |
| 23 | */ |
| 24 | public $session_id; |
| 25 | |
| 26 | /** |
| 27 | * Unix timestamp when session expires. |
| 28 | * |
| 29 | * @var int |
| 30 | */ |
| 31 | protected $expires; |
| 32 | |
| 33 | /** |
| 34 | * Unix timestamp indicating when the expiration time needs to be reset. |
| 35 | * |
| 36 | * @var int |
| 37 | */ |
| 38 | protected $exp_variant; |
| 39 | |
| 40 | /** |
| 41 | * Singleton instance. |
| 42 | * |
| 43 | * @var bool|WP_PPress_Session |
| 44 | */ |
| 45 | private static $instance = false; |
| 46 | |
| 47 | /** |
| 48 | * Retrieve the current session instance. |
| 49 | * |
| 50 | * @return bool|WP_PPress_Session |
| 51 | */ |
| 52 | public static function get_instance() |
| 53 | { |
| 54 | if ( ! self::$instance) { |
| 55 | /** |
| 56 | * Initialize the session object and wire up any storage. |
| 57 | * |
| 58 | * Some operations (like database migration) need to be performed |
| 59 | * before the session is able to actually be populated with data. |
| 60 | * Ensure these operations are finished by wiring them to the |
| 61 | * session object's initialization hool. |
| 62 | */ |
| 63 | do_action('wp_ppress_session_init'); |
| 64 | self::$instance = new self(); |
| 65 | } |
| 66 | |
| 67 | return self::$instance; |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Default constructor. |
| 72 | * Will rebuild the session collection from the given session ID if it exists. Otherwise, will |
| 73 | * create a new session with that ID. |
| 74 | * |
| 75 | * @uses apply_filters Calls `wp_ppress_session_expiration` to determine how long until sessions expire. |
| 76 | */ |
| 77 | protected function __construct() |
| 78 | { |
| 79 | parent::__construct(); |
| 80 | |
| 81 | if (isset($_COOKIE[WP_PPRESS_SESSION_COOKIE])) { |
| 82 | $cookie = stripslashes($_COOKIE[WP_PPRESS_SESSION_COOKIE]); |
| 83 | $cookie_crumbs = explode('||', $cookie); |
| 84 | |
| 85 | $this->session_id = preg_replace("/[^A-Za-z0-9_]/", '', $cookie_crumbs[0]); |
| 86 | $this->expires = absint($cookie_crumbs[1]); |
| 87 | $this->exp_variant = absint($cookie_crumbs[2]); |
| 88 | |
| 89 | // Update the session expiration if we're past the variant time |
| 90 | if (time() > $this->exp_variant) { |
| 91 | $this->set_expiration(); |
| 92 | |
| 93 | WP_PPress_Session_Utils::update_session($this->session_id, array('session_expiry' => $this->expires)); |
| 94 | } |
| 95 | } else { |
| 96 | $this->session_id = WP_PPress_Session_Utils::generate_id(); |
| 97 | $this->set_expiration(); |
| 98 | } |
| 99 | |
| 100 | $this->read_data(); |
| 101 | |
| 102 | $this->set_cookie(); |
| 103 | |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Set both the expiration time and the expiration variant. |
| 108 | * |
| 109 | * If the current time is below the variant, we don't update the session's expiration time. If it's |
| 110 | * greater than the variant, then we update the expiration time in the database. This prevents |
| 111 | * writing to the database on every page load for active sessions and only updates the expiration |
| 112 | * time if we're nearing when the session actually expires. |
| 113 | * |
| 114 | * By default, the expiration time is set to 30 minutes. |
| 115 | * By default, the expiration variant is set to 24 minutes. |
| 116 | * |
| 117 | * As a result, the session expiration time - at a maximum - will only be written to the database once |
| 118 | * every 24 minutes. After 30 minutes, the session will have been expired. No cookie will be sent by |
| 119 | * the browser, and the old session will be queued for deletion by the garbage collector. |
| 120 | * |
| 121 | * @uses apply_filters Calls `wp_ppress_session_expiration_variant` to get the max update window for session data. |
| 122 | * @uses apply_filters Calls `wp_ppress_session_expiration` to get the standard expiration time for sessions. |
| 123 | */ |
| 124 | protected function set_expiration() |
| 125 | { |
| 126 | $this->exp_variant = time() + (int)apply_filters('wp_ppress_session_expiration_variant', 24 * 60); |
| 127 | $this->expires = time() + (int)apply_filters('wp_ppress_session_expiration', 30 * 60); |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Set the session cookie |
| 132 | * @uses apply_filters Calls `wp_ppress_session_cookie_secure` to set the $secure parameter of setcookie() |
| 133 | * @uses apply_filters Calls `wp_ppress_session_cookie_httponly` to set the $httponly parameter of setcookie() |
| 134 | */ |
| 135 | protected function set_cookie() |
| 136 | { |
| 137 | $secure = apply_filters('wp_ppress_session_cookie_secure', false); |
| 138 | $httponly = apply_filters('wp_ppress_session_cookie_httponly', false); |
| 139 | setcookie(WP_PPRESS_SESSION_COOKIE, $this->session_id . '||' . $this->expires . '||' . $this->exp_variant, $this->expires, COOKIEPATH, COOKIE_DOMAIN, $secure, $httponly); |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Read data from a transient for the current session. |
| 144 | * |
| 145 | * Automatically resets the expiration time for the session transient to some time in the future. |
| 146 | * |
| 147 | * @return array |
| 148 | */ |
| 149 | protected function read_data() |
| 150 | { |
| 151 | $this->container = WP_PPress_Session_Utils::get_session($this->session_id, array()); |
| 152 | |
| 153 | return $this->container; |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Write the data from the current session to the data storage system. |
| 158 | */ |
| 159 | public function write_data() |
| 160 | { |
| 161 | // Nothing has changed, don't update the session |
| 162 | if ( ! $this->dirty) { |
| 163 | return; |
| 164 | } |
| 165 | |
| 166 | // Session is dirty, but also empty. Purge it! |
| 167 | if (empty($this->container)) { |
| 168 | WP_PPress_Session_Utils::delete_session($this->session_id); |
| 169 | |
| 170 | return; |
| 171 | } |
| 172 | |
| 173 | // Session is dirty and needs to be updated, do so! |
| 174 | if (false === WP_PPress_Session_Utils::session_exists($this->session_id)) { |
| 175 | WP_PPress_Session_Utils::add_session(array('session_key' => $this->session_id, 'session_value' => serialize($this->container), 'session_expiry' => $this->expires)); |
| 176 | } else { |
| 177 | WP_PPress_Session_Utils::update_session($this->session_id, array('session_value' => serialize($this->container))); |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * Output the current container contents as a JSON-encoded string. |
| 183 | * |
| 184 | * @return string |
| 185 | */ |
| 186 | public function json_out() |
| 187 | { |
| 188 | return json_encode($this->container); |
| 189 | } |
| 190 | |
| 191 | /** |
| 192 | * Decodes a JSON string and, if the object is an array, overwrites the session container with its contents. |
| 193 | * |
| 194 | * @param string $data |
| 195 | * |
| 196 | * @return bool |
| 197 | */ |
| 198 | public function json_in($data) |
| 199 | { |
| 200 | $array = json_decode($data, true); |
| 201 | |
| 202 | if (is_array($array)) { |
| 203 | $this->container = $array; |
| 204 | |
| 205 | return true; |
| 206 | } |
| 207 | |
| 208 | return false; |
| 209 | } |
| 210 | |
| 211 | /** |
| 212 | * Regenerate the current session's ID. |
| 213 | * |
| 214 | * @param bool $delete_old Flag whether or not to delete the old session data from the server. |
| 215 | */ |
| 216 | public function regenerate_id($delete_old = false) |
| 217 | { |
| 218 | if ($delete_old) { |
| 219 | WP_PPress_Session_Utils::delete_session($this->session_id); |
| 220 | } |
| 221 | |
| 222 | $this->session_id = WP_PPress_Session_Utils::generate_id(); |
| 223 | |
| 224 | $this->set_cookie(); |
| 225 | } |
| 226 | |
| 227 | /** |
| 228 | * Check if a session has been initialized. |
| 229 | * |
| 230 | * @return bool |
| 231 | */ |
| 232 | public function session_started() |
| 233 | { |
| 234 | return ! ! self::$instance; |
| 235 | } |
| 236 | |
| 237 | /** |
| 238 | * Return the read-only cache expiration value. |
| 239 | * |
| 240 | * @return int |
| 241 | */ |
| 242 | public function cache_expiration() |
| 243 | { |
| 244 | return $this->expires; |
| 245 | } |
| 246 | |
| 247 | /** |
| 248 | * Flushes all session variables. |
| 249 | */ |
| 250 | public function reset() |
| 251 | { |
| 252 | $this->container = array(); |
| 253 | } |
| 254 | } |
| 255 |