Last active
July 21, 2016 01:18
-
-
Save trcio/39ed1d122a5f9b55291c to your computer and use it in GitHub Desktop.
Netseal's PHP wrapper is dreadful, so I remade it
This file contains hidden or 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 | |
class NetsealProvider { | |
const ENDPOINT = 'http://seal.nimoru.com/'; | |
const LICENSE_FREE = 0; | |
const LICENSE_BRONZE = 1; | |
const LICENSE_SILVER = 2; | |
const LICENSE_GOLD = 3; | |
const LICENSE_PLATINUM = 4; | |
const LICENSE_DIAMOND = 5; | |
const STATUS_SUCCESS = "\x0"; | |
const STATUS_FAILED = "\x1"; | |
const STATUS_BAD_PARAMS = "\x4"; | |
const STATUS_BAD_LENGTH = "\x5"; | |
const STATUS_BAD_FORMAT = "\x6"; | |
const STATUS_NULL_VALUE = "\x7"; | |
const STATUS_USED_VALUE = "\x8"; | |
const STATUS_ACCESS_DENIED = "\x9"; | |
const STATUS_LIMIT_REACHED = "\xA"; | |
const STATUS_SYSTEM_OFFLINE = "\xFF"; | |
public $token; | |
public $status; | |
public function __construct($token) { | |
if (empty($token) || strlen($token) != 20) | |
throw new Exception('Invalid token'); | |
$this->token = $token; | |
} | |
private function callRemoteFunction($name, $data) { | |
$remoteEndpoint = self::ENDPOINT; | |
if (!$remoteEndpoint) | |
return false; | |
$data['key'] = $this->token; | |
$response = $this->curl($remoteEndpoint . 'Remote/' . $name . '.php', $data); | |
if (!$response) | |
return false; | |
if (strlen($response) == 0) { | |
$this->status = self::STATUS_FAILED; | |
return false; | |
} | |
$this->status = $response[0]; | |
return $response; | |
} | |
private function curl($url, array $postData) { | |
$curl = curl_init($url); | |
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); | |
curl_setopt($curl, CURLOPT_MAXREDIRS, 4); | |
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); | |
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 8000); | |
curl_setopt($curl, CURLOPT_POST, true); | |
curl_setopt($curl, CURLOPT_POSTFIELDS, $postData); | |
$res = curl_exec($curl); | |
curl_close($curl); | |
return $res; | |
} | |
public function getStatus() { | |
switch ($this->status) { | |
case self::STATUS_SUCCESS: | |
return 'SUCCESS'; | |
case self::STATUS_FAILED: | |
return 'FAILED'; | |
case self::STATUS_BAD_PARAMS: | |
return 'BAD PARAMS'; | |
case self::STATUS_BAD_LENGTH: | |
return 'BAD LENGTH'; | |
case self::STATUS_NULL_VALUE: | |
return 'NULL VALUE'; | |
case self::STATUS_USED_VALUE: | |
return 'USED VALUE'; | |
case self::STATUS_ACCESS_DENIED: | |
return 'ACCESS DENIED'; | |
case self::STATUS_LIMIT_REACHED: | |
return 'LIMIT REACHED'; | |
case self::STATUS_SYSTEM_OFFLINE: | |
return 'SYSTEM OFFLINE'; | |
default: | |
return 'UNKNOWN ERROR'; | |
} | |
} | |
public function encrypt($data, $key) { | |
$len = strlen($data); | |
$out = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $data, MCRYPT_MODE_CBC, $key); | |
return pack('i', $len) . $out; | |
} | |
public function decrypt($base64, $key) { | |
$data = base64_decode($base64); | |
$array = unpack('i', $data); | |
$len = $array[1]; | |
$out = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, substr($data, 4), MCRYPT_MODE_CBC, $key); | |
return substr($out, 0, $len); | |
} | |
public function createCode($time, $points, $type, $track) { | |
$data = [ | |
'time' => $time, | |
'points' => $points, | |
'type' => $type, | |
'track' => $track | |
]; | |
$response = $this->callRemoteFunction('createCode3', $data); | |
if ($response) | |
return substr($response, 1); | |
return false; | |
} | |
public function deleteCode($code) { | |
$data = [ | |
'code' => $code | |
]; | |
if ($this->callRemoteFunction('deleteCode', $data)) | |
return $this->status == self::STATUS_SUCCESS; | |
return false; | |
} | |
public function isUserOnline($name, $checkIp = true) { | |
$data = [ | |
'name' => $name | |
]; | |
$response = $this->callRemoteFunction('userOnline2', $data); | |
if ($response) { | |
$array = explode("\x0", substr($response, 1)); | |
if ($checkIp && @$array[1] != $_SERVER['REMOTE_ADDR']) { | |
return false; | |
} | |
return (bool)$array[0]; | |
} | |
return false; | |
} | |
public function getPrivateKey($publicToken) { | |
$data = [ | |
'pub' => $publicToken | |
]; | |
$response = $this->callRemoteFunction('privateKey', $data); | |
if ($response) | |
return substr($response, 1); | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment