AJAX
3 years ago
Migrations
3 years ago
Models
3 years ago
Queries
3 years ago
Tables
3 years ago
Utils
3 years ago
Campaign_Builder.php
3 years ago
Capability_Manager.php
3 years ago
Chart.php
3 years ago
Chart_Geo.php
3 years ago
Chart_SVG.php
3 years ago
Current_Resource.php
3 years ago
Dashboard_Options.php
3 years ago
Dashboard_Widget.php
3 years ago
Email_Reports.php
3 years ago
Filters.php
3 years ago
Geo_Database.php
3 years ago
Geo_Database_Download_Job.php
3 years ago
Health_Check.php
3 years ago
Independent_Analytics.php
3 years ago
Known_Referrers.php
3 years ago
PDF.php
3 years ago
Query.php
3 years ago
Quick_Stats.php
3 years ago
REST_API.php
3 years ago
Real_Time.php
3 years ago
Settings.php
3 years ago
Track_Resource_Changes.php
3 years ago
View_Counter.php
3 years ago
WP_Option_Cache_Bust.php
3 years ago
REST_API.php
226 lines
| 1 | <?php |
| 2 | |
| 3 | namespace IAWP; |
| 4 | |
| 5 | use IAWP\Models\Visitor; |
| 6 | use IAWP\Queries\View; |
| 7 | use IAWP\Utils\Request; |
| 8 | use IAWP\Utils\Salt; |
| 9 | use IAWP\Utils\Security; |
| 10 | use IAWP\Utils\String_Util; |
| 11 | |
| 12 | class REST_API |
| 13 | { |
| 14 | public function __construct() |
| 15 | { |
| 16 | add_action('wp_footer', [$this, 'echo_tracking_script']); |
| 17 | add_action('rest_api_init', [$this, 'register_rest_api']); |
| 18 | // Support for PDF Viewer by Themencode |
| 19 | add_action('tnc_pvfw_viewer_head', [$this, 'echo_tracking_script']); |
| 20 | // Support for Coming Soon and Maintenance by Colorlib |
| 21 | add_action('ccsm_header', [$this, 'echo_tracking_script']); |
| 22 | } |
| 23 | |
| 24 | public function echo_tracking_script() |
| 25 | { |
| 26 | if (!get_option('iawp_track_authenticated_users') && is_user_logged_in()) { |
| 27 | // Todo - Can we clear the cache plugins to make sure that the pages so tracking code as user logs in & out? |
| 28 | return; |
| 29 | } |
| 30 | |
| 31 | if ($this->block_user_role()) { |
| 32 | return; |
| 33 | } |
| 34 | |
| 35 | // Don't track post or page previews |
| 36 | if (is_preview()) { |
| 37 | return; |
| 38 | } |
| 39 | |
| 40 | $payload = []; |
| 41 | |
| 42 | $current_resource = Current_Resource::get_resource(); |
| 43 | |
| 44 | if (is_null($current_resource)) { |
| 45 | return; |
| 46 | } |
| 47 | |
| 48 | $payload['resource'] = $current_resource->type(); |
| 49 | if ($current_resource->has_meta()) { |
| 50 | $payload[$current_resource->meta_key()] = $current_resource->meta_value(); |
| 51 | } |
| 52 | |
| 53 | $payload['page'] = max(1, get_query_var('paged')); |
| 54 | |
| 55 | $valid_data = !empty($payload); |
| 56 | if ($valid_data) { |
| 57 | $data = [ |
| 58 | 'payload' => $payload, |
| 59 | ]; |
| 60 | $data['signature'] = md5(Salt::request_payload_salt() . json_encode($data['payload'])); |
| 61 | $url = get_rest_url() . 'iawp/search'; ?> |
| 62 | <script> |
| 63 | (function () { |
| 64 | document.addEventListener("DOMContentLoaded", function (e) { |
| 65 | if (document.hasOwnProperty("visibilityState") && document.visibilityState === "prerender") { |
| 66 | return; |
| 67 | } |
| 68 | |
| 69 | if (navigator.webdriver || /bot|crawler|spider|crawling/i.test(navigator.userAgent)) { |
| 70 | return; |
| 71 | } |
| 72 | |
| 73 | let referrer_url = null; |
| 74 | |
| 75 | if (typeof document.referrer === 'string' && document.referrer.length > 0) { |
| 76 | referrer_url = document.referrer; |
| 77 | } |
| 78 | |
| 79 | const params = location.search.slice(1).split('&').reduce((acc, s) => { |
| 80 | const [k, v] = s.split('=') |
| 81 | return Object.assign(acc, {[k]: v}) |
| 82 | }, {}) |
| 83 | |
| 84 | const url = "<?= $url; ?>" |
| 85 | const body = { |
| 86 | referrer_url, |
| 87 | utm_source: params.utm_source, |
| 88 | utm_medium: params.utm_medium, |
| 89 | utm_campaign: params.utm_campaign, |
| 90 | utm_term: params.utm_term, |
| 91 | utm_content: params.utm_content, |
| 92 | ...<?= json_encode($data); ?> |
| 93 | } |
| 94 | const xhr = new XMLHttpRequest() |
| 95 | xhr.open("POST", url, true) |
| 96 | xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8") |
| 97 | xhr.send(JSON.stringify(body)) |
| 98 | }) |
| 99 | })(); |
| 100 | </script> |
| 101 | <?php |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | public function register_rest_api() |
| 106 | { |
| 107 | register_rest_route('iawp', '/search', [ |
| 108 | 'methods' => 'POST', |
| 109 | 'callback' => [$this, 'track_view'], |
| 110 | 'permission_callback' => function () { |
| 111 | return true; |
| 112 | }, |
| 113 | ]); |
| 114 | } |
| 115 | |
| 116 | public function track_view($request) |
| 117 | { |
| 118 | Migrations\Migration::create_or_migrate(); |
| 119 | |
| 120 | if (Migrations\Migration::is_migrating()) { |
| 121 | return; |
| 122 | } |
| 123 | |
| 124 | if ($this->blocked_ip(Request::ip())) { |
| 125 | return; |
| 126 | } |
| 127 | |
| 128 | $visitor = new Visitor(Request::ip(), Request::user_agent()); |
| 129 | $signature = md5(Salt::request_payload_salt() . json_encode($request['payload'])); |
| 130 | $campaign = []; |
| 131 | |
| 132 | if (iawp_is_pro()) { |
| 133 | $campaign = [ |
| 134 | 'utm_source' => $this->decode_or_nullify($request['utm_source']), |
| 135 | 'utm_medium' => $this->decode_or_nullify($request['utm_medium']), |
| 136 | 'utm_campaign' => $this->decode_or_nullify($request['utm_campaign']), |
| 137 | 'utm_term' => $this->decode_or_nullify($request['utm_term']), |
| 138 | 'utm_content' => $this->decode_or_nullify($request['utm_content']), |
| 139 | ]; |
| 140 | } |
| 141 | |
| 142 | if ($signature == $request['signature']) { |
| 143 | new View($request['payload'], $request['referrer_url'], $visitor, $campaign); |
| 144 | |
| 145 | return new \WP_REST_Response(['success' => true], 200, ['X-IAWP' => 'IAWP']); |
| 146 | } else { |
| 147 | return new \WP_REST_Response(['success' => false], 200, ['X-IAWP' => 'IAWP']); |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | private function decode_or_nullify($string) |
| 152 | { |
| 153 | if (!isset($string)) { |
| 154 | return null; |
| 155 | } |
| 156 | |
| 157 | $safe_string = trim(urldecode($string)); |
| 158 | $safe_string = str_replace('+', ' ', $safe_string); |
| 159 | $safe_string = Security::string($safe_string); |
| 160 | |
| 161 | if (strlen($safe_string) === 0) { |
| 162 | return null; |
| 163 | } |
| 164 | |
| 165 | return $safe_string; |
| 166 | } |
| 167 | |
| 168 | private function blocked_ip($visitor_ip) |
| 169 | { |
| 170 | $blocked_ips = iawp()->get_option('iawp_blocked_ips', []); |
| 171 | |
| 172 | if (count($blocked_ips) == 0) { |
| 173 | return false; |
| 174 | } |
| 175 | if (in_array($visitor_ip, $blocked_ips)) { |
| 176 | return true; |
| 177 | } |
| 178 | |
| 179 | $wildcard_ips = []; |
| 180 | foreach ($blocked_ips as $blocked_ip) { |
| 181 | if (String_Util::str_contains($blocked_ip, '*')) { |
| 182 | $wildcard_ips[] = $blocked_ip; |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | if (count($wildcard_ips) == 0) { |
| 187 | return false; |
| 188 | } |
| 189 | |
| 190 | $visitor_parts = explode('.', $visitor_ip); |
| 191 | $goal = count($visitor_parts); |
| 192 | foreach ($wildcard_ips as $blocked_ip) { |
| 193 | $blocked_parts = explode('.', $blocked_ip); |
| 194 | $matches = 0; |
| 195 | for ($i = 0; $i < count($visitor_parts); $i++) { |
| 196 | if (!array_key_exists($i, $blocked_parts)) { |
| 197 | $matches++; |
| 198 | } elseif ($visitor_parts[$i] == $blocked_parts[$i] || $blocked_parts[$i] == '*') { |
| 199 | $matches++; |
| 200 | continue; |
| 201 | } else { |
| 202 | break; |
| 203 | } |
| 204 | } |
| 205 | if ($matches == $goal) { |
| 206 | return true; |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | return false; |
| 211 | } |
| 212 | |
| 213 | private function block_user_role(): bool |
| 214 | { |
| 215 | $blocked_roles = iawp()->get_option('iawp_blocked_roles', []); |
| 216 | |
| 217 | foreach (wp_get_current_user()->roles as $visitor_role) { |
| 218 | if (in_array($visitor_role, $blocked_roles)) { |
| 219 | return true; |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | return false; |
| 224 | } |
| 225 | } |
| 226 |