-
Notifications
You must be signed in to change notification settings - Fork 48
/
class.findmyiphone.php
319 lines (297 loc) · 11.6 KB
/
class.findmyiphone.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
<?PHP
/*
Copyright (C) Alan Beebe ([email protected]).
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
v1.0 - January 2, 2015
*/
class FindMyiPhone {
private $client = array(
"app-version" => "4.0",
"user-agent" => "FindMyiPhone/472.1 CFNetwork/711.1.12 Darwin/14.0.0",
"headers" => array(
"X-Apple-Realm-Support" => "1.0",
"X-Apple-Find-API-Ver" => "3.0",
"X-Apple-AuthScheme" => "UserIdGuest"
)
);
private $debug;
private $username;
private $password;
public $devices = array();
/**
* This is where you initialize FindMyiPhone with your iCloud credentials
* Example: $fmi = new FindMyiPhone("[email protected]", "MyPassWord123");
*
* @param username iCloud username
* @param password iCloud password
* @param debug (Optional) Set to TRUE and all the API requests and responses will be printed out
* @return FindMyiPhone instance
*/
public function __construct($username, $password, $debug = false) {
$this->username = $username;
$this->password = $password;
$this->debug = $debug;
$this->authenticate();
}
/**
* This method attempts to get the most current location of a device
* Example: $fmi->locate("dCsaBcqBOdnNop4wvy2VfIk8+HlQ/DRuqrmiwpsLdLTuiCORQDJ9eHYVQSUzmWV", 30);
*
* @param deviceID ID of the device you want to locate
* @param timeout (Optional) Maximum number of seconds to spend trying to locate the device
* @return FindMyiPhoneLocation object
*/
public function locate($deviceID, $timeout = 60) {
$startTime = time();
$initialTimestamp = $this->devices[$deviceID]->location->timestamp;
while ($initialTimestamp == $this->devices[$deviceID]->location->timestamp) {
if ((time() - $startTime) > $timeout) break;
$this->refreshDevices($deviceID);
sleep(5);
}
return $this->devices[$deviceID]->location;
}
/**
* Play a sound and display a message on a device
* Example: $fmi->playSound("dCsaBcqBOdnNop4wvy2VfIk8+HlQ/DRuqrmiwpsLdLTuiCORQDJ9eHYVQSUzmWV", "Whats up?");
*
* @param deviceID ID of the device you want to play a sound
* @param message Message you want displayed on the device
*/
public function playSound($deviceID, $message) {
$url = "https://fmipmobile.icloud.com/fmipservice/device/".$this->username."/playSound";
$body = json_encode(array("device"=>$deviceID, "subject"=>$message));
list($headers, $body) = $this->curlPOST($url, $body, $this->username.":".$this->password);
}
/**
* Put a device into lost mode. The device will immediately lock until the user enters the correct passcode
* Example: $fmi->lostMode("dCsaBcqBOdnNop4wvy2VfIk8+HlQ/DRuqrmiwpsLdLTuiCORQDJ9eHYVQSUzmWV", "You got locked out", "555-555-5555");
*
* @param deviceID ID of the device you want to lock
* @param message Message you want displayed on the device
* @param phoneNumber (Optional) Phone number you want displayed on the lock screen
*/
public function lostMode($deviceID, $message, $phoneNumber = "") {
$url = "https://fmipmobile.icloud.com/fmipservice/device/".$this->username."/lostDevice";
$body = json_encode(array("device"=>$deviceID, "ownerNbr"=>$phoneNumber, "text"=>$message, "lostModeEnabled"=>true));
list($headers, $body) = $this->curlPOST($url, $body, $this->username.":".$this->password);
}
/**
* Print all the available information for every device on the users account.
* This is really useful when you want to get the ID for a device.
* Example: $fmi->printDevices();
*/
public function printDevices() {
if (sizeof($this->devices) == 0) $this->getDevices();
print <<<TABLEHEADER
<PRE>
<TABLE BORDER="1" CELLPADDING="3">
<TR>
<TD VALIGN="top"><B>ID</B></TD>
<TD VALIGN="top"><B>name</B></TD>
<TD VALIGN="top"><B>displayName</B></TD>
<TD VALIGN="top"><B>location</B></TD>
<TD VALIGN="top"><B>class</B></TD>
<TD VALIGN="top"><B>model</B></TD>
<TD VALIGN="top"><B>modelDisplayName</B></TD>
<TD VALIGN="top"><B>batteryLevel</B></TD>
<TD VALIGN="top"><B>batteryStatus</B></TD>
</TR>
TABLEHEADER;
foreach ($this->devices as $device) {
$location = <<<LOCATION
<TABLE BORDER="1">
<TR>
<TD VALIGN="top">timestamp</TD>
<TD VALIGN="top">{$device->location->timestamp}</TD>
</TR>
<TR>
<TD VALIGN="top">horizontalAccuracy</TD>
<TD VALIGN="top">{$device->location->horizontalAccuracy}</TD>
</TR>
<TR>
<TD VALIGN="top">positionType</TD>
<TD VALIGN="top">{$device->location->positionType}</TD>
</TR>
<TR>
<TD VALIGN="top">longitude</TD>
<TD VALIGN="top">{$device->location->longitude}</TD>
</TR>
<TR>
<TD VALIGN="top">latitude</TD>
<TD VALIGN="top">{$device->location->latitude}</TD>
</TR>
</TABLE>
LOCATION;
print <<<DEVICE
<TR>
<TD VALIGN="top">{$device->ID}</TD>
<TD VALIGN="top">{$device->name}</TD>
<TD VALIGN="top">{$device->displayName}</TD>
<TD VALIGN="top">$location</TD>
<TD VALIGN="top">{$device->class}</TD>
<TD VALIGN="top">{$device->model}</TD>
<TD VALIGN="top">{$device->modelDisplayName}</TD>
<TD VALIGN="top">{$device->batteryLevel}</TD>
<TD VALIGN="top">{$device->batteryStatus}</TD>
</TR>
DEVICE;
}
print <<<TABLEFOOTER
</TABLE>
</PRE>
TABLEFOOTER;
}
/**
* This is where the users credentials are authenticated.
* The Apple_MMe_Host and Apple_MMe_Scope values are saved and used to generate the URL for all subsequent API calls
*/
private function authenticate() {
$url = "https://fmipmobile.icloud.com/fmipservice/device/".$this->username."/initClient";
list($headers, $body) = $this->curlPOST($url, "", $this->username.":".$this->password);
if ($headers["http_code"] == 401) {
throw new Exception('Your iCloud username and/or password are invalid');
}
}
/**
* This is where all the devices are downloaded and processed
* Example: print_r($fmi->devices)
*/
private function getDevices() {
$url = "https://fmipmobile.icloud.com/fmipservice/device/".$this->username."/initClient";
list($headers, $body) = $this->curlPOST($url, "", $this->username.":".$this->password);
$this->devices = array();
for ($x = 0; $x < sizeof($body["content"]); $x++) {
$device = $this->generateDevice($body["content"][$x]);
$this->devices[$device->ID] = $device;
}
}
/**
* This method takes the raw device details from the API and converts it to a FindMyiPhoneDevice object
*/
private function generateDevice($deviceDetails) {
$device = new FindMyiPhoneDevice();
$device->API = $deviceDetails;
$device->ID = $device->API["id"];
$device->batteryLevel = $device->API["batteryLevel"];
$device->batteryStatus = $device->API["batteryStatus"];
$device->class = $device->API["deviceClass"];
$device->displayName = $device->API["deviceDisplayName"];
$device->location = new FindMyiPhoneLocation();
$device->location->timestamp = $device->API["location"]["timeStamp"];
$device->location->horizontalAccuracy = $device->API["location"]["horizontalAccuracy"];
$device->location->positionType = $device->API["location"]["positionType"];
$device->location->longitude = $device->API["location"]["longitude"];
$device->location->latitude = $device->API["location"]["latitude"];
$device->model = $device->API["rawDeviceModel"];
$device->modelDisplayName = $device->API["modelDisplayName"];
$device->name = $device->API["name"];
return $device;
}
/**
* This method refreshes the list of devices on the users iCloud account
*/
private function refreshDevices($deviceID = "") {
$url = "https://fmipmobile.icloud.com/fmipservice/device/".$this->username."/refreshClient";
if (strlen($deviceID) > 0) {
$body = json_encode(array("clientContext"=>array("appVersion"=>$this->client["app-version"], "shouldLocate"=>true, "selectedDevice"=>$deviceID, "fmly"=>true)));
}
list($headers, $body) = $this->curlPOST($url, $body, $this->username.":".$this->password);
$this->devices = array();
for ($x = 0; $x < sizeof($body["content"]); $x++) {
$device = $this->generateDevice($body["content"][$x]);
$this->devices[$device->ID] = $device;
}
}
/**
* Helper method for making POST requests
*/
private function curlPOST($url, $body, $authentication = "") {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, $this->client["user-agent"]);
if (strlen($authentication) > 0) {
curl_setopt($ch, CURLOPT_USERPWD, $authentication);
}
$arrHeaders = array();
$arrHeaders["Content-Length"] = strlen($request);
foreach ($this->client["headers"] as $key=>$value) {
array_push($arrHeaders, $key.": ".$value);
}
curl_setopt($ch, CURLOPT_HTTPHEADER, $arrHeaders);
$response = curl_exec($ch);
$info = curl_getinfo($ch);
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$responseBody = substr($response, $header_size);
$headers = array();
foreach (explode("\r\n", substr($response, 0, $header_size)) as $i => $line) {
if ($i === 0)
$headers['http_code'] = $info["http_code"];
else {
list ($key, $value) = explode(': ', $line);
if (strlen($key) > 0)
$headers[$key] = $value;
}
}
if ($this->debug) {
$debugURL = htmlentities($url);
$debugRequestBody = htmlentities(print_r(json_decode($body, true), true));
$debugHeaders = htmlentities(print_r($headers, true));
$debugResponseBody = htmlentities(print_r(json_decode($responseBody, true), true));
print <<<HTML
<PRE>
<TABLE BORDER="1" CELLPADDING="3">
<TR>
<TD VALIGN="top"><B>URL</B></TD>
<TD VALIGN="top">$debugURL</TD>
</TR>
<TR>
<TD VALIGN="top"><B>Request Body</B></TD>
<TD VALIGN="top"><PRE>$debugRequestBody</PRE></TD>
</TR>
<TR>
<TD VALIGN="top"><B>Response Headers</B></TD>
<TD VALIGN="top"><PRE>$debugHeaders</PRE></TD>
</TR>
<TR>
<TD VALIGN="top"><B>Response Body</B></TD>
<TD VALIGN="top"><PRE>$debugResponseBody</PRE></TD>
</TR>
</TABLE>
</PRE>
HTML;
}
return array($headers, json_decode($responseBody, true));
}
}
class FindMyiPhoneDevice {
public $ID;
public $batteryLevel;
public $batteryStatus;
public $class;
public $displayName;
public $location;
public $model;
public $modelDisplayName;
public $name;
public $API;
}
class FindMyiPhoneLocation {
public $timestamp;
public $horizontalAccuracy;
public $positionType;
public $longitude;
public $latitude;
}