View file index.php

File size: 7.49Kb
<?php
/**
*  This program is free software; you can redistribute it and/or modify
*  it
*
*  This program is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/

error_reporting( E_ALL );

ini_set( 'display_errors', 0 );
ini_set( 'html_errors', 0 );
// ini_set( 'error_log', 'error.log' );

$st = microtime( true );

$nocache = time();

define( 'ROOTPATH', getcwd());
define( 'PATH', dirname($_SERVER['PHP_SELF']) == '/' ? '' : dirname($_SERVER['PHP_SELF']));
define( 'EXT', substr(strrchr(__FILE__, '.'), 1));

include( ROOTPATH . '/includes/config.' . EXT );
include( ROOTPATH . '/includes/classes/template.class' );
include( ROOTPATH . '/includes/functions.' . EXT );
include( ROOTPATH . '/includes/functions_session.' . EXT );

if( version_compare(PHP_VERSION, '5.0.0', '<') )
{
	put_error(FATAL_ERROR, 'You are running an unsupported PHP version. Please upgrade to PHP 5.0.0');
}

switch( DBMS )
{
	case 'MySQL':
	include( ROOTPATH . '/includes/classes/mysql.db.class' );
	break;

	default:

	put_error(FATAL_ERROR, 'Invalid DBMS value', __LINE__, __FILE__);
}

$sql = new sql(DBMSHOST, DBUSER, DBPASS, DBNAME);

if( !$sql->connect_id )
{
	put_error(DBMS_ERROR, htmlspecialchars($sql->error['message']), __LINE__, __FILE__);
}

if( !($qresult = $sql->query("SELECT * FROM `" . CATALOGUE_CONFIG . "` LIMIT 1;")) )
{
	put_error(DBMS_ERROR, htmlspecialchars($sql->error['message']), __LINE__, __FILE__);
}

if( $sql->num_rows() < 1 )
{
	put_error(FATAL_ERROR, 'Configuration does not exist');
}

$config = $sql->fetch_assoc( $qresult );

$_SERVER['HTTP_USER_AGENT'] = isset( $_SERVER['HTTP_USER_AGENT'] ) ? (string) $_SERVER['HTTP_USER_AGENT'] : '';
$_SERVER['HTTP_ACCEPT'] = isset( $_SERVER['HTTP_ACCEPT'] ) ? (string) $_SERVER['HTTP_ACCEPT'] : '';

// User-Agent

$ua = '';

if( strpos($_SERVER['HTTP_USER_AGENT'], 'Google') !== false )
{
	$ua = 'Googlebot';
}
else if( strpos($_SERVER['HTTP_USER_AGENT'], 'Yandex') !== false )
{
	$ua = 'Yandex';
}

if( isset($_SERVER['HTTP_X_OPERAMINI_PHONE_UA']) )
{
	$ua = 'OM#' . substr($_SERVER['HTTP_X_OPERAMINI_PHONE_UA'], 0, 47);
}
else
{
	$ua = substr($_SERVER['HTTP_USER_AGENT'], 0, 50);
}

$ua = cleanup( $ua );

session_init();

$m = isset( $_GET['m'] ) ? (string) $_GET['m'] : 'index';

if( strpos($m, '.') !== false || strpos($m, '/') !== false )
{
	header('Location: ' . append_sid( PATH . '/' ));

	exit;
}

if( !$sql->query("SELECT MAX(`dt`) FROM `" . CATALOGUE_STAT . "`;") )
{
	put_error(DBMS_ERROR, htmlspecialchars($sql->error['message']), __LINE__, __FILE__);
}

if( $sql->num_rows() < 1 )
{
	$dt = 0;
}
else
{
	$dt = $sql->result($sql->result, 0);
}

if( $dt < mktime(0, 0, 0, date('m'), date('d'), date('Y')) )
{
	$timestamp = mktime(0, 0, 0, date('m'), date('d'), date('Y'));

	if( !$sql->query("LOCK TABLES `" . CATALOGUE_SITES . "` WRITE, `" . CATALOGUE_CLICKS . "` WRITE, `" . CATALOGUE_STAT . "` WRITE;") )
	{
		put_error(DBMS_ERROR, htmlspecialchars($sql->error['message']), __LINE__, __FILE__);
	}

	if( !$sql->query("INSERT INTO `" . CATALOGUE_STAT . "` SELECT NULL, SUM(`in_today_all`), SUM(`in_today_all`), SUM(`out_today`), " . $timestamp . " FROM `" . CATALOGUE_SITES . "`;") )
	{
		put_error(DBMS_ERROR, htmlspecialchars($sql->error['message']), __LINE__, __FILE__);
	}

	if( !$sql->query("UPDATE `" . CATALOGUE_SITES . "` SET `in_today_all` = 0, `in_today_mp` = 0, `out_today` = 0;") )
	{
		put_error(DBMS_ERROR, htmlspecialchars($sql->error['message']), __LINE__, __FILE__);
	}

	if( !$sql->query("UNLOCK TABLES;") )
	{
		put_error(DBMS_ERROR, htmlspecialchars($sql->error['message']), __LINE__, __FILE__);
	}

	if( !$sql->query("TRUNCATE TABLE `" . CATALOGUE_CLICKS . "`;") )
	{
		put_error(DBMS_ERROR, htmlspecialchars($sql->error['message']), __LINE__, __FILE__);
	}
}

if( in_array($m, array('in', 'out', 'adv')) )
{
	if( !is_file( ROOTPATH . '/includes/modules/' . $m . '.' . EXT ) )
	{
		put_error(FATAL_ERROR, 'Some modules does not exist');
	} 

	include( ROOTPATH . '/includes/modules/' . $m . '.' . EXT );

	exit;
}

$version = isset( $_GET['version'] ) ? (string) $_GET['version'] : '';

if( !in_array($version, array('wml', 'xhtml')) )
{
	$_SESSION['version'] = isset( $_SESSION['version'] ) ? $_SESSION['version'] : '';

	if( $_SESSION['version'] != '' )
	{
		$version = $_SESSION['version'];
	}
	else
	{
		$xhtml_accept = strpos(
		$_SERVER['HTTP_ACCEPT'], 'html') !== false
		|| strpos($_SERVER['HTTP_ACCEPT'], 'xhtml') !== false
		|| strpos($_SERVER['HTTP_USER_AGENT'], 'Opera') !== false
		|| strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== false
		|| strpos($_SERVER['HTTP_USER_AGENT'], 'Symbian') !== false;

		if( $xhtml_accept )
		{
			$_SESSION['version'] = $version = 'xhtml';
		}
		else
		{
			$_SESSION['version'] = $version = 'wml';
		}
	}
}
else
{
	$_SESSION['version'] = $version;
}

$mime = array('wml' => 'text/vnd.wap.wml', 'xhtml' => 'text/html');

define('VERSION', $version);
define('MIME', $mime[VERSION]);

if( VERSION == 'xhtml' )
{
	$theme_id = 1;

	$query = "SELECT * FROM `" . CATALOGUE_THEMES . "` WHERE `id` = " . $theme_id . " OR `id` = 1 ORDER BY `id` DESC LIMIT 1;";

	if( !($qresult = $sql->query($query)) )
	{
		put_error(DBMS_ERROR, htmlspecialchars($sql->error['message']), __LINE__, __FILE__);
	}

	if( $sql->num_rows($qresult) == 0 )
	{
		put_error(FATAL_ERROR, 'Default theme does not exist', __LINE__, __FILE__);		
	}

	$theme = $sql->fetch_assoc($qresult);

	$css_vars = array(
	'FW' => $theme['font-weight'],
	'FC' => $theme['color'],
	'BGC' => $theme['background-color'],
	'AC' => $theme['acolor'],
	'DIV_FC' => $theme['div.color'],
	'DIV_BGC' => $theme['div.background-color']
	);
}
else
{
	$css = '';
}

$lang_list = array('russian', 'english');

if( isset($_GET['lang']) && in_array($_GET['lang'], $lang_list) )
{
	$_SESSION['language'] = $_GET['lang'];
}
else
{
	$_SESSION['language'] = isset( $_SESSION['language'] ) ? $_SESSION['language'] : 'russian';
}

include( ROOTPATH . '/language/' . $_SESSION['language'] . '/lang_main.' . EXT );

if( !is_file(ROOTPATH . '/includes/modules/' . $m . '.' . EXT) )
{
	if( $m <> 'index' )
	{
		redirect( gen_uri('index') );
	}
	else
	{
		put_error(FATAL_ERROR, 'Module "index" does not exist');
	}
}

$_SESSION['crc32'] = isset( $_SESSION['crc32'] ) ? $_SESSION['crc32'] : NULL;

$template = new template( ROOTPATH );

if( VERSION == 'xhtml' && $m == 'index' )
{
	$template->load_template( 'templates/' . VERSION . '/index_header.tpl', 'header' );
}
else
{
	$template->load_template( 'templates/' . VERSION . '/header.tpl', 'header' );
}

$template->load_template( 'templates/' . VERSION . '/' . $m . '.tpl', 'body' );
$template->load_template( 'templates/' . VERSION . '/footer.tpl', 'footer' );

if( VERSION == 'xhtml' )
{
	$template->load_template( 'templates/default.css', 'css' );
	$template->set_vars('css', $css_vars);
	$css = $template->evaluate_tpl('css');
	$template->cancel('css');
}

include( ROOTPATH . '/includes/modules/' . $m . '.' . EXT );

header('Content-type: ' . MIME . '; charset=utf-8');
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
header('Cache-Control: no-cache, must-relative');

$template->evaluate();

// PLEASE NOTE: don't remove comments

echo "\r\n<!-- Developed by Unkind -->";
echo "\r\n<!-- " . (microtime( true ) - $st) . " -->";
?>