C:\xampp\htdocs\landing\wp-content\plugins\google-analytics-for-wordpress\includes\api-request.php


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
<?php
/**
 * API Request class.
 *
 * @since 7.0.0
 *
 * @package MonsterInsights
 * @author  Chris Christoff
 */
final class MonsterInsights_API_Request {

    
/**
     * Base API route.
     *
     * @since 7.0.0
     *
     * @var string
     */
    
public $base 'api.monsterinsights.com/v2/';

    
/**
     * Current API route.
     *
     * @since 7.0.0
     *
     * @var bool|string
     */
    
public $route false;

    
/**
     * Full API URL endpoint.
     *
     * @since 7.0.0
     *
     * @var bool|string
     */
    
public $url false;

    
/**
     * Current API method.
     *
     * @since 7.0.0
     *
     * @var bool|string
     */
    
public $method false;

    
/**
     * Is a network request.
     *
     * @since 7.2.0
     *
     * @var bool
     */
    
public $network false;

    
/**
     * API token.
     *
     * @since 7.0.0
     *
     * @var bool|string
     */
    
public $token false;

    
/**
     * API Key.
     *
     * @since 7.0.0
     *
     * @var bool|string
     */
    
public $key false;

    
/**
     * API tt.
     *
     * @since 7.0.0
     *
     * @var bool|string
     */
    
public $tt false;

    
/**
     * API return.
     *
     * @since 7.0.0
     *
     * @var bool|string
     */
    
public $return false;

    
/**
     * Start date.
     *
     * @since 7.0.0
     *
     * @var string
     */
    
public $start '';

    
/**
     * End Date.
     *
     * @since 7.0.0
     *
     * @var string
     */
    
public $end '';

    
/**
     * Plugin slug.
     *
     * @since 7.0.0
     *
     * @var bool|string
     */
    
public $plugin false;

    
/**
     * URL to test connection with.
     *
     * @since 7.3.2
     *
     * @var string
     */
    
public $testurl '';

    
/**
     * Additional data to add to request body
     *
     * @since 7.0.0
     *
     * @var array
     */
    
protected $additional_data = array();

    
/**
     * Primary class constructor.
     *
     * @since 7.0.0
     *
     * @param string $route  The API route to target.
     * @param array  $args   Array of API credentials.
     * @param string $method The API method.
     */
    
public function __construct$route$args$method 'POST' ) {

        
// Set class properties.
        
$this->base      trailingslashitmonsterinsights_get_api_url() );
        
$this->route     $route;
        
$this->protocol  'https://';
        
$this->url       trailingslashit$this->protocol $this->base $this->route );
        
$this->method    $method;
        
$this->network   is_network_admin() || ! empty( $args['network'] );

        
$default_token   $this->network MonsterInsights()->auth->get_network_token() : MonsterInsights()->auth->get_token();
        
$default_key     $this->network MonsterInsights()->auth->get_network_key()   : MonsterInsights()->auth->get_key();

        
$this->token     = ! empty( $args['token'] )     ? $args['token']  : $default_token;
        
$this->key       = ! empty( $args['key'] )          ? $args['key']    : $default_key;
        
$this->tt        = ! empty( $args['tt'] )          ? $args['tt']     : '';
        
$this->return    = ! empty( $args['return'] )    ? $args['return'] : '';
        
$this->start     = ! empty( $args['start'] )     ? $args['start']  : '';
        
$this->end       = ! empty( $args['end'] )       ? $args['end']    : '';

        
// We need to do this hack so that the network panel + the site_url of the main site are distinct
        
$this->site_url  is_network_admin() ? network_admin_url() : site_url();

        if ( 
monsterinsights_is_pro_version() ) {
            
$this->license   $this->network MonsterInsights()->license->get_network_license_key() : MonsterInsights()->license->get_site_license_key();
        }
        
$this->plugin    MonsterInsights()->plugin_slug;
        
$this->miversion MONSTERINSIGHTS_VERSION;
        
$this->sitei     = ! empty( $args['sitei'] )   ? $args['sitei'] : '';
        
$this->testurl   = ! empty( $args['testurl'] ) ? $args['testurl']    : '';
    }

    
/**
     * Processes the API request.
     *
     * @since 7.0.0
     *
     * @return mixed $value The response to the API call.
     */
    
public function request() {
        
// Make sure we're not blocked
        
$blocked $this->is_blocked$this->url );
        if ( 
$blocked || is_wp_error$blocked )  ) {
            if ( 
is_wp_error$blocked ) ) {
                
// Translators: Placeholder gets replaced with the error message.
                
return new WP_Error'api-error'sprintf__'The firewall of your server is blocking outbound calls. Please contact your hosting provider to fix this issue. %s''google-analytics-for-wordpress' ), $blocked->get_error_message() ) );
            } else {
                return new 
WP_Error'api-error'__'The firewall of your server is blocking outbound calls. Please contact your hosting provider to fix this issue.''google-analytics-for-wordpress' ) );
            }
        }

        
// Build the body of the request.
        
$body = array();

        if ( ! empty( 
$this->token ) ) {
            
$body['token'] = $this->token;
        }

        if ( ! empty( 
$this->key ) ) {
            
$body['key'] = $this->key;
        }

        if ( ! empty( 
$this->tt ) ) {
            
$body['tt'] = $this->tt;
        }

        if ( ! empty( 
$this->return ) ) {
            
$body['return'] = $this->return;
        }

        if ( 
monsterinsights_is_pro_version() && ! empty( $this->license ) ) {
            
$body['license'] = $this->license;
        }

        if ( ! empty( 
$this->start ) ) {
            
$body['start'] = $this->start;
        }

        if ( ! empty( 
$this->end ) ) {
            
$body['end'] = $this->end;
        }

        if ( ! empty( 
$this->sitei ) ) {
            
$body['sitei'] = $this->sitei;
        }

        
$body['siteurl']   = $this->site_url;
        
$body['miversion'] = $this->miversion;

        
// If a plugin API request, add the data.
        
if ( 'info' == $this->route || 'update' == $this->route ) {
            
$body['miapi-plugin'] = $this->plugin;
        }

        
// Add in additional data if needed.
        
if ( ! empty( $this->additional_data ) ) {
            
$body['miapi-data'] = maybe_serialize$this->additional_data );
        }

        if ( 
'GET' == $this->method ) {
            
$body['time']   = time(); // just to avoid caching
        
}

        
$body['timezone'] = date('e');

        
$body['network']  = $this->network 'network' 'site';

        
$body['ip']   = ! empty( $_SERVER['SERVER_ADDR'] ) ? $_SERVER['SERVER_ADDR'] : '';

        
// This filter will be removed in the future.
        
