<?
/* DISABLE SESSION FOR SEARCH ENGINES */
if (preg_match('/^(aport|stackrambler|yandex|googlebot|scooter|win16|slurp|fast|grub|fluffy|msnie|crawl|wisenutbot|looksmart|msn)/i', $_SERVER['HTTP_USER_AGENT'])){
	$NO_SESSION = true;
}

ini_set('magic_quotes_sybase',0);
ini_set('magic_quotes_gpc',0);

setlocale(LC_TIME, "ru_RU.CP1251");

set_magic_quotes_runtime(0);
//mysql_query("update task_modules set setup_params='".serialize(array("min_objects_amount"=>2,"max_objects_amount"=>10,"task_amount"=>2,"tasks_per_row"=>2,"picture_max_width"=>150,"picture_max_height"=>150))."' where code='count_picture_items'");

error_reporting(7);
if(!$NO_SESSION){
	@session_start();
}
/*************************/

$SITE_NAME ="";
$SITE_DOMAIN = "";
$SITE_URL = "";
//$SITE_URL_SSL = "http://$SITE_DOMAIN";
$ADMIN_URL = "";
$PARTNER_URL = "";
$DATE_FORMAT="%d %b %Y";

$DOCUMENT_ROOT = "";
$TEMP_ROOT     = "";
$PROJECT_ROOT  = "";
$ADMIN_ROOT    = $DOCUMENT_ROOT."/admin";
$PG_DUMP_PATH  = "/usr/local/bin";

/*
$ADMIN_EMAIL = "";
$SUPPORT_EMAIL = $ADMIN_EMAIL;
$WEBMASTER_EMAIL = $ADMIN_EMAIL;
$BILLING_EMAIL = $ADMIN_EMAIL;
*/

$IMG_PATH = "/images";
$CURR_IMG_PATH = $IMG_PATH."/currencies";
$COUNTRY_IMG_PATH = $IMG_PATH."/countries";

$ROBOT_SIGN = "\n\nС уважением,\nРобот $SITE_NAME";
$EMAIL_SIGNATURE = "\n\nС уважением,\nАдминистрация $SITE_NAME\n".$SUPPORT_EMAIL;
$MONTH_RUS = array("янв","фев","мар","апр","мая","июн","июл","авг","сен","окт","нояб","дек");
$ANON_PROXY_PORTS = array("8080","80","3128","8000");
$PROXY_CHECK_TIMEOUT = 5;
$ADMIN_DIR = "admin";
$ADMIN_PAGE_PER_BLOCK = 10;
$ADMIN_ITEMS_PER_PAGE = 100;
$ORDER_TIMEOUT = 60*30;
/***********************/

$STATUS_NAMES[orders]=Array(
			  0=>"Progress",
			  1=>"OK",
	              2=>"Fail (error)",
			  3=>"Refund",
			  4=>"Chargeback"
			  );

                                         
/* includes */ 
include ("content_funcs.php");
include("db_config.php");
include("functions.php");
include($DOCUMENT_ROOT."/../includes/money_config.php");


$DB_LINK = mysql_connect($DB_HOST, $DB_USER, $DB_PASS);
mysql_select_db($DB_NAME);
mysql_query("SET NAMES cp1251");

//mysql_query("SET NAMES 'cp1251'");

$SITE_DOCUMENT_ROOT = $PROJECT_ROOT;
$PM_LOGO_PATH = "/img/p_logos";

/* REGULAR EXPRESSIONS*/
$REG_EXP["email"]="^[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*(\.[a-zA-Z0-9]{2,6})$";
$REG_EXP["phone"]="^[0-9 /+()/-]+$";//"^[+]{1}[0-9]+"
$REG_EXP["login"]="^[0-9a-zA-Z_/-]+$";
$REG_EXP["int"]="^[0-9]+$";
$REG_EXP["float"]="^[0-9]+([.,]{1}[0-9]{0,2})?$";
$REG_EXP["url"]="^http[s]{0,1}://.+$";
$REG_EXP["zip"]="^[A-Za-z0-9_-]+$";

/* loading settings */
$SETTINGS = LoadSettings();
$SETTINGS['min_partner_payout'] = 100;
$SETTINGS['partner_default_payout_perc'] = 0.05;
$SETTINGS['subpartner_payout_perc'] = 0.05;

