Migrations
3 years ago
ajax
3 years ago
models
3 years ago
queries
3 years ago
sql
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
dashboard_options.php
3 years ago
dashboard_widget.php
3 years ago
email_reports.php
3 years ago
filters.php
3 years ago
freemius.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
real_time.php
3 years ago
rest_api.php
3 years ago
settings.php
3 years ago
track_resource_changes.php
3 years ago
view_counter.php
3 years ago
rest_api.php
261 lines
| 1 | <?php |
| 2 | |
| 3 | namespace IAWP; |
| 4 | |
| 5 | class REST_API |
| 6 | { |
| 7 | public function __construct() |
| 8 | { |
| 9 | add_action( 'wp_footer', [ $this, 'echo_tracking_script' ] ); |
| 10 | add_action( 'rest_api_init', [ $this, 'register_rest_api' ] ); |
| 11 | // Support for PDF Viewer by Themencode |
| 12 | add_action( 'tnc_pvfw_head', [ $this, 'echo_tracking_script' ] ); |
| 13 | } |
| 14 | |
| 15 | private function get_date_archive_date() |
| 16 | { |
| 17 | $str = get_query_var( 'year' ); |
| 18 | |
| 19 | if ( is_month() || is_day() ) { |
| 20 | $month = get_query_var( 'monthnum' ); |
| 21 | $str = $str . '-' . str_pad( |
| 22 | $month, |
| 23 | 2, |
| 24 | '0', |
| 25 | STR_PAD_LEFT |
| 26 | ); |
| 27 | } |
| 28 | |
| 29 | |
| 30 | if ( is_day() ) { |
| 31 | $day = get_query_var( 'day' ); |
| 32 | $str = $str . '-' . str_pad( |
| 33 | $day, |
| 34 | 2, |
| 35 | '0', |
| 36 | STR_PAD_LEFT |
| 37 | ); |
| 38 | } |
| 39 | |
| 40 | return $str; |
| 41 | } |
| 42 | |
| 43 | public function echo_tracking_script() |
| 44 | { |
| 45 | if ( !get_option( 'iawp_track_authenticated_users' ) && is_user_logged_in() ) { |
| 46 | // Todo - Can we clear the cache plugins to make sure that the pages so tracking code as user logs in & out? |
| 47 | return; |
| 48 | } |
| 49 | if ( $this->block_user_role() ) { |
| 50 | return; |
| 51 | } |
| 52 | // Don't track post or page previews |
| 53 | if ( is_preview() ) { |
| 54 | return; |
| 55 | } |
| 56 | $payload = []; |
| 57 | |
| 58 | if ( is_singular() ) { |
| 59 | $payload['resource'] = 'singular'; |
| 60 | $payload['singular_id'] = get_queried_object_id(); |
| 61 | } elseif ( is_author() ) { |
| 62 | $payload['resource'] = 'author_archive'; |
| 63 | $payload['author_id'] = get_queried_object_id(); |
| 64 | } elseif ( is_date() ) { |
| 65 | $payload['resource'] = 'date_archive'; |
| 66 | $payload['date_archive'] = $this->get_date_archive_date(); |
| 67 | } elseif ( is_post_type_archive() ) { |
| 68 | $payload['resource'] = 'post_type_archive'; |
| 69 | $payload['post_type'] = get_queried_object()->name; |
| 70 | } elseif ( is_category() || is_tag() || is_tax() ) { |
| 71 | $payload['resource'] = 'term_archive'; |
| 72 | $payload['term_id'] = get_queried_object_id(); |
| 73 | } elseif ( is_search() ) { |
| 74 | $payload['resource'] = 'search'; |
| 75 | $payload['search_query'] = get_search_query(); |
| 76 | } elseif ( is_home() ) { |
| 77 | $payload['resource'] = 'home'; |
| 78 | } elseif ( is_404() ) { |
| 79 | $path = Request::path_relative_to_site_url(); |
| 80 | if ( is_null( $path ) ) { |
| 81 | return; |
| 82 | } |
| 83 | $payload['resource'] = '404'; |
| 84 | $payload['not_found_url'] = $path; |
| 85 | } else { |
| 86 | // Don't record views for unknown types |
| 87 | return; |
| 88 | } |
| 89 | |
| 90 | $payload['page'] = max( 1, get_query_var( 'paged' ) ); |
| 91 | $valid_data = !empty($payload); |
| 92 | |
| 93 | if ( $valid_data ) { |
| 94 | $data = [ |
| 95 | 'payload' => $payload, |
| 96 | ]; |
| 97 | $data['signature'] = md5( Salt::request_payload_salt() . json_encode( $data['payload'] ) ); |
| 98 | $url = get_rest_url() . 'iawp/search'; |
| 99 | ?> |
| 100 | <script> |
| 101 | (function () { |
| 102 | document.addEventListener("DOMContentLoaded", function (e) { |
| 103 | if (document.hasOwnProperty("visibilityState") && document.visibilityState === "prerender") { |
| 104 | return; |
| 105 | } |
| 106 | |
| 107 | if (navigator.webdriver || /bot|crawler|spider|crawling/i.test(navigator.userAgent)) { |
| 108 | return; |
| 109 | } |
| 110 | |
| 111 | let referrer_url = null; |
| 112 | |
| 113 | if (typeof document.referrer === 'string' && document.referrer.length > 0) { |
| 114 | referrer_url = document.referrer; |
| 115 | } |
| 116 | |
| 117 | const params = location.search.slice(1).split('&').reduce((acc, s) => { |
| 118 | const [k, v] = s.split('=') |
| 119 | return Object.assign(acc, {[k]: v}) |
| 120 | }, {}) |
| 121 | |
| 122 | const url = "<?php |
| 123 | echo $url ; |
| 124 | ?>" |
| 125 | const body = { |
| 126 | referrer_url, |
| 127 | utm_source: params.utm_source, |
| 128 | utm_medium: params.utm_medium, |
| 129 | utm_campaign: params.utm_campaign, |
| 130 | utm_term: params.utm_term, |
| 131 | utm_content: params.utm_content, |
| 132 | ...<?php |
| 133 | echo json_encode( $data ) ; |
| 134 | ?> |
| 135 | } |
| 136 | const xhr = new XMLHttpRequest() |
| 137 | xhr.open("POST", url, true) |
| 138 | xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8") |
| 139 | xhr.send(JSON.stringify(body)) |
| 140 | }) |
| 141 | })(); |
| 142 | </script> |
| 143 | <?php |
| 144 | } |
| 145 | |
| 146 | } |
| 147 | |
| 148 | public function register_rest_api() |
| 149 | { |
| 150 | register_rest_route( 'iawp', '/search', [ |
| 151 | 'methods' => 'POST', |
| 152 | 'callback' => [ $this, 'track_view' ], |
| 153 | 'permission_callback' => function () { |
| 154 | return true; |
| 155 | }, |
| 156 | ] ); |
| 157 | } |
| 158 | |
| 159 | public function track_view( $request ) |
| 160 | { |
| 161 | Migrations\Migration::create_or_migrate(); |
| 162 | if ( Migrations\Migration::is_migrating() ) { |
| 163 | return; |
| 164 | } |
| 165 | if ( $this->blocked_ip( Request::ip() ) ) { |
| 166 | return; |
| 167 | } |
| 168 | $visitor = new Visitor( Request::ip(), Request::user_agent() ); |
| 169 | $signature = md5( Salt::request_payload_salt() . json_encode( $request['payload'] ) ); |
| 170 | $campaign = []; |
| 171 | |
| 172 | if ( $signature == $request['signature'] ) { |
| 173 | new View( |
| 174 | $request['payload'], |
| 175 | $request['referrer_url'], |
| 176 | $visitor, |
| 177 | $campaign |
| 178 | ); |
| 179 | return new \WP_REST_Response( [ |
| 180 | 'success' => true, |
| 181 | ], 200, [ |
| 182 | 'X-IAWP' => 'IAWP', |
| 183 | ] ); |
| 184 | } else { |
| 185 | return new \WP_REST_Response( [ |
| 186 | 'success' => false, |
| 187 | ], 200, [ |
| 188 | 'X-IAWP' => 'IAWP', |
| 189 | ] ); |
| 190 | } |
| 191 | |
| 192 | } |
| 193 | |
| 194 | private function decode_or_nullify( $string ) |
| 195 | { |
| 196 | if ( !isset( $string ) ) { |
| 197 | return null; |
| 198 | } |
| 199 | $safe_string = trim( urldecode( $string ) ); |
| 200 | $safe_string = str_replace( '+', ' ', $safe_string ); |
| 201 | $safe_string = Security::string( $safe_string ); |
| 202 | if ( strlen( $safe_string ) === 0 ) { |
| 203 | return null; |
| 204 | } |
| 205 | return $safe_string; |
| 206 | } |
| 207 | |
| 208 | private function blocked_ip( $visitor_ip ) |
| 209 | { |
| 210 | $blocked_ips = IAWP()->get_option( 'iawp_blocked_ips', [] ); |
| 211 | if ( count( $blocked_ips ) == 0 ) { |
| 212 | return false; |
| 213 | } |
| 214 | if ( in_array( $visitor_ip, $blocked_ips ) ) { |
| 215 | return true; |
| 216 | } |
| 217 | $wildcard_ips = []; |
| 218 | foreach ( $blocked_ips as $blocked_ip ) { |
| 219 | if ( StringUtil::str_contains( $blocked_ip, '*' ) ) { |
| 220 | $wildcard_ips[] = $blocked_ip; |
| 221 | } |
| 222 | } |
| 223 | if ( count( $wildcard_ips ) == 0 ) { |
| 224 | return false; |
| 225 | } |
| 226 | $visitor_parts = explode( '.', $visitor_ip ); |
| 227 | $goal = count( $visitor_parts ); |
| 228 | foreach ( $wildcard_ips as $blocked_ip ) { |
| 229 | $blocked_parts = explode( '.', $blocked_ip ); |
| 230 | $matches = 0; |
| 231 | for ( $i = 0 ; $i < count( $visitor_parts ) ; $i++ ) { |
| 232 | |
| 233 | if ( !array_key_exists( $i, $blocked_parts ) ) { |
| 234 | $matches++; |
| 235 | } elseif ( $visitor_parts[$i] == $blocked_parts[$i] || $blocked_parts[$i] == '*' ) { |
| 236 | $matches++; |
| 237 | continue; |
| 238 | } else { |
| 239 | break; |
| 240 | } |
| 241 | |
| 242 | } |
| 243 | if ( $matches == $goal ) { |
| 244 | return true; |
| 245 | } |
| 246 | } |
| 247 | return false; |
| 248 | } |
| 249 | |
| 250 | private function block_user_role() : bool |
| 251 | { |
| 252 | $blocked_roles = IAWP()->get_option( 'iawp_blocked_roles', [] ); |
| 253 | foreach ( wp_get_current_user()->roles as $visitor_role ) { |
| 254 | if ( in_array( $visitor_role, $blocked_roles ) ) { |
| 255 | return true; |
| 256 | } |
| 257 | } |
| 258 | return false; |
| 259 | } |
| 260 | |
| 261 | } |