PluginProbe
The GDPR Framework By Data443 / 2.1.0
The GDPR Framework By Data443 v2.1.0
2.5.0 2.4.0 trunk 1.0.0 1.0.1 1.0.10 1.0.11 1.0.12 1.0.13 1.0.14 1.0.3 1.0.33 1.0.34 1.0.35 1.0.36 1.0.37 1.0.39 1.0.4 1.0.40 1.0.41 1.0.42 1.0.43 1.0.44 1.0.45 1.0.46 All 41 releases
gdpr-framework / src / DataSubject / DataSubjectAuthenticator.php

DataSubjectAuthenticator.php in The GDPR Framework By Data443 2.1.0, at src/DataSubject/DataSubjectAuthenticator.php

136 lines 3.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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