<?php
/*-------------------------------------------------------+
| PHP-Fusion Content Management System
| Copyright (C) 2002 - 2013 Nick Jones
| http://www.php-fusion.co.uk/
+--------------------------------------------------------+
| Filename: cookie_control_admin.php
| Version: 2.00
| Author: PHP-Fusion Mods UK
| Developers: Craig
| Sites: http://www.phpfusionmods.co.uk
+--------------------------------------------------------+
| This program is released as free software under the
| Affero GPL license. You can redistribute it and/or
| modify it under the terms of this license which you
| can read by viewing the included agpl.txt or online
| at www.gnu.org/licenses/agpl.html. Removal of this
| copyright header is strictly prohibited without
| written permission from the original author(s).
+--------------------------------------------------------*/
require_once "../../maincore.php";

if (!checkrights("CCP") || !defined("iAUTH") || !isset($_GET['aid']) || $_GET['aid'] != iAUTH) { redirect("../index.php"); }

require_once THEMES."templates/admin_header.php";
require_once INCLUDES."bbcode_include.php";

if (file_exists(INFUSIONS."cookie_control/locale/".$settings['locale'].".php")) {
		include INFUSIONS."cookie_control/locale/".$settings['locale'].".php";
	} else {
		include INFUSIONS."cookie_control/locale/English.php";
	}
	
	include INFUSIONS."cookie_control/infusion_db.php";
	
add_to_title($locale['global_200'].$locale['ecdp_001'].$locale['global_200'].$locale['ecdp_070']);

global $settings;

	
if (isset($_GET['status']) && !isset($message)) {
	if ($_GET['status'] == "del") {
		$message = "<strong>".$locale['ecdp_004']."</strong>";
	} elseif ($_GET['status'] == "go") {
		$message = "<strong>".$locale['ecdp_031']."</strong>";
	} elseif ($_GET['status'] == "edit") {
		$message = "<strong>".$locale['ecdp_032']."</strong>";
	} elseif ($_GET['status'] == "editd") {
		$message = "<strong>".$locale['ecdp_032']."</strong>";
	}
	if ($message) {	echo "<div id='close-message'><div class='admin-message'>".$message."</div></div>\n"; }
}

$cookie_cookie ="";
$cookie_usage ="";
$cookie_desc ="";

if ((isset($_GET['action']) && $_GET['action'] == "delete") && (isset($_GET['cookie_id']) && isnum($_GET['cookie_id']))) {
	$result = dbquery("DELETE FROM ".DB_COOKIE_CONTROL." WHERE !cookie_default AND cookie_id='".$_GET['cookie_id']."'");
	redirect(FUSION_SELF.$aidlink."&status=del");
    } else {
	if (isset($_POST['go'])) {
		$cookie_cookie = stripinput($_POST['cookie_cookie']);
		$cookie_usage = isNum($_POST['cookie_usage']) ? $_POST['cookie_usage'] : "0";
		$cookie_desc = stripinput($_POST['cookie_desc']);
		}
		
		if ($cookie_cookie || $cookie_desc) {
		if ((isset($_GET['action']) && $_GET['action'] == "editd") && (isset($_GET['cookie_id']) && isnum($_GET['cookie_id']))) {
				$result = dbquery("UPDATE ".DB_COOKIE_CONTROL." SET cookie_desc='$cookie_desc' WHERE cookie_id='".$_GET['cookie_id']."'");
				redirect(FUSION_SELF.$aidlink."&status=editd");
			}elseif ((isset($_GET['action']) && $_GET['action'] == "edit") && (isset($_GET['cookie_id']) && isnum($_GET['cookie_id']))) {
				$result = dbquery("UPDATE ".DB_COOKIE_CONTROL." SET cookie_cookie='$cookie_cookie', cookie_usage='$cookie_usage', cookie_desc='$cookie_desc' WHERE cookie_id='".$_GET['cookie_id']."'");
				redirect(FUSION_SELF.$aidlink."&status=edit");
			} else {
				$result = dbquery("INSERT INTO ".DB_COOKIE_CONTROL." (cookie_cookie,  cookie_usage, cookie_desc) VALUES ('$cookie_cookie', '$cookie_usage', '$cookie_desc')");
				redirect(FUSION_SELF.$aidlink."&status=go");
			}
				redirect(FUSION_SELF.$aidlink);
		}
	
	}
	
	
