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