<?php

if(!defined('APP_INCLUDE')){ die('invalid inclusion'); }
 
# the license object
class lo {
    # this will run when the program starts
    function __construct() {
        global $edition, $running_from_command_line,$linfo;
        if (@$running_from_command_line!=true) {
            if (strpos($_SERVER['PHP_SELF'], "app/model") !==false) {
                $real_path = realpath("../../index.php");   
            } else if (file_exists("index.php")==TRUE) {
                $real_path = realpath("index.php");
            } else if (file_exists("../index.php")==TRUE) {
                $real_path = realpath("../index.php");
            } else if (file_exists("../../index.php")==TRUE) {   
                $real_path = realpath("../../index.php");   
            } else {
                echo "Can't find path to license file.<br>";   
            }
            $path = dirname($real_path);
            //$path = logaholic_dir();
            
            $gfile=$path . "/files/licensefile.txt";

            if (file_exists($gfile)==FALSE) {
				$linfo = $this->DL();
            
			} else {
				$l=file_get_contents($gfile);
				$linfo = $this->RL($l);
			}
            
             
            # This file contains enablers to turn on / off certain functionality
            define("_ENABLE_IMPORTANT_PARAMETER_EDITOR_", 1);
            
            // _LICENSE_MODE_ - currently on checks for defined and a value, but could maybe later 
            // do a constant or string or something to say what kind of license is here.  Other licensing systems
            // will probably be used, though.

            // _LICENSE_MODE_ 0, or undefined = DEMO
            // _LICENSE_MODE_ 1 = not demo.
            //define("_LICENSE_MODE_", 1);
            // LICENSE MODE IS SET BY THE LICENSE ABOVE!!!

            //_LIMIT_PROFILES 0, or undefined = no profile limit
            //_LIMIT_PROFILES 50 = 50 profile limit
            //define("_LIMIT_PROFILES", 5);
            // LIMIT PROFILES IS SET BY THE LICENSE ABOVE!!!!

            //define("_LIMIT_USERS", 1);

            if (0 == strcmp($_SERVER["SERVER_SOFTWARE"], "PHPDock/2.0")) {
                define("IS_PHPDOCK", 1);
            } else {
                define("IS_PHPDOCK", 0);
            }
        } else {
            define("_ENABLE_IMPORTANT_PARAMETER_EDITOR_", 1);
            define("_LICENSE_MODE_", 1);
            define("IS_PHPDOCK", 0);
        }
        if (defined("_LOGAHOLIC_EDITION")) {
            define("_LOGAHOLIC_PRODUCTNAME", $edition[_LOGAHOLIC_EDITION]);
        }
    }
    
    # this will make the initialization work with php4
    function lo() {
        return $this->__construct();  
    }
    
    function Linfo() {
        global $linfo;
        return $linfo;    
    }
    
    function PrintInfo($title="<b>License Information:</b><br>") {
		echo $title;
        foreach ($this->Linfo() as $k=> $v) {
            echo " $k: $v<br>";
        }    
    }   

