PluginProbe
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) / 1.7.0.3
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) v1.7.0.3
1.8.13 1.8.12.3 1.8.12.2 1.8.12.1 1.8.12 1.8.11.3 1.8.11.2 1.8.11.1 1.8.11 1.6.6 1.6.60 1.6.7 1.6.8 1.6.9 1.7.0 1.7.0.1 1.7.0.11 1.7.0.12 1.7.0.14 1.7.0.2 1.7.0.3 1.7.0.5 1.7.0.6 1.7.0.7 1.7.0.9 All 211 releases
charitable / includes / libraries / wp-session / class-wp-session.php

class-wp-session.php in Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) 1.7.0.3, at includes/libraries/wp-session/class-wp-session.php

241 lines 6.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * WordPress session managment.
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 * @since 3.7.0
11 */
12
13 // Exit if accessed directly.
14 if ( ! defined( 'ABSPATH' ) ) {
15 exit;
16 }
17
18 /**
19 * WordPress Session class for managing user session data.
20 *
21 * @package WordPress
22 * @since 3.7.0
23 */
24 final class WP_Session extends Recursive_ArrayAccess {
25 /**
26 * ID of the current session.
27 *
28 * @var string
29 */
30 public $session_id;
31
32 /**
33 * Unix timestamp when session expires.
34 *
35 * @var int
36 */
37 protected $expires;
38
39 /**
40 * Unix timestamp indicating when the expiration time needs to be reset.
41 *
42 * @var int
43 */
44 protected $exp_variant;
45
46 /**
47 * Singleton instance.
48 *
49 * @var WP_Session
50 */
51 private static $instance;
52
53 /**
54 * Retrieve the current session instance.
55 *
56 * @return WP_Session
57 */
58 public static function get_instance() {
59 if ( ! isset( self::$instance ) ) {
60 self::$instance = new self();
61 }
62
63 return self::$instance;
64 }
65
66 /**
67 * Default constructor.
68 * Will rebuild the session collection from the given session ID if it exists. Otherwise, will
69 * create a new session with that ID.
70 *
71 * @param $session_id
72 */
73 protected function __construct() {
74 if ( isset( $_COOKIE[WP_SESSION_COOKIE] ) ) {
75 $cookie = stripslashes( $_COOKIE[WP_SESSION_COOKIE] );
76 $cookie_crumbs = explode( '||', $cookie );
77
78 $this->session_id = preg_replace("/[^A-Za-z0-9_]/", '', $cookie_crumbs[0] );
79 $this->expires = absint( $cookie_crumbs[1] );
80 $this->exp_variant = absint( $cookie_crumbs[2] );
81
82 // Update the session expiration if we're past the variant time
83 if ( time() > $this->exp_variant ) {
84 $this->set_expiration();
85 delete_option( "_wp_session_expires_{$this->session_id}" );
86 add_option( "_wp_session_expires_{$this->session_id}", $this->expires, '', 'no' );
87 }
88
89 $this->read_data();
90 } else {
91 $this->session_id = '';
92 $this->container = array();
93 }
94 }
95
96 /**
97 * Set both the expiration time and the expiration variant.
98 *
99 * If the current time is below the variant, we don't update the session's expiration time. If it's
100 * greater than the variant, then we update the expiration time in the database. This prevents
101 * writing to the database on every page load for active sessions and only updates the expiration
102 * time if we're nearing when the session actually expires.
103 *
104 * By default, the expiration time is set to 30 minutes.
105 * By default, the expiration variant is set to 24 minutes.
106 *
107 * As a result, the session expiration time - at a maximum - will only be written to the database once
108 * every 24 minutes. After 30 minutes, the session will have been expired. No cookie will be sent by
109 * the browser, and the old session will be queued for deletion by the garbage collector.
110 *
111 * @uses apply_filters Calls `wp_session_expiration_variant` to get the max update window for session data.
112 * @uses apply_filters Calls `wp_session_expiration` to get the standard expiration time for sessions.
113 */
114 protected function set_expiration() {
115 $this->exp_variant = time() + (int) apply_filters( 'wp_session_expiration_variant', 24 * 60 );
116 $this->expires = time() + (int) apply_filters( 'wp_session_expiration', 30 * 60 );
117 }
118
119 /**
120 * Set the session cookie
121 *
122 * @uses apply_filters Calls `wp_session_cookie_secure` to set the $secure parameter of setcookie()
123 * @uses apply_filters Calls `wp_session_cookie_httponly` to set the $httponly parameter of setcookie()
124 */
125 protected function set_cookie() {
126 $secure = apply_filters( 'wp_session_cookie_secure', false );
127 $httponly = apply_filters( 'wp_session_cookie_httponly', false );
128 setcookie( WP_SESSION_COOKIE, $this->session_id . '||' . $this->expires . '||' . $this->exp_variant,
129 $this->expires,
130 COOKIEPATH,
131 COOKIE_DOMAIN,
132 $secure,
133 $httponly
134 );
135 }
136
137 /**
138 * Read data from a transient for the current session.
139 *
140 * Automatically resets the expiration time for the session transient to some time in the future.
141 *
142 * @return array
143 */
144 protected function read_data() {
145 $this->container = get_option( "_wp_session_{$this->session_id}", array() );
146
147 return $this->container;
148 }
149
150 /**
151 * Write the data from the current session to the data storage system.
152 */
153 public function write_data() {
154 /* Nothing has changed. */
155 if ( ! $this->dirty ) {
156 return;
157 }
158
159 if ( empty( $this->container ) ) {
160 delete_option( "_wp_session_{$this->session_id}" );
161 delete_option( "_wp_session_expires_{$this->session_id}" );
162 return;
163 }
164
165 if ( false === get_option( "_wp_session_{$this->session_id}" ) ) {
166 add_option( "_wp_session_{$this->session_id}", $this->container, '', 'no' );
167 add_option( "_wp_session_expires_{$this->session_id}", $this->expires, '', 'no' );
168 } else {
169 delete_option( "_wp_session_{$this->session_id}" );
170 add_option( "_wp_session_{$this->session_id}", $this->container, '', 'no' );
171 }
172 }
173
174 /**
175 * Output the current container contents as a JSON-encoded string.
176 *
177 * @return string
178 */
179 public function json_out() {
180 return json_encode( $this->container );
181 }
182
183 /**
184 * Decodes a JSON string and, if the object is an array, overwrites the session container with its contents.
185 *
186 * @param string $data
187 *
188 * @return bool
189 */
190 public function json_in( $data ) {
191 $array = json_decode( $data );
192
193 if ( is_array( $array ) ) {
194 $this->container = $array;
195 return true;
196 }
197
198 return false;
199 }
200
201 /**
202 * Regenerate the current session's ID.
203 *
204 * @param bool $delete_old Flag whether or not to delete the old session data from the server.
205 */
206 public function regenerate_id( $delete_old = false ) {
207 if ( $delete_old ) {
208 delete_option( "_wp_session_{$this->session_id}" );
209 }
210
211 $this->session_id = WP_Session_Utils::generate_id();
212
213 $this->set_cookie();
214 }
215
216 /**
217 * Check if a session has been initialized.
218 *
219 * @return bool
220 */
221 public function session_started() {
222 return !!self::$instance;
223 }
224
225 /**
226 * Return the read-only cache expiration value.
227 *
228 * @return int
229 */
230 public function cache_expiration() {
231 return $this->expires;
232 }
233
234 /**
235 * Flushes all session variables.
236 */
237 public function reset() {
238 $this->container = array();
239 }
240 }
241