PluginProbe ʕ •ᴥ•ʔ
Independent Analytics – WordPress Analytics Plugin / 2.3.1
Independent Analytics – WordPress Analytics Plugin v2.3.1
2.15.0 2.14.10 trunk 1.1 1.10 1.10.1 1.11 1.12 1.13 1.14 1.15 1.16 1.17 1.17.1 1.17.2 1.17.3 1.17.4 1.18 1.18.1 1.19.0 1.19.1 1.2 1.20.0 1.21.0 1.22.0 1.22.1 1.23.0 1.23.1 1.24.0 1.24.1 1.25.0 1.25.1 1.26.0 1.27.0 1.28.0 1.28.1 1.28.2 1.28.3 1.29.0 1.3 1.30.0 1.30.1 1.4 1.5 1.6 1.7 1.8 1.9 2.0.0 2.0.1 2.1.4 2.1.5 2.1.6 2.10.0 2.10.1 2.10.2 2.10.3 2.10.4 2.11.0 2.11.1 2.11.10 2.11.2 2.11.3 2.11.4 2.11.5 2.11.6 2.11.7 2.11.8 2.11.9 2.12.0 2.12.1 2.12.2 2.13.1 2.13.2 2.13.5 2.13.6 2.14.0 2.14.1 2.14.2 2.14.4 2.14.6 2.14.7 2.14.8 2.14.9 2.2.0 2.2.1 2.3.1 2.3.2 2.4.2 2.4.3 2.5.0 2.5.1 2.6.0 2.6.1 2.6.2 2.6.3 2.6.4 2.7.0 2.7.1 2.7.2 2.7.3 2.8.2 2.8.3 2.8.4 2.8.5 2.8.6 2.8.7 2.8.8 2.8.9 2.9.2 2.9.3 2.9.4 2.9.5 2.9.6 2.9.7
independent-analytics / IAWP / REST_API.php
independent-analytics / IAWP Last commit date
AJAX 2 years ago Admin_Page 2 years ago Date_Range 2 years ago Filter_Lists 2 years ago Interval 2 years ago Menu_Bar_Stats 2 years ago Migrations 2 years ago Models 2 years ago Public_API 2 years ago Rows 2 years ago Statistics 2 years ago Tables 2 years ago Utils 2 years ago Campaign_Builder.php 2 years ago Capability_Manager.php 2 years ago Chart.php 2 years ago Chart_Geo.php 2 years ago Cron_Manager.php 2 years ago Current_Traffic_Finder.php 2 years ago Dashboard_Options.php 2 years ago Dashboard_Widget.php 2 years ago Database.php 2 years ago Database_Manager.php 2 years ago Email_Chart.php 2 years ago Email_Reports.php 2 years ago Empty_Report_Option.php 2 years ago Env.php 2 years ago Filters.php 2 years ago Geo_Database_Background_Job.php 2 years ago Geo_Database_Manager.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 2 years ago Independent_Analytics.php 2 years ago Interrupt.php 2 years ago Known_Referrers.php 2 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 Report.php 2 years ago Report_Finder.php 2 years ago Report_Options_Parser.php 2 years ago Resource_Identifier.php 2 years ago Settings.php 2 years ago Sort_Configuration.php 2 years ago Track_Resource_Changes.php 2 years ago View.php 2 years ago View_Counter.php 2 years ago Visitors_Over_Time_Finder.php 2 years ago WP_Option_Cache_Bust.php 2 years ago WooCommerce_Order.php 2 years ago WooCommerce_Referrer_Meta_Box.php 2 years ago
REST_API.php
216 lines
1 <?php
2
3 namespace IAWP;
4
5 use IAWP\Models\Visitor;
6 use IAWP\Utils\Device;
7 use IAWP\Utils\Request;
8 use IAWP\Utils\Salt;
9 use IAWP\Utils\Security;
10 use IAWP\Utils\String_Util;
11 use IAWP\Utils\URL;
12 /** @internal */
13 class REST_API
14 {
15 public function __construct()
16 {
17 \add_action('wp_footer', [$this, 'echo_tracking_script']);
18 \add_action('rest_api_init', [$this, 'register_rest_api']);
19 // Support for PDF Viewer by Themencode (free and pro versions)
20 \add_action('tnc_pvfw_viewer_head', [$this, 'echo_tracking_script']);
21 \add_action('tnc_pvfw_head', [$this, 'echo_tracking_script']);
22 // Support for Coming Soon and Maintenance by Colorlib
23 \add_action('ccsm_header', [$this, 'echo_tracking_script']);
24 }
25 public function echo_tracking_script()
26 {
27 if (!\get_option('iawp_track_authenticated_users') && \is_user_logged_in()) {
28 // Todo - Can we clear the cache plugins to make sure that the pages so tracking code as user logs in & out?
29 return;
30 }
31 if ($this->block_user_role()) {
32 return;
33 }
34 // Don't track post or page previews
35 if (\is_preview()) {
36 return;
37 }
38 $payload = [];
39 $current_resource = \IAWP\Resource_Identifier::for_resource_being_viewed();
40 if (\is_null($current_resource)) {
41 return;
42 }
43 $payload['resource'] = $current_resource->type();
44 if ($current_resource->has_meta()) {
45 $payload[$current_resource->meta_key()] = $current_resource->meta_value();
46 }
47 $payload['page'] = \max(1, \get_query_var('paged'));
48 $data = ['payload' => $payload];
49 $data['signature'] = \md5(Salt::request_payload_salt() . \json_encode($data['payload']));
50 $url = \get_rest_url() . 'iawp/search';
51 ?>
52 <script>
53 (function () {
54 document.addEventListener("DOMContentLoaded", function (e) {
55 if (document.hasOwnProperty("visibilityState") && document.visibilityState === "prerender") {
56 return;
57 }
58
59 <?php
60 if (!\defined('IAWP_TESTING')) {
61 ?>
62 if (navigator.webdriver || /bot|crawler|spider|crawling|semrushbot|chrome-lighthouse/i.test(navigator.userAgent)) {
63 return;
64 }
65 <?php
66 }
67 ?>
68
69 let referrer_url = null;
70
71 if (typeof document.referrer === 'string' && document.referrer.length > 0) {
72 referrer_url = document.referrer;
73 }
74
75 const params = location.search.slice(1).split('&').reduce((acc, s) => {
76 const [k, v] = s.split('=')
77 return Object.assign(acc, {[k]: v})
78 }, {})
79
80 const url = "<?php
81 echo $url;
82 ?>"
83 const body = {
84 referrer_url,
85 utm_source: params.utm_source,
86 utm_medium: params.utm_medium,
87 utm_campaign: params.utm_campaign,
88 utm_term: params.utm_term,
89 utm_content: params.utm_content,
90 gclid: params.gclid,
91 ...<?php
92 echo \json_encode($data);
93 ?>
94 }
95 const xhr = new XMLHttpRequest()
96 xhr.open("POST", url, true)
97 xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8")
98 xhr.send(JSON.stringify(body))
99 })
100 })();
101 </script>
102 <?php
103 }
104 public function register_rest_api()
105 {
106 \register_rest_route('iawp', '/search', ['methods' => 'POST', 'callback' => [$this, 'track_view'], 'permission_callback' => function () {
107 return \true;
108 }]);
109 }
110 public function track_view($request)
111 {
112 if (Device::getInstance()->is_bot() && !\defined('IAWP_TESTING')) {
113 return;
114 }
115 \IAWP\Migrations\Migrations::handle_migration_18_error();
116 \IAWP\Migrations\Migrations::handle_migration_22_error();
117 \IAWP\Migrations\Migrations::create_or_migrate();
118 if (\IAWP\Migrations\Migrations::is_migrating()) {
119 return;
120 }
121 if ($this->blocked_ip(Request::ip())) {
122 return;
123 }
124 $visitor = new Visitor(Request::ip(), Request::user_agent());
125 $signature = \md5(Salt::request_payload_salt() . \json_encode($request['payload']));
126 $campaign = [];
127 if (\IAWPSCOPED\iawp_is_pro()) {
128 $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'])];
129 }
130 if ($signature == $request['signature']) {
131 new \IAWP\View($request['payload'], $this->calculate_referrer_url($request), $visitor, $campaign);
132 return new \WP_REST_Response(['success' => \true], 200, ['X-IAWP' => 'iawp']);
133 } else {
134 return new \WP_REST_Response(['success' => \false], 200, ['X-IAWP' => 'iawp']);
135 }
136 }
137 private function calculate_referrer_url($request) : ?string
138 {
139 $referrer_url = $request['referrer_url'];
140 $url = new URL($referrer_url ?? '');
141 if (!\is_null($this->decode_or_nullify($request['gclid'])) && $url->get_domain() !== 'googleads.g.doubleclick.net') {
142 $referrer_url = 'https://googleads.iawp';
143 }
144 if (\is_null($referrer_url)) {
145 return null;
146 }
147 return $referrer_url;
148 }
149 private function decode_or_nullify($string)
150 {
151 if (!isset($string)) {
152 return null;
153 }
154 $safe_string = \trim(\urldecode($string));
155 $safe_string = \str_replace('+', ' ', $safe_string);
156 $safe_string = Security::string($safe_string);
157 if (\strlen($safe_string) === 0) {
158 return null;
159 }
160 return $safe_string;
161 }
162 private function blocked_ip($visitor_ip)
163 {
164 if (\defined('IAWP_TEST_IP')) {
165 $visitor_ip = \IAWP_TEST_IP;
166 }
167 $blocked_ips = \IAWPSCOPED\iawp()->get_option('iawp_blocked_ips', []);
168 if (\count($blocked_ips) == 0) {
169 return \false;
170 }
171 if (\in_array($visitor_ip, $blocked_ips)) {
172 return \true;
173 }
174 $wildcard_ips = [];
175 foreach ($blocked_ips as $blocked_ip) {
176 if (String_Util::str_contains($blocked_ip, '*')) {
177 $wildcard_ips[] = $blocked_ip;
178 }
179 }
180 if (\count($wildcard_ips) == 0) {
181 return \false;
182 }
183 $delimeter = String_Util::str_contains($visitor_ip, '.') ? '.' : ':';
184 $visitor_parts = \explode($delimeter, $visitor_ip);
185 $goal = \count($visitor_parts);
186 foreach ($wildcard_ips as $blocked_ip) {
187 $blocked_parts = \explode($delimeter, $blocked_ip);
188 $matches = 0;
189 for ($i = 0; $i < \count($visitor_parts); $i++) {
190 if (!\array_key_exists($i, $blocked_parts)) {
191 $matches++;
192 } elseif ($visitor_parts[$i] == $blocked_parts[$i] || $blocked_parts[$i] == '*') {
193 $matches++;
194 continue;
195 } else {
196 break;
197 }
198 }
199 if ($matches == $goal) {
200 return \true;
201 }
202 }
203 return \false;
204 }
205 private function block_user_role() : bool
206 {
207 $blocked_roles = \IAWPSCOPED\iawp()->get_option('iawp_blocked_roles', []);
208 foreach (\wp_get_current_user()->roles as $visitor_role) {
209 if (\in_array($visitor_role, $blocked_roles)) {
210 return \true;
211 }
212 }
213 return \false;
214 }
215 }
216