PluginProbe ʕ •ᴥ•ʔ
Paid Membership Plugin, Ecommerce, User Registration Form, Login Form, User Profile & Restrict Content – ProfilePress / trunk
Paid Membership Plugin, Ecommerce, User Registration Form, Login Form, User Profile & Restrict Content – ProfilePress vtrunk
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 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