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