<?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
	*/

	if (!defined("IN-GEKKO")) die("Get a life!");

	// URL Prototype
	Extract (
		URLGetArgs("null/base=alpha")
	);

	// default conf. variables
	$strict_permissions = conf::getkey('admin', 'strict_panel_permissions', 0, 'b');

	// this will check for administrative privileges
	if (gauth("#")) {
		if ($base) {
			if ($mc->exists($base, false)) {

				blocks::hideAll();

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

				$row = $db->fetchRow();

				$is_admin = gauth($row["auth_manage"]);

				$modinc = GEKKO_SOURCE_DIR."modules/$base/admin.inc.php";

				$stop = false;
				if (file_exists($modinc)) {

					// default toolbar
					$tpl = new contentBlock();
					$tpl->load("admin/_admin.toolbar.inc.tpl");
     				$admin_toolbar = $tpl->make();

					// this user is not an admin for this module
					if (!$is_admin) {
						if ($strict_permissions) {
							// strict permissions
							$mc->save(mkMessageBox("error", _l("E_ACCESS_DENIED")));
							$stop = true;
						} else {
							// letting him to know about his limited privileges
							$mc->save(mkMessageBox("info", _l("E_READ_ONLY_ACCESS")));
						}
					}
					if (!$stop) {
						// loading module language files
						$Lang->loadFromModule($base);
						// attaching module title to main page's title
						pageSetSubtitle(_l("L_MODULE_".strtoupper($base)));
						// including admin module
						include $modinc;
					}

					$mcBuff = $admin_toolbar.$mcBuff;
				} else {
					$mcBuff = mkMessageBox("error", _l("E_ACCESS_DENIED"));
				}
			}
		} else {

			pageSetSubtitle(_L("L_ADMIN_PANEL"));

			$mods = Array();

			$db->query("SELECT module, auth_manage
			FROM ".db_table("module")."
			WHERE hidden != '1'", __FILE__, __LINE__);

			// installed modules
			while ($row = $db->fetchRow()) {
				if (!$strict_permissions || gauth($row['auth_manage']))
					$mods[$row["module"]] = true;
			}

			$Panel = new Panel(_l("L_ADMIN_PANEL"), "admin.png");

			$moddir = listdir(GEKKO_SOURCE_DIR."modules");

			natsort($moddir);

			foreach  ($moddir as $module) {
				$modpath = GEKKO_SOURCE_DIR."modules/$module";
				if (file_exists($modpath."/admin.inc.php") && isset($mods[$module])) {
					// loading icon (if exists) or creating a generic one
					if (file_exists($modpath."/admin.panel.php")) {
						include $modpath."/admin.panel.php";
					} else {
						// generic icon
						$Panel->AddIcon (
							$Panel->Icon("$module.png", "index.php/module=admin/base=$module", _l("L_MODULE_".strtoupper($module))),
							_l("L_ADMIN_TASKS")
						);
					}
				}
			}

			$mcBuff .= $Panel->make();
		}
	} else {
		$mcBuff = mkMessageBox("error", _l("E_ACCESS_DENIED"));
	}

	if (file_exists(GEKKO_SOURCE_DIR."install") || file_exists(GEKKO_SOURCE_DIR."install.php")) {
		if (!GEKKO_ENABLE_DEBUG) {
			// error for non-developers
			$mcBuff = mkMessageBox("warning", _l("L_PLEASE_DELETE_INSTALLER")).$mcBuff;
		}
	}

?>