api-request.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. <?php
  2. /**
  3. * API Request class.
  4. *
  5. * @since 7.0.0
  6. *
  7. * @package MonsterInsights
  8. * @author Chris Christoff
  9. */
  10. final class MonsterInsights_API_Request {
  11. /**
  12. * Base API route.
  13. *
  14. * @since 7.0.0
  15. *
  16. * @var string
  17. */
  18. public $base = 'api.monsterinsights.com/v2/';
  19. /**
  20. * Current API route.
  21. *
  22. * @since 7.0.0
  23. *
  24. * @var bool|string
  25. */
  26. public $route = false;
  27. /**
  28. * Full API URL endpoint.
  29. *
  30. * @since 7.0.0
  31. *
  32. * @var bool|string
  33. */
  34. public $url = false;
  35. /**
  36. * Current API method.
  37. *
  38. * @since 7.0.0
  39. *
  40. * @var bool|string
  41. */
  42. public $method = false;
  43. /**
  44. * Is a network request.
  45. *
  46. * @since 7.2.0
  47. *
  48. * @var bool
  49. */
  50. public $network = false;
  51. /**
  52. * API token.
  53. *
  54. * @since 7.0.0
  55. *
  56. * @var bool|string
  57. */
  58. public $token = false;
  59. /**
  60. * API Key.
  61. *
  62. * @since 7.0.0
  63. *
  64. * @var bool|string
  65. */
  66. public $key = false;
  67. /**
  68. * API tt.
  69. *
  70. * @since 7.0.0
  71. *
  72. * @var bool|string
  73. */
  74. public $tt = false;
  75. /**
  76. * API return.
  77. *
  78. * @since 7.0.0
  79. *
  80. * @var bool|string
  81. */
  82. public $return = false;
  83. /**
  84. * Start date.
  85. *
  86. * @since 7.0.0
  87. *
  88. * @var string
  89. */
  90. public $start = '';
  91. /**
  92. * End Date.
  93. *
  94. * @since 7.0.0
  95. *
  96. * @var string
  97. */
  98. public $end = '';
  99. /**
  100. * Plugin slug.
  101. *
  102. * @since 7.0.0
  103. *
  104. * @var bool|string
  105. */
  106. public $plugin = false;
  107. /**
  108. * Additional data to add to request body
  109. *
  110. * @since 7.0.0
  111. *
  112. * @var array
  113. */
  114. protected $additional_data = array();
  115. /**
  116. * Primary class constructor.
  117. *
  118. * @since 7.0.0
  119. *
  120. * @param string $route The API route to target.
  121. * @param array $args Array of API credentials.
  122. * @param string $method The API method.
  123. */
  124. public function __construct( $route, $args, $method = 'POST' ) {
  125. // Set class properties.
  126. $this->base = trailingslashit( monsterinsights_get_api_url() );
  127. $this->route = $route;
  128. $this->protocol = 'https://';
  129. $this->url = trailingslashit( $this->protocol . $this->base . $this->route );
  130. $this->method = $method;
  131. $this->network = is_network_admin() || ! empty( $args['network'] );
  132. $default_token = $this->network ? MonsterInsights()->auth->get_network_token() : MonsterInsights()->auth->get_token();
  133. $default_key = $this->network ? MonsterInsights()->auth->get_network_key() : MonsterInsights()->auth->get_key();
  134. $this->token = ! empty( $args['token'] ) ? $args['token'] : $default_token;
  135. $this->key = ! empty( $args['key'] ) ? $args['key'] : $default_key;
  136. $this->tt = ! empty( $args['tt'] ) ? $args['tt'] : '';
  137. $this->return = ! empty( $args['return'] ) ? $args['return'] : '';
  138. $this->start = ! empty( $args['start'] ) ? $args['start'] : '';
  139. $this->end = ! empty( $args['end'] ) ? $args['end'] : '';
  140. // We need to do this hack so that the network panel + the site_url of the main site are distinct
  141. $this->site_url = is_network_admin() ? network_admin_url() : site_url();
  142. if ( monsterinsights_is_pro_version() ) {
  143. $this->license = $this->network ? MonsterInsights()->license->get_network_license_key() : MonsterInsights()->license->get_site_license_key();
  144. }
  145. $this->plugin = MonsterInsights()->plugin_slug;
  146. $this->miversion = MONSTERINSIGHTS_VERSION;
  147. $this->sitei = ! empty( $args['sitei'] ) ? $args['sitei'] : '';
  148. }
  149. /**
  150. * Processes the API request.
  151. *
  152. * @since 7.0.0
  153. *
  154. * @return mixed $value The response to the API call.
  155. */
  156. public function request() {
  157. // Make sure we're not blocked
  158. if ( $this->is_blocked( $this->url ) ) {
  159. 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' ) );
  160. }
  161. // Build the body of the request.
  162. $body = array();
  163. if ( ! empty( $this->token ) ) {
  164. $body['token'] = $this->token;
  165. }
  166. if ( ! empty( $this->key ) ) {
  167. $body['key'] = $this->key;
  168. }
  169. if ( ! empty( $this->tt ) ) {
  170. $body['tt'] = $this->tt;
  171. }
  172. if ( ! empty( $this->return ) ) {
  173. $body['return'] = $this->return;
  174. }
  175. if ( monsterinsights_is_pro_version() && ! empty( $this->license ) ) {
  176. $body['license'] = $this->license;
  177. }
  178. if ( ! empty( $this->start ) ) {
  179. $body['start'] = $this->start;
  180. }
  181. if ( ! empty( $this->end ) ) {
  182. $body['end'] = $this->end;
  183. }
  184. if ( ! empty( $this->sitei ) ) {
  185. $body['sitei'] = $this->sitei;
  186. }
  187. $body['siteurl'] = $this->site_url;
  188. $body['miversion'] = $this->miversion;
  189. // If a plugin API request, add the data.
  190. if ( 'info' == $this->route || 'update' == $this->route ) {
  191. $body['miapi-plugin'] = $this->plugin;
  192. }
  193. // Add in additional data if needed.
  194. if ( ! empty( $this->additional_data ) ) {
  195. $body['miapi-data'] = maybe_serialize( $this->additional_data );
  196. }
  197. if ( 'GET' == $this->method ) {
  198. $body['time'] = time(); // just to avoid caching
  199. }
  200. $body['timezone'] = date('e');
  201. $body['network'] = $this->network ? 'network' : 'site';
  202. $body['ip'] = ! empty( $_SERVER['SERVER_ADDR'] ) ? $_SERVER['SERVER_ADDR'] : '';
  203. // This filter will be removed in the future.
  204. $body = apply_filters( 'monsterinsights_api_request_body', $body );
  205. $string = http_build_query( $body, '', '&' );
  206. // Build the headers of the request.
  207. $headers = array(
  208. 'Content-Type' => 'application/x-www-form-urlencoded',
  209. 'Cache-Control' => 'no-store, no-cache, must-revalidate, max-age=0, post-check=0, pre-check=0',
  210. 'Pragma' => 'no-cache',
  211. 'Expires' => 0,
  212. 'MIAPI-Referer' => is_network_admin() ? network_admin_url() : site_url(),
  213. 'MIAPI-Sender' => 'WordPress',
  214. );
  215. //if ( $this->apikey ) {
  216. // $headers['X-MonsterInsights-ApiKey'] = $this->apikey;
  217. //}
  218. // Setup data to be sent to the API.
  219. $data = array(
  220. 'headers' => $headers,
  221. 'body' => $body,
  222. 'timeout' => 3000,
  223. 'user-agent' => 'MI/' . MONSTERINSIGHTS_VERSION . '; ' . $this->site_url,
  224. 'sslverify' => false
  225. );
  226. // Perform the query and retrieve the response.
  227. $response = 'GET' == $this->method ? wp_remote_get( esc_url_raw( $this->url ) . '?' . $string, $data ) : wp_remote_post( esc_url_raw( $this->url ), $data );
  228. //return new WP_Error( 'debug', '<pre>' . var_export( $response, true ) . '</pre>' );
  229. $response_code = wp_remote_retrieve_response_code( $response );
  230. $response_body = json_decode( wp_remote_retrieve_body( $response ), true );
  231. //return new WP_Error( 'debug', '<pre>' . var_export( $response_body, true ) . '</pre>' );
  232. //var_dump( $response_body );
  233. // Bail out early if there are any errors.
  234. if ( is_wp_error( $response_body ) ) {
  235. return $response_body;
  236. }
  237. // If not a 200 status header, send back error.
  238. if ( 200 != $response_code ) {
  239. $type = ! empty( $response_body['type'] ) ? $response_body['type'] : 'api-error';
  240. if ( empty( $response_code ) ) {
  241. return new WP_Error( $type, __( 'The API was unreachable.', 'google-analytics-for-wordpress' ) );
  242. }
  243. if ( empty( $response_body ) || ( empty( $response_body['message'] ) && empty( $response_body['error'] ) ) ) {
  244. return new WP_Error( $type, sprintf( __( 'The API returned a <strong>%s</strong> response', 'google-analytics-for-wordpress' ), $response_code ) );
  245. }
  246. if ( ! empty( $response_body['message'] ) ) {
  247. return new WP_Error( $type, sprintf( __( 'The API returned a <strong>%d</strong> response with this message: <strong>%s</strong>', 'google-analytics-for-wordpress' ), $response_code, stripslashes( $response_body['message'] ) ) );
  248. }
  249. if ( ! empty( $response_body['error'] ) ) {
  250. return new WP_Error( $type, sprintf( __( 'The API returned a <strong>%d</strong> response with this message: <strong>%s</strong>', 'google-analytics-for-wordpress' ), $response_code, stripslashes( $response_body['error'] ) ) );
  251. }
  252. }
  253. // If TT required
  254. if ( ! empty( $this->tt ) ) {
  255. if ( empty( $response_body['tt'] ) || $response_body['tt'] != $this->tt ) {
  256. // TT isn't set on return or doesn't match
  257. return new WP_Error( 'validation-error', sprintf( __( 'Improper API request.', 'google-analytics-for-wordpress' ) ) );
  258. } else {
  259. // if the TT is valid, reset so it cannot be replayed
  260. MonsterInsights()->api_auth->rotate_tt();
  261. }
  262. }
  263. // Return the json decoded content.
  264. return $response_body;
  265. }
  266. /**
  267. * Sets a class property.
  268. *
  269. * @since 7.0.0
  270. *
  271. * @param string $key The property to set.
  272. * @param string $val The value to set for the property.
  273. * @return mixed $value The response to the API call.
  274. */
  275. public function set( $key, $val ) {
  276. $this->{$key} = $val;
  277. }
  278. /**
  279. * Allow additional data to be passed in the request
  280. *
  281. * @since 7.0.0
  282. *
  283. * @param array $data
  284. * return void
  285. */
  286. public function set_additional_data( array $data ) {
  287. $this->additional_data = array_merge( $this->additional_data, $data );
  288. }
  289. /**
  290. * Checks for SSL for making API requests.
  291. *
  292. * @since 7.0.0
  293. *
  294. * return bool True if SSL is enabled, false otherwise.
  295. */
  296. public function is_ssl() {
  297. // Use the base is_ssl check first.
  298. if ( is_ssl() ) {
  299. return true;
  300. } else if ( isset( $_SERVER['HTTP_X_FORWARDED_PROTO'] ) && 'https' == $_SERVER['HTTP_X_FORWARDED_PROTO'] ) {
  301. // Also catch proxies and load balancers.
  302. return true;
  303. } else if ( defined( 'FORCE_SSL_ADMIN' ) && FORCE_SSL_ADMIN ) {
  304. return true;
  305. }
  306. // Otherwise, return false.
  307. return false;
  308. }
  309. private function is_blocked( $url = '' ) {
  310. if ( defined( 'AIRMDE_VER' ) ) {
  311. return false; // Airplane mode is active
  312. }
  313. // The below page is a testing empty content HTML page used for firewall/router login detection
  314. // and for image linking purposes in Google Images. We use it to test outbound connections since it is run on google.com
  315. // 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
  316. // use case of quickly testing outbound connections.
  317. $testurl = 'http://www.google.com/blank.html';
  318. if ( defined( 'WP_HTTP_BLOCK_EXTERNAL' ) && WP_HTTP_BLOCK_EXTERNAL ) {
  319. if ( defined( 'WP_ACCESSIBLE_HOSTS' ) ) {
  320. $wp_http = new WP_Http();
  321. $on_blacklist = $wp_http->block_request( $url );
  322. if ( $on_blacklist ) {
  323. return true;
  324. } else {
  325. $params = array(
  326. 'sslverify' => false,
  327. 'timeout' => 2,
  328. 'user-agent' => 'MonsterInsights/' . MONSTERINSIGHTS_VERSION,
  329. 'body' => ''
  330. );
  331. $response = wp_remote_get( $testurl, $params );
  332. if( ! is_wp_error( $response ) && $response['response']['code'] >= 200 && $response['response']['code'] < 300 ) {
  333. return false;
  334. } else {
  335. return true;
  336. }
  337. }
  338. } else {
  339. return true;
  340. }
  341. } else {
  342. $params = array(
  343. 'sslverify' => false,
  344. 'timeout' => 2,
  345. 'user-agent' => 'MonsterInsights/' . MONSTERINSIGHTS_VERSION,
  346. 'body' => ''
  347. );
  348. $response = wp_remote_get( $testurl, $params );
  349. if( ! is_wp_error( $response ) && $response['response']['code'] >= 200 && $response['response']['code'] < 300 ) {
  350. return false;
  351. } else {
  352. return true;
  353. }
  354. }
  355. }
  356. }