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

	/*
		Those are core functions supposed for being used in every
		script.
	*/

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

	/*
		WARNING: This is a RELATIVE is_admin() function, will return true
		when user owns administrator privileges for the main requested module.
		Use with care.
	*/
	function is_admin($absolute = false) {
		return gauth((defined("ACTION_MODULE") && !$absolute) ? ACTION_MODULE : "admin");
	}

	// returns a error message and instructions.
	// useful for ajax requests, please read core.js:gekkoAjaxFormSubmit();
	function formInputError($message, $instruction = null) {
		abort(($instruction ? "$instruction\n" : "").$message);
	}
	function varImport() {
		$args = func_get_args();

		foreach ($args as $arg) {

			unset($match);

			// default value passed as argument "variable|default"
			if (preg_match("/^(.*?)\|(.*?)$/", $arg, $match))
				$arg = $match[1];

			$GLOBALS[$arg] = null;

			if (isset($_POST[$arg])) {
				$GLOBALS[$arg] = $_POST[$arg];
				continue;
			}

			if (isset($_GET[$arg])) {
				$GLOBALS[$arg] = $_GET[$arg];
				continue;
			}

			// default value
			if (!$GLOBALS[$arg] && isset($match[2]))
				$GLOBALS[$arg] = $match[2];

		}
	}
	function execWithEach(&$var, $exec) {
		if (is_array($var)) {
			foreach ($var as $key => $tmp) {
				execWithEach($var[$key], $exec);
			}
		} else {
			eval("$exec(\$var);");
		}
	}
	function varRequire() {
		$args = func_get_args();
		foreach ($args as $arg) {
			if (!isset($GLOBALS[$arg]) || !$GLOBALS[$arg])
				abort(_L('E_INSUFFICIENT_DATA'));
		}
	}
	function varGetValue($index) {
		$val = null;
		if (isset($_GET[$index]))
			$val = $_GET[$index];
		if (isset($_POST[$index]))
			$val = $_POST[$index];
		return $val;
	}
?>