$SETTINGS['study_matherials_path']  = "/matherials";
$SETTINGS['members_items_per_page'] = 15;
$SETTINGS['logo_filename'] = "/img/logo.gif";

$ADMIN_EMAIL 	 = $SETTINGS['admin_email'];
$SUPPORT_EMAIL 	 = $SETTINGS['support_email'];
$WEBMASTER_EMAIL = $SETTINGS['webmaster_email'];
$BILLING_EMAIL 	 = $SETTINGS['billing_email'];

$SITE_NAME	= $SETTINGS['site_name'];
$SITE_DOMAIN 	= $SETTINGS['site_url'];
$SITE_URL 	= "http://$SITE_DOMAIN";
$ADMIN_URL 	= $SITE_URL.($SETTINGS['admin_dir']!=''?$SETTINGS['admin_dir']:"admin");
$PARTNER_URL 	= "$SITE_URL/partner";

/* these settings are vital for admin panel functioning */
if (empty($SETTINGS['admin_items_per_page']))
{
	$SETTINGS['admin_items_per_page'] = 20;
}
if (empty($SETTINGS['admin_pages_per_block']))
{
	$SETTINGS['admin_pages_per_block'] = 5;
}

/* client */
$CLIENT_ID = CheckClientLogin();

if($CLIENT_ID>0){
	$cl_row = mysql_fetch_object(mysql_query($DB_LINK,"select clients.*, countries.name as country, client_worksheet_logos.img_filename from (clients inner join countries on clients.country_id = countries.id) left outer join client_worksheet_logos on client_worksheet_logos.id = clients.worksheet_logo_id where clients.id=$CLIENT_ID"));
	
	$CLIENT_DATA = Array();
	$CLIENT_DATA["title"] = stripslashes($cl_row->title);
	$CLIENT_DATA["first_name"] = stripslashes($cl_row->first_name);
	$CLIENT_DATA["last_name"] = stripslashes($cl_row->last_name);
	$CLIENT_DATA["email"] = stripslashes($cl_row->email);
	$CLIENT_DATA["password"] = stripslashes($cl_row->password);
	$CLIENT_DATA["country_id"] = $cl_row->country_id;
	$CLIENT_DATA["country"] = stripslashes($cl_row->country);
	$CLIENT_DATA["county"] = stripslashes($cl_row->county);
	$CLIENT_DATA["zip"] = stripslashes($cl_row->postcode);
	$CLIENT_DATA["city"] = stripslashes($cl_row->city);
	$CLIENT_DATA["address1"] = stripslashes($cl_row->address1);
	$CLIENT_DATA["address2"] = stripslashes($cl_row->address2);
	$CLIENT_DATA["company"] = stripslashes($cl_row->COMPANY);
	$CLIENT_DATA["phone"] = stripslashes($cl_row->phone);
	$CLIENT_DATA["discount_perc"] = $cl_row->discount_perc;
	$CLIENT_DATA["discount_fix"] = $cl_row->discount_fix;
	$CLIENT_DATA["discount_currency_id"] = $cl_row->discount_currency_id;
	$CLIENT_DATA["receive_news"] = $cl_row->receive_news;
	$CLIENT_DATA["logo_filename"] = $cl_row->img_filename;
	$CLIENT_DATA["logo_id"] = $cl_row->worksheet_logo_id;
	$CLIENT_DATA["student_name"] = stripslashes($cl_row->student_name);
	$CLIENT_DATA["print_ref"] = $cl_row->print_ref;
	
	/* tariff info */
	$show_purchase_link = false;
	if(mysql_num_rows(mysql_query("select * from clients2tariffs where client_id=$CLIENT_ID and status=1"))<=0){
		$show_purchase_link = true;
	}
}


/*SMARTY*/
define("SMARTY_DIR",$DOCUMENT_ROOT."/../includes/smarty/libs/");
require(SMARTY_DIR.'Smarty.class.php');
$smarty = new Smarty;
$smarty->template_dir = $DOCUMENT_ROOT."/../templates/";
$smarty->compile_dir = $DOCUMENT_ROOT."/../includes/smarty/templates_c/";
$smarty->config_dir = $DOCUMENT_ROOT."/../includes/smarty/configs/";
$smarty->cache_dir = $DOCUMENT_ROOT."/../includes/smarty/cache/";
$smarty->caching = false;