$body   apply_filters'monsterinsights_api_request_body'$body );

        
$string http_build_query$body'''&' );

        
// Build the headers of the request.
        
$headers = array(
            
'Content-Type'          => 'application/x-www-form-urlencoded',
            
'Cache-Control'         => 'no-store, no-cache, must-revalidate, max-age=0, post-check=0, pre-check=0',
            
'Pragma'                => 'no-cache',
            
'Expires'                => 0,
            
'MIAPI-Referer'         => is_network_admin() ? network_admin_url() : site_url(),
            
'MIAPI-Sender'          => 'WordPress',
        );

        
//if ( $this->apikey ) {
        //    $headers['X-MonsterInsights-ApiKey'] = $this->apikey;
        //}

        // Setup data to be sent to the API.
        
$data = array(
            
'headers'    => $headers,
            
'body'       => $body,
            
'timeout'    => 3000,
            
'user-agent' => 'MI/' MONSTERINSIGHTS_VERSION '; ' $this->site_url,
            
'sslverify'  => false
        
);

        
// Perform the query and retrieve the response.
        
$response      'GET' == $this->method wp_remote_getesc_url_raw$this->url ) . '?' $string$data ) : wp_remote_postesc_url_raw$this->url ), $data );

        
//return new WP_Error( 'debug', '<pre>' . var_export( $response, true ) . '</pre>' );

        
if ( is_wp_error$response ) ) {
            return 
$response;
        }

        
$response_code wp_remote_retrieve_response_code$response );
        
$response_body json_decodewp_remote_retrieve_body$response ), true );
        
//return new WP_Error( 'debug', '<pre>' . var_export( $response_body, true ) . '</pre>' );
        //var_dump( $response_body );
        // Bail out early if there are any errors.
        
if ( is_wp_error$response_body ) ) {
            return 
$response_body;
        }

        
// If not a 200 status header, send back error.
        
if ( 200 != $response_code ) {
            
$type  = ! empty( $response_body['type'] ) ? $response_body['type'] : 'api-error';

            if ( empty( 
$response_code ) ) {
                return new 
WP_Error$type__'The API was unreachable.''google-analytics-for-wordpress' ) );
            }

            if ( empty( 
$response_body ) || ( empty( $response_body['message'] ) && empty( $response_body['error'] ) ) ) {
                
// Translators: placeholder adds the response code.
                
return new WP_Error$typesprintf__'The API returned a <strong>%s</strong> response''google-analytics-for-wordpress' ), $response_code ) );
            }

            if ( ! empty( 
$response_body['message'] ) ) {
                
// Translators: placeholder adds the response code and response message.
                
return new WP_Error$typesprintf__'The API returned a <strong>%1$d</strong> response with this message: <strong>%2$s</strong>''google-analytics-for-wordpress' ), $response_codestripslashes$response_body['message'] ) ) );
            }

            if ( ! empty( 
$response_body['error'] ) ) {
                
// Translators: placeholder adds the response code and response message.
                
return new WP_Error$typesprintf__'The API returned a <strong>%1$d</strong> response with this message: <strong>%2$s</strong>''google-analytics-for-wordpress' ), $response_codestripslashes$response_body['error'] ) ) );
            }
        }

        
// If TT required
        
if ( ! empty( $this->tt ) ) {
            if ( empty( 
$response_body['tt'] ) || ! hash_equals$this->tt$response_body['tt'] ) ) {
                
// TT isn't set on return or doesn't match
                
return new WP_Error'validation-error'sprintf__'Improper API request.''google-analytics-for-wordpress' ) ) );
            }
        }

        
// Return the json decoded content.
        
return $response_body;
    }

    
/**
     * Sets a class property.
     *
     * @since 7.0.0
     *
     * @param string $key The property to set.
     * @param string $val The value to set for the property.
     * @return mixed $value The response to the API call.
     */
    
public function set$key$val ) {
        
$this->{$key} = $val;
    }

    
/**
     * Allow additional data to be passed in the request
     *
     * @since 7.0.0
     *
     * @param array $data
     * return void
     */
    
public function set_additional_data( array $data ) {
        
$this->additional_data array_merge$this->additional_data$data );
    }

    
/**
     * Checks for SSL for making API requests.
     *
     * @since 7.0.0
     *
     * return bool True if SSL is enabled, false otherwise.
     */
    
public function is_ssl() {
        
// Use the base is_ssl check first.
        
if ( is_ssl() ) {
            return 
true;
        } else if ( isset( 
$_SERVER['HTTP_X_FORWARDED_PROTO'] ) && 'https' == $_SERVER['HTTP_X_FORWARDED_PROTO'] ) {
            
// Also catch proxies and load balancers.
            
return true;
        } else if ( 
defined'FORCE_SSL_ADMIN' ) && FORCE_SSL_ADMIN ) {
            return 
true;
        }

        
// Otherwise, return false.
        
return false;
    }

    private function 
is_blocked$url '' ) {
        global 
$Airplane_Mode_Core;
        if ( 
defined'AIRMDE_VER' ) && ! empty( $Airplane_Mode_Core ) && $Airplane_Mode_Core->enabled() ) {
            return new 
WP_Error'api-error'__'Reason: The API was unreachable because the Airplane Mode plugin is active.''google-analytics-for-wordpress' ) );
        }

        
// The below page is a testing empty content HTML page used for firewall/router login detection
        // and for image linking purposes in Google Images. We use it to test outbound connections since it is run on google.com
        // and is only a few bytes large. Plus on Google's main CDN so it loads in most places in 0.07 seconds or less. Perfect for our
        // use case of quickly testing outbound connections.
        
$testurl = ! empty( $this->testurl ) ? $this->testurl :'https://www.google.com/blank.html';
        if ( 
defined'WP_HTTP_BLOCK_EXTERNAL' ) && WP_HTTP_BLOCK_EXTERNAL ) {
            if ( 
defined'WP_ACCESSIBLE_HOSTS' ) ) {
                
$wp_http      = new WP_Http();
                
$on_blacklist $wp_http->block_request$url );
                if ( 
$on_blacklist ) {
                    return new 
WP_Error'api-error'__'Reason: The API was unreachable because the API url is on the WP HTTP blocklist.''google-analytics-for-wordpress' ) );
                } else {
                    
$params = array(
                        
'sslverify'     => false,
                        
'timeout'       => 2,
                        
'user-agent'    => 'MonsterInsights/' MONSTERINSIGHTS_VERSION,
                        
'body'          => ''
                    
);
                    
$response wp_remote_get$testurl$params );
                    if( ! 
is_wp_error$response ) && $response['response']['code'] >= 200 && $response['response']['code'] < 300 ) {
                        return 
false;
                    } else {
                        if ( 
is_wp_error$response ) ) {
                            return 
$response;
                        } else {
                            return new 
WP_Error'api-error'__'Reason: The API was unreachable because the call to Google failed.''google-analytics-for-wordpress' ) );
                        }
                    }
                }
            } else {
                return new 
WP_Error'api-error'__'Reason: The API was unreachable because no external hosts are allowed on this site.''google-analytics-for-wordpress' ) );
            }
        } else {
            
$params = array(
                
'sslverify'     => false,
                
'timeout'       => 2,
                
'user-agent'    => 'MonsterInsights/' MONSTERINSIGHTS_VERSION,
                
'body'          => ''
            
);
            
$response wp_remote_get$testurl$params );

            if( ! 
is_wp_error$response ) && $response['response']['code'] >= 200 && $response['response']['code'] < 300 ) {
                return 
false;
            } else {
                if ( 
is_wp_error$response ) ) {
                    return 
$response;
                } else {
                    return new 
WP_Error'api-error'__'Reason: The API was unreachable because the call to Google failed.''google-analytics-for-wordpress' ) );
                }
            }
        }
    }
}
x

Windows NT KPTV 6.2 build 9200 (Windows Server 2012 Datacenter Edition) i586