<?php
    session_start();
    include ("dbinfo.php");
	if ($dbhost == "cms_dbhost" && $install == "") {
	header("location: install.php");
	die;
	}
	if (@mysql_select_db($dbname, @mysql_connect($dbhost, $dbuser, $dbpass)) == FALSE && $install == "") {
	echo "Sorry but we are currently experiencing some issues. Please try again in a moment. Thank you.";
	die;
	}
	include ("functions.php");
	$info = new BrowserInfo($_SERVER['HTTP_USER_AGENT']);

    $version = "1.4";
	$cpage = basename($_SERVER['REQUEST_URI']);
	$apage = basename($_SERVER['PHP_SELF']);
   
    if ($install == "") {
	if (@mysql_query("SELECT * FROM ".$pre."articles") == FALSE) {
	header("location: install.php");
	die;
	}

	if (detect_bot()) {
	$user = detect_bot();
	$bot = detect_bot();
	} else {
	$user = mysql_real_escape_string(stripslashes($_COOKIE[adaptcms_username]));
	}
	$times = time() - 3601;
    
	$sql = mysql_query("SELECT * FROM ".$pre."settings WHERE type = 'setting'");
	while($row = mysql_fetch_array($sql)) {
	$name = $row[name];

	$setting["$name"] = $row[data];
	$name = "";
	}
	$siteurl = "http://".$_SERVER['HTTP_HOST'].str_replace($apage, "", $_SERVER['PHP_SELF']);
	$sitepath = str_replace($apage, "", $_SERVER['SCRIPT_FILENAME']);
	$domain = check_domain($siteurl);

	$sitedir = str_replace($apage, "", $_SERVER['PHP_SELF']);
	$pageurl = substr($siteurl,0,-1).str_replace($sitedir,"/",$_SERVER['REQUEST_URI']);

	if (strtolower($setting["mod_rewrite"]) == "yes") {
	$mrw_1["content"] = $siteurl."article/";
	$mrw_1["gallery"] = $siteurl."gallery/";
	$mrw_1["login"] = $siteurl;
	$mrw_1["logout"] = $siteurl;
	$mrw_1["page"] = $siteurl."page/";
	$mrw_1["register"] = $siteurl;
	$mrw_1["section"] = $siteurl."section/";
	$mrw_1["archive"] = $siteurl."archive/";
	$mrw_1["user"] = $siteurl."user/";
	$mrw["af_apply"] = $siteurl."affiliate-apply/";
	$mrw["affiliates"] = $siteurl."affiliates/";
	$mrw["archive"] = $siteurl."archive/";
	$mrw["edit_profile"] = $siteurl."profile/";
	$mrw["members_list"] = $siteurl."memberslist/";
	$mrw["poll_results"] = $siteurl."poll-results/";
	$mrw["polls"] = $siteurl."polls/";
	$mrw["profile"] = $siteurl."profile/";
	$mrw["rss"] = $siteurl."rss/";
	$mrw["search"] = $siteurl."search/";
	$mrw["user_list"] = $siteurl."memberslist/";
	} else {
	$mrw_1["content"] = $siteurl."?view=article&id=";
	$mrw_1["gallery"] = $siteurl."?view=gallery&id=";
	$mrw_1["login"] = $siteurl."?view=";
	$mrw_1["logout"] = $siteurl."?view=";
	$mrw_1["page"] = $siteurl."?view=page&id=";
	$mrw_1["register"] = $siteurl."?view=";
	$mrw_1["section"] = $siteurl."?cat=";
	$nrw_1["archive"] = $siteurl."?view=list";
	$mrw_1["user"] = $siteurl."?view=profile&user=";
	$mrw["af_apply"] = $siteurl."?view=af_apply";
	$mrw["affiliates"] = $siteurl."?view=affiliates";
	$nrw["archive"] = $siteurl."?view=list";
	$mrw["edit_profile"] = $siteurl."?view=edit_profile";
	$mrw["members_list"] = $siteurl."?view=memberslist";
	$mrw["poll_results"] = $siteurl."?results=yes";
	$mrw["polls"] = $siteurl."?view=polls";
	$mrw["profile"] = $siteurl."?view=profile";
	$mrw["rss"] = $siteurl."?view=rss";
	$mrw["search"] = $siteurl."?view=search";
	$mrw["user_list"] = $siteurl."?view=memberslist";
	}

	if (date("N") == 1 or date("j") == 1 or date("z") == 0) {
	echo stats_check();
	}

	// Daily stats cleaning
	$statsarch = mysql_fetch_row(mysql_query("SELECT data FROM ".$pre."fielddata WHERE aid = 'stats_archive' AND fname = 'last_update'"));
	$statsupdate = time() - $statsarch[0];
    if ($statsarch[0] == "") {
	mysql_query("INSERT INTO ".$pre."fielddata VALUES (null, 'last_update', '".time()."', 'stats_archive')");
	mysql_query("DELETE FROM ".$pre."stats_archive WHERE views < 5 AND uniques = 0 AND name = 'page' OR page = 'referral' AND views < 5 AND uniques = 0");
	} else {
	if ($statsupdate > 86400) {
	mysql_query("DELETE FROM ".$pre."stats_archive WHERE views < 5 AND uniques = 0 AND name = 'page' OR name = 'referral' AND views < 5 AND uniques = 0 OR name = 'keyword' AND views < 5 AND uniques = 0");
	mysql_query("UPDATE ".$pre."fielddata SET data = '".time()."' WHERE aid = 'stats_archive' AND fname = 'last_update'");
	}
	}

	// Checks for articles that were set to be published at a certain date and time
	$sql_pub = mysql_query("SELECT * FROM ".$pre."articles WHERE ver != '' AND ver != 'saved' AND ver <= '".time()."'");
	while($pubr = mysql_fetch_array($sql_pub)) {
	mysql_query("UPDATE ".$pre."articles SET ver = '', date = '".$pubr[ver]."', mdate = '".date("m", $pubr[ver])."', ydate = '".date("Y", $pubr[ver])."' WHERE id = '".$pubr[id]."'");
	}

	if ($_POST['answer'] && is_numeric($_POST['pid']) or $_POST['custom_answer']) {
	if ($_POST['custom'] && $_POST['custom_answer']) {
	$answer = badwords(substr(htmlentities(check($_POST['custom_answer'])), 0, 15));
	mysql_query("INSERT INTO ".$pre."polls VALUES (null, '".$answer."', 'custom_option', '', '".htmlentities(urldecode($_POST['question']))."', 1, '".time()."')");
	mysql_query("UPDATE ".$pre."polls SET votes=votes+1 WHERE name = '".htmlentities(urldecode($_POST['question']))."'");
	$pid = htmlentities(stripslashes($_POST['pid']));
	$_SESSION["poll_".$pid] = $answer;
	} else {
	if ($_POST['multi']) {
	while (list(, $i) = each($_POST['answer'])) {
	mysql_query("UPDATE ".$pre."polls SET votes=votes+1 WHERE name = '".addslashes(urldecode($i))."' AND pname = '".htmlentities(urldecode($_POST['question']))."'");
	mysql_query("UPDATE ".$pre."polls SET votes=votes+1 WHERE name = '".htmlentities(urldecode($_POST['question']))."'");
	$op .= htmlentities(urldecode($i)).", ";
	}
	$pid = htmlentities(stripslashes($_POST['pid']));
	$_SESSION["poll_".$pid] = $op;
	} else {
	mysql_query("UPDATE ".$pre."polls SET votes=votes+1 WHERE name = '".htmlentities(urldecode($_POST['answer']))."' AND pname = '".htmlentities(urldecode($_POST['question']))."'");
	mysql_query("UPDATE ".$pre."polls SET votes=votes+1 WHERE name = '".htmlentities(urldecode($_POST['question']))."'");
	$pid = htmlentities(stripslashes($_POST['pid']));
	$_SESSION["poll_".$pid] = htmlentities(stripslashes($_POST['answer']));
	}
	}
	header("location: index.php?view=redirect&url=".$_SERVER['REQUEST_URI']);
	}
	$op = "";$pid = "";$i = "";

	if ($_POST['skin']) {
    setcookie("adaptcms_skin", $_POST['skin'], time()+3600*60*24*14);
    }
    
	if ($_COOKIE[adaptcms_username] && $_COOKIE[adaptcms_password]) {
	$upd = sprintf("UPDATE ".$pre."users SET logged = '".time()."' WHERE username = '%s' AND password = '%s'",
    mysql_real_escape_string(check($_COOKIE[adaptcms_username])),
    mysql_real_escape_string($_COOKIE[adaptcms_password]));
	mysql_query($upd);
	$prf2 = sprintf("SELECT email,level,rank,logged,date,id FROM ".$pre."users WHERE username = '%s' AND password = '%s'",
    mysql_real_escape_string(check($_COOKIE[adaptcms_username])),
    mysql_real_escape_string($_COOKIE[adaptcms_password]));
	$prf = mysql_fetch_row(mysql_query($prf2));
	$useridn = $prf[5];
	$email = $prf[0];
	$level = $prf[1];
	$rank = $prf[2];
	$logged = $prf[3];
	$regdate = $prf[4];

	$lev2 = mysql_fetch_row(mysql_query("SELECT value FROM ".$pre."levels WHERE name = '".$level."'"));
	$lev = $lev2[0];
	}
	if ($_COOKIE[adaptcms_username] && $_COOKIE[adaptcms_password] && $useridn == "" && $_GET['view'] != "login" & $_GET['view'] != "logout" && $_GET['view'] != "login2") {
	echo "Your information does not match. Please clear your cookies and <a href='".$mrw_1["register"]."register'>register</a> or <a href='".$mrw_1["login"]."login'>login</a>. Thank you.<br /><br />Stopping Script.";
	die;
	}

	if ($protect == "yes") {
	$adinfo = sprintf("SELECT email,level,rank,logged,date,id FROM ".$pre."users WHERE username = '%s' AND password = '%s'",
    mysql_real_escape_string(check($_SESSION[adaptcms_username])),
    mysql_real_escape_string($_SESSION[adaptcms_password]));
	$admin_info = mysql_fetch_row(mysql_query($adinfo));
	if ($_GET['view'] == "denied" or $_GET['view'] == "login" or $_GET['view'] == "login2") {
	} else {
	if ($lev > 1 or $admin_info[1] > 1) {
	header('location: admin.php?view=denied');
	}
	if ($lev == "" or $admin_info[1] == "") {
	header('location: admin.php?view=login');
	}
	}
	}

	$plugins_sql = mysql_query("SELECT * FROM ".$pre."plugins ORDER BY `name` ASC");
	while($rplgs = @mysql_fetch_array($plugins_sql)) {
	if ($rplgs['status'] == "On" && $rplgs['name']) {
	if (file_exists("plugins/".strtolower(str_replace(" ", "_", $rplgs['name']))."_functions.php")) {
	include ("plugins/".strtolower(str_replace(" ", "_", $rplgs['name']))."_functions.php");
	}
	}
	}

	if (preg_match("/admin.php/", $cpage)) {
	$ptype = "backend";
	} else {
	$ptype = "frontend";
	}

    $stype2 = mysql_num_rows(mysql_query("SELECT * FROM ".$pre."stats WHERE ip = '".$_SERVER['REMOTE_ADDR']."'"));

	if ($stype2 == 0) {
	mysql_query("INSERT INTO ".$pre."stats VALUES (null, '".$pageurl."', '".$_SERVER['HTTP_REFERER']."', 'unique', '".$ptype."', '".$user."', '".$_SERVER['REMOTE_ADDR']."', '".$info->OS." ".$info->OS_Version."|".$info->Browser." ".$info->Browser_Version."|".$_SERVER['HTTP_USER_AGENT']."', '".date("d")."', '".date("z")."', '".date("m")."', '".date("W")."', '".date("Y")."', '".time()."')");
	}
	mysql_query("INSERT INTO ".$pre."stats VALUES (null, '".$pageurl."', '".$_SERVER['HTTP_REFERER']."', 'view', '".$ptype."', '".$user."', '".$_SERVER['REMOTE_ADDR']."', '".$info->OS." ".$info->OS_Version."|".$info->Browser." ".$info->Browser_Version."|".$_SERVER['HTTP_USER_AGENT']."', '".date("d")."', '".date("z")."', '".date("m")."', '".date("W")."', '".date("Y")."', '".time()."')");

	// Advanced stats
	if ($ptype == "frontend" && $pageurl) {
	if (@mysql_num_rows(@mysql_query("SELECT * FROM ".$pre."stats_archive WHERE name = 'page' AND data = '".$pageurl."'")) == 0) {
	mysql_query("INSERT INTO ".$pre."stats_archive VALUES (null, 'page', '".$pageurl."', '', '', '', '1', '', '')");
	} else {
	mysql_query("UPDATE ".$pre."stats_archive SET views=views+1 WHERE name = 'page' AND data = '".$pageurl."'");
	}
	}

	if ($_SERVER['HTTP_REFERER'] && preg_match("/admin.php/", $_SERVER['HTTP_REFERER']) == 0 && $ptype == "frontend") {
	if (@mysql_num_rows(@mysql_query("SELECT * FROM ".$pre."stats_archive WHERE name = 'referral' AND data = '".$_SERVER['HTTP_REFERER']."'")) == 0) {
	mysql_query("INSERT INTO ".$pre."stats_archive VALUES (null, 'referral', '".$_SERVER['HTTP_REFERER']."', '', '', '', '1', '', '')");
	} else {
	mysql_query("UPDATE ".$pre."stats_archive SET views=views+1 WHERE name = 'referral' AND data = '".$_SERVER['HTTP_REFERER']."'");
	}
	if (preg_match("/q=/", $_SERVER['HTTP_REFERER']) == 1) {
	$kwd = explode("q=", $_SERVER['HTTP_REFERER']);
	$kod = explode("&", $kwd[1]);
	if (mysql_num_rows(mysql_query("SELECT * FROM ".$pre."stats_archive WHERE name = 'keyword' AND data = '".urldecode($kod[0])."'")) == 0) {
	mysql_query("INSERT INTO ".$pre."stats_archive VALUES (null, 'keyword', '".urldecode($kod[0])."', '', '', '', '1', '', '')");
	} else {
	mysql_query("UPDATE ".$pre."stats_archive SET views=views+1 WHERE name = 'keyword' AND data = '".urldecode($kod[0])."'");
	}
	}
	}

	if ($info->OS." ".$info->OS_Version != " " && $ptype == "frontend") {
	if (mysql_num_rows(mysql_query("SELECT * FROM ".$pre."stats_archive WHERE name = 'operating_system' AND data = '".$info->OS." ".$info->OS_Version."'")) == 0) {
	mysql_query("INSERT INTO ".$pre."stats_archive VALUES (null, 'operating_system', '".$info->OS." ".$info->OS_Version."', '', '', '', '1', '', '')");
	} else {
	mysql_query("UPDATE ".$pre."stats_archive SET views=views+1 WHERE name = 'operating_system' AND data = '".$info->OS." ".$info->OS_Version."'");
	}
	}

	if ($info->Browser." ".$info->Browser_Version != " " && $ptype == "frontend") {
	if (mysql_num_rows(mysql_query("SELECT * FROM ".$pre."stats_archive WHERE name = 'browser' AND data = '".$info->Browser." ".$info->Browser_Version."'")) == 0) {
	mysql_query("INSERT INTO ".$pre."stats_archive VALUES (null, 'browser', '".$info->Browser." ".$info->Browser_Version."', '', '', '', '1', '', '')");
	} else {
	mysql_query("UPDATE ".$pre."stats_archive SET views=views+1 WHERE name = 'browser' AND data = '".$info->Browser." ".$info->Browser_Version."'");
	}
	}

	if ($_POST['comment'] && $_POST['aid']) {
	if (strtolower($setting["captcha_comments"]) == "yes" && md5(strtoupper($_POST['captcha'])) != $_SESSION['captcha']) {
    echo "<center><i>Sorry but you did not enter the correct captcha code. Please try again</i></center>";
    } else {
	$_POST['comment'] = str_replace("quote]<br><br>","quote]<br>",$_POST['comment']);
	$_POST['comment'] = str_replace("<br>","\n",$_POST['comment']);
	mysql_query("INSERT INTO ".$pre."comments VALUES (null, '".mysql_real_escape_string(addslashes($_POST['aid']))."', '".mysql_real_escape_string(preg_replace("/\n/", "\n", badwords(addslashes(check($_POST['comment'])))))."', '".mysql_real_escape_string(stripslashes($_COOKIE[adaptcms_username]))."', '".mysql_real_escape_string(addslashes($_POST['email']))."', '".mysql_real_escape_string(addslashes($_POST['website']))."', '".$_SERVER['REMOTE_ADDR']."', '".time()."')");
	header("location: ".$_SERVER['HTTP_REFERER']);
	}
	}

	if ($_POST['rating']) {
	$rex = explode("|", $_POST['cur_rating']);
    $rate1 = $rex[0] + 1;
    $rate2 = $rex[1] + $_POST['rating'];
	mysql_query("UPDATE ".$pre."articles SET rate = '".$rate1."|".$rate2."' WHERE id = '".$_POST['aid']."'");
	}

	if (strtolower($setting["wysiwyg"]) == "yes") {
	if ($_GET['view'] == "add_article" or $_GET['view'] == "edit_article" or $_GET['view'] == "submit_article2" or $_GET['view'] == "add_page" or $_GET['view'] == "edit_pages") {
	echo wysiwyg();
	}
	}
    if ($setting["banned_ips"]) {
	$ips = explode(",", $setting["banned_ips"]);
	while (list(, $pi) = each ($ips)) {
	if ($_SERVER['REMOTE_ADDR'] == $pi) {
	echo "Sorry but your IP Address <b>".$_SERVER['REMOTE_ADDR']."</b>, has been banned from this website.";
	die;
	}
	}
	}
	if ($setting["banned_users"]) {
	$userss = explode(",", $setting["banned_users"]);
	while (list(, $mi) = each ($userss)) {
	if ($_COOKIE[adaptcms_username] == $mi) {
	echo "Sorry but your user account <b>".$_COOKIE[adaptcms_username]."</b>, has been banned from this website.";
	die;
	}
	}
	}
	}
?>