if ((isset($_GET['action']) && $_GET['action'] == "edit") || (isset($_GET['action']) && $_GET['action'] == "editd") && (isset($_GET['cookie_id']) && isnum($_GET['cookie_id']))) {
	
		$result = dbquery("SELECT * FROM ".DB_COOKIE_CONTROL." WHERE cookie_id='".$_GET['cookie_id']."'");
		if (dbrows($result)) {
			$data = dbarray($result);
			$cookie_cookie = $data['cookie_cookie'];
			$cookie_usage = $data['cookie_usage'];
			$cookie_desc = $data['cookie_desc'];
			$form_title = $locale['ecdp_033'];
			if (isset($_GET['action']) && $_GET['action'] == "editd") {
			$form_action = FUSION_SELF.$aidlink."&amp;action=editd&amp;cookie_id=".$data['cookie_id'];
			}else{
			$form_action = FUSION_SELF.$aidlink."&amp;action=edit&amp;cookie_id=".$data['cookie_id'];
			}
		} else {
			redirect(FUSION_SELF.$aidlink);
		}
		} else {
			$cookie_cookie = "";
			$cookie_usage = "";
			$cookie_desc = "";
			$form_title = $locale['ecdp_033'];
			$form_action = FUSION_SELF.$aidlink;
		}
	
	if (isset($_GET['action']) && $_GET['action'] == "edit") {
	opentable($locale['ecdp_078']);
	}else{
	opentable((!isset($_GET['action']) || $_GET['action'] !== "editd" ? $form_title : $locale['ecdp_079']));
	}
	
		$folder_img = INFUSIONS."cookie_control/images/folder_open.png";
		$subfolder_img = INFUSIONS."cookie_control/images/sub.png";
		echo "<table width='100%' border='0' cellpadding='2' cellspacing='0'><tr>";
		echo "<td width='1'><img src='".$folder_img."' alt=''></td>";
		echo "<td style='padding-left:3px;'><a href='".INFUSIONS."cookie_control/cookie_control_admin.php".$aidlink."' class='side'>".$locale['ecdp_001']."</a>";
		echo"</td>";
		echo "</tr></table>";
		echo "<table width='100%' border='0' cellpadding='0' cellspacing='0' style='margin-bottom: 10px;'><tr>";
		echo "<td style='width:31px;' align='right'><img src='".$subfolder_img."' alt=''></td>";
		echo "<td style='padding-left:3px;'><span style='font-weight: bold;'>".$locale['ecdp_070']."</span>";
		echo"</td></tr></table>";
	
		echo "<form name='cookie_control_form' method='post' action='$form_action'>\n";
		echo "<table cellpadding='0' cellspacing='0' width='95%'  class='tbl-border center' >\n<tr>\n";

		if (!isset($_GET['action']) || $_GET['action'] !== "editd") {
		echo "<tr><td class='tbl' width='50%'>".$locale['ecdp_005'].":<br /><span style='font-size: 9px;'>(".$locale['ecdp_080'].")</span></td>\n";
		echo "<td class='tbl'><input type='text' name='cookie_cookie' value='".$cookie_cookie."' class='textbox' style='width:150px' /></td>\n";
		echo "</tr>\n<tr>\n";
		}elseif (isset($_GET['action']) && $_GET['action'] == "editd"){

		echo "<tr><td class='tbl' width='50%'>".$locale['ecdp_005'].":<br /><span style='font-size: 9px;'>(".$locale['ecdp_081'].")</span></td>\n";
		echo "<td class='tbl'>";
		if ($data['cookie_default'] == 1) { echo "<span style='color: #157707;'>".COOKIE_PREFIX."lastvisit</span>";}
		elseif ($data['cookie_default'] == 2) { echo "<span style='color: #157707;'>".COOKIE_PREFIX."user</span>"; }
		elseif ($data['cookie_default'] == 3) { echo "<span style='color: #157707;'>".COOKIE_PREFIX."admin</span>"; }
		elseif ($data['cookie_default'] == 4) { echo "<span style='color: #157707;'>fusion_box"; }
		elseif ($data['cookie_default'] == 5) { echo "<span style='color: #157707;'>".COOKIE_PREFIX."visited</span>"; }
		elseif ($data['cookie_default'] == 6) { echo "<span style='font-weight:bold; color: #157707;'>".COOKIE_PREFIX."cookie_control</span>"; }
		echo"</td>\n";
		echo "</tr>\n<tr>\n";
		}
		
		if (!isset($_GET['action']) || $_GET['action'] !== "editd") {
		echo "<td class='tbl'>".$locale['ecdp_006'].":</td>\n";
		echo "<td  align='left' class='tbl'><select name='cookie_usage' class='textbox'>\n";
		echo "<option value='0'".($cookie_usage == "0" ? " selected='selected'" : "").">".$locale['ecdp_007']."</option>\n";
		echo "<option value='1'".($cookie_usage == "1" ? " selected='selected'" : "").">".$locale['ecdp_008']."</option>\n";
		echo "<option value='2'".($cookie_usage == "2" ? " selected='selected'" : "").">".$locale['ecdp_009']."</option>\n";
		echo "<option value='3'".($cookie_usage == "3" ? " selected='selected'" : "").">".$locale['ecdp_010']."</option>\n";
		echo "<option value='4'".($cookie_usage == "4" ? " selected='selected'" : "").">".$locale['ecdp_011']."</option>\n";
		echo "<option value='5'".($cookie_usage == "5" ? " selected='selected'" : "").">".$locale['ecdp_012']."</option>\n";
		echo "<option value='6'".($cookie_usage == "6" ? " selected='selected'" : "").">".$locale['ecdp_013']."</option>\n";
		echo "<option value='7'".($cookie_usage == "7" ? " selected='selected'" : "").">".$locale['ecdp_014']."</option>\n";
		echo "<option value='8'".($cookie_usage == "8" ? " selected='selected'" : "").">".$locale['ecdp_062']."</option>\n";
		echo "</select></td></tr><tr>\n";
		}
		
		echo "<td style='vertical-align: top;' class='tbl'>".$locale['ecdp_015'].":</td>\n";
		echo "<td class='tbl'><textarea name='cookie_desc' cols='60' rows='5'  class='textbox' style='width:400px;'>$cookie_desc</textarea><br />\n";
		echo display_bbcodes("400px;", "cookie_desc", "cookie_control_form", "color|b|i|u|small|url")."\n";
		echo "</td>\n</tr>\n<tr>\n";
		echo "<td align='center' colspan='2' class='tbl'><br />\n";
		if ((!isset($_GET['action']) || $_GET['action'] !== "editd") && (!isset($_GET['action']) || $_GET['action'] !== "edit")){
		echo "<input type='submit' name='go' value='".$locale['ecdp_016b']."' class='button' /></td>\n";
		}else{
		echo "<input type='submit' name='go' value='".$locale['ecdp_064']."' class='button' /></td>\n";
		}
		echo "</tr>\n</table>\n</form>\n";
		closetable();

		$formb_title = $locale['ecdp_026'];
	
		opentable($locale['ecdp_026']);

		$i = 0;
		$row_color = ($i % 2 == 0 ? "tbl1" : "tbl2");

		echo "<table cellpadding='0' cellspacing='1' width='80%' class='tbl-border center'>\n<tr>\n";
		echo "<td class='tbl2'><strong>".$locale['ecdp_063']."</strong></td>\n";
		echo "<td class='tbl2'><strong>".$locale['ecdp_015']."</strong></td>\n";
		echo "<td class='tbl2' width='20%'><strong>".$locale['ecdp_006']."</strong></td>\n";
		echo "<td align='center'  class='tbl2' style='white-space:nowrap'><strong>".$locale['ecdp_017']."</strong></td>\n";
		echo "</tr>\n";

		$rows = dbcount("(cookie_id)", DB_COOKIE_CONTROL);

		if (!isset($_GET['rowstart']) || !isnum($_GET['rowstart'])) { $_GET['rowstart'] = 0; }

		if ($rows != 0) {

		$result = dbquery("SELECT * FROM ".DB_COOKIE_CONTROL." ORDER BY cookie_id DESC LIMIT ".$_GET['rowstart'].",20");

		while ($data = dbarray($result)) {
		$desc = nl2br(parseubb($data['cookie_desc'], "color|b|i|u|small|url"));
		echo "<tr>\n";
		echo "<td class='$row_color'><span style='color: #ff0000;'>".$data['cookie_cookie']."</span>";
		if ($data['cookie_default'] == 1) { echo "<span style='color: #157707;'>".COOKIE_PREFIX."lastvisit</span>";}
		elseif ($data['cookie_default'] == 2) { echo "<span style='color: #157707;'>".COOKIE_PREFIX."user</span>"; }
		elseif ($data['cookie_default'] == 3) { echo "<span style='color: #157707;'>".COOKIE_PREFIX."admin</span>"; }
		elseif ($data['cookie_default'] == 4) { echo "<span style='color: #157707;'>fusion_box"; }
		elseif ($data['cookie_default'] == 5) { echo "<span style='color: #157707;'>".COOKIE_PREFIX."visited</span>"; }
		elseif ($data['cookie_default'] == 6) { echo "<span style='font-weight:bold; color: #157707;'>".COOKIE_PREFIX."cookie_control</span>"; }
		echo"</td>";
		echo "<td class='$row_color'>".$desc."</td>\n";
		echo "<td class='$row_color'>";
		if ($data['cookie_usage'] == 1) { echo $locale['ecdp_008'];}
		elseif ($data['cookie_usage'] == 2) { echo $locale['ecdp_009']; }
		elseif ($data['cookie_usage'] == 3) { echo $locale['ecdp_010']; }
		elseif ($data['cookie_usage'] == 4) { echo $locale['ecdp_011']; }
		elseif ($data['cookie_usage'] == 5) { echo $locale['ecdp_012']; }
		elseif ($data['cookie_usage'] == 6) { echo $locale['ecdp_013']; }
		elseif ($data['cookie_usage'] == 7) { echo $locale['ecdp_014']; }
		elseif ($data['cookie_usage'] == 8) { echo $locale['ecdp_062']; }
		else{ echo $locale['ecdp_007']; }
		echo"</td>\n";
		echo "<td  class='$row_color' style='text-align: center; white-space:nowrap'>".($data['cookie_default'] == "1" || $data['cookie_default'] == "2" || $data['cookie_default'] == "3" || $data['cookie_default'] == "4" || $data['cookie_default'] == "5" || $data['cookie_default'] == "6" ? "<a href='".FUSION_SELF.$aidlink."&amp;action=editd&amp;cookie_id=".$data['cookie_id']."'>".$locale['ecdp_017b']."</a>" : "<a href='".FUSION_SELF.$aidlink."&amp;action=edit&amp;cookie_id=".$data['cookie_id']."'>".$locale['ecdp_017b']."</a>")." \n";
		echo ($data['cookie_default'] == "1" || $data['cookie_default'] == "2" || $data['cookie_default'] == "3" || $data['cookie_default'] == "4" || $data['cookie_default'] == "5" || $data['cookie_default'] == "6" ? "&dash; <span style='text-decoration: line-through;'>".$locale['ecdp_017c']."</span>" : " &dash; <a href='".FUSION_SELF.$aidlink."&amp;action=delete&amp;cookie_id=".$data['cookie_id']."'>".$locale['ecdp_017c']."</a>");
		echo "</td>\n";
		echo "</tr>\n";
		}
		echo "</table>\n";
		} else {
		echo "</table>\n";
		}
	
	if (($rows) > 20) { echo "<div align='center' style='margin-top:5px;'>\n".makepagenav($_GET['rowstart'],20,$rows,3,FUSION_SELF.$aidlink."&amp;")."\n</div>\n"; }

	 // Please do not remove copyright info
 include INFUSIONS."cookie_control/includes/copyright_func.php";
 echo showccocopyright();
// End copyright info
	
	closetable();
	
	add_to_footer("<script type='text/javascript'>
function ValidateForm(frm) {
	if(frm.cookie_cookie.value=='') {
		alert('".$locale['ecdp_065']."');
		return false;
	}
	if(frm.cookie_desc.value=='') {
		alert('".$locale['ecdp_065']."');
		return false;
	}
}

</script>");
	
require_once THEMES."templates/footer.php";
?>