Advertisement
Halyspectro

Untitled

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