-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathcheck.php
More file actions
246 lines (233 loc) · 9.67 KB
/
check.php
File metadata and controls
246 lines (233 loc) · 9.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
<?php
/**
* WHMCS Licensing Addon - Integration Code Sample
* http://www.whmcs.com/addons/licensing-addon/
*
* The following code is a fully working code sample demonstrating how to
* perform license checks using the WHMCS Licensing Addon. It is PHP 4 and
* 5 compatable. Requires the WHMCS Licensing Addon to be used.
*
* @package WHMCS
* @author WHMCS Limited <development@whmcs.com>
* @copyright Copyright (c) WHMCS Limited 2005-2013
* @license http://www.whmcs.com/license/ WHMCS Eula
* @version $Id$
* @link http://www.whmcs.com/
*/
// This prevents any direct access
exit;
// Replace "yourprefix" with your own unique prefix to avoid conflicts with
// other instances of the licensing addon included within the same scope
function ipals20_check_license($licensekey, $localkey='') {
// -----------------------------------
// -- Configuration Values --
// -----------------------------------
// Enter the url to your WHMCS installation here
$whmcsurl = 'http://www.portal.stallioninternet.com/';
// Must match what is specified in the MD5 Hash Verification field
// of the licensing product that will be used with this check.
$licensing_secret_key = 'Jordan#2014$';
// The number of days to wait between performing remote license checks
$localkeydays = 15;
// The number of days to allow failover for after local key expiry
$allowcheckfaildays = 5;
// -----------------------------------
// -- Do not edit below this line --
// -----------------------------------
$check_token = time() . md5(mt_rand(1000000000, 9999999999) . $licensekey);
$checkdate = date("Ymd");
$domain = $_SERVER['SERVER_NAME'];
$usersip = isset($_SERVER['SERVER_ADDR']) ? $_SERVER['SERVER_ADDR'] : $_SERVER['LOCAL_ADDR'];
$dirpath = dirname(__FILE__);
$verifyfilepath = 'modules/servers/licensing/verify.php';
$localkeyvalid = false;
if ($localkey) {
$localkey = str_replace("\n", '', $localkey); # Remove the line breaks
$localdata = substr($localkey, 0, strlen($localkey) - 32); # Extract License Data
$md5hash = substr($localkey, strlen($localkey) - 32); # Extract MD5 Hash
if ($md5hash == md5($localdata . $licensing_secret_key)) {
$localdata = strrev($localdata); # Reverse the string
$md5hash = substr($localdata, 0, 32); # Extract MD5 Hash
$localdata = substr($localdata, 32); # Extract License Data
$localdata = base64_decode($localdata);
$localkeyresults = unserialize($localdata);
$originalcheckdate = $localkeyresults['checkdate'];
if ($md5hash == md5($originalcheckdate . $licensing_secret_key)) {
$localexpiry = date("Ymd", mktime(0, 0, 0, date("m"), date("d") - $localkeydays, date("Y")));
if ($originalcheckdate > $localexpiry) {
$localkeyvalid = true;
$results = $localkeyresults;
$validdomains = explode(',', $results['validdomain']);
if (!in_array($_SERVER['SERVER_NAME'], $validdomains)) {
$localkeyvalid = false;
$localkeyresults['status'] = "Invalid";
$results = array();
}
$validips = explode(',', $results['validip']);
if (!in_array($usersip, $validips)) {
$localkeyvalid = false;
$localkeyresults['status'] = "Invalid";
$results = array();
}
$validdirs = explode(',', $results['validdirectory']);
if (!in_array($dirpath, $validdirs)) {
$localkeyvalid = false;
$localkeyresults['status'] = "Invalid";
$results = array();
}
}
}
}
}
if (!$localkeyvalid) {
$postfields = array(
'licensekey' => $licensekey,
'domain' => $domain,
'ip' => $usersip,
'dir' => $dirpath,
);
if ($check_token) $postfields['check_token'] = $check_token;
$query_string = '';
foreach ($postfields AS $k=>$v) {
$query_string .= $k.'='.urlencode($v).'&';
}
if (function_exists('curl_exec')) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $whmcsurl . $verifyfilepath);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $query_string);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
curl_close($ch);
} else {
$fp = fsockopen($whmcsurl, 80, $errno, $errstr, 5);
if ($fp) {
$newlinefeed = "\r\n";
$header = "POST ".$whmcsurl . $verifyfilepath . " HTTP/1.0" . $newlinefeed;
$header .= "Host: ".$whmcsurl . $newlinefeed;
$header .= "Content-type: application/x-www-form-urlencoded" . $newlinefeed;
$header .= "Content-length: ".@strlen($query_string) . $newlinefeed;
$header .= "Connection: close" . $newlinefeed . $newlinefeed;
$header .= $query_string;
$data = '';
@stream_set_timeout($fp, 20);
@fputs($fp, $header);
$status = @socket_get_status($fp);
while (!@feof($fp)&&$status) {
$data .= @fgets($fp, 1024);
$status = @socket_get_status($fp);
}
@fclose ($fp);
}
}
if (!$data) {
$localexpiry = date("Ymd", mktime(0, 0, 0, date("m"), date("d") - ($localkeydays + $allowcheckfaildays), date("Y")));
if ($originalcheckdate > $localexpiry) {
$results = $localkeyresults;
} else {
$results = array();
$results['status'] = "Invalid";
$results['description'] = "Remote Check Failed";
return $results;
}
} else {
preg_match_all('/<(.*?)>([^<]+)<\/\\1>/i', $data, $matches);
$results = array();
foreach ($matches[1] AS $k=>$v) {
$results[$v] = $matches[2][$k];
}
}
if (!is_array($results)) {
die("Invalid License Server Response");
}
if ($results['md5hash']) {
if ($results['md5hash'] != md5($licensing_secret_key . $check_token)) {
$results['status'] = "Invalid";
$results['description'] = "MD5 Checksum Verification Failed";
return $results;
}
}
if ($results['status'] == "Active") {
$results['checkdate'] = $checkdate;
$data_encoded = serialize($results);
$data_encoded = base64_encode($data_encoded);
$data_encoded = md5($checkdate . $licensing_secret_key) . $data_encoded;
$data_encoded = strrev($data_encoded);
$data_encoded = $data_encoded . md5($data_encoded . $licensing_secret_key);
$data_encoded = wordwrap($data_encoded, 80, "\n", true);
$results['localkey'] = $data_encoded;
}
$results['remotecheck'] = true;
}
unset($postfields,$data,$matches,$whmcsurl,$licensing_secret_key,$checkdate,$usersip,$localkeydays,$allowcheckfaildays,$md5hash);
return $results;
}
// Get the license key and local key from storage
// These are typically stored either in flat files or an SQL database
$licensekey = "";
$localkey = "";
$base = __DIR__;
$handle = fopen($base."/license.txt", "r");
if ($handle) {
$count = 0;
while (($line = fgets($handle)) !== false) {
// process the line read.
if ($count == 0) {
$licensekey = trim($line);
} else if ($count == 1) {
$localkey = trim($line);
break;
}
$count++;
}
fclose($handle);
} else {
die("Could not read license file. Please contact support.");
}
echo $licensekey."<br/>";
echo $localkey."<br/>";
// Validate the license key information
$results = licensetest_check_license($licensekey, $localkey);
// Raw output of results for debugging purpose
echo '<textarea cols="100" rows="20">' . print_r($results, true) . '</textarea>';
// Interpret response
switch ($results['status']) {
case "Active":
// get new local key and save it somewhere
$localkeydata = str_replace(' ','',preg_replace('/\s+/', ' ', $results['localkey']));
$handle = fopen($base."/license.txt", "r");
if ($handle) {
$count = 0;
while (($line = fgets($handle)) !== false) {
// process the line read.
if ($count == 0) {
$licensekey = trim($line);
break;
}
$count++;
}
fclose($handle);
if (isset($results['localkey'])) {
$textfile = fopen($base . "/license.txt", "w") or die("Unable to open file!");
$contents = $licensekey . "\n" . $localkeydata . "\n";
fwrite($textfile, $contents);
fclose($textfile);
}
} else {
die("Could not read license file. Please contact support.");
}
break;
case "Invalid":
die("License key is Invalid");
break;
case "Expired":
die("License key is Expired");
break;
case "Suspended":
die("License key is Suspended");
break;
default:
die("Invalid Response");
break;
}