2013-01-15 19:51:19 +00:00
|
|
|
<?php
|
2014-09-20 18:59:26 +00:00
|
|
|
if ( ! defined( 'ABSPATH' ) ) {
|
2015-01-23 11:11:27 +00:00
|
|
|
exit;
|
2014-09-20 18:59:26 +00:00
|
|
|
}
|
2013-02-20 17:14:46 +00:00
|
|
|
|
2013-01-15 19:51:19 +00:00
|
|
|
/**
|
|
|
|
* Handle data for the current customers session.
|
2015-10-07 13:47:29 +00:00
|
|
|
* Implements the WC_Session abstract class.
|
2013-01-15 19:51:19 +00:00
|
|
|
*
|
2015-10-07 13:47:29 +00:00
|
|
|
* From 2.5 this uses a custom table for session storage. Based on https://github.com/kloon/woocommerce-large-sessions.
|
2013-01-15 19:51:19 +00:00
|
|
|
*
|
2015-10-07 13:58:13 +00:00
|
|
|
* @class WC_Session_Handler
|
|
|
|
* @version 2.5.0
|
|
|
|
* @package WooCommerce/Classes
|
|
|
|
* @category Class
|
|
|
|
* @author WooThemes
|
2013-01-15 19:51:19 +00:00
|
|
|
*/
|
|
|
|
class WC_Session_Handler extends WC_Session {
|
|
|
|
|
2015-10-07 13:47:29 +00:00
|
|
|
/** @var string cookie name */
|
2013-01-15 19:51:19 +00:00
|
|
|
private $_cookie;
|
|
|
|
|
2015-10-07 13:47:29 +00:00
|
|
|
/** @var string session due to expire timestamp */
|
2013-01-16 11:05:50 +00:00
|
|
|
private $_session_expiring;
|
|
|
|
|
2015-10-07 13:47:29 +00:00
|
|
|
/** @var string session expiration timestamp */
|
2013-01-15 19:51:19 +00:00
|
|
|
private $_session_expiration;
|
|
|
|
|
2015-10-07 13:47:29 +00:00
|
|
|
/** $var bool Bool based on whether a cookie exists **/
|
2014-05-08 09:36:19 +00:00
|
|
|
private $_has_cookie = false;
|
|
|
|
|
2015-10-07 13:47:29 +00:00
|
|
|
/** @var string Custom session table name */
|
|
|
|
private $_table;
|
|
|
|
|
2013-01-15 19:51:19 +00:00
|
|
|
/**
|
2015-10-07 13:58:13 +00:00
|
|
|
* Constructor for the session class
|
2013-01-15 19:51:19 +00:00
|
|
|
*/
|
|
|
|
public function __construct() {
|
2015-10-07 13:47:29 +00:00
|
|
|
global $wpdb;
|
|
|
|
|
2013-10-17 14:29:39 +00:00
|
|
|
$this->_cookie = 'wp_woocommerce_session_' . COOKIEHASH;
|
2015-10-07 13:47:29 +00:00
|
|
|
$this->_table = $wpdb->prefix . 'woocommerce_sessions';
|
2013-01-15 19:51:19 +00:00
|
|
|
|
|
|
|
if ( $cookie = $this->get_session_cookie() ) {
|
|
|
|
$this->_customer_id = $cookie[0];
|
2013-01-16 11:05:50 +00:00
|
|
|
$this->_session_expiration = $cookie[1];
|
|
|
|
$this->_session_expiring = $cookie[2];
|
2014-05-08 09:36:19 +00:00
|
|
|
$this->_has_cookie = true;
|
2013-01-15 19:51:19 +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();
|
2015-10-07 13:47:29 +00:00
|
|
|
$this->update_session_timestamp( $this->_customer_id, $this->_session_expiration );
|
2013-01-15 19:51:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
2013-01-16 11:05:50 +00:00
|
|
|
$this->set_session_expiration();
|
2013-01-15 19:51:19 +00:00
|
|
|
$this->_customer_id = $this->generate_customer_id();
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->_data = $this->get_session_data();
|
|
|
|
|
2015-10-07 13:58:13 +00:00
|
|
|
// Actions
|
|
|
|
add_action( 'woocommerce_set_cart_cookies', array( $this, 'set_customer_session_cookie' ), 10 );
|
|
|
|
add_action( 'woocommerce_cleanup_sessions', array( $this, 'cleanup_sessions' ), 10 );
|
|
|
|
add_action( 'shutdown', array( $this, 'save_data' ), 20 );
|
|
|
|
add_action( 'wp_logout', array( $this, 'destroy_session' ) );
|
|
|
|
if ( ! is_user_logged_in() ) {
|
|
|
|
add_action( 'woocommerce_thankyou', array( $this, 'destroy_session' ) );
|
|
|
|
add_filter( 'nonce_user_logged_out', array( $this, 'nonce_user_logged_out' ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the session cookie on-demand (usually after adding an item to the cart)
|
|
|
|
*
|
|
|
|
* Since the cookie name (as of 2.1) is prepended with wp, cache systems like batcache will not cache pages when set
|
|
|
|
*
|
|
|
|
* Warning: Cookies will only be set if this is called before the headers are sent
|
|
|
|
*/
|
|
|
|
public function set_customer_session_cookie( $set ) {
|
|
|
|
if ( $set ) {
|
|
|
|
// Set/renew our cookie
|
2014-05-08 09:36:19 +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;
|
2013-10-17 14:29:39 +00:00
|
|
|
|
2015-10-07 13:58:13 +00:00
|
|
|
// Set the cookie
|
|
|
|
wc_setcookie( $this->_cookie, $cookie_value, $this->_session_expiration, apply_filters( 'wc_session_use_secure_cookie', false ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return true if the current user has an active session, i.e. a cookie to retrieve values
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function has_session() {
|
|
|
|
return isset( $_COOKIE[ $this->_cookie ] ) || $this->_has_cookie || is_user_logged_in();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set session expiration.
|
|
|
|
*/
|
|
|
|
public function set_session_expiration() {
|
|
|
|
$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
|
|
|
|
}
|
2013-01-15 19:51:19 +00:00
|
|
|
|
|
|
|
/**
|
2015-10-07 13:58:13 +00:00
|
|
|
* Generate a unique customer ID for guests, or return user ID if logged in
|
2014-09-20 18:59:26 +00:00
|
|
|
*
|
2015-10-07 13:58:13 +00:00
|
|
|
* Uses Portable PHP password hashing framework to generate a unique cryptographically strong ID
|
2013-01-15 19:51:19 +00:00
|
|
|
*
|
2014-05-20 10:08:31 +00:00
|
|
|
* @return int|string
|
2013-01-15 19:51:19 +00:00
|
|
|
*/
|
2013-09-13 12:39:46 +00:00
|
|
|
public function generate_customer_id() {
|
2014-05-08 09:36:19 +00:00
|
|
|
if ( is_user_logged_in() ) {
|
2013-01-15 19:51:19 +00:00
|
|
|
return get_current_user_id();
|
2014-05-08 09:36:19 +00:00
|
|
|
} else {
|
2014-05-20 10:08:31 +00:00
|
|
|
require_once( ABSPATH . 'wp-includes/class-phpass.php');
|
|
|
|
$hasher = new PasswordHash( 8, false );
|
|
|
|
return md5( $hasher->get_random_bytes( 32 ) );
|
2014-05-08 09:36:19 +00:00
|
|
|
}
|
2013-01-15 19:51:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-10-07 13:58:13 +00:00
|
|
|
* Get session cookie
|
2013-01-15 19:51:19 +00:00
|
|
|
*
|
2015-01-23 11:11:27 +00:00
|
|
|
* @return bool|array
|
2013-01-15 19:51:19 +00:00
|
|
|
*/
|
2013-09-13 12:39:46 +00:00
|
|
|
public function get_session_cookie() {
|
2014-05-08 09:36:19 +00:00
|
|
|
if ( empty( $_COOKIE[ $this->_cookie ] ) ) {
|
2013-01-15 19:51:19 +00:00
|
|
|
return false;
|
2014-05-08 09:36:19 +00:00
|
|
|
}
|
2013-01-15 19:51:19 +00:00
|
|
|
|
2013-01-16 11:05:50 +00:00
|
|
|
list( $customer_id, $session_expiration, $session_expiring, $cookie_hash ) = explode( '||', $_COOKIE[ $this->_cookie ] );
|
2013-01-15 19:51:19 +00:00
|
|
|
|
|
|
|
// Validate hash
|
2013-01-16 11:05:50 +00:00
|
|
|
$to_hash = $customer_id . $session_expiration;
|
2013-01-15 19:51:19 +00:00
|
|
|
$hash = hash_hmac( 'md5', $to_hash, wp_hash( $to_hash ) );
|
|
|
|
|
2014-05-08 09:36:19 +00:00
|
|
|
if ( $hash != $cookie_hash ) {
|
2013-01-15 19:51:19 +00:00
|
|
|
return false;
|
2014-05-08 09:36:19 +00:00
|
|
|
}
|
2013-01-15 19:51:19 +00:00
|
|
|
|
2013-01-16 11:05:50 +00:00
|
|
|
return array( $customer_id, $session_expiration, $session_expiring, $cookie_hash );
|
2013-01-15 19:51:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-10-07 13:58:13 +00:00
|
|
|
* Get session data
|
2013-01-15 19:51:19 +00:00
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2013-09-13 12:39:46 +00:00
|
|
|
public function get_session_data() {
|
2015-10-07 13:47:29 +00:00
|
|
|
return $this->has_session() ? (array) $this->get_session( $this->_customer_id, array() ) : array();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-10-07 13:58:13 +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:47:29 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
private function get_cache_prefix() {
|
|
|
|
$prefix_num = wp_cache_get( 'wc_session_prefix', WC_SESSION_CACHE_GROUP );
|
|
|
|
|
|
|
|
if ( $prefix_num === false ) {
|
|
|
|
wp_cache_set( 'wc_session_prefix', 1, WC_SESSION_CACHE_GROUP );
|
|
|
|
}
|
|
|
|
|
|
|
|
return 'wc_session_' . $prefix_num . '_';
|
2013-01-15 19:51:19 +00:00
|
|
|
}
|
|
|
|
|
2015-10-07 13:58:13 +00:00
|
|
|
/**
|
|
|
|
* Save data
|
|
|
|
*/
|
|
|
|
public function save_data() {
|
|
|
|
// Dirty if something changed - prevents saving nothing new
|
|
|
|
if ( $this->_dirty && $this->has_session() ) {
|
2015-10-07 13:47:29 +00:00
|
|
|
global $wpdb;
|
|
|
|
|
|
|
|
$session_id = $wpdb->get_var( $wpdb->prepare( "SELECT session_id FROM $this->_table WHERE session_key = %s;", $this->_customer_id ) );
|
|
|
|
|
|
|
|
if ( $session_id ) {
|
|
|
|
$wpdb->update(
|
|
|
|
$this->_table,
|
|
|
|
array(
|
|
|
|
'session_key' => $this->_customer_id,
|
|
|
|
'session_value' => maybe_serialize( $this->_data ),
|
|
|
|
'session_expiry' => $this->_session_expiration
|
|
|
|
),
|
|
|
|
array( 'session_id' => $session_id ),
|
|
|
|
array(
|
|
|
|
'%s',
|
|
|
|
'%s',
|
|
|
|
'%d'
|
|
|
|
),
|
|
|
|
array( '%d' )
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
$wpdb->insert(
|
|
|
|
$this->_table,
|
|
|
|
array(
|
|
|
|
'session_key' => $this->_customer_id,
|
|
|
|
'session_value' => maybe_serialize( $this->_data ),
|
|
|
|
'session_expiry' => $this->_session_expiration
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'%s',
|
|
|
|
'%s',
|
|
|
|
'%d'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2013-01-16 11:05:50 +00:00
|
|
|
|
2015-10-07 13:47:29 +00:00
|
|
|
// Set cache
|
|
|
|
wp_cache_set( $this->get_cache_prefix() . $this->_customer_id, $this->_data, WC_SESSION_CACHE_GROUP, $this->_session_expiration - time() );
|
2013-01-16 11:05:50 +00:00
|
|
|
|
2015-10-07 13:58:13 +00:00
|
|
|
// Mark session clean after saving
|
|
|
|
$this->_dirty = false;
|
|
|
|
}
|
|
|
|
}
|
2013-01-16 09:53:08 +00:00
|
|
|
|
2015-10-07 13:58:13 +00:00
|
|
|
/**
|
|
|
|
* Destroy all session data
|
|
|
|
*/
|
|
|
|
public function destroy_session() {
|
2014-05-20 09:01:26 +00:00
|
|
|
// Clear cookie
|
|
|
|
wc_setcookie( $this->_cookie, '', time() - YEAR_IN_SECONDS, apply_filters( 'wc_session_use_secure_cookie', false ) );
|
|
|
|
|
2015-10-07 13:47:29 +00:00
|
|
|
$this->delete_session( $this->_customer_id );
|
2014-05-20 09:01:26 +00:00
|
|
|
|
|
|
|
// Clear cart
|
|
|
|
wc_empty_cart();
|
2014-09-20 18:59:26 +00:00
|
|
|
|
2014-05-20 09:01:26 +00:00
|
|
|
// Clear data
|
|
|
|
$this->_data = array();
|
|
|
|
$this->_dirty = false;
|
|
|
|
$this->_customer_id = $this->generate_customer_id();
|
|
|
|
}
|
|
|
|
|
2015-05-21 23:23:33 +00:00
|
|
|
/**
|
2015-10-07 13:58:13 +00:00
|
|
|
* When a user is logged out, ensure they have a unique nonce by using the customer/session ID
|
|
|
|
*
|
2015-05-21 23:23:33 +00:00
|
|
|
* @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
|
|
|
/**
|
|
|
|
* Cleanup sessions
|
2013-01-16 09:53:08 +00:00
|
|
|
*/
|
|
|
|
public function cleanup_sessions() {
|
|
|
|
global $wpdb;
|
|
|
|
|
2014-01-13 10:45:31 +00:00
|
|
|
if ( ! defined( 'WP_SETUP_CONFIG' ) && ! defined( 'WP_INSTALLING' ) ) {
|
|
|
|
|
2015-10-07 13:47:29 +00:00
|
|
|
// Delete expired sessions
|
|
|
|
$wpdb->query( $wpdb->prepare( "DELETE FROM $this->_table WHERE session_expiry < %d", time() ) );
|
|
|
|
|
|
|
|
// Invalidate cache
|
|
|
|
wp_cache_incr( 'wc_session_prefix', 1, WC_SESSION_CACHE_GROUP );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the session
|
2015-10-07 13:58:13 +00:00
|
|
|
*
|
2015-10-07 13:47:29 +00:00
|
|
|
* @param string $customer_id
|
|
|
|
* @param mixed $default
|
|
|
|
* @return string|array
|
|
|
|
*/
|
2015-10-07 13:58:13 +00:00
|
|
|
public function get_session( $customer_id, $default = false ) {
|
2015-10-07 13:47:29 +00:00
|
|
|
global $wpdb;
|
|
|
|
|
|
|
|
if ( defined( 'WP_SETUP_CONFIG' ) ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try 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 ) {
|
|
|
|
$value = $wpdb->get_var( $wpdb->prepare( "SELECT session_value FROM $this->_table WHERE session_key = %s", $customer_id ) );
|
|
|
|
|
|
|
|
if ( is_null( $value ) ) {
|
|
|
|
$value = $default;
|
2014-01-13 10:45:31 +00:00
|
|
|
}
|
2015-10-07 13:47:29 +00:00
|
|
|
|
|
|
|
wp_cache_add( $this->get_cache_prefix() . $customer_id, $value, WC_SESSION_CACHE_GROUP, $this->_session_expiration - time() );
|
2014-01-13 10:45:31 +00:00
|
|
|
}
|
2015-10-07 13:47:29 +00:00
|
|
|
|
|
|
|
return maybe_unserialize( $value );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete the session from the cache and database
|
2015-10-07 13:58:13 +00:00
|
|
|
*
|
|
|
|
* @param int $customer_id
|
2015-10-07 13:47:29 +00:00
|
|
|
*/
|
2015-10-07 13:58:13 +00:00
|
|
|
public function delete_session( $customer_id ) {
|
2015-10-07 13:47:29 +00:00
|
|
|
global $wpdb;
|
|
|
|
|
|
|
|
wp_cache_delete( $this->get_cache_prefix() . $customer_id, WC_SESSION_CACHE_GROUP );
|
|
|
|
|
|
|
|
$wpdb->delete(
|
|
|
|
$this->_table,
|
|
|
|
array(
|
|
|
|
'session_key' => $customer_id
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the session expiry timestamp
|
2015-10-07 13:58:13 +00:00
|
|
|
*
|
|
|
|
* @param string $customer_id
|
|
|
|
* @param int $timestamp
|
2015-10-07 13:47:29 +00:00
|
|
|
*/
|
|
|
|
public function update_session_timestamp( $customer_id, $timestamp ) {
|
|
|
|
global $wpdb;
|
2015-10-07 13:58:13 +00:00
|
|
|
|
2015-10-07 13:47:29 +00:00
|
|
|
$wpdb->update(
|
|
|
|
$this->_table,
|
|
|
|
array(
|
|
|
|
'session_expiry' => $timestamp
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'session_key' => $customer_id
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'%d'
|
|
|
|
)
|
|
|
|
);
|
2013-01-16 09:53:08 +00:00
|
|
|
}
|
2014-09-20 18:59:26 +00:00
|
|
|
}
|