<?php
define('protect', TRUE);
require_once 'includes/protect.php';

$protocol = strtolower(substr($_SERVER["SERVER_PROTOCOL"],0,strpos( $_SERVER["SERVER_PROTOCOL"],'/'))).'://';
$fullurl = (htmlentities($_SERVER['SERVER_NAME'].$_SERVER['SCRIPT_NAME'], ENT_QUOTES, "UTF-8"));
$siteurl1 = substr($fullurl, 0, strrpos( $fullurl, '/'));
$siteurl = substr($siteurl1, strrpos($siteurl1, '/'));
$sitename = substr($siteurl, 0, strrpos( $siteurl, '.'));

if (strpos($fullurl, '/sites/') === false) {
require_once 'includes/connect.php';
} else {
require_once 'sites'.$sitename.'/includes/connect.php';
$sitepath1='/sites'.$sitename.'/';
}

$pagename = basename (htmlentities($_SERVER['SCRIPT_NAME'], ENT_QUOTES, "UTF-8")); 
$pageurl = basename (htmlentities($_SERVER['REQUEST_URI'], ENT_QUOTES, "UTF-8"));
$pagepath = (htmlentities($_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'], ENT_QUOTES, "UTF-8"));

if (strpos($pageurl, '?') !== false AND strpos($pageurl, 'pag=') == false AND strpos($pageurl, 'orderp=') == false AND $pagename!='contact.php') {
header ("Location: .");
exit;
}
?>
<?php
$result = $mysqli->query("SELECT * FROM siteinfo");
$row = $result->fetch_array();
$site_id=$row['site_id'];
$domain=$row['domain'];
$firma=$row['firma'];
$sitemail=$row['sitemail'];
$paymail=$row['paymail'];
if ($paymail=='') { $paymail=$sitemail; }
$x=$row['x'];
$y=$row['y'];
$map=$row['map'];
$resurl=$row['resurl'];
$resform=$row['resform'];
$payurl=$row['payurl'];
$sitenr=$row['sitenr'];
$sitecode=$row['sitecode'];
$promocode=$row['promocode'];
$sitescript=$row['sitescript'];
$sitescript2=$row['sitescript2'];
$sitescript3=$row['sitescript3'];
$package=$row['package'];
$cookies=$row['cookies'];
$type=$row['type'];
$inquiry=$row['inquiry'];
$payment=$row['payment'];
$imgsite=$row['imgsite'];
$imgcat=$row['imgcat'];
$imgitem=$row['imgitem'];
$nritems=$row['nritems'];
$decimalprice=$row['decimalprice'];
$sitediscount=$row['discount'];
$sitediscount2=$row['discount2'];
$siteweight=$row['weight'];
$weightunit=$row['weightunit'];
$weightpac=$row['weightpac'];
$siteitem=$row['siteitem'];
$siteform=$row['siteform'];
$sitepath=$row['sitepath'];
$slide=$row['slide'];
$stylesheet=$row['stylesheet'];
$customcss=$row['customcss'];
$meta=$row['meta'];

if ($siteitem=='') {
$itempage="1";
}
$pid="1";
?>
<?php
$result = $mysqli->query("SELECT * FROM lang WHERE active=1 AND main=1");
$row = $result->fetch_array(); 
$lang=$row['id'];
$langname=$row['dir'];
$langdiscount=$row['discount'];
$dir = $mysqli->real_escape_string($_REQUEST['dir']);
if ($dir=='') { 
$dir="";
$canonical=$sitepath;
}else{
$result = $mysqli->query("SELECT * FROM lang WHERE dir='$dir' AND main=0");
$row = $result->fetch_array(); 
$lang=$row['id'];
$langname=$row['dir'];
$language=$row['language'];
$dir=$row['dir'];
$langdiscount=$row['discount'];
$activel=$row['active'];
$canonical=$sitepath.$dir."/";

if ($activel==0) {
header ("Location: /");
exit;
}
}

if ($dir) { 
$codepath='../'; 
}

if ($sitepath1) {
$filepath=$sitepath1.'files/';
$imagepath='..'.$sitepath1.'files/';
$imgsize=".".$filepath;

} else {

$filepath=$sitepath.'files/';
$imagepath='../files/';
$imgsize=$sitepath.'files/';
if ($sitepath) {
$codepath=$sitepath;
} else {
$filepath=$codepath.'files/';
}

}
?>
<?php
$result = $mysqli->query("SELECT * FROM langvars WHERE lang_id=$lang AND name NOT LIKE ('admin%') ORDER BY id");
while($row = $result->fetch_array()) {
$fname=$row['name'];
$ftext=$row['text'];

$fresult=$$fname="$ftext";
}
$sitecompany1=$sitecompany;
$sitecompany=strip_tags($sitecompany);
$siteactivity=strip_tags($siteactivity);
if (!$logotext) {
$logotext=mb_strtolower($titleactivity, 'UTF-8');	
}
?>
<?php
$result = $mysqli->query("SELECT * FROM menu ORDER BY id");
while($row = $result->fetch_array()) {
$ffeature=$row['feature'];
$factive=$row['active'];
			
$fresultmenu=$$ffeature="$factive";
}?>
<?php
$result = $mysqli->query("SELECT * FROM paymethods WHERE active=1 ORDER BY id");
while($row = $result->fetch_array()) {
$mname=$row['name'];
$mactive=$row['active'];
			
$mresult=$$mname="$mactive";
}?>
<?php
if ($type==2 OR $cookies==1) {
$resultcnr = $mysqli->query("SELECT COUNT(id) FROM cookies");
$crows = $resultcnr->fetch_row();
$cookienr = $crows[0];
if ($cookienr>5000){
$mysqli->query("TRUNCATE cookies");
$mysqli->query("TRUNCATE cart");
}
$hostnew=getenv("REMOTE_ADDR");
$namenew=gethostbyaddr($hostnew);
$result = $mysqli->query("SELECT * FROM cookies WHERE host='$hostnew' OR name='$namenew'");
$row = $result->fetch_array();
$cookieid=$row['id'];
if (!$cookieid){
$mysqli->query("INSERT INTO cookies (host,name) VALUES ('$hostnew','$namenew')");
setcookie ("$domain", $cookieid, time()+200000);
}
}
if ($type==2) {
if(isset($_POST['deleteactive'])) {
$mysqli->query("DELETE FROM cart WHERE cookie_id='$cookieid'");
$mysqli->query("ALTER TABLE cart AUTO_INCREMENT = 1");
}
if (isset($_POST['delete'.$_REQUEST['activeid']]) OR $_REQUEST['quantity']==0) {
$mysqli->query("DELETE FROM cart WHERE active_id='{$_REQUEST['activeid']}'");
}
$result = $mysqli->query("SELECT id FROM cart WHERE cookie_id='$cookieid' AND item_id=ANY (SELECT insert_id FROM items WHERE publish='yes') GROUP BY active_id");
$activenr = mysqli_num_rows($result);
if (isset($_POST['submitcart'.$_REQUEST['id']])) {
header ("Location: cart");	
}
}
?>