| 1 |
<?php |
| 2 |
|
| 3 |
namespace Codelight\GDPR\Components\Consent; |
| 4 |
|
| 5 |
use DateTime; |
| 6 |
|
| 7 |
/** |
| 8 |
* Class UserConsentModel |
| 9 |
* |
| 10 |
* @package Codelight\GDPR\Components\Consent |
| 11 |
*/ |
| 12 |
class UserConsentModel |
| 13 |
{ |
| 14 |
/* @var string */ |
| 15 |
public $tableName; |
| 16 |
|
| 17 |
/* @var string */ |
| 18 |
public $logtableName; |
| 19 |
|
| 20 |
/* @var string */ |
| 21 |
public $version = GDPR_FRAMEWORK_VERSION; |
| 22 |
|
| 23 |
/* @var string */ |
| 24 |
public $primaryKey = 'id'; |
| 25 |
|
| 26 |
/** |
| 27 |
* UserConsentModel constructor. |
| 28 |
*/ |
| 29 |
public function __construct() |
| 30 |
{ |
| 31 |
$this->setTableName(); |
| 32 |
$this->setUserLogTableName(); |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* Set the table name with wpdb-s prefix |
| 37 |
*/ |
| 38 |
protected function setTableName() |
| 39 |
{ |
| 40 |
global $wpdb; |
| 41 |
$this->tableName = $wpdb->prefix . 'gdpr_consent'; |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* Set the table name with wpdb-s prefix |
| 46 |
*/ |
| 47 |
protected function setUserLogTableName() |
| 48 |
{ |
| 49 |
global $wpdb; |
| 50 |
$this->logtableName = $wpdb->prefix . 'gdpr_userlogs'; |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Check if a user has given a consent |
| 55 |
* |
| 56 |
* @param $email |
| 57 |
* @param $consent |
| 58 |
* @return int |
| 59 |
*/ |
| 60 |
public function given($email, $consent) |
| 61 |
{ |
| 62 |
global $wpdb; |
| 63 |
|
| 64 |
// FRAM-140 fix invalid access: should be '$consent' not '$consent->consent' |
| 65 |
return count( $wpdb->get_results( $wpdb->prepare( |
| 66 |
"SELECT * FROM {$this->tableName} WHERE email = %s AND consent = %s AND status = 1;", |
| 67 |
$email, |
| 68 |
$consent |
| 69 |
) ) ); |
| 70 |
|
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Check if a user has withdrawn a consent |
| 75 |
* |
| 76 |
* @param $email |
| 77 |
* @param $consent |
| 78 |
* @return int |
| 79 |
*/ |
| 80 |
public function withdrawn($email, $consent) |
| 81 |
{ |
| 82 |
global $wpdb; |
| 83 |
|
| 84 |
return count($wpdb->get_results($wpdb->prepare( |
| 85 |
"SELECT * FROM {$this->tableName} WHERE email = %s AND consent = %s AND status = 0;", |
| 86 |
$email, |
| 87 |
$consent |
| 88 |
))); |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* Check if a consent exists in the database |
| 93 |
* |
| 94 |
* @param $email |
| 95 |
* @param $consent |
| 96 |
* @return array|null|object |
| 97 |
*/ |
| 98 |
public function exists($email, $consent) |
| 99 |
{ |
| 100 |
global $wpdb; |
| 101 |
|
| 102 |
return count($wpdb->get_results($wpdb->prepare( |
| 103 |
"SELECT * FROM {$this->tableName} WHERE email = %s AND consent = %s;", |
| 104 |
$email, |
| 105 |
$consent |
| 106 |
))); |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* Set a consent to 'given' |
| 111 |
* |
| 112 |
* @param $email |
| 113 |
* @param $consent |
| 114 |
* @param $status |
| 115 |
* @return false|int |
| 116 |
*/ |
| 117 |
public function give($email, $consent, $valid_until) |
| 118 |
{ |
| 119 |
$this->set($email, $consent, 1, $valid_until); |
| 120 |
} |
| 121 |
|
| 122 |
/** |
| 123 |
* Set a consent to 'withdrawn' |
| 124 |
* |
| 125 |
* @param $email |
| 126 |
* @param $consent |
| 127 |
* @param $status |
| 128 |
* @return false|int |
| 129 |
*/ |
| 130 |
public function withdraw($email, $consent) |
| 131 |
{ |
| 132 |
$this->set($email, $consent, 0, null); |
| 133 |
} |
| 134 |
|
| 135 |
/** |
| 136 |
* Set a consent to 'given' or 'withdrawn' |
| 137 |
* |
| 138 |
* @param $email |
| 139 |
* @param $consent |
| 140 |
* @param $status |
| 141 |
* @return false|int |
| 142 |
*/ |
| 143 |
protected function set($email, $consent, $status, $valid_until, $version = 1) |
| 144 |
{ |
| 145 |
global $wpdb; |
| 146 |
|
| 147 |
if( $valid_until !== null && $valid_until !== '' ) { |
| 148 |
$future_date = new DateTime( current_time( 'mysql' ) ); |
| 149 |
date_add( $future_date, date_interval_create_from_date_string( $valid_until . 'months' ) ); |
| 150 |
$future_date = date_format($future_date, 'Y-m-d H:i:s'); |
| 151 |
}else{ |
| 152 |
$future_date = '9999-12-31 23:59:59'; |
| 153 |
} |
| 154 |
|
| 155 |
if ($this->exists($email, $consent)) { |
| 156 |
return $wpdb->update( |
| 157 |
$this->tableName, |
| 158 |
[ |
| 159 |
'version' => $version, |
| 160 |
'status' => $status, |
| 161 |
'updated_at' => current_time( 'mysql' ), |
| 162 |
'ip' => $_SERVER['REMOTE_ADDR'], |
| 163 |
'valid_until' => $future_date, |
| 164 |
], |
| 165 |
[ |
| 166 |
'email' => $email, |
| 167 |
'consent' => $consent, |
| 168 |
] |
| 169 |
); |
| 170 |
} else { |
| 171 |
return $wpdb->insert( |
| 172 |
$this->tableName, |
| 173 |
[ |
| 174 |
'email' => $email, |
| 175 |
'version' => $version, |
| 176 |
'consent' => $consent, |
| 177 |
'status' => $status, |
| 178 |
'updated_at' => current_time( 'mysql' ), |
| 179 |
'ip' => $_SERVER['REMOTE_ADDR'], |
| 180 |
'valid_until' => $future_date, |
| 181 |
] |
| 182 |
); |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
/** |
| 187 |
* Set a userlog |
| 188 |
* |
| 189 |
* @param $email |
| 190 |
* @param $userlog |
| 191 |
*/ |
| 192 |
public function savelog($email, $userlog) |
| 193 |
{ |
| 194 |
|
| 195 |
$this->savelog_gdpr($email, $userlog); |
| 196 |
} |
| 197 |
|
| 198 |
/** |
| 199 |
* Set a userlog to show previous data |
| 200 |
* |
| 201 |
* @param $userid |
| 202 |
* @param $userlog |
| 203 |
*/ |
| 204 |
protected function savelog_gdpr($user_id,$userlog) |
| 205 |
{ |
| 206 |
global $wpdb; |
| 207 |
if (!empty($user_id) && !empty($userlog)) { |
| 208 |
$sa= $wpdb->insert( |
| 209 |
$this->logtableName, |
| 210 |
[ |
| 211 |
'user_id' => $user_id, |
| 212 |
'userlog' => $userlog, |
| 213 |
'updated_at' => current_time( 'mysql' ), |
| 214 |
'ip' => $_SERVER['REMOTE_ADDR'], |
| 215 |
] |
| 216 |
); |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
/** |
| 221 |
* Get all consent given by data subject |
| 222 |
* |
| 223 |
* @param $email |
| 224 |
*/ |
| 225 |
public function getAll($email) |
| 226 |
{ |
| 227 |
global $wpdb; |
| 228 |
|
| 229 |
/** |
| 230 |
* Workaround to an issue with array_column in PHP5.6 - thanks @paulnewson |
| 231 |
*/ |
| 232 |
if (version_compare(PHP_VERSION, '7') >= 0) { |
| 233 |
return $wpdb->get_results( $wpdb->prepare( |
| 234 |
"SELECT * FROM {$this->tableName} WHERE email = %s and status = 1;", |
| 235 |
$email |
| 236 |
)); |
| 237 |
} else { |
| 238 |
return json_decode( json_encode( $wpdb->get_results($wpdb->prepare( |
| 239 |
"SELECT * FROM {$this->tableName} WHERE email = %s and status = 1;", |
| 240 |
$email |
| 241 |
))), true); |
| 242 |
} |
| 243 |
} |
| 244 |
|
| 245 |
|
| 246 |
/** |
| 247 |
* Get all consent given by data subject with data |
| 248 |
* |
| 249 |
* @param $email |
| 250 |
*/ |
| 251 |
public function getAllwithdetails($email) |
| 252 |
{ |
| 253 |
global $wpdb; |
| 254 |
|
| 255 |
/** |
| 256 |
* Workaround to an issue with array_column in PHP5.6 - thanks @paulnewson |
| 257 |
*/ |
| 258 |
if (version_compare(PHP_VERSION, '7') >= 0) { |
| 259 |
return $wpdb->get_results($wpdb->prepare( |
| 260 |
"SELECT * FROM {$this->tableName} WHERE email = %s and status = 1;", |
| 261 |
$email |
| 262 |
)); |
| 263 |
} else { |
| 264 |
return json_decode(json_encode($wpdb->get_results($wpdb->prepare( |
| 265 |
"SELECT * FROM {$this->tableName} WHERE email = %s and status = 1;", |
| 266 |
$email |
| 267 |
))), true); |
| 268 |
} |
| 269 |
} |
| 270 |
|
| 271 |
/** |
| 272 |
* Get all consent given by data subject |
| 273 |
* |
| 274 |
* @param $email |
| 275 |
*/ |
| 276 |
public function getuserlogs($userid) |
| 277 |
{ |
| 278 |
global $wpdb; |
| 279 |
/** |
| 280 |
* Workaround to an issue with array_column in PHP5.6 - thanks @paulnewson |
| 281 |
*/ |
| 282 |
if (version_compare(PHP_VERSION, '7') >= 0) { |
| 283 |
return $wpdb->get_results($wpdb->prepare( |
| 284 |
"SELECT * FROM {$this->logtableName} WHERE user_id = %s;", |
| 285 |
$userid |
| 286 |
)); |
| 287 |
} else { |
| 288 |
return json_decode(json_encode($wpdb->get_results($wpdb->prepare( |
| 289 |
"SELECT * FROM {$this->logtableName} WHERE user_id = %s;", |
| 290 |
$userid |
| 291 |
))), true); |
| 292 |
} |
| 293 |
} |
| 294 |
|
| 295 |
/** |
| 296 |
* Remove a log row from the database |
| 297 |
* |
| 298 |
* @param $id |
| 299 |
*/ |
| 300 |
public function deletelog($id) |
| 301 |
{ |
| 302 |
global $wpdb; |
| 303 |
|
| 304 |
return $wpdb->delete( |
| 305 |
$this->logtableName, |
| 306 |
[ |
| 307 |
'user_id' => $id, |
| 308 |
] |
| 309 |
); |
| 310 |
} |
| 311 |
|
| 312 |
/** |
| 313 |
* Remove a consent row from the database |
| 314 |
* |
| 315 |
* @param $email |
| 316 |
* @param $consent |
| 317 |
* @return false|int |
| 318 |
*/ |
| 319 |
public function delete($email, $consent) |
| 320 |
{ |
| 321 |
global $wpdb; |
| 322 |
|
| 323 |
return $wpdb->delete( |
| 324 |
$this->tableName, |
| 325 |
[ |
| 326 |
'email' => $email, |
| 327 |
'consent' => $consent, |
| 328 |
] |
| 329 |
); |
| 330 |
} |
| 331 |
|
| 332 |
/** |
| 333 |
* Withdraw consent and anonymize data subject's email |
| 334 |
* |
| 335 |
* @param $email |
| 336 |
* @param $consent |
| 337 |
* @param $anonymizedId |
| 338 |
* @return false|int |
| 339 |
*/ |
| 340 |
public function anonymize($email, $consent, $anonymizedId) |
| 341 |
{ |
| 342 |
global $wpdb; |
| 343 |
|
| 344 |
if ($this->exists($email, $consent)) { |
| 345 |
return $wpdb->update( |
| 346 |
$this->tableName, |
| 347 |
[ |
| 348 |
'email' => $anonymizedId, |
| 349 |
'status' => 0, |
| 350 |
'updated_at' => current_time( 'mysql' ), |
| 351 |
'ip' => $_SERVER['REMOTE_ADDR'], |
| 352 |
], |
| 353 |
[ |
| 354 |
'email' => $email, |
| 355 |
'consent' => $consent, |
| 356 |
] |
| 357 |
); |
| 358 |
} |
| 359 |
} |
| 360 |
|
| 361 |
/** |
| 362 |
* Get columns and formats |
| 363 |
*/ |
| 364 |
public function getColumns() |
| 365 |
{ |
| 366 |
return [ |
| 367 |
'id' => '%d', |
| 368 |
'version' => '%d', |
| 369 |
'email' => '%s', |
| 370 |
'consent' => '%s', |
| 371 |
'status' => '%d', |
| 372 |
'updated_at' => '%s', |
| 373 |
'ip' => '%s', |
| 374 |
'valid_until' => '%s', |
| 375 |
]; |
| 376 |
} |
| 377 |
|
| 378 |
/** |
| 379 |
* Get default column values |
| 380 |
*/ |
| 381 |
public function getColumnDefaults() |
| 382 |
{ |
| 383 |
return [ |
| 384 |
'id' => '', |
| 385 |
'version' => 1, |
| 386 |
'email' => '', |
| 387 |
'consent' => '', |
| 388 |
'status' => '', |
| 389 |
'updated_at' => '', |
| 390 |
'ip' => '', |
| 391 |
'valid_until' => '', |
| 392 |
]; |
| 393 |
} |
| 394 |
|
| 395 |
/** |
| 396 |
* Create the table |
| 397 |
*/ |
| 398 |
public function createTable() |
| 399 |
{ |
| 400 |
|
| 401 |
require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); |
| 402 |
$sql = "CREATE TABLE " . $this->tableName . " ( |
| 403 |
id bigint(20) NOT NULL AUTO_INCREMENT, |
| 404 |
version int NOT NULL, |
| 405 |
email varchar(64) NOT NULL, |
| 406 |
consent varchar(128) NOT NULL, |
| 407 |
status tinyint NOT NULL, |
| 408 |
updated_at TIMESTAMP NULL, |
| 409 |
ip varchar(64) NOT NULL, |
| 410 |
valid_until DATETIME NOT NULL, |
| 411 |
PRIMARY KEY (id) |
| 412 |
) CHARACTER SET utf8 COLLATE utf8_general_ci;"; |
| 413 |
dbDelta($sql); |
| 414 |
update_option($this->tableName . '_db_version', $this->version); |
| 415 |
} |
| 416 |
/** |
| 417 |
* create table to user logs |
| 418 |
*/ |
| 419 |
public function createUserTable() |
| 420 |
{ |
| 421 |
require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); |
| 422 |
$sql = "CREATE TABLE " . $this->logtableName . " ( |
| 423 |
id bigint(20) NOT NULL AUTO_INCREMENT, |
| 424 |
user_id int NOT NULL, |
| 425 |
userlog varchar(4000) NOT NULL, |
| 426 |
updated_at TIMESTAMP NULL, |
| 427 |
ip varchar(64) NOT NULL, |
| 428 |
PRIMARY KEY (id) |
| 429 |
) CHARACTER SET utf8 COLLATE utf8_general_ci;"; |
| 430 |
dbDelta($sql); |
| 431 |
update_option($this->logtableName . '_db_version', $this->version); |
| 432 |
} |
| 433 |
} |