woocommerce/includes/class-wc-session-handler.php

377 lines
10 KiB
PHP
Raw Normal View History

<?php
/**
* Handle data for the current customers session.
* Implements the WC_Session abstract class.
*
* From 2.5 this uses a custom table for session storage. Based on https://github.com/kloon/woocommerce-large-sessions.
*
2015-10-07 13:58:13 +00:00
* @class WC_Session_Handler
* @version 2.5.0
* @package WooCommerce/Classes
2017-12-15 14:34:04 +00:00
*/
defined( 'ABSPATH' ) || exit;
2017-12-15 14:34:04 +00:00
/**
* Session handler class.
*/
class WC_Session_Handler extends WC_Session {
2017-12-15 14:34:04 +00:00
/**
* Cookie name used for the session.
*
* @var string cookie name
*/
protected $_cookie;
2017-12-15 14:34:04 +00:00
/**
* Stores session expiry.
*
* @var string session due to expire timestamp
*/
protected $_session_expiring;
2013-01-16 11:05:50 +00:00
2017-12-15 14:34:04 +00:00
/**
* Stores session due to expire timestamp.
*
* @var string session expiration timestamp
*/
protected $_session_expiration;
2017-12-15 14:34:04 +00:00
/**
* True when the cookie exists.
*
* @var bool Based on whether a cookie exists.
*/
protected $_has_cookie = false;
2017-12-15 14:34:04 +00:00
/**
* Table name for session data.
*
* @var string Custom session table name
*/
protected $_table;
/**
2015-11-03 13:31:20 +00:00
* Constructor for the session class.
*/
public function __construct() {
2017-05-10 19:46:43 +00:00
$this->_cookie = apply_filters( 'woocommerce_cookie', 'wp_woocommerce_session_' . COOKIEHASH );
2017-12-15 14:34:04 +00:00
$this->_table = $GLOBALS['wpdb']->prefix . 'woocommerce_sessions';
}
/**
* Init hooks and session data.
*
* @since 3.3.0
*/
public function init() {
$this->init_session_cookie();
add_action( 'woocommerce_set_cart_cookies', array( $this, 'set_customer_session_cookie' ), 10 );
add_action( 'shutdown', array( $this, 'save_data' ), 20 );
add_action( 'wp_logout', array( $this, 'destroy_session' ) );
if ( ! is_user_logged_in() ) {
add_filter( 'nonce_user_logged_out', array( $this, 'nonce_user_logged_out' ) );
}
}
/**
* Setup cookie and customer ID.
*
* @since 3.6.0
*/
public function init_session_cookie() {
2017-12-15 14:34:04 +00:00
$cookie = $this->get_session_cookie();
2017-12-15 14:34:04 +00:00
if ( $cookie ) {
$this->_customer_id = $cookie[0];
2013-01-16 11:05:50 +00:00
$this->_session_expiration = $cookie[1];
$this->_session_expiring = $cookie[2];
$this->_has_cookie = true;
2019-03-12 16:36:15 +00:00
$this->_data = $this->get_session_data();
// If the user logs in, update session.
if ( is_user_logged_in() && get_current_user_id() !== $this->_customer_id ) {
$this->_customer_id = get_current_user_id();
$this->_dirty = true;
$this->save_data();
$this->set_customer_session_cookie( true );
}
2017-12-15 14:34:04 +00:00
// Update session if its close to expiring.
2013-01-16 11:05:50 +00:00
if ( time() > $this->_session_expiring ) {
$this->set_session_expiration();
$this->update_session_timestamp( $this->_customer_id, $this->_session_expiration );
}
} else {
2013-01-16 11:05:50 +00:00
$this->set_session_expiration();
$this->_customer_id = $this->generate_customer_id();
2019-03-12 16:36:15 +00:00
$this->_data = $this->get_session_data();
}
2015-10-07 13:58:13 +00:00
}
/**
2015-11-03 13:31:20 +00:00
* Sets the session cookie on-demand (usually after adding an item to the cart).
2015-10-07 13:58:13 +00:00
*
2015-11-03 13:31:20 +00:00
* Since the cookie name (as of 2.1) is prepended with wp, cache systems like batcache will not cache pages when set.
2015-10-07 13:58:13 +00:00
*
2015-11-03 13:31:20 +00:00
* Warning: Cookies will only be set if this is called before the headers are sent.
*
2017-12-15 14:34:04 +00:00
* @param bool $set Should the session cookie be set.
2015-10-07 13:58:13 +00:00
*/
public function set_customer_session_cookie( $set ) {
if ( $set ) {
2015-10-23 11:02:29 +00:00
$to_hash = $this->_customer_id . '|' . $this->_session_expiration;
$cookie_hash = hash_hmac( 'md5', $to_hash, wp_hash( $to_hash ) );
$cookie_value = $this->_customer_id . '||' . $this->_session_expiration . '||' . $this->_session_expiring . '||' . $cookie_hash;
$this->_has_cookie = true;
if ( ! isset( $_COOKIE[ $this->_cookie ] ) || $_COOKIE[ $this->_cookie ] !== $cookie_value ) {
2019-03-14 17:14:57 +00:00
wc_setcookie( $this->_cookie, $cookie_value, $this->_session_expiration, $this->use_secure_cookie(), true );
}
2015-10-07 13:58:13 +00:00
}
}
2019-03-14 17:14:57 +00:00
/**
* Should the session cookie be secure?
*
* @since 3.6.0
* @return bool
*/
protected function use_secure_cookie() {
return apply_filters( 'wc_session_use_secure_cookie', wc_site_is_https() && is_ssl() );
}
2015-10-07 13:58:13 +00:00
/**
2015-11-03 13:31:20 +00:00
* Return true if the current user has an active session, i.e. a cookie to retrieve values.
2015-10-07 13:58:13 +00:00
*
* @return bool
*/
public function has_session() {
2017-12-15 14:34:04 +00:00
return isset( $_COOKIE[ $this->_cookie ] ) || $this->_has_cookie || is_user_logged_in(); // @codingStandardsIgnoreLine.
2015-10-07 13:58:13 +00:00
}
/**
* Set session expiration.
*/
public function set_session_expiration() {
2015-11-03 13:31:20 +00:00
$this->_session_expiring = time() + intval( apply_filters( 'wc_session_expiring', 60 * 60 * 47 ) ); // 47 Hours.
$this->_session_expiration = time() + intval( apply_filters( 'wc_session_expiration', 60 * 60 * 48 ) ); // 48 Hours.
2015-10-07 13:58:13 +00:00
}
/**
2015-11-03 13:31:20 +00:00
* Generate a unique customer ID for guests, or return user ID if logged in.
*
2015-11-03 13:31:20 +00:00
* Uses Portable PHP password hashing framework to generate a unique cryptographically strong ID.
*
* @return string
*/
public function generate_customer_id() {
$customer_id = '';
if ( is_user_logged_in() ) {
$customer_id = get_current_user_id();
}
if ( empty( $customer_id ) ) {
2017-12-15 14:34:04 +00:00
require_once ABSPATH . 'wp-includes/class-phpass.php';
$hasher = new PasswordHash( 8, false );
$customer_id = md5( $hasher->get_random_bytes( 32 ) );
}
return $customer_id;
}
/**
* Get the session cookie, if set. Otherwise return false.
*
* Session cookies without a customer ID are invalid.
*
2015-01-23 11:11:27 +00:00
* @return bool|array
*/
public function get_session_cookie() {
2017-12-15 14:34:04 +00:00
$cookie_value = isset( $_COOKIE[ $this->_cookie ] ) ? wp_unslash( $_COOKIE[ $this->_cookie ] ) : false; // @codingStandardsIgnoreLine.
if ( empty( $cookie_value ) || ! is_string( $cookie_value ) ) {
return false;
}
2017-12-15 14:34:04 +00:00
list( $customer_id, $session_expiration, $session_expiring, $cookie_hash ) = explode( '||', $cookie_value );
if ( empty( $customer_id ) ) {
return false;
}
2017-12-15 14:34:04 +00:00
// Validate hash.
2015-10-23 11:02:29 +00:00
$to_hash = $customer_id . '|' . $session_expiration;
$hash = hash_hmac( 'md5', $to_hash, wp_hash( $to_hash ) );
if ( empty( $cookie_hash ) || ! hash_equals( $hash, $cookie_hash ) ) {
return false;
}
2013-01-16 11:05:50 +00:00
return array( $customer_id, $session_expiration, $session_expiring, $cookie_hash );
}
/**
2015-11-03 13:31:20 +00:00
* Get session data.
*
* @return array
*/
public function get_session_data() {
return $this->has_session() ? (array) $this->get_session( $this->_customer_id, array() ) : array();
}
/**
2015-11-03 13:31:20 +00:00
* Gets a cache prefix. This is used in session names so the entire cache can be invalidated with 1 function call.
2015-10-07 13:58:13 +00:00
*
* @return string
*/
private function get_cache_prefix() {
return WC_Cache_Helper::get_cache_prefix( WC_SESSION_CACHE_GROUP );
}
2015-10-07 13:58:13 +00:00
/**
2015-11-03 13:31:20 +00:00
* Save data.
2015-10-07 13:58:13 +00:00
*/
public function save_data() {
2017-12-15 14:34:04 +00:00
// Dirty if something changed - prevents saving nothing new.
2015-10-07 13:58:13 +00:00
if ( $this->_dirty && $this->has_session() ) {
global $wpdb;
$wpdb->query(
$wpdb->prepare(
"INSERT INTO {$wpdb->prefix}woocommerce_sessions (`session_key`, `session_value`, `session_expiry`) VALUES (%s, %s, %d)
ON DUPLICATE KEY UPDATE `session_value` = VALUES(`session_value`), `session_expiry` = VALUES(`session_expiry`)",
$this->_customer_id,
maybe_serialize( $this->_data ),
$this->_session_expiration
)
);
2013-01-16 11:05:50 +00:00
wp_cache_set( $this->get_cache_prefix() . $this->_customer_id, $this->_data, WC_SESSION_CACHE_GROUP, $this->_session_expiration - time() );
2015-10-07 13:58:13 +00:00
$this->_dirty = false;
}
}
2015-10-07 13:58:13 +00:00
/**
2015-11-03 13:31:20 +00:00
* Destroy all session data.
2015-10-07 13:58:13 +00:00
*/
public function destroy_session() {
$this->delete_session( $this->_customer_id );
$this->forget_session();
}
/**
* Forget all session data without destroying it.
*/
public function forget_session() {
2019-03-14 17:14:57 +00:00
wc_setcookie( $this->_cookie, '', time() - YEAR_IN_SECONDS, $this->use_secure_cookie(), true );
wc_empty_cart();
$this->_data = array();
$this->_dirty = false;
$this->_customer_id = $this->generate_customer_id();
}
/**
2015-11-03 13:31:20 +00:00
* When a user is logged out, ensure they have a unique nonce by using the customer/session ID.
2015-10-07 13:58:13 +00:00
*
2017-12-15 14:34:04 +00:00
* @param int $uid User ID.
* @return string
*/
public function nonce_user_logged_out( $uid ) {
return $this->has_session() && $this->_customer_id ? $this->_customer_id : $uid;
}
2015-10-07 13:58:13 +00:00
/**
2017-12-15 14:46:02 +00:00
* Cleanup session data from the database and clear caches.
*/
public function cleanup_sessions() {
global $wpdb;
2017-12-15 14:46:02 +00:00
$wpdb->query( $wpdb->prepare( "DELETE FROM $this->_table WHERE session_expiry < %d", time() ) ); // @codingStandardsIgnoreLine.
2017-12-15 14:46:02 +00:00
if ( class_exists( 'WC_Cache_Helper' ) ) {
WC_Cache_Helper::incr_cache_prefix( WC_SESSION_CACHE_GROUP );
}
}
/**
2015-11-03 13:31:20 +00:00
* Returns the session.
2015-10-07 13:58:13 +00:00
*
2017-12-15 14:34:04 +00:00
* @param string $customer_id Custo ID.
* @param mixed $default Default session value.
* @return string|array
*/
2015-10-07 13:58:13 +00:00
public function get_session( $customer_id, $default = false ) {
global $wpdb;
if ( defined( 'WP_SETUP_CONFIG' ) ) {
return false;
}
2017-12-15 14:34:04 +00:00
// Try to get it from the cache, it will return false if not present or if object cache not in use.
$value = wp_cache_get( $this->get_cache_prefix() . $customer_id, WC_SESSION_CACHE_GROUP );
if ( false === $value ) {
2017-12-15 14:46:02 +00:00
$value = $wpdb->get_var( $wpdb->prepare( "SELECT session_value FROM $this->_table WHERE session_key = %s", $customer_id ) ); // @codingStandardsIgnoreLine.
if ( is_null( $value ) ) {
$value = $default;
}
$cache_duration = $this->_session_expiration - time();
if ( 0 < $cache_duration ) {
wp_cache_add( $this->get_cache_prefix() . $customer_id, $value, WC_SESSION_CACHE_GROUP, $cache_duration );
}
}
return maybe_unserialize( $value );
}
/**
2015-11-03 13:31:20 +00:00
* Delete the session from the cache and database.
2015-10-07 13:58:13 +00:00
*
2017-12-15 14:34:04 +00:00
* @param int $customer_id Customer ID.
*/
2015-10-07 13:58:13 +00:00
public function delete_session( $customer_id ) {
global $wpdb;
wp_cache_delete( $this->get_cache_prefix() . $customer_id, WC_SESSION_CACHE_GROUP );
$wpdb->delete(
$this->_table,
array(
'session_key' => $customer_id,
)
);
}
/**
2015-11-03 13:31:20 +00:00
* Update the session expiry timestamp.
2015-10-07 13:58:13 +00:00
*
2017-12-15 14:34:04 +00:00
* @param string $customer_id Customer ID.
* @param int $timestamp Timestamp to expire the cookie.
*/
public function update_session_timestamp( $customer_id, $timestamp ) {
global $wpdb;
2015-10-07 13:58:13 +00:00
$wpdb->update(
$this->_table,
array(
'session_expiry' => $timestamp,
),
array(
'session_key' => $customer_id,
),
array(
'%d',
)
);
}
}