PluginProbe
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) / 1.6.8
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) v1.6.8
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 1.8.0 All 210 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.6.8, at includes/libraries/wp-session/class-wp-session.php

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