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0-beta.1 3.2.2 6.3.0-rc.1 3.2.3 6.3.0-rc.2 3.2.4 6.3.1 3.2.5 6.3.2 3.2.6 6.4.0 3.3.0 6.4.0-beta.1 3.3.1 6.4.0-rc.1 3.3.2 6.4.1 3.3.2-rc.1 6.4.2 3.3.3 6.5.0 3.3.4 6.5.0-beta.1 3.3.5 6.5.0-rc.1 3.3.6 6.5.0-rc.2 3.4.0 6.5.1 3.4.0-beta.1 6.5.2 3.4.0-rc.2 6.6.0 3.4.1 6.6.0-beta.1 3.4.2 6.6.0-rc.1 3.4.3 6.6.0-rc.2 3.4.4 6.6.1 3.4.5 6.6.2 3.4.6 6.7.0 3.4.7 6.7.0-beta.1 3.4.8 6.7.0-beta.2 3.5.0 6.7.0-rc.1 3.5.0-beta.1 6.7.1 3.5.0-rc.1 6.8.0 3.5.0-rc.2 6.8.0-beta.1 3.5.1 6.8.0-beta.2 3.5.10 6.8.0-rc.1 3.5.2 6.8.1 3.5.3 6.8.2 3.5.4 6.8.3 3.5.5 6.9.0 3.5.6 6.9.0-beta.1 3.5.7 6.9.0-beta.2 3.5.8 6.9.0-rc.1 3.5.9 6.9.1 3.6.0 6.9.2 3.6.0-beta.1 6.9.3 3.6.0-rc.1 6.9.4 3.6.0-rc.2 6.9.5 3.6.0-rc.3 7.0.0 3.6.1 7.0.0-beta.1 3.6.2 7.0.0-beta.2 3.6.3 7.0.0-beta.3 3.6.4 7.0.0-rc.1 3.6.5 7.0.0-rc.2 3.6.6 7.0.1 3.6.7 7.0.2 3.7.0 7.1.0 3.7.0-beta.1 7.1.0-beta.1 3.7.0-rc.1 7.1.0-beta.2 3.7.0-rc.2 7.1.0-rc.1 3.7.1 7.1.0-rc.2 3.7.2 7.1.1
woocommerce / includes / class-wc-session-handler.php
woocommerce / includes Last commit date
abstracts 4 years ago admin 4 years ago blocks 5 years ago cli 4 years ago customizer 4 years ago data-stores 4 years ago emails 4 years ago export 4 years ago gateways 4 years ago import 4 years ago integrations 5 years ago interfaces 4 years ago legacy 5 years ago libraries 6 years ago log-handlers 5 years ago payment-tokens 5 years ago queue 5 years ago rest-api 4 years ago shipping 5 years ago shortcodes 5 years ago theme-support 4 years ago tracks 4 years ago traits 5 years ago walkers 5 years ago wccom-site 5 years ago widgets 4 years ago class-wc-ajax.php 4 years ago class-wc-api.php 5 years ago class-wc-auth.php 4 years ago class-wc-autoloader.php 5 years ago class-wc-background-emailer.php 5 years ago class-wc-background-updater.php 5 years ago class-wc-breadcrumb.php 5 years ago class-wc-cache-helper.php 4 years ago class-wc-cart-fees.php 5 years ago class-wc-cart-session.php 5 years ago class-wc-cart-totals.php 4 years ago class-wc-cart.php 4 years ago class-wc-checkout.php 4 years ago class-wc-cli.php 4 years ago class-wc-comments.php 4 years ago class-wc-countries.php 4 years ago class-wc-coupon.php 4 years ago class-wc-customer-download-log.php 5 years ago class-wc-customer-download.php 4 years ago class-wc-customer.php 4 years ago class-wc-data-exception.php 8 years ago class-wc-data-store.php 6 years ago class-wc-datetime.php 5 years ago class-wc-deprecated-action-hooks.php 8 years ago class-wc-deprecated-filter-hooks.php 7 years ago class-wc-discounts.php 4 years ago class-wc-download-handler.php 4 years ago class-wc-emails.php 5 years ago class-wc-embed.php 5 years ago class-wc-form-handler.php 5 years ago class-wc-frontend-scripts.php 4 years ago class-wc-geo-ip.php 5 years ago class-wc-geolite-integration.php 6 years ago class-wc-geolocation.php 4 years ago class-wc-https.php 4 years ago class-wc-install.php 4 years ago class-wc-integrations.php 5 years ago class-wc-log-levels.php 5 years ago class-wc-logger.php 5 years ago class-wc-meta-data.php 7 years ago class-wc-order-factory.php 5 years ago class-wc-order-item-coupon.php 4 years ago class-wc-order-item-fee.php 4 years ago class-wc-order-item-meta.php 4 years ago class-wc-order-item-product.php 4 years ago class-wc-order-item-shipping.php 4 years ago class-wc-order-item-tax.php 4 years ago class-wc-order-item.php 4 years ago class-wc-order-query.php 4 years ago class-wc-order-refund.php 5 years ago class-wc-order.php 4 years ago class-wc-payment-gateways.php 4 years ago class-wc-payment-tokens.php 5 years ago class-wc-post-data.php 4 years ago class-wc-post-types.php 4 years ago class-wc-privacy-background-process.php 5 years ago class-wc-privacy-erasers.php 4 years ago class-wc-privacy-exporters.php 4 years ago class-wc-privacy.php 4 years ago class-wc-product-attribute.php 4 years ago class-wc-product-download.php 4 years ago class-wc-product-external.php 5 years ago class-wc-product-factory.php 5 years ago class-wc-product-grouped.php 8 years ago class-wc-product-query.php 5 years ago class-wc-product-simple.php 5 years ago class-wc-product-variable.php 4 years ago class-wc-product-variation.php 4 years ago class-wc-query.php 4 years ago class-wc-rate-limiter.php 4 years ago class-wc-regenerate-images-request.php 5 years ago class-wc-regenerate-images.php 5 years ago class-wc-register-wp-admin-settings.php 4 years ago class-wc-rest-authentication.php 5 years ago class-wc-rest-exception.php 5 years ago class-wc-session-handler.php 5 years ago class-wc-shipping-rate.php 4 years ago class-wc-shipping-zone.php 5 years ago class-wc-shipping-zones.php 5 years ago class-wc-shipping.php 4 years ago class-wc-shortcodes.php 5 years ago class-wc-structured-data.php 5 years ago class-wc-tax.php 5 years ago class-wc-template-loader.php 4 years ago class-wc-tracker.php 4 years ago class-wc-validation.php 5 years ago class-wc-webhook.php 5 years ago class-woocommerce.php 4 years ago wc-account-functions.php 5 years ago wc-attribute-functions.php 4 years ago wc-cart-functions.php 5 years ago wc-conditional-functions.php 4 years ago wc-core-functions.php 4 years ago wc-coupon-functions.php 5 years ago wc-deprecated-functions.php 4 years ago wc-formatting-functions.php 4 years ago wc-notice-functions.php 5 years ago wc-order-functions.php 4 years ago wc-order-item-functions.php 5 years ago wc-page-functions.php 5 years ago wc-product-functions.php 4 years ago wc-rest-functions.php 5 years ago wc-stock-functions.php 5 years ago wc-template-functions.php 4 years ago wc-template-hooks.php 5 years ago wc-term-functions.php 4 years ago wc-update-functions.php 4 years ago wc-user-functions.php 5 years ago wc-webhook-functions.php 5 years ago wc-widget-functions.php 5 years ago
class-wc-session-handler.php
424 lines
1 <?php
2 /**
3 * Handle data for the current customers session.
4 * Implements the WC_Session abstract class.
5 *
6 * From 2.5 this uses a custom table for session storage. Based on https://github.com/kloon/woocommerce-large-sessions.
7 *
8 * @class WC_Session_Handler
9 * @version 2.5.0
10 * @package WooCommerce\Classes
11 */
12
13 use Automattic\Jetpack\Constants;
14
15 defined( 'ABSPATH' ) || exit;
16
17 /**
18 * Session handler class.
19 */
20 class WC_Session_Handler extends WC_Session {
21
22 /**
23 * Cookie name used for the session.
24 *
25 * @var string cookie name
26 */
27 protected $_cookie;
28
29 /**
30 * Stores session expiry.
31 *
32 * @var string session due to expire timestamp
33 */
34 protected $_session_expiring;
35
36 /**
37 * Stores session due to expire timestamp.
38 *
39 * @var string session expiration timestamp
40 */
41 protected $_session_expiration;
42
43 /**
44 * True when the cookie exists.
45 *
46 * @var bool Based on whether a cookie exists.
47 */
48 protected $_has_cookie = false;
49
50 /**
51 * Table name for session data.
52 *
53 * @var string Custom session table name
54 */
55 protected $_table;
56
57 /**
58 * Constructor for the session class.
59 */
60 public function __construct() {
61 $this->_cookie = apply_filters( 'woocommerce_cookie', 'wp_woocommerce_session_' . COOKIEHASH );
62 $this->_table = $GLOBALS['wpdb']->prefix . 'woocommerce_sessions';
63 }
64
65 /**
66 * Init hooks and session data.
67 *
68 * @since 3.3.0
69 */
70 public function init() {
71 $this->init_session_cookie();
72
73 add_action( 'woocommerce_set_cart_cookies', array( $this, 'set_customer_session_cookie' ), 10 );
74 add_action( 'shutdown', array( $this, 'save_data' ), 20 );
75 add_action( 'wp_logout', array( $this, 'destroy_session' ) );
76
77 if ( ! is_user_logged_in() ) {
78 add_filter( 'nonce_user_logged_out', array( $this, 'maybe_update_nonce_user_logged_out' ), 10, 2 );
79 }
80 }
81
82 /**
83 * Setup cookie and customer ID.
84 *
85 * @since 3.6.0
86 */
87 public function init_session_cookie() {
88 $cookie = $this->get_session_cookie();
89
90 if ( $cookie ) {
91 $this->_customer_id = $cookie[0];
92 $this->_session_expiration = $cookie[1];
93 $this->_session_expiring = $cookie[2];
94 $this->_has_cookie = true;
95 $this->_data = $this->get_session_data();
96
97 // If the user logs in, update session.
98 if ( is_user_logged_in() && strval( get_current_user_id() ) !== $this->_customer_id ) {
99 $guest_session_id = $this->_customer_id;
100 $this->_customer_id = strval( get_current_user_id() );
101 $this->_dirty = true;
102 $this->save_data( $guest_session_id );
103 $this->set_customer_session_cookie( true );
104 }
105
106 // Update session if its close to expiring.
107 if ( time() > $this->_session_expiring ) {
108 $this->set_session_expiration();
109 $this->update_session_timestamp( $this->_customer_id, $this->_session_expiration );
110 }
111 } else {
112 $this->set_session_expiration();
113 $this->_customer_id = $this->generate_customer_id();
114 $this->_data = $this->get_session_data();
115 }
116 }
117
118 /**
119 * Sets the session cookie on-demand (usually after adding an item to the cart).
120 *
121 * Since the cookie name (as of 2.1) is prepended with wp, cache systems like batcache will not cache pages when set.
122 *
123 * Warning: Cookies will only be set if this is called before the headers are sent.
124 *
125 * @param bool $set Should the session cookie be set.
126 */
127 public function set_customer_session_cookie( $set ) {
128 if ( $set ) {
129 $to_hash = $this->_customer_id . '|' . $this->_session_expiration;
130 $cookie_hash = hash_hmac( 'md5', $to_hash, wp_hash( $to_hash ) );
131 $cookie_value = $this->_customer_id . '||' . $this->_session_expiration . '||' . $this->_session_expiring . '||' . $cookie_hash;
132 $this->_has_cookie = true;
133
134 if ( ! isset( $_COOKIE[ $this->_cookie ] ) || $_COOKIE[ $this->_cookie ] !== $cookie_value ) {
135 wc_setcookie( $this->_cookie, $cookie_value, $this->_session_expiration, $this->use_secure_cookie(), true );
136 }
137 }
138 }
139
140 /**
141 * Should the session cookie be secure?
142 *
143 * @since 3.6.0
144 * @return bool
145 */
146 protected function use_secure_cookie() {
147 return apply_filters( 'wc_session_use_secure_cookie', wc_site_is_https() && is_ssl() );
148 }
149
150 /**
151 * Return true if the current user has an active session, i.e. a cookie to retrieve values.
152 *
153 * @return bool
154 */
155 public function has_session() {
156 return isset( $_COOKIE[ $this->_cookie ] ) || $this->_has_cookie || is_user_logged_in(); // @codingStandardsIgnoreLine.
157 }
158
159 /**
160 * Set session expiration.
161 */
162 public function set_session_expiration() {
163 $this->_session_expiring = time() + intval( apply_filters( 'wc_session_expiring', 60 * 60 * 47 ) ); // 47 Hours.
164 $this->_session_expiration = time() + intval( apply_filters( 'wc_session_expiration', 60 * 60 * 48 ) ); // 48 Hours.
165 }
166
167 /**
168 * Generate a unique customer ID for guests, or return user ID if logged in.
169 *
170 * Uses Portable PHP password hashing framework to generate a unique cryptographically strong ID.
171 *
172 * @return string
173 */
174 public function generate_customer_id() {
175 $customer_id = '';
176
177 if ( is_user_logged_in() ) {
178 $customer_id = strval( get_current_user_id() );
179 }
180
181 if ( empty( $customer_id ) ) {
182 require_once ABSPATH . 'wp-includes/class-phpass.php';
183 $hasher = new PasswordHash( 8, false );
184 $customer_id = md5( $hasher->get_random_bytes( 32 ) );
185 }
186
187 return $customer_id;
188 }
189
190 /**
191 * Get session unique ID for requests if session is initialized or user ID if logged in.
192 * Introduced to help with unit tests.
193 *
194 * @since 5.3.0
195 * @return string
196 */
197 public function get_customer_unique_id() {
198 $customer_id = '';
199
200 if ( $this->has_session() && $this->_customer_id ) {
201 $customer_id = $this->_customer_id;
202 } elseif ( is_user_logged_in() ) {
203 $customer_id = (string) get_current_user_id();
204 }
205
206 return $customer_id;
207 }
208
209 /**
210 * Get the session cookie, if set. Otherwise return false.
211 *
212 * Session cookies without a customer ID are invalid.
213 *
214 * @return bool|array
215 */
216 public function get_session_cookie() {
217 $cookie_value = isset( $_COOKIE[ $this->_cookie ] ) ? wp_unslash( $_COOKIE[ $this->_cookie ] ) : false; // @codingStandardsIgnoreLine.
218
219 if ( empty( $cookie_value ) || ! is_string( $cookie_value ) ) {
220 return false;
221 }
222
223 list( $customer_id, $session_expiration, $session_expiring, $cookie_hash ) = explode( '||', $cookie_value );
224
225 if ( empty( $customer_id ) ) {
226 return false;
227 }
228
229 // Validate hash.
230 $to_hash = $customer_id . '|' . $session_expiration;
231 $hash = hash_hmac( 'md5', $to_hash, wp_hash( $to_hash ) );
232
233 if ( empty( $cookie_hash ) || ! hash_equals( $hash, $cookie_hash ) ) {
234 return false;
235 }
236
237 return array( $customer_id, $session_expiration, $session_expiring, $cookie_hash );
238 }
239
240 /**
241 * Get session data.
242 *
243 * @return array
244 */
245 public function get_session_data() {
246 return $this->has_session() ? (array) $this->get_session( $this->_customer_id, array() ) : array();
247 }
248
249 /**
250 * Gets a cache prefix. This is used in session names so the entire cache can be invalidated with 1 function call.
251 *
252 * @return string
253 */
254 private function get_cache_prefix() {
255 return WC_Cache_Helper::get_cache_prefix( WC_SESSION_CACHE_GROUP );
256 }
257
258 /**
259 * Save data and delete guest session.
260 *
261 * @param int $old_session_key session ID before user logs in.
262 */
263 public function save_data( $old_session_key = 0 ) {
264 // Dirty if something changed - prevents saving nothing new.
265 if ( $this->_dirty && $this->has_session() ) {
266 global $wpdb;
267
268 $wpdb->query(
269 $wpdb->prepare(
270 "INSERT INTO {$wpdb->prefix}woocommerce_sessions (`session_key`, `session_value`, `session_expiry`) VALUES (%s, %s, %d)
271 ON DUPLICATE KEY UPDATE `session_value` = VALUES(`session_value`), `session_expiry` = VALUES(`session_expiry`)",
272 $this->_customer_id,
273 maybe_serialize( $this->_data ),
274 $this->_session_expiration
275 )
276 );
277
278 wp_cache_set( $this->get_cache_prefix() . $this->_customer_id, $this->_data, WC_SESSION_CACHE_GROUP, $this->_session_expiration - time() );
279 $this->_dirty = false;
280 if ( get_current_user_id() != $old_session_key && ! is_object( get_user_by( 'id', $old_session_key ) ) ) {
281 $this->delete_session( $old_session_key );
282 }
283 }
284 }
285
286 /**
287 * Destroy all session data.
288 */
289 public function destroy_session() {
290 $this->delete_session( $this->_customer_id );
291 $this->forget_session();
292 }
293
294 /**
295 * Forget all session data without destroying it.
296 */
297 public function forget_session() {
298 wc_setcookie( $this->_cookie, '', time() - YEAR_IN_SECONDS, $this->use_secure_cookie(), true );
299
300 wc_empty_cart();
301
302 $this->_data = array();
303 $this->_dirty = false;
304 $this->_customer_id = $this->generate_customer_id();
305 }
306
307 /**
308 * When a user is logged out, ensure they have a unique nonce by using the customer/session ID.
309 *
310 * @deprecated 5.3.0
311 * @param int $uid User ID.
312 * @return int|string
313 */
314 public function nonce_user_logged_out( $uid ) {
315 wc_deprecated_function( 'WC_Session_Handler::nonce_user_logged_out', '5.3', 'WC_Session_Handler::maybe_update_nonce_user_logged_out' );
316
317 return $this->has_session() && $this->_customer_id ? $this->_customer_id : $uid;
318 }
319
320 /**
321 * When a user is logged out, ensure they have a unique nonce to manage cart and more using the customer/session ID.
322 * This filter runs everything `wp_verify_nonce()` and `wp_create_nonce()` gets called.
323 *
324 * @since 5.3.0
325 * @param int $uid User ID.
326 * @param string $action The nonce action.
327 * @return int|string
328 */
329 public function maybe_update_nonce_user_logged_out( $uid, $action ) {
330 if ( Automattic\WooCommerce\Utilities\StringUtil::starts_with( $action, 'woocommerce' ) ) {
331 return $this->has_session() && $this->_customer_id ? $this->_customer_id : $uid;
332 }
333
334 return $uid;
335 }
336
337 /**
338 * Cleanup session data from the database and clear caches.
339 */
340 public function cleanup_sessions() {
341 global $wpdb;
342
343 $wpdb->query( $wpdb->prepare( "DELETE FROM $this->_table WHERE session_expiry < %d", time() ) ); // @codingStandardsIgnoreLine.
344
345 if ( class_exists( 'WC_Cache_Helper' ) ) {
346 WC_Cache_Helper::invalidate_cache_group( WC_SESSION_CACHE_GROUP );
347 }
348 }
349
350 /**
351 * Returns the session.
352 *
353 * @param string $customer_id Custo ID.
354 * @param mixed $default Default session value.
355 * @return string|array
356 */
357 public function get_session( $customer_id, $default = false ) {
358 global $wpdb;
359
360 if ( Constants::is_defined( 'WP_SETUP_CONFIG' ) ) {
361 return false;
362 }
363
364 // Try to get it from the cache, it will return false if not present or if object cache not in use.
365 $value = wp_cache_get( $this->get_cache_prefix() . $customer_id, WC_SESSION_CACHE_GROUP );
366
367 if ( false === $value ) {
368 $value = $wpdb->get_var( $wpdb->prepare( "SELECT session_value FROM $this->_table WHERE session_key = %s", $customer_id ) ); // @codingStandardsIgnoreLine.
369
370 if ( is_null( $value ) ) {
371 $value = $default;
372 }
373
374 $cache_duration = $this->_session_expiration - time();
375 if ( 0 < $cache_duration ) {
376 wp_cache_add( $this->get_cache_prefix() . $customer_id, $value, WC_SESSION_CACHE_GROUP, $cache_duration );
377 }
378 }
379
380 return maybe_unserialize( $value );
381 }
382
383 /**
384 * Delete the session from the cache and database.
385 *
386 * @param int $customer_id Customer ID.
387 */
388 public function delete_session( $customer_id ) {
389 global $wpdb;
390
391 wp_cache_delete( $this->get_cache_prefix() . $customer_id, WC_SESSION_CACHE_GROUP );
392
393 $wpdb->delete(
394 $this->_table,
395 array(
396 'session_key' => $customer_id,
397 )
398 );
399 }
400
401 /**
402 * Update the session expiry timestamp.
403 *
404 * @param string $customer_id Customer ID.
405 * @param int $timestamp Timestamp to expire the cookie.
406 */
407 public function update_session_timestamp( $customer_id, $timestamp ) {
408 global $wpdb;
409
410 $wpdb->update(
411 $this->_table,
412 array(
413 'session_expiry' => $timestamp,
414 ),
415 array(
416 'session_key' => $customer_id,
417 ),
418 array(
419 '%d',
420 )
421 );
422 }
423 }
424