iCloud positionsspårning för IPhone

Diskussioner runt hur den tänkta funktionaliteten nås

Moderator: elf98

bobkelso_527
Wannabe
Inlägg: 15
Blev medlem: 20 sep 2016, 10:35
Ort: Hjo

iCloud positionsspårning för IPhone

Inlägg av bobkelso_527 » 23 dec 2016, 13:03

Hej Gott Folk.
Efter en tid har jag letat efter en (bra) lösning för att spåra telefoner (Iphone) och dess position.
Befintliga lösningar har inte fungerat tillfredställande.

Så här kommer min lösning. Fortfarande under arbete men bör fungera.
Metoden bygger på att hämta koordinaterna från iCloud (Hitta min iPhone).
Jag kör scriptet på min lokala server.

Senare kommer jag även lägga till batterinivå och annat gott.

Om nån vill vara med och bidra (finns instruktioner för att komma igång)
https://github.com/raess1/iCloud-HC2-Bridge




index.php

Kod: Markera allt

<?PHP
	
    //Save data to Home Centar 2
	$url = 'http://user:password@dittip/api/globalVariables';

	function setGlobal($globalVariables, $value)
	{
		$curl = curl_init();

		curl_setopt_array($curl, array(
		  CURLOPT_URL => "http://dittip/api/globalVariables/".$globalVariables,
		  CURLOPT_RETURNTRANSFER => true,
		  CURLOPT_ENCODING => "",
		  CURLOPT_MAXREDIRS => 10,
		  CURLOPT_TIMEOUT => 30,
		  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
		  CURLOPT_CUSTOMREQUEST => "PUT",
		  CURLOPT_POSTFIELDS => '{"value":"'.$value.'"}',
		  CURLOPT_HTTPHEADER => array(
		    "authorization: Basic asdagsdgsdfsfs=",
		    "cache-control: no-cache",
		    "content-type: application/json",
		    "postman-token: 60c9034f-7294-f178-a440-8d6d23d23ddc"
		  ),
		));

		$response = curl_exec($curl);
		$err = curl_error($curl);

		curl_close($curl);

		if ($err) {
		  echo "cURL Error #:" . $err;
		} else {
		  return $response;
		};
	}

	// This should properly display device names that contain special characters
	header("Content-type: text/html; charset=utf-8");

	// This will prevent any errors that occur from potentially displaying your username/password
	error_reporting(1);


	$phone_id = 'ID';
	

	// Include the FindMyiPhone class
	include ("class.findmyiphone.php");
	
	// This is where we log in to iCloud
	try {
		$fmi = new FindMyiPhone("iClouduser","password");
	} catch (Exception $e) {
		print "Error: ".$e->getMessage();
		exit;
	}

	$devices = $fmi->listDevices();
	/*
	foreach ($devices as $key => $device) {
		if($key == $phone_id){
			$latitude = $device->API['location']['latitude'];
			$longitude = $device->API['location']['longitude'];



			setGlobal('location_latitude',$latitude);
			setGlobal('location_longitude',$longitude);
		}
	}*/

	/*

	[latitude] => 58.2994802739
                    [horizontalAccuracy] => 8
                    [locationType] => 
                    [longitude] => 14.2716976115
                    */

	$delta = 0.001;//chnage this
	//centar of home
	$lat['min']=58.2994752034 - $delta;
	$lat['max']=58.2994802739 + $delta;
	$long['min']=14.2716976115 - $delta;
	$long['max']=14.2716976115 + $delta;

	$home = 0;

	foreach ($devices as $key => $device) {

		//debug
		$device_name = $device->displayName;
		$location = $device->API['location'];

		ddd('device', $device_name); ddd('location', $location);
		//end dubug

		$latitude = $device->API['location']['latitude'];
		$longitude = $device->API['location']['longitude'];

		if($latitude > $lat['min'] and $latitude < $lat['max'] and $longitude > $long['min'] and $longitude < $long['max'] )
		{
			$home = 1;
		}
	}

	$result = setGlobal('home',$home);
	ddd('result', $result);


	function ddd($context, $var){
		echo '<pre>';
		print_r($var);
		echo '</pre>';	
	}
class.findmyiphone.php

Kod: Markera allt

<?PHP

/*
 Copyright (C) Alan Beebe (alan.beebe@gmail.com).
 
 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;
	private $Apple_MMe_Host;
	private $Apple_MMe_Scope;
	public $devices = array();
	
	/**
     * This is where you initialize FindMyiPhone with your iCloud credentials
     * Example: $fmi = new FindMyiPhone("you@example.com", "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://".$this->Apple_MMe_Host."/fmipservice/device/".$this->Apple_MMe_Scope."/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://".$this->Apple_MMe_Host."/fmipservice/device/".$this->Apple_MMe_Scope."/lostDevice";
		$body = json_encode(array("device"=>$deviceID, "ownerNbr"=>$phoneNumber, "text"=>$message, "lostModeEnabled"=>true)); 
		list($headers, $body) = $this->curlPOST($url, $body, $this->username.":".$this->password);
	}


	public function listDevices(){
		if (sizeof($this->devices) == 0) $this->getDevices();

		return $this->devices;

	}

	/**
     * 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);
		$this->Apple_MMe_Host = $headers["X-Apple-MMe-Host"];
		$this->Apple_MMe_Scope = $headers["X-Apple-MMe-Scope"];
		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://".$this->Apple_MMe_Host."/fmipservice/device/".$this->Apple_MMe_Scope."/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://".$this->Apple_MMe_Host."/fmipservice/device/".$this->Apple_MMe_Scope."/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;
}
ih.jpg
svar
Bilagor
hom.jpg
hc2
hom.jpg (13.33 KiB) Visad 1606 gånger

bobkelso_527
Wannabe
Inlägg: 15
Blev medlem: 20 sep 2016, 10:35
Ort: Hjo

Re: iCloud positionsspårning för IPhone

Inlägg av bobkelso_527 » 23 dec 2016, 16:46

Ändring:
Nu kan den även skriva variablerna battery_level och battery_status 8)
Kod ändringarna finns på https://github.com/raess1/iCloud-HC2-Bridge
hc2.tiff
hc2
hc2.tiff (48.6 KiB) Visad 1580 gånger

bobkelso_527
Wannabe
Inlägg: 15
Blev medlem: 20 sep 2016, 10:35
Ort: Hjo

Re: iCloud positionsspårning för IPhone

Inlägg av bobkelso_527 » 24 dec 2016, 10:43

Update:
Nu kan man även se vart telefonen befinner sig.
sdf.jpg
vd iPhone track
sdf.jpg (53.54 KiB) Visad 1523 gånger
senaste koden finns på git.

bobkelso_527
Wannabe
Inlägg: 15
Blev medlem: 20 sep 2016, 10:35
Ort: Hjo

Re: iCloud positionsspårning för IPhone

Inlägg av bobkelso_527 » 26 dec 2016, 22:53

Update:
Funktionslista
- Få adress på platsen telefonen befinner sig på (frågar google om adress för telefonens koordinater)
- Få status på om telefonen sitter i laddaren eller inte.
- Få batteriprocent
- Är telefonen hemma? (Hemma koordinater ställs in)
- När datan senast hämtas (ska konvertera till något läsbart)
- Hur hittades datan Gps, Wifi, osv.
- Få information om hur pålitlig datan är med "Accuracy range"
- Home Since (ej färdig. Ska visa information om när telefonen kom hem eller är borta)
- Avstånd till hem. Beräknar avstånden från telefonen till hem koordinater. (Kan anges i Centimeter, Meter, Kilometer.)
- Hur långt tid tar det att ta sig hem. Beräknar tiden från telefonen till hem koordinater och frågar google hur långt tid det tar att köra. Kan fås i Sekunder och minturer. (Kommer lägga till Timmar också.

senaste koden finns på git.
123.jpg
update
123.jpg (19.36 KiB) Visad 1422 gånger

Skriv svar