    function RL($l) {
        global $edition;
        $la = explode(":", $l);
        
        if (md5($_SERVER['HTTP_HOST'])!=$la[0] && $la[0]!="100b8cad7cf2a56f6df78f171f97a1ec") { echo "<P>License is not valid for this server ".$_SERVER['HTTP_HOST']."<br><a href=getlicense.php>"._GET_LICENSE."</a>"; exit(); } 
        $linfo["Host"]=$_SERVER['HTTP_HOST'];
        if (count($la) < 7) { echo "<P>Wrong License structure.<br>Your current License file is incompatible with this version (Logaholic ".LOGAHOLIC_VERSION_NUMBER."). Please update your license.<br><a href=getlicense.php>"._GET_LICENSE."</a>"; exit(); } 
        if (trim($la[5])!="7fa3b767c460b54a2be4d49030b349c7") {        
            $n=time();
            $ex = $this->Ltime($la);
            //echo $ex;
            //echo "mktime(0,0,0,$mv,$dv,$yv)";
            //echo "($n > $ex)";
            $linfo["Expires"]=date("d M Y",$ex);
            define("_LOGAHOLIC_TRIAL", "(Trial) ");        
            if ($n > $ex) { echo "<P>This Logaholic license has expired. Please click here to order a license.If you have already purchased a license, please use the Get License tool now."; exit(); }  
        } else {
            define("_LOGAHOLIC_TRIAL", "");
            $linfo["Expires"]="never";    
        }
        
        $x=0;  
        if ($la[4]!=md5("nolimit")) {
            for ($i=0;$i <= 20000;$i++) {
                if (md5($i)==$la[4]) {
                    $x=$i;
                    break;  
                }         
            }
            define("_LIMIT_PROFILES", $x);
            $linfo["Profile Limit"]=$x;
        } else {
            $linfo["Profile Limit"]="none";
        }
        $x=0;
        if ($la[6]!=md5("nolimit")) {
            for ($i=0;$i <= 20000;$i++) {
                if (md5($i)==$la[6]) {
                    $x=$i;
                    break;  
                }         
            }
            define("_LIMIT_USERS", $x);
            $linfo["User Limit"]=$x;
        } else {
            $linfo["User Limit"]="none";           
        }
        
        $linfo["Product"]="unknown"; 
        foreach($edition as $k => $v) {
            if (md5($v)==$la[7]) {
                define("_LOGAHOLIC_EDITION",$k);
                $linfo["Product"]=$v . " "._LOGAHOLIC_TRIAL;
                break;
            }   
        }        
        if ($la[9]!=md5(substr(LOGAHOLIC_VERSION_NUMBER,0,3))) {
            $linfo["License Version"]="unknown";
            for($i=0;$i<10;$i++) {
                $oldver=substr(LOGAHOLIC_VERSION_NUMBER,0,2).$i; 
                if (md5($oldver) == $la[9]) {       
                    $linfo["License Version"]=substr(LOGAHOLIC_VERSION_NUMBER,0,2).$i;
                    break; 
                }
            }
            @setcookie("update_license","1");
            echo "<P><h3>Upgrading from Logaholic {$linfo['License Version']} to version ".LOGAHOLIC_VERSION_NUMBER."...</h3> To activate this version, please update your license file now using the link below:<a href=\"getlicense.php\"><ul><li>Get License</a></li></ul>(Updating your license file is free while you have access to upgrades from Logaholic.com)"; exit();
        } else {
            $linfo["License Version"]=substr(LOGAHOLIC_VERSION_NUMBER,0,3);   
        }
		if ($la[8]==md5(trim($la[10]))) { $linfo["License Email"]=$la[10]; } else  { echo "<P>Your License file is invalid. Please update your license.<br><a href=getlicense.php>Get License</a>"; 
            
            exit(); 
        } 


		define("_LOGAHOLIC_REGISTERED",1);
		
        return $linfo;

    }
	
	function DL() {
		global $edition;
		$linfo["Host"]=$_SERVER['HTTP_HOST'];
		define("_LOGAHOLIC_TRIAL", "");
        $linfo["Expires"]="never";
		define("_LIMIT_PROFILES", 300);
        $linfo["Profile Limit"] = 300;
		define("_LIMIT_USERS", 300);
        $linfo["User Limit"] = 300;
		define("_LOGAHOLIC_EDITION",0);
		define("_LOGAHOLIC_REGISTERED",0);
        $linfo["Product"]=@$edition[_LOGAHOLIC_EDITION] . " (Unregistered Free Version)";
		$linfo["License Version"]=LOGAHOLIC_VERSION_NUMBER;
		$linfo["License Email"] = "none";
		return $linfo;
	}
	
    function Ltime($la) {
        for ($i=0;$i<=31;$i++) {
             if (md5($i)==$la[1]) {
                $dv=$i;
                break;  
            }
        }
        for ($i=0;$i<=12;$i++) { 
            if (md5($i)==$la[2]) {
                $mv=$i;
                break;  
            }
        }
        for ($i=date("Y");$i<=2038;$i++) {
            if (md5($i)==$la[3]) {
                $yv=$i;
                break;  
            } 
        }
        return mktime(0,0,0,@$mv,@$dv,@$yv);        
    }
}
$lo = new lo();
//$lo->PrintInfo();
?>