Skip to content

Instantly share code, notes, and snippets.

@cvele

cvele/Base.php Secret

Created February 20, 2015 15:05
Show Gist options
  • Save cvele/b485eec10d16d155c723 to your computer and use it in GitHub Desktop.
Save cvele/b485eec10d16d155c723 to your computer and use it in GitHub Desktop.
<?php
namespace AppBundle\iCloud;
use GuzzleHttp\Client as GuzzleClient;
use Rhumsaa\Uuid\Uuid;
class Base {
private $_home_endpoint = 'https://www.icloud.com';
private $_setup_endpoint = 'https://p12-setup.icloud.com/setup/ws/1';
private $_push_endpoint = 'https://p12-pushws.icloud.com';
private $_base_login_url;
private $_base_validate_url;
private $_base_system_url;
private $_base_webauth_url;
private $cookie_jar;
private $cookie_object;
private $headers;
private $http_client;
private $params = [];
private $debug = false;
private $username;
private $password;
private $discovery;
private $webservices;
public function __construct($username, $password)
{
$this->username = $username;
$this->password = $password;
$this->_base_login_url = sprintf('%s/login', $this->_setup_endpoint);
$this->_base_validate_url = sprintf('%s/validate', $this->_setup_endpoint);
$this->_base_system_url = sprintf('%s/system/version.json', $this->_home_endpoint);
$this->_base_webauth_url = sprintf('%s/refreshWebAuth', $this->_push_endpoint);
$this->headers = [
'Host' => 'setup.icloud.com',
'Origin' => $this->_home_endpoint,
'Referer' => sprintf('%s/', $this->_home_endpoint),
'User-Agent' => 'Opera/9.52 (X11; Linux i686; U; en)'
];
$this->http_client = new GuzzleClient([
'defaults' => [
'debug' => $this->debug,
'cookies' => true,
'headers' => $this->headers,
'verify' => false,
'allow_redirects' => true
]
]);
$this->authenticate();
$this->authenticate();
}
protected function refreshValidate()
{
try
{
$response = $this->http_client->get($this->_base_validate_url);
}
catch (\Exception $e)
{
return;
// the first request will compain about a X-APPLE-WEBAUTH-TOKEN cookie
}
$resp = $response->json();
if (isset($resp['dsInfo']))
{
$dsid = $resp['dsInfo']['dsid'];
$this->params['dsid'] = $dsid;
}
if (isset($resp['instance']))
{
$instance = $resp['instance'];
}
else
{
$uuid4 = Uuid::uuid4();
$instance = str_replace("-", "", $uuid4->toString());
}
$sha = sha1($this->username . $instance);
$this->params['id'] = strtoupper($sha);
$uuid1 = Uuid::uuid1();
$clientId = strtoupper($uuid1->toString());
$this->params['clientBuildNumber'] = '14E45';
$this->params['clientId'] = $clientId;
}
public function authenticate()
{
$this->refreshValidate();
$id = strtoupper(sha1($this->username));
$data = [
'extended_login' => False,
'id' => $id,
'apple_id' => $this->username,
'password' => $this->password
];
try
{
$response = $this->http_client->post($this->_base_login_url, ['query'=>$this->params, 'body'=>$this->params, 'json'=>$data]);
}
catch (\Exception $e)
{
$msg = "Invalid username/password combination";
}
$this->refreshValidate();
$this->discovery = $response->json();
$this->webservices = $this->discovery['webservices'];
}
public function devices()
{
$service_root = $this->webservices['findme']['url'];
$findMyiPhoneServiceManager = new Services\FindMyiPhoneServiceManager($service_root, $this->http_client, $this->params);
}
}
<?php
namespace AppBundle\iCloud\Services;
class FindMyiPhoneServiceManager {
private $_service_root;
private $_fmip_endpoint;
private $_fmip_refresh_url;
private $_fmip_sound_url;
private $_fmip_message_url;
private $_fmip_lost_url;
private $http_client;
private $params;
private $devices = [];
public function __construct($service_root, $http_client, $params)
{
$this->http_client = $http_client;
$this->params = $params;
$this->_service_root = $service_root;
$this->_fmip_endpoint = sprintf('%s/fmipservice/client/web', $this->_service_root);
$this->_fmip_refresh_url = sprintf('%s/refreshClient', $this->_fmip_endpoint);
$this->_fmip_sound_url = sprintf('%s/playSound', $this->_fmip_endpoint);
$this->_fmip_message_url = sprintf('%s/sendMessage', $this->_fmip_endpoint);
$this->_fmip_lost_url = sprintf('%s/lostDevice', $this->_fmip_endpoint);
$this->refreshClient();
}
private function refreshClient()
{
$parse = parse_url($this->_service_root);
$host = $parse['host'];
$data = [
'clientContext'=>[
'fmly' => True,
'shouldLocate' => True,
'selectedDevice' => 'all',
]
];
$response = $this->http_client->post($this->_fmip_refresh_url, [
'headers' => [
'host' => $host
],
'debug' => true,
'query' => $this->params,
'body' => $this->params,
'json' => $data
]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment