<?php


	// Meditate version 1.3
	// Copyright 2008-2012 Arlo Leach (http://www.arlomedia.com)
	// This file may be used, modified, or redistributed under the terms
	// of the open-source agreement contained in docs/license.txt
	
	
	$ns = ""; // if not using integration mode, use this line
	//namespace editor; // use this line in integration mode; integration requires PHP 5.3 or higher
	//$ns = "editor\\"; // use this line in integration mode; we'll prepend this to our constant definitions (rather than using the CONST keyword) to retain compatibility with PHP versions lower than 5.3

	eval(include_namespace("config/constants.php"));
	
	if (SHOW_ERRORS) {
		ini_set("error_reporting", E_ALL);
		ini_set("display_errors", 1);
	}
	
	// set some variables to use as the basis for page links in the system
	// for popups and AJAX requests, we'll use the editor index; for full pages, we'll use the wrapper
	$editor_index = EDITOR_INDEX;
	$wrapper_page = (INTEGRATION_MODE) ? WRAPPER_PAGE : EDITOR_INDEX ;
	$login_page = (INTEGRATION_MODE) ? LOGIN_PAGE : EDITOR_INDEX."?page=login" ;

	eval(include_namespace("shared/functions.php"));
	eval(include_namespace("shared/db.php"));
	if (!THIS_PAGE_WRAPPED) {
		// if this page is embedded into the parent page, we'll have to get session info from the parent system
		eval(include_namespace("shared/sessions.php"));
	}
	
	unregister_globals();
	
	$user = initialize("user", "session", "integer");
	$username = initialize("username", "session", "text");
	$timezone = initialize("timezone", "session", "text");
	$role = initialize("role", "session", "integer");
	$access = initialize("access", "session", "text");
	
	// if a page is not specified, load the home page
	$page = initialize("page", "get", "filename");
	if (!$page) { $page = "home"; }

	// unless we're on one of the few pages that doesn't require it, make sure the user is logged in
	$open_pages = array("login", "login_submit", "logout_submit", "integration_login", "install", "cron_publish");
	if (!in_array($page, $open_pages)) {
		require_user($user);
	}
	
	// if we're logged in as a demo user, don't save any changes
	$demo = ($username == "demo");
	
	// load the page
	eval(strip_template(include_namespace("pages/$page.php")));
	
	//
	// define some functions here instead of functions.php so we can use them to include functions.php
	//
	
	function include_namespace($file) {
		// instead of including the file, which puts it back into the global namespace, load the file, add the current namespace, and then evaluate it
		// this is the equivalent of putting the file into the current namespace
		$file_contents = file_get_contents($file);
		if ((phpversion() >= "5.3.0")&&(__NAMESPACE__)) { // defined("__NAMESPACE__") always returns false
			$file_contents = "<?php namespace ".__NAMESPACE__."; ?>\n".$file_contents;
		}
		//extract($GLOBALS);
		//eval("/?/>".$file_contents); // we can't do the eval here because then all the variables we create will be stuck in this function's scope
		return "?>".$file_contents;
	}

	function eval_namespace($code) {
		// add the current namespace to the code and then evaluate it
		// this is the equivalent of evaluating the code in the current namespace
		if ((phpversion() >= "5.3.0")&&(__NAMESPACE__)) { // defined("__NAMESPACE__") always returns false
			$code = "namespace ".__NAMESPACE__.";\n".$code;
		}
		return $code;
	}

?>