// очищаем все файлы кэша
$smarty->clear_all_cache();

/* modificators & functions */
include($DOCUMENT_ROOT."/../includes/smarty_functions.php");

$smarty->register_function("array_size","smarty_array_size");
$smarty->register_function("get_input","smarty_common_add_get_input");
$smarty->register_function("helptip","smarty_helptip");
$smarty->register_function("get_left_menu","smarty_get_left_menu");
$smarty->register_function("get_top_menu","smarty_get_top_menu");
$smarty->register_function("get_bottom_menu","smarty_get_bottom_menu");
$smarty->register_function("get_maths_topics","smarty_get_maths_topics");
$smarty->register_function("strip_tags","smarty_strip_tags");
$smarty->register_function("addslashes","addslashes");
$smarty->register_function("addslashes","addslashes");
$smarty->register_function("htmlspecialchars","htmlspecialchars");


/* plugins */
$smarty->register_resource("db", array("db_get_template",
                                       "db_get_timestamp",
                                       "db_get_secure",
                                       "db_get_trusted"));

/* error and status */
$smarty->assign('error',$error);
$smarty->assign('status',$status);

/* settings */
$smarty->assign('SETTINGS',$SETTINGS);

$smarty->assign("DEF_CURRENCY", $DEF_CURRENCY);

$smarty->assign('SITE_URL',$SITE_URL);
$smarty->assign('SITE_NAME',$SITE_NAME);
$smarty->assign('SITE_URL_SSL',$SITE_URL_SSL);
$smarty->assign('ADMIN_URL',$ADMIN_URL);
$smarty->assign('PARTNER_URL',$PARTNER_URL);
$smarty->assign('REG_EXP',$REG_EXP);
$smarty->assign('SETTINGS',$SETTINGS);
$smarty->assign('CLIENT_TITLES',$CLIENT_TITLES);
$smarty->assign('CATEGORY_DIR',$CATEGORY_DIR);
$smarty->assign('FULL_TREE_DIR',$FULL_TREE_DIR);
$smarty->assign('CLIENT_DIR',$CLIENT_DIR);
$smarty->assign('CLIENT_ID',$CLIENT_ID);
$smarty->assign('CLIENT_DATA',$CLIENT_DATA);

$smarty->assign('show_purchase_link',$show_purchase_link);

if (defined("ADMIN_AUTHORIZATION")){
	/* ADMIN ACTIONS */
	$ADMIN_ID = CheckAdminLogin();
	if (!$ADMIN_ID) {
		$_SESSION['ERROR_MSG'] = "Пожалуйста, введите логин и пароль.";
		header("Location: $ADMIN_URL/login.php");
		exit;	
	}
	
	$admin_row = mysql_fetch_object(mysql_query("select * from administrators where id=$ADMIN_ID"));

	$ADMIN_DATA = Array();
	$ADMIN_DATA["login"] = stripslashes($admin_row->login);
	$ADMIN_DATA["email"] = stripslashes($admin_row->email);
	$ADMIN_DATA["name"] = stripslashes($admin_row->name);
}

if (defined("MERCHANT_AUTHORIZATION")) 
{
	/* ADMIN ACTIONS */
	$MERCHANT_ID = CheckMerchantLogin();
	if (0 >= $MERCHANT_ID) 
	{
		$_SESSION['ERROR_MSG'] = "Пожалуйста, введите логин и пароль.";
		header("Location: /merchant/cabinet/login.php");
		exit;	
	}
	
	
}

