PluginProbe ʕ •ᴥ•ʔ
Paid Membership Plugin, Ecommerce, User Registration Form, Login Form, User Profile & Restrict Content – ProfilePress / 3.2.7
Paid Membership Plugin, Ecommerce, User Registration Form, Login Form, User Profile & Restrict Content – ProfilePress v3.2.7
4.17.2 4.17.1 4.17.0 4.16.19 4.16.18 4.16.17 4.16.16 trunk 1.0 1.0.1 1.0.2 1.1 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.5a 1.1.6 1.1.7 1.2 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.3 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 1.4 1.4.1 1.4.2 1.5 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.6 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 1.6.6 1.6.7 1.6.8 1.7 1.7.1 1.7.2 1.8 1.8.1 1.8.10 1.8.2 1.8.3 1.8.4 1.8.5 1.8.6 1.8.7 1.8.8 1.8.9 1.9 1.9.1 1.9.10 1.9.11 1.9.12 1.9.13 1.9.2 1.9.3 1.9.4 1.9.5 1.9.6 1.9.7 1.9.8 1.9.9 2.1.9 2.2.10 2.2.11 2.2.12 2.2.13 2.2.14 2.2.15 2.2.16 2.2.2 2.2.5 2.2.6 2.2.7 2.2.8 2.2.9 3.0 3.1 3.1.1 3.1.10 3.1.11 3.1.12 3.1.13 3.1.14 3.1.15 3.1.16 3.1.17 3.1.18 3.1.19 3.1.2 3.1.3 3.1.4 3.1.5 3.1.6 3.1.7 3.1.8 3.1.9 3.2.0 3.2.1 3.2.10 3.2.11 3.2.12 3.2.13 3.2.14 3.2.15 3.2.16 3.2.2 3.2.3 3.2.4 3.2.5 3.2.6 3.2.7 3.2.8 3.2.9 4.0.0 4.0.1 4.0.2 4.0.3 4.1.0 4.1.1 4.1.2 4.1.3 4.1.4 4.10.0 4.10.1 4.10.2 4.10.3 4.11.0 4.12.0 4.13.0 4.13.1 4.13.2 4.13.3 4.13.4 4.14.0 4.14.1 4.14.2 4.14.3 4.14.4 4.15.0 4.15.1 4.15.10 4.15.11 4.15.12 4.15.13 4.15.14 4.15.15 4.15.16 4.15.17 4.15.18 4.15.19 4.15.2 4.15.20 4.15.20.1 4.15.21 4.15.22 4.15.23 4.15.24 4.15.25 4.15.3 4.15.4 4.15.5 4.15.6 4.15.7 4.15.8 4.15.9 4.16.0 4.16.1 4.16.10 4.16.11 4.16.12 4.16.13 4.16.14 4.16.15 4.16.2 4.16.3 4.16.4 4.16.5 4.16.6 4.16.7 4.16.8 4.16.9 4.2.0 4.3.0 4.3.1 4.3.2 4.4.0 4.4.1 4.5.0 4.5.1 4.5.2 4.5.3 4.5.4 4.5.5 4.6.0 4.7.0 4.8.0 4.9.0
wp-user-avatar / src / lib / wp_session / class-wp-session.php
wp-user-avatar / src / lib / wp_session Last commit date
class-recursive-arrayaccess.php 5 years ago class-wp-session.php 5 years ago wp-session.php 5 years ago
class-wp-session.php
332 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 * @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 implements Iterator, Countable {
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 bool|WP_Session
48 */
49 private static $instance = false;
50
51 /**
52 * Retrieve the current session instance.
53 *
54 * @param bool $session_id Session ID from which to populate data.
55 *
56 * @return bool|WP_Session
57 */
58 public static function get_instance() {
59 if ( ! 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 * @uses apply_filters Calls `wp_session_expiration` to determine how long until sessions expire.
73 */
74 protected function __construct() {
75 if ( isset( $_COOKIE[WP_SESSION_COOKIE] ) ) {
76 $cookie = stripslashes( $_COOKIE[WP_SESSION_COOKIE] );
77 $cookie_crumbs = explode( '||', $cookie );
78
79 if( $this->is_valid_md5( $cookie_crumbs[0] ) ) {
80
81 $this->session_id = $cookie_crumbs[0];
82
83 } else {
84
85 $this->regenerate_id( true );
86
87 }
88
89 $this->expires = $cookie_crumbs[1];
90 $this->exp_variant = $cookie_crumbs[2];
91
92 // Update the session expiration if we're past the variant time
93 if ( time() > $this->exp_variant ) {
94 $this->set_expiration();
95 delete_option( "_wp_session_expires_{$this->session_id}" );
96 add_option( "_wp_session_expires_{$this->session_id}", $this->expires, '', 'no' );
97 }
98 } else {
99 $this->session_id = $this->generate_id();
100 $this->set_expiration();
101 }
102
103 $this->read_data();
104
105 $this->set_cookie();
106 }
107
108 /**
109 * Set both the expiration time and the expiration variant.
110 *
111 * If the current time is below the variant, we don't update the session's expiration time. If it's
112 * greater than the variant, then we update the expiration time in the database. This prevents
113 * writing to the database on every page load for active sessions and only updates the expiration
114 * time if we're nearing when the session actually expires.
115 *
116 * By default, the expiration time is set to 30 minutes.
117 * By default, the expiration variant is set to 24 minutes.
118 *
119 * As a result, the session expiration time - at a maximum - will only be written to the database once
120 * every 24 minutes. After 30 minutes, the session will have been expired. No cookie will be sent by
121 * the browser, and the old session will be queued for deletion by the garbage collector.
122 *
123 * @uses apply_filters Calls `wp_session_expiration_variant` to get the max update window for session data.
124 * @uses apply_filters Calls `wp_session_expiration` to get the standard expiration time for sessions.
125 */
126 protected function set_expiration() {
127 $this->exp_variant = time() + (int) apply_filters( 'wp_session_expiration_variant', 24 * 60 );
128 $this->expires = time() + (int) apply_filters( 'wp_session_expiration', 30 * 60 );
129 }
130
131 /**
132 * Set the session cookie
133 */
134 protected function set_cookie() {
135 @setcookie( WP_SESSION_COOKIE, $this->session_id . '||' . $this->expires . '||' . $this->exp_variant , $this->expires, COOKIEPATH, COOKIE_DOMAIN );
136 }
137
138 /**
139 * Generate a cryptographically strong unique ID for the session token.
140 *
141 * @return string
142 */
143 protected function generate_id() {
144 require_once( ABSPATH . 'wp-includes/class-phpass.php');
145 $hasher = new PasswordHash( 8, false );
146
147 return md5( $hasher->get_random_bytes( 32 ) );
148 }
149
150 /**
151 * Checks if is valid md5 string
152 *
153 * @param string $md5
154 * @return int
155 */
156 protected function is_valid_md5( $md5 = '' ){
157 return preg_match( '/^[a-f0-9]{32}$/', $md5 );
158 }
159
160 /**
161 * Read data from a transient for the current session.
162 *
163 * Automatically resets the expiration time for the session transient to some time in the future.
164 *
165 * @return array
166 */
167 protected function read_data() {
168 $this->container = get_option( "_wp_session_{$this->session_id}", array() );
169
170 return $this->container;
171 }
172
173 /**
174 * Write the data from the current session to the data storage system.
175 */
176 public function write_data() {
177 $option_key = "_wp_session_{$this->session_id}";
178
179 // Only write the collection to the DB if it's changed.
180 if ( $this->dirty ) {
181 if ( false === get_option( $option_key ) ) {
182 add_option( "_wp_session_{$this->session_id}", $this->container, '', 'no' );
183 add_option( "_wp_session_expires_{$this->session_id}", $this->expires, '', 'no' );
184 } else {
185 delete_option( "_wp_session_{$this->session_id}" );
186 add_option( "_wp_session_{$this->session_id}", $this->container, '', 'no' );
187 }
188 }
189 }
190
191 /**
192 * Output the current container contents as a JSON-encoded string.
193 *
194 * @return string
195 */
196 public function json_out() {
197 return json_encode( $this->container );
198 }
199
200 /**
201 * Decodes a JSON string and, if the object is an array, overwrites the session container with its contents.
202 *
203 * @param string $data
204 *
205 * @return bool
206 */
207 public function json_in( $data ) {
208 $array = json_decode( $data );
209
210 if ( is_array( $array ) ) {
211 $this->container = $array;
212 return true;
213 }
214
215 return false;
216 }
217
218 /**
219 * Regenerate the current session's ID.
220 *
221 * @param bool $delete_old Flag whether or not to delete the old session data from the server.
222 */
223 public function regenerate_id( $delete_old = false ) {
224 if ( $delete_old ) {
225 delete_option( "_wp_session_{$this->session_id}" );
226 }
227
228 $this->session_id = $this->generate_id();
229
230 $this->set_cookie();
231 }
232
233 /**
234 * Check if a session has been initialized.
235 *
236 * @return bool
237 */
238 public function session_started() {
239 return !!self::$instance;
240 }
241
242 /**
243 * Return the read-only cache expiration value.
244 *
245 * @return int
246 */
247 public function cache_expiration() {
248 return $this->expires;
249 }
250
251 /**
252 * Flushes all session variables.
253 */
254 public function reset() {
255 $this->container = array();
256 }
257
258 /*****************************************************************/
259 /* Iterator Implementation */
260 /*****************************************************************/
261
262 /**
263 * Current position of the array.
264 *
265 * @link http://php.net/manual/en/iterator.current.php
266 *
267 * @return mixed
268 */
269 public function current() {
270 return current( $this->container );
271 }
272
273 /**
274 * Key of the current element.
275 *
276 * @link http://php.net/manual/en/iterator.key.php
277 *
278 * @return mixed
279 */
280 public function key() {
281 return key( $this->container );
282 }
283
284 /**
285 * Move the internal point of the container array to the next item
286 *
287 * @link http://php.net/manual/en/iterator.next.php
288 *
289 * @return void
290 */
291 public function next() {
292 next( $this->container );
293 }
294
295 /**
296 * Rewind the internal point of the container array.
297 *
298 * @link http://php.net/manual/en/iterator.rewind.php
299 *
300 * @return void
301 */
302 public function rewind() {
303 reset( $this->container );
304 }
305
306 /**
307 * Is the current key valid?
308 *
309 * @link http://php.net/manual/en/iterator.rewind.php
310 *
311 * @return bool
312 */
313 public function valid() {
314 return $this->offsetExists( $this->key() );
315 }
316
317 /*****************************************************************/
318 /* Countable Implementation */
319 /*****************************************************************/
320
321 /**
322 * Get the count of elements in the container array.
323 *
324 * @link http://php.net/manual/en/countable.count.php
325 *
326 * @return int
327 */
328 public function count() {
329 return count( $this->container );
330 }
331 }
332