Halyspectro

Untitled

Jan 13th, 2017
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.55 KB | None | 0 0
  1. <?php
  2. class FindMyiPhone {
  3.  
  4. private $username;
  5. private $password;
  6.  
  7. public $devices = array();
  8.  
  9. private $email_updates = true;
  10.  
  11. private $host = 'fmipmobile.icloud.com';
  12.  
  13. private $client_context = array(
  14. 'appName' => 'FindMyiPhone',
  15. 'appVersion' => '3.0',
  16. 'buildVersion' => '376',
  17. 'clientTimestamp' => 0,
  18. 'deviceUDID' => null,
  19. 'inactiveTime' => 1,
  20. 'osVersion' => '7.0.3',
  21. 'productType' => 'iPhone6,1'
  22. );
  23.  
  24. private $server_context = array(
  25. 'callbackIntervalInMS' => 10000,
  26. 'classicUser' => false,
  27. 'clientId' => null,
  28. 'cloudUser' => true,
  29. 'deviceLoadStatus' => '200',
  30. 'enableMapStats' => false,
  31. 'isHSA' => false,
  32. 'lastSessionExtensionTime' => null,
  33. 'macCount' => 0,
  34. 'maxDeviceLoadTime' => 60000,
  35. 'maxLocatingTime' => 90000,
  36. 'preferredLanguage' => 'en-us',
  37. 'prefsUpdateTime' => 0,
  38. 'sessionLifespan' => 900000,
  39. 'timezone' => null,
  40. 'trackInfoCacheDurationInSecs' => 86400,
  41. 'validRegion' => true
  42. );
  43.  
  44.  
  45. /**
  46. * Constructor
  47. * Checks requred extensions, sets username/password and gets url host for the user.
  48. * @param $username - iCloud Apple ID
  49. * @param $password - iCloud Password
  50. */
  51. public function __construct($username, $password) {
  52. if (!extension_loaded('curl')) {
  53. throw new FindMyiPhoneException('PHP extension cURL is not loaded.');
  54. }
  55.  
  56. $this->username = $username;
  57. $this->password = $password;
  58.  
  59. $this->init_client();
  60. }
  61.  
  62.  
  63.  
  64.  
  65. /**
  66. * Init Client
  67. *
  68. */
  69. private function init_client() {
  70. $post_data = json_encode(array(
  71. 'clientContext' => $this->client_context
  72. ));
  73.  
  74. $headers = $this->parse_curl_headers($this->make_request('initClient', $post_data, true));
  75.  
  76. $this->refresh_client();
  77. }
  78.  
  79.  
  80. /**
  81. * Refresh Client
  82. *
  83. */
  84. public function refresh_client() {
  85. $post_data = json_encode(array(
  86. 'clientContext' => $this->client_context,
  87. 'serverContext' => $this->server_context
  88. ));
  89.  
  90. /*foreach (json_decode($this->make_request('refreshClient', $post_data))->content as $id => $device) {
  91. $this->devices[$id] = $device;
  92. }*/
  93. }
  94.  
  95.  
  96.  
  97.  
  98. /**
  99. * Make request to the Find My iPhone server.
  100. * @param $method - the method
  101. * @param $post_data - the POST data
  102. * @param $return_headers - also return headers when true
  103. * @param $headers - optional headers to send
  104. * @return HTTP response
  105. */
  106. private function make_request($method, $post_data, $return_headers = false, $headers = array()) {
  107. if(!is_string($method)) throw new FindMyiPhoneException('Expected $method to be a string');
  108. if(!$this->is_json($post_data)) throw new FindMyiPhoneException('Expected $post_data to be json');
  109. if(!is_array($headers)) throw new FindMyiPhoneException('Expected $headers to be an array');
  110. if(!is_bool($return_headers)) throw new FindMyiPhoneException('Expected $return_headers to be a bool');
  111.  
  112.  
  113. array_push($headers, 'Accept-Language: en-us');
  114. array_push($headers, 'Content-Type: application/json; charset=utf-8');
  115. array_push($headers, 'X-Apple-Realm-Support: 1.0');
  116. array_push($headers, 'X-Apple-Find-Api-Ver: 3.0');
  117. array_push($headers, 'X-Apple-Authscheme: UserIdGuest');
  118.  
  119. $curl = curl_init();
  120. curl_setopt_array($curl, array(
  121. CURLOPT_TIMEOUT => 9,
  122. CURLOPT_CONNECTTIMEOUT => 5,
  123. CURLOPT_SSL_VERIFYPEER => false,
  124. CURLOPT_FOLLOWLOCATION => false,
  125. CURLOPT_RETURNTRANSFER => true,
  126. CURLOPT_AUTOREFERER => true,
  127. CURLOPT_VERBOSE => false,
  128. CURLOPT_POST => true,
  129. CURLOPT_POSTFIELDS => $post_data,
  130. CURLOPT_HTTPHEADER => $headers,
  131. CURLOPT_HEADER => $return_headers,
  132. CURLOPT_URL => sprintf("https://%s/fmipservice/device/%s/%s", $this->host, $method),
  133. CURLOPT_USERPWD => $this->username . ':' . $this->password,
  134. CURLOPT_USERAGENT => 'FindMyiPhone/376 CFNetwork/672.0.8 Darwin/14.0.0'
  135. ));
  136.  
  137. $http_result = curl_exec($curl);
  138. curl_close($curl);
  139.  
  140. return $http_result;
  141. }
  142.  
  143.  
  144. /**
  145. * Parse cURL headers
  146. * @param $response - cURL response including the headers
  147. * @return array of headers
  148. */
  149. private function parse_curl_headers($response) {
  150. $headers = array();
  151. foreach (explode("\r\n", substr($response, 0, strpos($response, "\r\n\r\n"))) as $i => $line) {
  152. if ($i === 0) {
  153. $headers['http_code'] = $line;
  154. } else {
  155. list($key, $value) = explode(': ', $line);
  156. $headers[$key] = $value;
  157. }
  158. }
  159. return $headers;
  160. }
  161.  
  162. /**
  163. * Finds whether a variable is json.
  164. */
  165. private function is_json($var) {
  166. json_decode($var);
  167. return (json_last_error() == JSON_ERROR_NONE);
  168. }
  169. }
  170.  
  171. class FindMyiPhoneException extends Exception {}
  172.  
  173. ?>
Add Comment
Please, Sign In to add comment