if(defined("ADMIN_AUTHORIZATION")||defined("PARTNER_AUTHORIZATION")||defined("MERCHANT_AUTHORIZATION")){
	/* ADMIN ICONS*/
	$ADMIN_ICONS[edit]= $smarty->fetch("admin_common_btn_edit.tpl");
	$ADMIN_ICONS[options]= $smarty->fetch("admin_common_btn_options.tpl");
	$ADMIN_ICONS[del]= $smarty->fetch("admin_common_btn_del.tpl");
	$ADMIN_ICONS[add]= $smarty->fetch("admin_common_btn_add.tpl");
	$ADMIN_ICONS[bill]= $smarty->fetch("admin_common_btn_bill.tpl");
	$ADMIN_ICONS[status_on]= $smarty->fetch("admin_common_status_on.tpl");
	$ADMIN_ICONS[status_off]= $smarty->fetch("admin_common_status_off.tpl");
	$ADMIN_ICONS[status_error]= $smarty->fetch("admin_common_status_error.tpl");
	$ADMIN_ICONS[status_refund]= $smarty->fetch("admin_common_status_refund.tpl");
	$ADMIN_ICONS[status_chargeback]= $smarty->fetch("admin_common_status_chargeback.tpl");
	$ADMIN_ICONS[status_progress]= $smarty->fetch("admin_common_status_progress.tpl");
	$ADMIN_ICONS[yes]= $smarty->fetch("admin_common_yes.tpl");
	$ADMIN_ICONS[no]= $smarty->fetch("admin_common_no.tpl");
	$ADMIN_ICONS[cattype_simple]= $smarty->fetch("admin_cattype_simple.tpl");
	$ADMIN_ICONS[cattype_final]= $smarty->fetch("admin_cattype_final.tpl");
	$ADMIN_ICONS[cattype_subfinal]= $smarty->fetch("admin_cattype_subfinal.tpl");
	$ADMIN_ICONS[preview]= $smarty->fetch("admin_common_btn_preview.tpl");
	$ADMIN_ICONS["print"]= $smarty->fetch("admin_common_btn_print.tpl");
}

/* partner authorization */

if (defined("PARTNER_AUTHORIZATION")) {

	/* PARTNER ACTIONS */
	$PARTNER_ID = CheckPartnerLogin();
	if ($PARTNER_ID<=0) {
		$_SESSION['ERROR_MSG'] = "Пожалуйста, введите логин и пароль.";
		header("Location: $PARTNER_URL/login.php");
		exit;	
	}

	$p_row = mysql_fetch_object(mysql_query("select partners.*, currencies.symbol, currencies.name as currency_name from partners left outer join currencies on currencies.id = partners.currency_id where partners.id=$PARTNER_ID"));

	
	$PARTNER_DATA = Array();
	$PARTNER_DATA["login"] = stripslashes($p_row->login);
	$PARTNER_DATA["title"] = stripslashes($p_row->title);
	$PARTNER_DATA["email"] = stripslashes($p_row->email);
	$PARTNER_DATA["comission_perc"] = $p_row->payout_perc;
	$PARTNER_DATA["amount"] = $p_row->amount;
	$PARTNER_DATA["amount_str"] = MoneyToStr($p_row->amount);
	$PARTNER_DATA["currency_id"] = $p_row->currency_id;
	$PARTNER_DATA["currency_symbol"] = $p_row->symbol;
	$PARTNER_DATA["currency_name"] = $p_row->currency_name;
	$PARTNER_DATA["trans_notify"] = $p_row->trans_notify;
	
	$smarty->assign("PARTNER_DATA",$PARTNER_DATA);
}

/* error and status */
if($_SESSION['ERROR_MSG']!=""){
	$error = $_SESSION['ERROR_MSG'];
	if(!$SAVE_MSG){
		unset($_SESSION['ERROR_MSG']);
	}	
}
if($_SESSION['STATUS_MSG']!=""){
	$status = $_SESSION['STATUS_MSG'];
	if(!$SAVE_MSG){
		unset($_SESSION['STATUS_MSG']);
	}	
}
if($_SESSION['REDIRECT']!=""){
	$redirect = $_SESSION['REDIRECT'];
	if(!$SAVE_MSG){
		unset($_SESSION['REDIRECT']);
	}	
}

/* REFERRER */
if($_SERVER['HTTP_REFERER']!=""&&$_COOKIE['http_referer']==""){
	$arr1 = parse_url($_SERVER['HTTP_REFERER']);
	$arr2 = parse_url($SITE_URL);
	if(str_replace("www.","",$arr1['host'])!=str_replace("www.","",$arr2['host'])){
		$_SESSION['http_referer'] = $_SERVER['HTTP_REFERER'];
		setcookie("http_referer",$_SERVER['HTTP_REFERER'],time()+90*86400);
	}
}
elseif($_COOKIE['http_referer']!=""){
	$_SESSION['http_referer'] = $_COOKIE['http_referer'];
}

$smarty->assign('error',$error);
$smarty->assign('status',$status);
$smarty->assign('redirect',$redirect);

?>