<?php

	/*
	*	Gekko - Open Source Web Development Framework
	*	------------------------------------------------------------------------
	*	Copyright (C) 2004-2006, J. Carlos Nieto <xiam@users.sourceforge.net>
	*	This program is Free Software.
	*
	*	@package	Gekko
	*	@license	http://www.gnu.org/copyleft/gpl.html GNU/GPL License 2.0
	*	@author		J. Carlos Nieto <xiam@users.sourceforge.net>
	*	@link		http://www.gekkoware.org
	*/

	define ("IN-GEKKO", true);

	require_once "../../conf.php";

	LoadLib (
		"conf"
	);

	require_once GEKKO_SOURCE_DIR."lib/auth.inc.php";

	varImport("action", "id", "return");

	if (is_admin()) {
		switch ($action) {
			case "delete_key":
				execWithEach($id, "conf::delKeyById");

				slog("conf: deleted key(s) '".(is_array($id) ? implode(',', $id) : $id)."'");
			break;
			case "quickedit":

				// I'm using a keylist because when a checkbox is unchecked it is prevented
				// from being sent, therefore we cannot know if this checkbox was active and now is
				// required to be inactive.

				varImport("key", "keylist");

				varRequire("keylist");

				$keys = explode(",", $keylist);

				foreach ($keys as $id) {
					$id = trim($id);

					$value = isset($key[$id]) ? $key[$id] : false;

					$q = $db->query("SELECT keytype
					FROM ".db_table("conf")." WHERE id='{$id}'");

					if ($db->numRows($q)) {
						$row = $db->fetchRow($q);
						$value = conf::fixValue($value, $row["keytype"]);
						$db->query("UPDATE ".db_table("conf")." SET
							keyvalue='{$value}'
						WHERE id = '{$id}' AND keyvalue != '{$value}'", __FILE__, __LINE__);
					}

					slog("conf: modified key id = '$id'");
				}
			break;
			case "create_key":

				varImport("module", "keytype", "keyname", "keyvalue", "locked");

				varRequire("module", "keyname");

				if (conf::checkKeyName($keyname))
					formInputError("{E_INVALID_CHARSET}", "focus: keyname");

				$keyvalue = conf::fixValue($keyvalue, $keytype);

				$q = $db->query ("SELECT id FROM ".db_table("conf")."
				WHERE keyname = '{$keyname}' AND module = '{$module}'",
				__FILE__, __LINE__);

				if (!$db->numRows($q)) {

					$db->query("
						INSERT INTO ".db_table("conf")." (
							id,
							module,
							keyname,
							keyvalue,
							keytype,
							date_created,
							locked
						) VALUES (
							".$db->findFreeId("conf").",
							'{$module}',
							'{$keyname}',
							'{$keyvalue}',
							'{$keytype}',
							CURRENT_TIMESTAMP(),
							'{$locked}'
						)
					", __FILE__, __LINE__);

					slog("conf: new key '$module/$keyname'");

				} else {
					// modifying key
					$row = $db->fetchRow($q);

					$db->query ("
						UPDATE ".db_table("conf")." SET
							module = '{$module}',
							keyname = '{$keyname}',
							keyvalue = '{$keyvalue}',
							keytype = '{$keytype}',
							locked = '{$locked}'
						WHERE id = '{$row["id"]}'
					", __FILE__, __LINE__);

					slog("conf: modified key '$module/$keyname'");
				}
			break;
			case "edit_key":

				varImport("id", "module", "keytype", "keyname", "keyvalue", "locked");

				varRequire("id", "module", "keyname");

				// checking for duplicated key name
				$q = $db->query ("SELECT id FROM ".db_table("conf")."
				WHERE keyname = '{$keyname}' AND module = '{$module}' AND id != '$id'",
				__FILE__, __LINE__);

				if (!$db->numRows($q)) {

					$db->query ("
						UPDATE ".db_table("conf")." SET
							module = '{$module}',
							keyname = '{$keyname}',
							keyvalue = '".conf::fixValue($keyvalue, $keytype)."',
							keytype = '$keytype',
							locked = '{$locked}'
						WHERE id = '{$id}'
					", __FILE__, __LINE__);

					slog("conf: modified key '$module/$keyname'");

				} else {
					formInputError("{E_DUPLICATED_ENTRY}", "focus: keyname");
				}
			break;
		}
	}

	redirect($return);

	db_exit();
?>