| 1 |
<?php |
| 2 |
|
| 3 |
namespace Codelight\GDPR\DataSubject; |
| 4 |
|
| 5 |
use Codelight\GDPR\Options\Options; |
| 6 |
use Codelight\GDPR\Components\Consent\UserConsentModel; |
| 7 |
/** |
| 8 |
* Identify the data subject by unique temporary key |
| 9 |
* |
| 10 |
* Class DataSubjectIdentificator |
| 11 |
* |
| 12 |
* @package Codelight\GDPR\DataSubject |
| 13 |
*/ |
| 14 |
class DataSubjectIdentificator |
| 15 |
{ |
| 16 |
/* @var DataSubjectManager */ |
| 17 |
protected $dataSubjectManager; |
| 18 |
|
| 19 |
/* @var Options */ |
| 20 |
protected $options; |
| 21 |
|
| 22 |
protected $UserConsentModel; |
| 23 |
/** |
| 24 |
* DataSubjectIdentificator constructor. |
| 25 |
* |
| 26 |
* @param DataSubjectManager $dataSubjectManager |
| 27 |
*/ |
| 28 |
public function __construct(DataSubjectManager $dataSubjectManager, Options $options,UserConsentModel $UserConsentModel) |
| 29 |
{ |
| 30 |
$this->dataSubjectManager = $dataSubjectManager; |
| 31 |
$this->options = $options; |
| 32 |
$this->UserConsentModel = $UserConsentModel; |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* Check if there is any data associated with the given email address |
| 37 |
* |
| 38 |
* @param $email |
| 39 |
* @return bool |
| 40 |
*/ |
| 41 |
public function isDataSubject($email) |
| 42 |
{ |
| 43 |
$email = sanitize_email($email); |
| 44 |
$dataSubject = $this->dataSubjectManager->getByEmail($email); |
| 45 |
$get_on_site_status=''; |
| 46 |
if($dataSubject->hasData()){ |
| 47 |
$get_on_site_status = $dataSubject->hasData(); |
| 48 |
}else{ |
| 49 |
$output = $this->UserConsentModel->getAll($email); |
| 50 |
if (!empty($output)) { |
| 51 |
$get_on_site_status = 1; |
| 52 |
} |
| 53 |
} |
| 54 |
return apply_filters('gdpr/data-subject/has-data', $get_on_site_status, $email); |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Send the email with the link that allows data subject to authenticate |
| 59 |
* |
| 60 |
* @param $email |
| 61 |
*/ |
| 62 |
public function sendIdentificationEmail($email) |
| 63 |
{ |
| 64 |
$email = sanitize_email($email); |
| 65 |
$key = $this->generateKey($email); |
| 66 |
$privacyToolsPageUrl = gdpr('helpers')->getPrivacyToolsPageUrl(); |
| 67 |
$privacyToolsPageUrl = apply_filters('privacy_tools_gdprf_page_url',$privacyToolsPageUrl); |
| 68 |
$identificationUrl = add_query_arg([ |
| 69 |
'gdpr_key' => $key, |
| 70 |
'email' => $email, |
| 71 |
], $privacyToolsPageUrl); |
| 72 |
|
| 73 |
$siteName = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES); |
| 74 |
|
| 75 |
// todo: handle or log email sending errors |
| 76 |
gdpr('helpers')->mail( |
| 77 |
$email, |
| 78 |
__("Your personal data on", 'gdpr-framework') . ' ' . $siteName, |
| 79 |
gdpr('view')->render('email/identify-data-subject', compact('identificationUrl', 'siteName')), |
| 80 |
['Content-Type: text/html; charset=UTF-8'] |
| 81 |
); |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* Notify the email address that we do not store any data about them |
| 86 |
* |
| 87 |
* @param $email |
| 88 |
*/ |
| 89 |
public function sendNoDataFoundEmail($email) |
| 90 |
{ |
| 91 |
$email = sanitize_email($email); |
| 92 |
$siteName = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES); |
| 93 |
|
| 94 |
gdpr('helpers')->mail( |
| 95 |
$email, |
| 96 |
__("Your personal data on", 'gdpr-framework') . ' ' . $siteName, |
| 97 |
gdpr('view')->render('email/no-data', compact('siteName')), |
| 98 |
['Content-Type: text/html; charset=UTF-8'] |
| 99 |
); |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Check if the given key is valid for the given email |
| 104 |
* |
| 105 |
* @param $email |
| 106 |
* @param $key |
| 107 |
* @return bool |
| 108 |
*/ |
| 109 |
public function isKeyValid($email, $key) |
| 110 |
{ |
| 111 |
$email = sanitize_email($email); |
| 112 |
$keyData = $this->options->get("key_{$email}"); |
| 113 |
|
| 114 |
if (!$keyData) { |
| 115 |
// No key exists |
| 116 |
return false; |
| 117 |
} |
| 118 |
|
| 119 |
if (!isset($keyData['hashed-key']) || empty($keyData['hashed-key'])) { |
| 120 |
// There was an error saving the data to database |
| 121 |
return false; |
| 122 |
} |
| 123 |
|
| 124 |
if (!$this->validateKey($key, $keyData['hashed-key'])) { |
| 125 |
// Invalid key |
| 126 |
return false; |
| 127 |
} |
| 128 |
|
| 129 |
if ($keyData['valid-until'] < strtotime('now')) { |
| 130 |
// expired key |
| 131 |
return false; |
| 132 |
} |
| 133 |
|
| 134 |
// Double-check everything just to make sure we leave no errors in the code |
| 135 |
return ($this->validateKey($key, $keyData['hashed-key']) && $keyData['valid-until'] > strtotime('now')); |
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* Generate a secret key using the same functionality WP itself is using for Forgot Password requests |
| 140 |
* |
| 141 |
* @param $email |
| 142 |
*/ |
| 143 |
public function generateKey($email) |
| 144 |
{ |
| 145 |
$key = wp_generate_password(20, false); |
| 146 |
$this->saveKey($email, $key); |
| 147 |
|
| 148 |
return $key; |
| 149 |
} |
| 150 |
|
| 151 |
/** |
| 152 |
* Save key into the database along with the expiration timestamp |
| 153 |
* |
| 154 |
* @param $email |
| 155 |
* @param $key |
| 156 |
*/ |
| 157 |
protected function saveKey($email, $key) |
| 158 |
{ |
| 159 |
$email = sanitize_email($email); |
| 160 |
$this->options->set("key_{$email}", [ |
| 161 |
'email' => $email, |
| 162 |
'hashed-key' => $this->hashKey($key), |
| 163 |
'valid-until' => strtotime('+15 minutes'), |
| 164 |
]); |
| 165 |
} |
| 166 |
|
| 167 |
/** |
| 168 |
* @param $submittedKey |
| 169 |
* @param $storedKey |
| 170 |
*/ |
| 171 |
protected function validateKey($submittedKey, $storedKey) |
| 172 |
{ |
| 173 |
return $this->getHasher()->CheckPassword($submittedKey, $storedKey); |
| 174 |
} |
| 175 |
|
| 176 |
/** |
| 177 |
* Hash the key before saving to database to keep it hidden from the prying eyes of your sysadmin |
| 178 |
* |
| 179 |
* @param $key |
| 180 |
* @return bool|string |
| 181 |
*/ |
| 182 |
protected function hashKey($key) |
| 183 |
{ |
| 184 |
return $this->getHasher()->HashPassword($key); |
| 185 |
} |
| 186 |
|
| 187 |
/** |
| 188 |
* @return \PasswordHash |
| 189 |
*/ |
| 190 |
protected function getHasher() |
| 191 |
{ |
| 192 |
global $wp_hasher; |
| 193 |
if (empty($wp_hasher)) { |
| 194 |
require_once ABSPATH . WPINC . '/class-phpass.php'; |
| 195 |
$wp_hasher = new \PasswordHash(8, true); |
| 196 |
} |
| 197 |
|
| 198 |
return $wp_hasher; |
| 199 |
} |
| 200 |
} |
| 201 |
|