<?php
$t = "Гостевая книга";
if ($VarIO->DefS("module_gb_enable", 1) == 0){
	twig("/gb.lm", ["block"	=>	"gb_disabled"]);
}
if ($user->auth() && !$acl->isGuestBookRead){
	twig("/gb.lm", ["block"	=>	"no_access"]);
} elseif (!$user->auth() && $VarIO->DefS("module_gb_guest_view", 1) == 0){
	twig("/gb.lm", ["block"	=>	"no_access"]);
}

if ($user->auth() && $act == "del" && $id != 0 && $acl->isGuestBookModer){
	$obj = $db->getArray("SELECT * FROM `gb` WHERE `sid` = '".$site->id."' AND `gid` = '".$id."' LIMIT 1");
	if ($obj != NULL){
		$arr = [
			"id"		=>	$obj["gid"],
			"time"		=>	$obj["timeadd"],
			"name"		=>	$obj["name"],
			"message"	=> 	$core->drawString($obj["sms"]),
			"user"		=>	getUserInfo($obj["uid"]),
			"ip"		=>	$obj["ip"],
			"ua"		=>	$obj["ua"],
			"isAuthor"	=>	($core->sverIPUA($obj["ip"], $obj["ua"], $user->uid, $obj["uid"])?1:0),
			"isModer"	=>	($obj["moder"] == 1?true:false),
		];
		$arr["user"] = getUserInfo($obj["uid"]);
		if (isset($_POST["send"])){
			$db->Query("DELETE FROM `gb` WHERE `sid` = '".$site->id."' AND `gid` = '".$id."' LIMIT 1");
			header("Location: ?msg=deleted");
			die();
		} 
		twig("/gb.lm", ["block"	=>	"delete_post", "post"	=> $arr]);
	}
}

if ($user->auth() && $act == "mod" && $id != 0 && $acl->isGuestBookModer){
	$obj = $db->getArray("SELECT * FROM `gb` WHERE `sid` = '".$site->id."' AND `gid` = '".$id."' LIMIT 1");
	if ($obj != NULL){
		$arr = [
			"id"		=>	$obj["gid"],
			"time"		=>	$obj["timeadd"],
			"name"		=>	$obj["name"],
			"message"	=> 	$core->drawString($obj["sms"]),
			"user"		=>	getUserInfo($obj["uid"]),
			"ip"		=>	$obj["ip"],
			"ua"		=>	$obj["ua"],
			"isAuthor"	=>	($core->sverIPUA($obj["ip"], $obj["ua"], $user->uid, $obj["uid"])?1:0),
			"isModer"	=>	($obj["moder"] == 1?true:false),
		];
		$arr["user"] = getUserInfo($obj["uid"]);
		if (isset($_POST["send"])){
			$db->Query("UPDATE `gb` SET `moder`='1' WHERE `sid` = '".$site->id."' AND `gid` = '".$id."' LIMIT 1");
			header("Location: ?msg=moder");
			die();
		} 
		twig("/gb.lm", ["block"	=>	"mod_post", "post"	=> $arr]);
	}
}

if ($user->auth() && $act == "clean" && $acl->isGuestBookModer){
	if (isset($_POST["send"])){
		$db->Query("DELETE FROM `gb` WHERE `sid` = '".$site->id."'");
		header("Location: ?msg=cleaned");
		die();
	} 
	twig("/gb.lm", ["block"	=>	"clean"]);

}

if (isset($_POST["send"])){
	if ((!isset($_POST["name"]) || empty($_POST["name"])) && !$user->auth()){
		$core->regError("","Введите имя", 1, "gb");
	} elseif (!isset($_POST["sms"]) || empty($_POST["sms"])){
		$core->regError("","Введите сообщение", 2, "gb");
	} elseif (!preg_match("#^([A-z0-9\_-])+$#ui", $_POST['name']) && !$user->auth()) {
		$core->regError("","Только латиница, цифры и вверхнее и нижние подчеркивание", 3, "gb");
	} elseif ($VarIO->DefS("module_gb_captcha", 1) == 1 && $_POST["code"] != $_SESSION['captcha']){
		$core->regError("","Неправильно введен код с картинки", 4, "gb");
	} elseif (!$core->getIP() || !$core->getUA()){
		$core->regError("","Вы не можете оставить сообщение. Обратитесь к администратору для решения данной проблемы.",4,"gb");
	} else {
		$name = ($user->auth()?$user->getData()->login:$core->FilterString($_POST["name"]));
		$db->Query("INSERT INTO `gb`(`sid`, `uid`, `ip`, `name`, `sms`, `ua`, `timeadd`, `moder`) VALUES ('".$site->id."','".$user->uid."','".$core->getIP()."','".$name."','".$core->FilterString($_POST["sms"])."','".$core->getUA()."','".(time())."','".$VarIO->DefS("module_gb_automod", 0)."')");
		header("Location: ?msg=sended");
		die();
	}
}
$max 		= $VarIO->DefS("items_per_page", 10);
$k_post 	= $db->Count("gb", "WHERE `sid` = '".$site->id."'");
$k_page 	= $core->k_page($k_post,$max);
$page 		= $core->page($k_page);
$start 		= $max*$page-$max;
$elements	= [];
$gb = $db->Query("SELECT * FROM `gb` WHERE `sid` = '".$site->id."' ORDER BY `gid` DESC LIMIT $start, $max"); 
while ($elem = mysqli_fetch_assoc($gb)){
	$elements[] = [
		"id"		=>	$elem["gid"],
		"time"		=>	$elem["timeadd"],
		"name"		=>	$elem["name"],
		"message"	=> 	$core->drawString($elem["sms"]),
		"user"		=>	getUserInfo($elem["uid"]),
		"ip"		=>	$elem["ip"],
		"ua"		=>	$elem["ua"],
		"isAuthor"	=>	($core->sverIPUA($elem["ip"], $elem["ua"], $user->uid, $elem["uid"])?1:0),
		"isModer"	=>	($elem["moder"] == 1?true:false),
	];
}
twig("/gb.lm", ["block"	=>	"base","elems" => $elements, "pages" => [
	"elemsCount"	=> 	$k_post,
	"api"			=>	$core->twig_page_mod($k_page, $page, ["msg","userID"]),
]]);

?>