| 1 |
<?php |
| 2 |
|
| 3 |
namespace Codelight\GDPR\DataSubject; |
| 4 |
|
| 5 |
/** |
| 6 |
* Handle authenticating the data subject either by logged in user or by email/cookie |
| 7 |
* |
| 8 |
* Class DataSubjectAuthenticator |
| 9 |
* |
| 10 |
* @package Codelight\GDPR\DataSubject |
| 11 |
*/ |
| 12 |
class DataSubjectAuthenticator |
| 13 |
{ |
| 14 |
var $dataSubjectManager; |
| 15 |
var $dataSubjectIdentificator; |
| 16 |
|
| 17 |
/** |
| 18 |
* DataSubjectAuthenticator constructor. |
| 19 |
* |
| 20 |
* @param DataSubjectManager $dataSubjectManager |
| 21 |
* @param DataSubjectIdentificator $dataSubjectIdentificator |
| 22 |
*/ |
| 23 |
public function __construct(DataSubjectManager $dataSubjectManager, DataSubjectIdentificator $dataSubjectIdentificator) |
| 24 |
{ |
| 25 |
$this->dataSubjectManager = $dataSubjectManager; |
| 26 |
$this->dataSubjectIdentificator = $dataSubjectIdentificator; |
| 27 |
} |
| 28 |
|
| 29 |
/** |
| 30 |
* Attempt to authenticate the data subject |
| 31 |
* |
| 32 |
* @return bool|\Codelight\GDPR\DataSubject\DataSubject |
| 33 |
*/ |
| 34 |
public function authenticate() |
| 35 |
{ |
| 36 |
// If the user is logged in, authenticate them |
| 37 |
if (is_user_logged_in()) { |
| 38 |
return apply_filters('gdpr/authenticate', $this->dataSubjectManager->getByLoggedInUser()); |
| 39 |
} |
| 40 |
|
| 41 |
// If the request contains the identification cookie, validate it and identify the |
| 42 |
// current user |
| 43 |
$cookieData = $this->getIdentificationCookieData(); |
| 44 |
if ($cookieData && $this->dataSubjectIdentificator->isKeyValid($cookieData[0], $cookieData[1])) { |
| 45 |
return apply_filters('gdpr/authenticate', $this->dataSubjectManager->getByEmail($cookieData[0])); |
| 46 |
} |
| 47 |
|
| 48 |
// Otherwise, we are not authenticated |
| 49 |
return apply_filters('gdpr/authenticate', false); |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* If the request contains a new identification key, validate it, then set a new key |
| 54 |
* to make the previous link obsolete. |
| 55 |
*/ |
| 56 |
public function identify() |
| 57 |
{ |
| 58 |
global $gdpr; |
| 59 |
// Do not attempt to identify logged in users |
| 60 |
if (is_user_logged_in()) { |
| 61 |
return; |
| 62 |
} |
| 63 |
|
| 64 |
if (isset($_REQUEST['gdpr_key']) && isset($_REQUEST['email'])) { |
| 65 |
$gdpr_email = sanitize_email($_REQUEST['email']); |
| 66 |
$privacyToolsPageUrl = get_permalink($gdpr->Options->get('tools_page')); |
| 67 |
$privacyToolsPageUrl = apply_filters('privacy_tools_gdprf_page_url',$privacyToolsPageUrl); |
| 68 |
|
| 69 |
if ($this->dataSubjectIdentificator->isKeyValid($gdpr_email, $_REQUEST['gdpr_key'])) { |
| 70 |
$this->setIdentificationCookie($gdpr_email); |
| 71 |
$url = $privacyToolsPageUrl; |
| 72 |
} else { |
| 73 |
$url = add_query_arg([ |
| 74 |
'gdpr_notice' => 'invalid_key', |
| 75 |
], $privacyToolsPageUrl); |
| 76 |
} |
| 77 |
|
| 78 |
wp_redirect($url); |
| 79 |
exit; |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Set the identification cookie with the given key |
| 85 |
* |
| 86 |
* @param $key |
| 87 |
*/ |
| 88 |
public function setIdentificationCookie($email) |
| 89 |
{ |
| 90 |
$key = $this->dataSubjectIdentificator->generateKey($email); |
| 91 |
|
| 92 |
setcookie( |
| 93 |
'gdpr_key', |
| 94 |
$email . '|' . $key, |
| 95 |
time() + (15 * 60), |
| 96 |
COOKIEPATH, |
| 97 |
COOKIE_DOMAIN, |
| 98 |
false, |
| 99 |
true |
| 100 |
); |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* @return string |
| 105 |
*/ |
| 106 |
public function getIdentificationCookieData() |
| 107 |
{ |
| 108 |
return isset($_COOKIE['gdpr_key']) ? explode('|', $_COOKIE['gdpr_key']) : null; |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* Remove the cookie |
| 113 |
*/ |
| 114 |
public function deleteSession($logout = true) |
| 115 |
{ |
| 116 |
unset($_COOKIE['gdpr_key']); |
| 117 |
|
| 118 |
setcookie( |
| 119 |
'gdpr_key', |
| 120 |
'', |
| 121 |
time() - 3600, |
| 122 |
COOKIEPATH, |
| 123 |
COOKIE_DOMAIN, |
| 124 |
false, |
| 125 |
true |
| 126 |
); |
| 127 |
|
| 128 |
if ($logout) { |
| 129 |
wp_logout(); |
| 130 |
} else { |
| 131 |
wp_destroy_current_session(); |
| 132 |
wp_clear_auth_cookie(); |
| 133 |
} |
| 134 |
} |
| 135 |
} |
| 136 |
|