/*********************/
/* */
/* Version : 5.1.0 */
/* Author : RM */
/* Comment : 071223 */
/* */
/*********************/
class Keitaro_Kernel
{
public $db = NULL;
public $cnf = NULL;
public $settings = NULL;
public $user = NULL;
public $session = NULL;
public $lang = "ru";
public $licenseData = NULL;
protected $_serverError = NULL;
public function init( )
{
$this->_initConfig( );
$this->_initTimezone( );
$this->_initDb( );
}
protected function _initConfig( )
{
if ( defined( "ENV" ) && ENV == "testing" )
{
$cnfFile = "config.testing.ini";
}
else
{
$cnfFile = "config.ini";
}
$this->cnf = parse_ini_file( ROOT."/application/config/".$cnfFile, true );
if ( !$this->cnf['system']['keitaro_server'] )
{
$this->cnf['system']['keitaro_server'] = "
http://www.keitarotds.com";
$this->cnf['system']['keitaro_reserve_server'] = "
http://reserve.keitarotds.com";
}
if ( !defined( "SALT" ) )
{
define( "SALT", $this->cnf['system']['salt'] );
}
}
protected function _initDb( )
{
if ( $this->cnf['system']['debug'] )
{
require_once( "adodb-exceptions.inc.php" );
require_once( "adodb-errorhandler.inc.php" );
}
require_once( "adodb.inc.php" );
$ADODB_COUNTRECS = false;
$this->db = NewADOConnection( "mysql", "date:extend:transaction" );
$this->db->LogSQL( false );
if ( $this->cnf['db']['cache'] == "file" )
{
$ADODB_CACHE_DIR = ROOT."/var/cache";
}
else if ( $this->cnf['db']['cache'] == "memcache" && class_exists( "Memcache" ) )
{
$host = "";
$port = "";
list( $host, $port ) = host; $this->db->memCache = true;
$this->db->memCacheHost = array(
$host
);
$this->db->memCachePort = $port;
}
if ( !$this->db->connect( $this->cnf['db']['server'], $this->cnf['db']['user'], $this->cnf['db']['password'], $this->cnf['db']['name'] ) )
{
if ( $this->cnf['system']['debug'] )
{
echo $this->db->ErrorMsg( );
}
else
{
header( "LOCATION: ".urldecode( $this->cnf['system']['extra_url'] ) );
}
}
$this->db->SetFetchMode( ADODB_FETCH_ASSOC );
$this->db->execute( "SET character_set_client=utf8" );
$this->db->execute( "SET character_set_connection=utf8" );
$this->db->execute( "SET character_set_results=utf8" );
$this->db->execute( "SET collation_connection=utf8_unicode_ci" );
$this->db->prefix = $this->cnf['db']['prefix'];
}
protected function _initSettings( )
{
$sql = "SELECT * FROM ".$this->db->prefix."settings ";
$rows = $this->db->cacheGetAll( 600, $sql );
$i = 0;
while ( $i < count( $rows ) )
{
$this->settings[$rows[$i]['key']] = $rows[$i]['value'];
++$i;
}
}
protected function _initAuth( )
{
require( ROOT."/application/models/User.php" );
$this->user = new User( $this );
}
protected function _initSession( )
{
require( "Session.php" );
$this->session = new Keitaro_Session( );
}
protected function _initTimezone( )
{
if ( isset( $this->cnf['system']['timezone'] ) )
{
date_default_timezone_set( $this->cnf['system']['timezone'] );
}
}
public function getMenu( )
{
$menu = array( );
require_once( "MenuBuilder.php" );
$menuBuilder = new Keitaro_MenuBuilder( $this->lang );
return $menuBuilder->getMenu( );
}
public function isDebug( )
{
return ( string )$this->cnf['system']['debug'];
}
public function isDemo( )
{
return ( string )$this->cnf['system']['demo'];
}
public function log( $str, $file, $separator = "\n- - - - - - - - - -\n" )
{
$dir = ROOT."/var/log/";
if ( is_file( $dir.$file ) && $this->cnf['system']['log_max_size'] * 1024 < filesize( $dir.$file ) )
{
unlink( $dir.$file );
}
$fp = @fopen( $dir.$file, "a+" );
@fwrite( $fp, $str.$separator );
@fclose( $fp );
}
public function checkLicense( $retry = false )
{
$data = $this->_decodeHash( $this->_getHash( ) );
if ( $data['hash_expired'] < time( ) && !$retry )
{
@unlink( ROOT."var/license/hash.lic" );
$this->checkLicense( true );
}
$this->licenseData = $data;
return $this->activeByLicense( $data );
}
public function activeByLicense( $data )
{
return true;
}
private function _getHash( )
{
$server = $this->getAvailableServer( );
if ( !$server )
{
$this->_serverError = true;
}
if ( !file_exists( ROOT."var/license/hash.lic" ) && !$server )
{
throw new Keitaro_Kernel_Exception( "Не удалось подключиться к серверам лицензий KeitaroTDS.\r\n Попробуйте обновить страницу, либо обратиться в службу техподдержки." );
}
if ( !file_exists( ROOT."var/license/hash.lic" ) )
{
$this->_downloadHash( $server );
}
$hash = @file_get_contents( ROOT."var/license/hash.lic" );
if ( !$hash )
{
throw new Keitaro_Kernel_Exception( "Не удается обновить информацию о состоянии лицензии. Пожайлуйста, проверьте наличие прав на запись в директорию /var/license." );
}
return $hash;
}
private function _getKey( )
{
if ( !file_exists( ROOT."var/license/key.lic" ) )
{
throw new Keitaro_Kernel_Exception( "Файл /var/license/key.lic не найден. В нем должен содержаться ключ вашей лицензии." );
}
return trim( @file_get_contents( ROOT."var/license/key.lic" ) );
}
private function _decodeHash( $hash )
{
if ( !$hash )
{
return array( );
}
$password = "51951AC=8293-B62=D9CFFA8E1FDCB51=951AC8293B-625D9CF=FA8E1FDCBE=83E8B251E8=5123-1E36E-2494D2C9C07A889AE8-3E8B251E=851231E36E-2494D2C9C07A8-89A";
$Crypt = new Crypt( $password );
$data = $Crypt->decrypt( $hash );
if ( $data )
{
$data = substr( $data, strpos( $data, "%%" ) + 2, strlen( $data ) );
$data = substr( $data, 0, strpos( $data, "%%" ) );
if ( $data )
{
return unserialize( $data );
}
return false;
}
return false;
}
private function _downloadHash( $server )
{
$ch = curl_init( );
$request = $server."/license/api/get?ip=".$this->_getIp( )."&key=".$this->_getKey( );
curl_setopt( $ch, CURLOPT_HTTP_VERSION, 1.1 );
curl_setopt( $ch, CURLOPT_URL, $request );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt( $ch, CURLOPT_HEADER, 0 );
curl_setopt( $ch, CURLOPT_NOBODY, 0 );
curl_setopt( $ch, CURLOPT_TIMEOUT, 20 );
curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, 20 );
curl_setopt( $ch, CURLOPT_USERAGENT, "KeitaroTDS License Updater" );
$result = curl_exec( $ch );
if ( !$result )
{
throw new Keitaro_Kernel_Exception( "Не удалось подключиться ни к одному из серверов лицензий KeitaroTDS. Срочно свяжитесь с техподдержкой." );
}
file_put_contents( ROOT."var/license/hash.lic", $result );
return true;
}
private function _getIp( )
{
$ip = getenv( "SERVER_ADDR" ) ? getenv( "SERVER_ADDR" ) : "127.0.0.1";
$ip = str_replace( "::ffff:", "", $ip );
return $ip;
}
public function getAvailableServer( )
{
$ch = curl_init( );
curl_setopt( $ch, CURLOPT_HTTP_VERSION, 1.1 );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt( $ch, CURLOPT_HEADER, 0 );
curl_setopt( $ch, CURLOPT_NOBODY, 0 );
curl_setopt( $ch, CURLOPT_TIMEOUT, 20 );
curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, 20 );
curl_setopt( $ch, CURLOPT_USERAGENT, "KeitaroTDS" );
curl_setopt( $ch, CURLOPT_URL, $this->cnf['system']['keitaro_server']."/check.txt" );
$server = $this->cnf['system']['keitaro_server'];
$result = curl_exec( $ch );
if ( !stristr( $result, "worked" ) )
{
$server = $this->cnf['system']['keitaro_reserve_server'];
curl_setopt( $ch, CURLOPT_URL, $this->cnf['system']['keitaro_reserve_server']."/check.txt" );
$result = curl_exec( $ch );
if ( !stristr( $result, "worked" ) )
{
return false;
}
}
return $server;
}
public function getLicenseInfo( )
{
if ( $this->licenseData['license_expired'] - time( ) < 60 * 60 * 24 * 7 )
{
$class = "license-warning";
}
else if ( $this->licenseData['license_expired'] - time( ) < 60 * 60 * 24 * 3 )
{
$class = "license-critical";
}
$info = "Лицензия действительна до
".date( $this->settings['date_format'], $this->licenseData['license_expired'] )."";
return $info;
}
public function showErrorPage( $content )
{
$ip = $this->_getIp( );
include( "error.phtml" );
exit( );
}
}
require_once( "Settings.php" );
require_once( "Crypt.php" );
require_once( "Kernel/Exception.php" );
?>