1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Farhadhp\ZhaketGuard; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Client; |
6
|
|
|
|
7
|
|
|
class ZhaketGuard |
8
|
|
|
{ |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Dont change this |
12
|
|
|
* |
13
|
|
|
* @var string |
14
|
|
|
*/ |
15
|
|
|
private static $apiUrl = 'http://guard.zhaket.com/api/'; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @param $method |
19
|
|
|
* @param array $params |
20
|
|
|
* @return \Psr\Http\Message\StreamInterface|string |
21
|
|
|
*/ |
22
|
|
|
public static function request($method, $params=[]) |
23
|
|
|
{ |
24
|
|
|
if (empty($method)) { |
25
|
|
|
return 'request method can not be empty.'; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
$client = new Client(); |
29
|
|
|
$response = $client->request('GET', self::$apiUrl.$method, [ |
30
|
|
|
'query' => $params, |
31
|
|
|
'http_errors' => false, |
32
|
|
|
]); |
33
|
|
|
return $response->getBody(); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* install license |
38
|
|
|
* |
39
|
|
|
* @param $licenseToken |
40
|
|
|
* @param $productToken |
41
|
|
|
* @return \Psr\Http\Message\StreamInterface|string |
42
|
|
|
*/ |
43
|
|
|
public static function installLicense($productToken, $licenseToken) |
44
|
|
|
{ |
45
|
|
|
return self::request('install-license', [ |
46
|
|
|
'product_token' => $productToken, |
47
|
|
|
'token' => $licenseToken, |
48
|
|
|
'domain' => self::getHost(), |
49
|
|
|
]); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Validate license |
54
|
|
|
* |
55
|
|
|
* @param $licenseToken |
56
|
|
|
* @return \Psr\Http\Message\StreamInterface|string |
57
|
|
|
*/ |
58
|
|
|
public static function isValidLicense($licenseToken) |
59
|
|
|
{ |
60
|
|
|
return self::request('validation-license', [ |
61
|
|
|
'token' => $licenseToken, |
62
|
|
|
'domain' => self::getHost(), |
63
|
|
|
]); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* get host |
68
|
|
|
* |
69
|
|
|
* @return string |
70
|
|
|
*/ |
71
|
|
|
public static function getHost() { |
72
|
|
|
$possibleHostSources = array('HTTP_X_FORWARDED_HOST', 'HTTP_HOST', 'SERVER_NAME', 'SERVER_ADDR'); |
73
|
|
|
$sourceTransformations = array( |
74
|
|
|
"HTTP_X_FORWARDED_HOST" => function($value) { |
75
|
|
|
$elements = explode(',', $value); |
76
|
|
|
return trim(end($elements)); |
77
|
|
|
} |
78
|
|
|
); |
79
|
|
|
$host = ''; |
80
|
|
|
foreach ($possibleHostSources as $source) |
81
|
|
|
{ |
82
|
|
|
if (!empty($host)) break; |
83
|
|
|
if (empty($_SERVER[$source])) continue; |
84
|
|
|
$host = $_SERVER[$source]; |
85
|
|
|
if (array_key_exists($source, $sourceTransformations)) |
86
|
|
|
{ |
87
|
|
|
$host = $sourceTransformations[$source]($host); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
// Remove port number from host |
92
|
|
|
$host = preg_replace('/:\d+$/', '', $host); |
93
|
|
|
// remove www from host |
94
|
|
|
$host = str_ireplace('www.', '', $host); |
95
|
|
|
|
96
|
|
|
return trim($host); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
} |