Last active
August 29, 2015 14:11
-
-
Save wolfposd/843da23ce869ac4a4bf9 to your computer and use it in GitHub Desktop.
PHP Cydia API wrapper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
The MIT License (MIT) | |
Copyright (c) <2014> <@wolfposd> | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in | |
all copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
THE SOFTWARE. | |
*/ | |
/* | |
How to use: | |
$c = new CydiaCheck("device-udid", "com.github.package", "vendor", "aabbccddeeffgg"); | |
$c->requestPurchaseStatus(); | |
if($c->isPaid()) { | |
echo "Is paid"; | |
} | |
else{ | |
echo "Is not paid"; | |
} | |
*/ | |
class CydiaCheck | |
{ | |
private $vendor; | |
private $udid; | |
private $package; | |
private $apikey; | |
public $paymentstatus; | |
/** | |
* Create a new CydiaApiCheck | |
* @param string $udid the UDID of the device | |
* @param string $package the package id to check, eg: com.website.package | |
* @param string $vendor the vendor id of your package | |
* @param string $apikey the secret cydia-api-key | |
*/ | |
function __construct($udid, $package, $vendor, $apikey) | |
{ | |
$this->vendor = $vendor; | |
$this->udid = $udid; | |
$this->apikey = $apikey; | |
$this->package = $package; | |
$this->paymentstatus = array(); | |
} | |
/** | |
* prepares the query-string for a curl-request | |
* @return string cydia-api-query | |
*/ | |
function constructAPIRequest() | |
{ | |
$tempQuery = 'api=store-0.9&device='.$this->udid. | |
'&mode=local&nonce='.time(). | |
'&package='.$this->package. | |
'×tamp='.time(). | |
"&vendor=".$this->vendor; | |
return "http://cydia.saurik.com/api/check?".$tempQuery."&signature=".hash_SHA1($tempQuery, $this->apikey); | |
} | |
/** | |
* Request the purchase status from the cydia-server | |
* @return associative array with purchase details | |
*/ | |
function requestPurchaseStatus() | |
{ | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
curl_setopt($ch, CURLOPT_URL, $this->constructAPIRequest()); | |
$result = curl_exec($ch); | |
curl_close($ch); | |
$this->paymentstatus = $this->convertPurchaseStatus($result); | |
return $result; | |
} | |
function convertPurchaseStatus($stringstatus) | |
{ | |
$output = array(); | |
parse_str($stringstatus, $output); | |
return $output; | |
} | |
function isPaid() | |
{ | |
return $this->paymentstatus["state"] == "completed"; | |
} | |
} | |
/** | |
* create an HMAC from message in BASE64 | |
* @param string $message the message to verify | |
* @param string $key the key to use | |
* @param boolean $rawdata , if false returns hex-chars | |
* @return string BASE64(HMAC(message)) | |
*/ | |
function hash_SHA1($message, $key, $rawdata = true) | |
{ | |
$hashed = base64_encode(hash_hmac('SHA1', $message, $key, $rawdata)); | |
$hashed = str_replace("/", "_", $hashed); | |
$hashed = str_replace("=", "", $hashed); | |
$hashed = str_replace("+", "-", $hashed); | |
return $hashed; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment