$expiryTime; } public static function verifySignedToken($propertyID, $cookie) { $tokens = explode(',', $_COOKIE[$cookie]); foreach ($tokens as $token) { $parts = explode('|', base64_decode($token)); if (self::isTokenBadOrExpired($parts)) { continue; } list($hash, $expiryTime) = $parts; $userAgent = array_key_exists('HTTP_USER_AGENT', $_SERVER) ? sanitize_text_field($_SERVER['HTTP_USER_AGENT']) : ''; // try this month, last month, and next month $dates = [ date('mY'), date('mY', strtotime('-1 month')), date('mY', strtotime('+1 month')), ]; foreach ($dates as $date) { $expectedToken = self::generateSignedToken($propertyID, $cookie, $date, $userAgent, $expiryTime); if (hash_equals($token, $expectedToken)) { self::setVerifiedEFSToken($cookie, true); return true; } } } self::setVerifiedEFSToken($cookie, false); return false; } public static function getTokenForUser($propertyID, $cookie) { $tokens = []; if (isset($_COOKIE[$cookie])) { // remove any expired tokens $tokens = array_filter(explode(',', $_COOKIE[$cookie]), function($token) { $parts = explode('|', base64_decode($token)); return !self::isTokenBadOrExpired($parts); }); } $date = date('mY'); $userAgent = array_key_exists('HTTP_USER_AGENT', $_SERVER) ? sanitize_text_field($_SERVER['HTTP_USER_AGENT']) : ''; $token = self::generateSignedToken($propertyID, $cookie, $date, $userAgent); array_push($tokens, $token); // only keep the last 2 tokens $tokens = array_slice($tokens, -2); $val = implode(',', $tokens); if (defined('ADM_TESTING')) { $_COOKIE[$cookie] = $val; } else { setcookie($cookie, $val, time() + (3 * 24 * 60 * 60), COOKIEPATH, COOKIE_DOMAIN, is_ssl(), true); } return array('success' => true); } }