PluginProbe
The GDPR Framework By Data443 / 2.5.0
The GDPR Framework By Data443 v2.5.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 / Router.php

Router.php in The GDPR Framework By Data443 2.5.0, at src/Router.php

169 lines 5.0 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;
4
5 if ( ! defined( 'ABSPATH' ) ) exit;
6
7 use Codelight\GDPR\DataSubject\DataSubjectAuthenticator;
8
9 /**
10 * Handles automatically identifying context and triggering actions based on $_REQUEST['gdpr_action']
11 *
12 * Class Router
13 *
14 * @package Codelight\GDPR
15 */
16 class Router
17 {
18 /* @var DataSubjectAuthenticator $authenticator */
19 protected $authenticator;
20
21 /**
22 * Router constructor.
23 *
24 * @param DataSubjectAuthenticator $authenticator
25 */
26 public function __construct(DataSubjectAuthenticator $authenticator)
27 {
28 $this->authenticator = $authenticator;
29
30 // Routing happens at priority 20 to allow other 'init' actions to complete before
31 add_action('init', [$this, 'routeFrontendRequest'], 20);
32 add_action('admin_init', [$this, 'routeAdminRequest'], 20);
33 }
34
35 /**
36 * Get and sanitize the action parameter
37 *
38 * @return bool|mixed
39 */
40 protected function getAction()
41 {
42 if (!isset($_REQUEST['gdpr_action'])) {
43 return false;
44 }
45
46 // Simple sanitization: allowed chars are alphanumeric, dash, underscore and forward slash.
47 return preg_replace("/[^a-zA-Z0-9_\-\/]/", "", sanitize_key($_REQUEST['gdpr_action']));
48 }
49
50 /**
51 * Detect and trigger proper action in front-end
52 *
53 * @param $action
54 */
55 public function routeFrontendRequest()
56 {
57 // Since the 'init' hooks runs in both admin and non-admin requests, double-check where we are
58 if (is_admin()) {
59 return;
60 }
61
62 // Handle identification by email
63 $this->authenticator->identify();
64
65 $action = $this->getAction();
66 $nonce = isset($_REQUEST['gdpr_nonce']) ? sanitize_key($_REQUEST['gdpr_nonce']) : null;
67
68 if (!$action) {
69 return;
70 }
71
72 $dataSubject = $this->authenticator->authenticate();
73
74 if ($dataSubject) {
75 $tag = "gdpr/frontend/privacy-tools-page/action/{$action}";
76 if (wp_verify_nonce($nonce, $tag)) {
77 $key = isset($_REQUEST['gdpr_key']) ? sanitize_key($_REQUEST['gdpr_key']) : null;
78 do_action($tag, $dataSubject, $key);
79 } else {
80 wp_die(
81 sprintf(
82 __('Nonce error for action "%s". Please go back and try again!', 'gdpr-framework'),
83 esc_html($action)
84 )
85 );
86 }
87 } else {
88 $tag = "gdpr/frontend/action/{$action}";
89 if (wp_verify_nonce($nonce, $tag)) {
90 do_action($tag);
91 } else {
92 wp_die(
93 sprintf(
94 __('Nonce error for action "%s". Please go back and try again!', 'gdpr-framework'),
95 esc_html($action)
96 )
97 );
98 }
99 }
100 }
101
102 /**
103 * Detect and trigger proper action in admin
104 *
105 * @param $action
106 */
107 public function routeAdminRequest()
108 {
109 $action = $this->getAction();
110 $nonce = isset($_REQUEST['gdpr_nonce']) ? sanitize_key($_REQUEST['gdpr_nonce']) : null;
111
112 if (!$action) {
113 return;
114 }
115
116 if (isset($_GET['page']) && 'gdpr-profile' === sanitize_key($_GET['page'])) {
117
118 $dataSubject = $this->authenticator->authenticate();
119 if ($dataSubject) {
120 $tag = "gdpr/dashboard/privacy-tools/action/{$action}";
121
122 if (wp_verify_nonce($nonce, $tag)) {
123 do_action($tag, $dataSubject);
124 } else {
125 wp_die(
126 sprintf(
127 __('Nonce error for action "%s". Please go back and try again!', 'gdpr-framework'),
128 esc_html($action)
129 )
130 );
131 }
132 }
133 } else {
134 if ($this->checkAdminPermissions()) {
135
136 $tag = "gdpr/admin/action/{$action}";
137
138 if (wp_verify_nonce($nonce, $tag)) {
139 do_action($tag);
140 } else {
141 wp_die(
142 sprintf(
143 __('Nonce error for action "%s". Please go back and try again!', 'gdpr-framework'),
144 esc_html($action)
145 )
146 );
147 }
148 } else {
149 wp_die(
150 sprintf(
151 _x('You do not have the required permissions to perform this action!', '(Admin)', 'gdpr-framework'),
152 esc_html($action)
153 )
154 );
155 }
156 }
157 }
158
159 /**
160 * Check if the current user has the correct capability to perform an admin action
161 *
162 * @return bool
163 */
164 protected function checkAdminPermissions()
165 {
166 return current_user_can(apply_filters('gdpr/capability', 'manage_options'));
167 }
168 }
169