<?php

	/*
	*	Gekko - Open Source Web Development Framework
	*	------------------------------------------------------------------------
	*	Copyright (C) 2004-2005, 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.sf.net>
	*	@version	CVS: $Id:$
	*	@link		http://gekkoware.sourceforge.net
	*/

	define ("IN-GEKKO", true);

	require_once "../../conf.php";
	require_once GEKKO_ROOT."lib/core.lib.php";
	require_once GEKKO_ROOT."modules/conf/main.lib.php";

	db_init();

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

	if (!is_admin()) abort(accessDenied());

	gpGet("action", 1);
	gpGet("id", 1);
	gpGet("return", 1);

	switch ($action) {
		case "manage":
			gpGlobalize("opt");

			requireVars($id, $opt);

			$db->query("SELECT Module FROM ".db_table("module")." WHERE ID = '{$id}'",
			__FILE__, __LINE__);

			if ($db->num_rows()) {
				$row = $db->fetch_row();

				modCore::modInstall($row["Module"], $opt);

				$return = "../../index.php/admin/modules";

				slog("modules: manage action performed ($opt) on '{$row["Module"]}'", "actions", 1);
			}
		break;
		case "set_default":
			gpGlobalize("default");

			requireVars($default);

			$db->query("SELECT Module FROM ".db_table("module")." WHERE ID = '$default'");

			if (!$db->num_rows()) trigger_error("Unknown module.", E_USER_ERROR);

			$row = $db->fetch_row();

			Conf::setKey("modules", "default", $row["Module"], "string");
		break;
		case "modify":
			gpGlobalize("id, auth_exec, auth_manage, hidden, status");

			requireVars($id);

			$db->query ("
				UPDATE ".db_table("module")." SET
					auth_Exec = '$auth_exec',
					auth_Manage = '$auth_manage',
					Hidden = '".(isset($hidden) ? $hidden : 0)."',
					Status = '".(isset($status) ? $status : 0)."',
					Modified = '".time()."'
				WHERE ID = '$id'",
				__FILE__, __LINE__
			);

			slog("modules: modified id='$id'", "actions", 1);
		break;
		case "install":
			gpGlobalize("module");

			requireVars($module);

			/* installing all requested modules */

			foreach ($module as $to_install) {

				/* checking if this module is already installed */
				$db->query("SELECT ID FROM ".db_table("module")." WHERE Module = '$to_install'",__FILE__, __LINE__);

				if (modCore::check($to_install, false) && !$db->num_rows()) {

					modCore::modInstall($to_install, "I");

					slog("modules: installed '$to_install'", "actions", 1);
				}
			}
		break;
	}

	redirect($return ? $return : $_SERVER["HTTP_REFERER"]);

	db_exit();
?>