Spaces:
Sleeping
Sleeping
File size: 20,115 Bytes
07c3cdd | 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 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 | <?php
class license_application extends Padl
{
/**
* The number of allowed differences between the $_SERVER vars and the vars
* stored in the key
*
* @var number
*/
public $_ALLOWED_SERVER_DIFS = 0;
/**
* The number of allowed differences between the $ip vars in the key and the ip
* vars collected from the server
*
* @var number
*/
public $_ALLOWED_IP_DIFS = 0;
/**
* the path of the license key file, remember this would be relative to the
* include path of the class file.
*/
public $_LICENSE_PATH;
/**
* Constructor
*
* @access public
* @param $use_mcrypt boolean Determines if mcrypt encryption is used or not (defaults to true,
* however if mcrypt is not available, it is set to false)
* @param $use_time boolean Sets if time binding should be used in the key (defaults to true)
* @param $use_server boolean Sets if server binding should be used in the key (defaults to true)
* @param $allow_local boolean Sets if server binding is in use then localhost servers are valid (defaults to false)
* */
public function license_application($license_path = 'license.dat', $use_mcrypt = true, $use_time = true, $use_server = true, $allow_local = false, $challenge = false)
{
//Check to see if the class has been secured
if (isset($_SESSION)) {
unset($_SESSION["__sw__"]);
if ($challenge) {
$_SESSION["__sw__"] = true;
}
}
$this->_check_secure();
$this->_LICENSE_PATH = $license_path;
$this->init($use_mcrypt, $use_time, $use_server, $allow_local);
if ($this->USE_SERVER) {
$this->_MAC = $this->_get_mac_address();
}
}
/**
* set_server_vars
*
* to protect against spoofing you should copy the $_SERVER vars into a
* seperate array right at the first line of your script so parameters can't
* be changed in unencoded php files. This doesn't have to be set. If it is
* not set then the $_SERVER is copied when _get_server_info (private) function
* is called.
*
* @access public
* @param $array array The copied $_SERVER array
* */
public function set_server_vars($array)
{
# check to see if the class has been secured
$this->_check_secure();
$this->_SERVER_VARS = $array;
# some of the ip data is dependant on the $_SERVER vars, so update them
# after the vars have been set
$this->_IPS = $this->_get_ip_address();
# update the server info
$this->_SERVER_INFO = $this->_get_server_info();
}
/**
* _get_os_var
*
* gets various vars depending on the os type
*
* @access private
* @return string various values
* */
public function _get_os_var($var_name, $os)
{
$var_name = strtolower($var_name);
# switch between the os's
switch ($os) {
# not sure if the string is correct for FreeBSD
# not tested
case 'freebsd':
# not sure if the string is correct for NetBSD
# not tested
case 'netbsd':
# not sure if the string is correct for Solaris
# not tested
case 'solaris':
# not sure if the string is correct for SunOS
# not tested
case 'sunos':
# darwin is mac os x
# tested only on the client os
case 'darwin':
# switch the var name
switch ($var_name) {
case 'conf':
$var = '/sbin/ifconfig';
break;
case 'mac':
$var = 'ether';
break;
case 'ip':
$var = 'inet ';
break;
}
break;
# linux variation
# tested on server
case 'linux':
# switch the var name
switch ($var_name) {
case 'conf':
$var = '/sbin/ifconfig';
break;
case 'mac':
$var = 'HWaddr';
break;
case 'ip':
$var = 'inet addr:';
break;
}
break;
}
return $var;
}
/**
* _get_config
*
* gets the server config file and returns it. tested on Linux,
* Darwin (Mac OS X), and Win XP. It may work with others as some other
* os's have similar ifconfigs to Darwin but they haven't been tested
*
* @access private
* @return string config file data
* */
public function _get_config()
{
# check to see if the class has been secured
$this->_check_secure();
if (ini_get('safe_mode')) {
# returns invalid because server is in safe mode thus not allowing
# sbin reads but will still allow it to open. a bit weird that one.
return 'SAFE_MODE';
}
# if anyone has any clues for windows environments
# or other server types let me know
$os = strtolower(PHP_OS);
if (substr($os, 0, 3) == 'win') {
# this windows version works on xp running apache
# based server. it has not been tested with anything
# else, however it should work with NT, and 2000 also
# execute the ipconfig
@exec('ipconfig/all', $lines);
# count number of lines, if none returned return MAC_404
# thanks go to Gert-Rainer Bitterlich <bitterlich -at- ima-dresden -dot- de>
if (count($lines) == 0) {
return 'ERROR_OPEN';
}
# $path the lines together
$conf = implode($this->_LINEBREAK, $lines);
} else {
# get the conf file name
$os_file = $this->_get_os_var('conf', $os);
# open the ipconfig
$fp = @popen($os_file, "rb");
# returns invalid, cannot open ifconfig
if (!$fp) {
return 'ERROR_OPEN';
}
# read the config
$conf = @fread($fp, 4096);
@pclose($fp);
}
return $conf;
}
/**
* _get_ip_address
*
* Used to get the MAC address of the host server. It works with Linux,
* Darwin (Mac OS X), and Win XP. It may work with others as some other
* os's have similar ifconfigs to Darwin but they haven't been tested
*
* @access private
* @return array IP Address(s) if found (Note one machine may have more than one ip)
* @return string ERROR_OPEN means config can't be found and thus not opened
* @return string IP_404 means ip adress doesn't exist in the config file and can't be found in the $_SERVER
* @return string SAFE_MODE means server is in safe mode so config can't be read
* */
public function _get_ip_address()
{
$ips = array();
# get the cofig file
$conf = $this->_get_config();
# if the conf has returned and error return it
if ($conf != 'SAFE_MODE' && $conf != 'ERROR_OPEN') {
# if anyone has any clues for windows environments
# or other server types let me know
$os = strtolower(PHP_OS);
if (substr($os, 0, 3) == 'win') {
# anyone any clues on win ip's
} else {
# explode the conf into seperate lines for searching
$lines = explode($this->_LINEBREAK, $conf);
# get the ip delim
$ip_delim = $this->_get_os_var('ip', $os);
# ip pregmatch
$num = "(\\d|[1-9]\\d|1\\d\\d|2[0-4]\\d|25[0-5])";
# seperate the lines
foreach ($lines as $key => $line) {
# check for the ip signature in the line
if (!preg_match("/^$num\\.$num\\.$num\\.$num$/", $line) && strpos($line, $ip_delim)) {
# seperate out the ip
$ip = substr($line, strpos($line, $ip_delim) + strlen($ip_delim));
$ip = trim(substr($ip, 0, strpos($ip, " ")));
# add the ip to the collection
if (!isset($ips[$ip])) {
$ips[$ip] = $ip;
}
}
}
}
}
# if the conf has returned nothing
# attempt to use the $_SERVER data
if (isset($this->_SERVER_VARS['SERVER_NAME'])) {
$ip = gethostbyname($this->_SERVER_VARS['SERVER_NAME']);
if (!isset($ips[$ip])) {
$ips[$ip] = $ip;
}
}
# count return ips and return if found
if (count($ips) > 0) {
return $ips;
}
if (isset($this->_SERVER_VARS['SERVER_ADDR'])) {
$name = gethostbyaddr($this->_SERVER_VARS['SERVER_ADDR']);
$ip = gethostbyname($name);
if (!isset($ips[$ip])) {
$ips[$ip] = $ip;
}
# if the $_SERVER addr is not the same as the returned ip include it aswell
if ($ip != $this->_SERVER_VARS['SERVER_ADDR']) {
if (!isset($ips[$this->_SERVER_VARS['SERVER_ADDR']])) {
$ips[$this->_SERVER_VARS['SERVER_ADDR']] = $this->_SERVER_VARS['SERVER_ADDR'];
}
}
}
# count return ips and return if found
if (count($ips) > 0) {
return $ips;
}
# failed to find an ip check for conf error or return 404
if ($conf == 'SAFE_MODE' || $conf == 'ERROR_OPEN') {
return $conf;
}
return 'IP_404';
}
/**
* _get_mac_address
*
* Used to get the MAC address of the host server. It works with Linux,
* Darwin (Mac OS X), and Win XP. It may work with others as some other
* os's have similar ifconfigs to Darwin but they haven't been tested
*
* @access private
* @return string Mac address if found
* @return string ERROR_OPEN means config can't be found and thus not opened
* @return string MAC_404 means mac adress doesn't exist in the config file
* @return string SAFE_MODE means server is in safe mode so config can't be read
* */
public function _get_mac_address()
{
# open the config file
$conf = $this->_get_config();
# if anyone has any clues for windows environments
# or other server types let me know
$os = strtolower(PHP_OS);
if (substr($os, 0, 3) == 'win') {
# explode the conf into lines to search for the mac
$lines = explode($this->_LINEBREAK, $conf);
# seperate the lines for analysis
foreach ($lines as $key => $line) {
# check for the mac signature in the line
# originally the check was checking for the existence of string 'physical address'
# however Gert-Rainer Bitterlich pointed out this was for english language
# based servers only. preg_match updated by Gert-Rainer Bitterlich. Thanks
if (preg_match("/([0-9a-f][0-9a-f][-:]){5}([0-9a-f][0-9a-f])/i", $line)) {
$trimmed_line = trim($line);
# take of the mac addres and return
return trim(substr($trimmed_line, strrpos($trimmed_line, " ")));
}
}
} else {
# get the mac delim
$mac_delim = $this->_get_os_var('mac', $os);
# get the pos of the os_var to look for
$pos = strpos($conf, $mac_delim);
if ($pos) {
# seperate out the mac address
$str1 = trim(substr($conf, ($pos + strlen($mac_delim))));
return trim(substr($str1, 0, strpos($str1, "\n")));
}
}
# failed to find the mac address
return 'MAC_404';
}
/**
* _get_server_info
*
* used to generate the server binds when server binding is needed.
*
* @access private
* @return array server bindings
* @return boolean false means that the number of bindings failed to
* meet the required number
* */
public function _get_server_info()
{
if (empty($this->_SERVER_VARS)) {
$this->set_server_vars($_SERVER);
}
# get the server specific uris
$a = array();
if (isset($this->_SERVER_VARS['SERVER_ADDR']) && (!strrpos($this->_SERVER_VARS['SERVER_ADDR'], '127.0.0.1') || $this->ALLOW_LOCAL)) {
$a['SERVER_ADDR'] = $this->_SERVER_VARS['SERVER_ADDR'];
}
# corrected by Gert-Rainer Bitterlich <bitterlich -at- ima-dresden -dot- de>, Thanks
if (isset($this->_SERVER_VARS['HTTP_HOST']) && (!strrpos($this->_SERVER_VARS['HTTP_HOST'], '127.0.0.1') || $this->ALLOW_LOCAL)) {
$a['HTTP_HOST'] = $this->_SERVER_VARS['HTTP_HOST'];
}
if (isset($this->_SERVER_VARS['SERVER_NAME'])) {
$a['SERVER_NAME'] = $this->_SERVER_VARS['SERVER_NAME'];
}
if (isset($this->_SERVER_VARS['PATH_TRANSLATED'])) {
$a['PATH_TRANSLATED'] = substr($this->_SERVER_VARS['PATH_TRANSLATED'], 0, strrpos($this->_SERVER_VARS['PATH_TRANSLATED'], '/'));
} elseif (isset($this->_SERVER_VARS['SCRIPT_FILENAME'])) {
$a['SCRIPT_FILENAME'] = substr($this->_SERVER_VARS['SCRIPT_FILENAME'], 0, strrpos($this->_SERVER_VARS['SCRIPT_FILENAME'], '/'));
}
if (isset($_SERVER['SCRIPT_URI'])) {
$a['SCRIPT_URI'] = substr($this->_SERVER_VARS['SCRIPT_URI'], 0, strrpos($this->_SERVER_VARS['SCRIPT_URI'], '/'));
}
# if the number of different uris is less than the required amount,
# fail the request
if (count($a) < $this->REQUIRED_URIS) {
return 'SERVER_FAILED';
}
return $a;
}
/**
* validate
*
* validates the server key and returns a data array.
*
* @access public
* @return array Main object in array is 'RESULT', it contains the result
* of the validation.
* OK - key is valid
* CORRUPT - key has been tampered with
* TMINUS - the key is being used before the valid start date
* EXPIRED - the key has expired
* ILLEGAL - the key is not on the same server the license was registered to
* ILLEGAL_LOCAL - the key is not allowed to be installed on a local machine
* INVALID - the the encryption key used to encrypt the key differs or the key is not complete
* EMPTY - the the key is empty
* 404 - the the key is missing
* */
public function validate($str = false, $dialhome = false, $dialhost = "", $dialpath = "", $dialport = "80")
{
# check to see if the class has been secured
$this->_check_secure();
# get the dat string
$dat_str = (!$str) ? @file_get_contents($this->_LICENSE_PATH) : $str;
if (strlen($dat_str) > 0) {
# decrypt the data
$DATA = $this->_unwrap_license($dat_str);
if (is_array($DATA)) {
# missing / incorrect id therefore it has been tampered with
/*
*Disable to accept licenses from other workspaces
*if ($DATA['ID'] != G::encryptOld($this->ID1)) {
$DATA['RESULT'] = 'CORRUPT';
}*/
if ($this->USE_TIME) {
# the license is being used before it's official start
if ($DATA['DATE']['START'] > time() + $this->START_DIF) {
$DATA['RESULT'] = 'TMINUS';
}
# the license has expired
if ($DATA['DATE']['END'] - time() < 0 && $DATA['DATE']['SPAN'] != 'NEVER' && $DATA['DATE']['SPAN'] != '~') {
$DATA['RESULT'] = 'EXPIRED';
}
$DATA['DATE']['HUMAN']['START'] = date($this->DATE_STRING, $DATA['DATE']['START']);
if (($DATA['DATE']['END'] == "") || ($DATA['DATE']['END'] == "NEVER")) {
$DATA['DATE']['HUMAN']['END'] = "PERPETUAL";
} else {
$DATA['DATE']['HUMAN']['END'] = date($this->DATE_STRING, $DATA['DATE']['END']);
}
}
if ($this->USE_SERVER) {
$mac = $DATA['SERVER']['MAC'] == $this->_MAC;
$path = count(array_diff($this->_SERVER_INFO, $DATA['SERVER']['PATH'])) <= $this->_ALLOWED_SERVER_DIFS;
$domain = $this->_compare_domain_ip($DATA['SERVER']['DOMAIN'], $this->_IPS);
$ip = count(array_diff($this->_IPS, $DATA['SERVER']['IP'])) <= $this->_ALLOWED_IP_DIFS;
# the server details
if (!$mac || !$path || !$domain || !$ip) {
$DATA['RESULT'] = 'ILLEGAL';
}
# check if local
$local = $this->ALLOW_LOCAL && (in_array('127.0.0.1', $DATA['SERVER']['IP']) || $DATA['PATH']['SERVER_ADDR'] == '127.0.0.1' || $DATA['PATH']['HTTP_HOST'] == '127.0.0.1');
if (!$local) {
$DATA['RESULT'] = 'ILLEGAL_LOCAL';
}
}
# passed all current test so license is ok
if (!isset($DATA['RESULT'])) {
# dial to home server if required
if ($dialhome) {
# create the details to send to the home server
$stuff_to_send = array();
$stuff_to_send['LICENSE_DATA'] = $DATA;
$stuff_to_send['LICENSE_DATA']['KEY'] = G::encryptOld($dat_str);
# dial home
$DATA['RESULT'] = $this->_call_home($stuff_to_send, $dialhost, $dialpath, $dialport);
} else {
# result is ok all test passed, license is legal
$DATA['RESULT'] = 'OK';
}
}
/*
*/
# data is returned for use
return $DATA;
} else {
# the are two reason that mean a invalid return
# 1 - the other hash key is different
# 2 - the key has been tampered with
return array('RESULT' => 'INVALID');
}
}
# returns empty because there is nothing in the dat_string
return array('RESULT' => 'EMPTY');
}
/**
* _call_home
*
* calls the dial home server (your server) andvalidates the clients license
* with the info in the mysql db
*
* @access private
* @param $data array Array that contains the info to be validated
* @param $dialhost string Host name of the server to be contacted
* @param $dialpath string Path of the script for the data to be sent to
* @param $dialport number Port Number to send the data through
* @return string Returns: the encrypted server validation result from the dial home call
* : SOCKET_FAILED => socket failed to connect to the server
* */
public function _call_home($data, $dialhost, $dialpath, $dialport)
{
print "_call_home($data, $dialhost, $dialpath, $dialport)";
# post the data home
$data = $this->_post_data($dialhost, $dialpath, $data, $dialport);
return (empty($data['RESULT'])) ? 'SOCKET_FAILED' : $data['RESULT'];
}
}
|