<?php

class License {
    
    function getLicense($table){
        $sql  = "SELECT vValue FROM ".$table." WHERE vName='vLicenceKey'";
        $result = mysql_query($sql);
        if (mysql_num_rows($result) > 0) {
            $row = mysql_fetch_array($result);
            $var_licencekey = stripslashes($row["vValue"]);
        }
        return $var_licencekey;
    }

    function checkAdminLogin($table,$password) {
        $sql       = "SELECT vName FROM ".$table." WHERE vName='adminpassword' AND vValue='".mysql_real_escape_string($password)."'";
        $result    = mysql_query($sql);
        $valAdmin  = mysql_fetch_assoc($result);
        return $valAdmin;
    }

   function updateLicense($table,$license) {

        $sql       = "UPDATE ".$table." SET vValue='".mysql_real_escape_string($license)."' WHERE vName='vLicenceKey'";
        $result    = mysql_query($sql);
        return true;
    }
    

    function getDomain($var_domain){
        preg_match("/^(http:\/\/)?([^\/]+)/i",$var_domain, $matches);
        $host = $matches[2];
        $var_dotexist	= 0;
        $j = mb_strlen($host);
        for ($k = 0;
        $k < $j;
        $k++) {
            $char = mb_substr($host, $k, 1);
            if($char == ".")
            $var_dotexist	=	$var_dotexist + 1;
        }
        $var_dot_count	=	explode('.',$host);

        if(count($var_dot_count) == 1){
            $var_org_domain	=	$host;
        }else if((count($var_dot_count) > 1) && (mb_strlen(substr($var_dot_count[count($var_dot_count)-2],0,38)) > 2)){
            preg_match("/[^\.\/]+\.[^\.\/]+$/", $host, $matches);
            $var_org_domain	=	$matches[0];
        }else{
            preg_match("/[^\.\/]+\.[^\.\/]+\.[^\.\/]+$/", $host, $matches);
            $var_org_domain	=	$matches[0];
        }
        $var_final_domain	=	explode('.',$var_org_domain);
        $main_domain 	=	strtoupper(trim($var_final_domain[0]));

        return $main_domain;
    }

    function isValid($var_domain,$table){
        if($this->checkChars($var_domain))
            return true;
        $var_domain		=	$this->getDomain($var_domain);
        $var_licencekey     = 	$this->getLicense($table);

        $first_domain 	= 	substr($var_domain, 0, 3);
        $last_domain 	= 	substr($var_domain, -3,3);

        $var_first		= 	substr($var_licencekey, 0, 3);
        $var_lic_first	= 	substr($var_licencekey, 3, 3);
        $var_lic_last 	= 	substr($var_licencekey, -3,3);

        $var_lic_first	=	$this->asc_shift($var_lic_first, 3);
        $var_lic_last	=	$this->asc_shift($var_lic_last, 3);

        $alphabet_array 	= array('A' => 1, 'B' => 2, 'C' => 3, 'D' => 4, 'E' => 5, 'F' => 6,
        'G' => 7, 'H' => 8, 'I' => 9, 'J' => 10,'K' => 11,'L' => 12,
        'M' => 13,'N' => 14,'O' => 15,'P' => 16,'Q' => 17,'R' => 18,
        'S' => 19,'T' => 20,'U' => 21,'V' => 22,'W' => 23,'X' => 24,
        'Y' => 25,'Z' => 26,'1' => 1, '2' => 2, '3' => 3, '4' => 4,
        '5' => 5, '6' => 6, '7' => 7, '8' => 8, '9' => 9);
        $j = mb_strlen($var_domain);
        $var_char_sum	=	0;
        for ($k = 0;$k < $j;$k++) {
            $char = mb_substr($var_domain, $k, 1);
            $var_char_sum	= $var_char_sum	+ $alphabet_array[$char];
        }
        if($var_char_sum != ($var_first - 23))
            return false;
        else if(strcmp($first_domain,$var_lic_first) != 0)
            return false;
        else if(strcmp($last_domain,$var_lic_last) != 0)
            return false;
        else
            return true;
    }


    function asc_shift($string, $amount) {
        $key = strrev($string);
        return $key;
    }


    function checkChars($domainName){
        if (preg_match("/^[0-9].+$/", $domainName)){
            return true;
        }else
            return false;
    }
}
?>