<?php
/*
EasyGUI XP is free software released under the terms of the following BSD license.

Copyright (c) 2005,2006 Gaetan Lauff (glauff@plansoft.de)
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

- Redistributions of source code must retain the above copyright notice, 
this list of conditions and the following disclaimer. 

- Redistributions in binary form must reproduce the above copyright notice, 
this list of conditions and the following disclaimer in the documentation 
and/or other materials provided with the distribution.

- Neither the name of the developer nor the names of its contributors may 
be used to endorse or promote products derived from this software without 
specific prior written permission. 

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

/**
 * EasyGUI XP
 *
 * EasyGUI XP: Enhanced Application System for Generic User Interfaces
 *
 * @author Gaetan Lauff <glauff@plansoft.de>
 * @version $Id: index.php 44 2006-02-28 13:40:01Z glauff $
 * @package System
 *
 * @todo styles in workspace file
 * @todo handle relative paths in workspace files.
 * @todo extend javascript library for opera browsers
 * @todo implement the access key for form elements
 * @todo implement the maskEdit
 * @todo
 */
$timer = false;
$sep = ( strstr( strtoupper( PHP_OS ) , 'WIN' )  ) ? ';' : ':' ;
$_version = (float) PHP_VERSION;

if ($_version >= 5.0) {
	define('EK_PHPVER',0x5000);
} elseif ($_version > 4.299999) {
	define('EK_PHPVER',0x4300);
} else {
	trigger_error( 'Incompatible PHP Version. Please use PHP Version > 4.3', E_USER_ERROR );
}

@error_reporting(E_ALL); # report all errors
//@ini_set("display_errors", "0"); # do not echo any errors 


@define( 'EK_ROOT' , str_replace('\\', '/', dirname(__FILE__) ) );

@ini_set( 'include_path' , ini_get('include_path') . $sep . EK_ROOT . '/lib' );
@ini_set( 'include_path' , ini_get('include_path') . $sep . EK_ROOT . '/pear' );

require_once('lib/util/class.ek_common.php');
require_once('lib/util/class.ek_domxml.php');

$_EK_SETTINGS = parse_ini_file( EK_ROOT . '/easygui.ini', TRUE);
$_EK_SETTINGS = array_change_key_case($_EK_SETTINGS, CASE_LOWER);

@define('EK_VERSION', $_EK_SETTINGS['version']);
@define('EK_BUILD', 	$_EK_SETTINGS['build']);
@define('EK_REVISION', str_replace('$', '', $_EK_SETTINGS['revision']) );

if (!isset($_WORKSPACE)) {
	$_CONFIG = $_EK_SETTINGS;
	
	if (empty($_SERVER['DOCUMENT_ROOT'])) {
		@define( 'EK_SUBMIT_TARGET', 'easygui.php' );
		@define( 'APPMODE', 'ek_cdrom' );
	}
} else {
	$_CONFIG = new EK_SimpleXML();
	$_CONFIG->setXML( $_WORKSPACE );
	
	$_CATALOG		= $_CONFIG->getElementsByTagName( 'Catalog' );
	
	@define( 'APPNAME', $_CONFIG->getAttribute( $_CATALOG, 'name', 'EasyGUI XP Web Application') );
	
	$_EK_SETTINGS 	= $_CONFIG->getElementsByTagName( 'EasyGUI' );
	
	@define( 'EK_SUBMIT_TARGET', $_CONFIG->getAttribute( $_EK_SETTINGS, 'submit_target', 'index.php' ));
	@define( 'EK_LOG_ERRORS', 	$_CONFIG->getAttribute( $_EK_SETTINGS, 'logging', 	FALSE ));
	@define( 'EK_LOG_REQUEST', $_CONFIG->getAttribute( $_EK_SETTINGS, 'request_log', FALSE ));
	
	if (EK_LOG_ERRORS) {
		@define( 'EK_LOG_FILE', 	APPPATH . '/logs/' . APPNAME . '_error.log');
		@define( 'EK_REPORT_ERRORS', $_CONFIG->getAttribute( $_EK_SETTINGS, 'logmail', FALSE));
	}
		 
	@define( 'EK_DEBUG', 		$_CONFIG->getAttribute( $_EK_SETTINGS, 	'debug', 	FALSE ));
	@define( 'EK_COMPRESSION', 	$_CONFIG->getAttribute( $_EK_SETTINGS,	'compression', TRUE));
	
	$_SETTINGS 		= $_CONFIG->getElementsByTagName( 'Settings' );
	
	@define ( 'CLASSPATH',		$_CONFIG->getAttribute( $_SETTINGS, 	'classpath', APPPATH . '/lib/classes'));
	@define ( 'APPMODE', 		$_CONFIG->getAttribute( $_SETTINGS, 	'mode', 	'ek_webapp'));
	@define ( 'EK_ENCODING', 	$_CONFIG->getAttribute( $_SETTINGS, 	'encoding', 'UTF-8'));
	
	//@define ( 'EK_GET_OVER_POST', $_CONFIG->getAttribute( $_SETTINGS, 'getoverpost', FALSE ) );

	if ( APPMODE == 'ek_webapp' ) {
		@define ( 'EK_INDEX_DLG',	$_CONFIG->getAttribute( $_SETTINGS,	'index_dlg', 'IDD_INDEX'));
		$transformerlib = $_CONFIG->getAttribute( $_SETTINGS, 'transformerlib' );
		$enginelib = $_CONFIG->getAttribute( $_SETTINGS, 'enginelib' );
	
		if ( $transformerlib ) {
			@define ( 'EK_TRANSFORMER_COMPLIB', $transformerlib );
		}
	
		if ( $enginelib ) {
			@define ( 'EK_ENGINE_COMPLIB', $enginelib );
		}
		
		$_LIBPATH 		= array();
		
		$_LIBRARIES		= $_CONFIG->getElementsByTagName( 'Libraries');
		//$_LIBRARIES		= $_CONFIG->childNodes( $_TAGS, $_CONFIG, 'Libraries' );
		if (!empty( $_LIBRARIES ) && array_key_exists( 'childNodes', $_LIBRARIES ) ) {
			foreach( $_LIBRARIES['childNodes'] as $library ) {
				$path = $_CONFIG->getAttribute( $library, 'path');
				$_LIBPATH[] = APPPATH . '/lib/' . basename( $path );
			}
		}
	}
	
	if (isset( $_LIBPATH )) {
		$_LIBPATH = $sep . implode(';', $_LIBPATH);
	} else {
		$_LIBPATH = '';
	}
	
	@ini_set('include_path', ini_get('include_path') . $_LIBPATH . $sep . CLASSPATH );
}

if ( isset( $_POST['__eksubmit_values__'] ) || isset( $_GET['__eksubmit_values__'] ) ) {
	$POSTVARS = (isset($_POST['__eksubmit_values__'])) ? $_POST['__eksubmit_values__'] : $_GET['__eksubmit_values__'];
	$POSTVARS = explode( '&', urldecode( $POSTVARS ) );
	
	foreach ( $POSTVARS as $key => $value ) {
		list( $idx, $val ) = split( "=", $value );
		
		if ( preg_match( "/(.*)\[(.*)\]\[(.*)]/", $idx, $index ) ) {
			if ( !isset( $_POST[$index[1]]) ) {
				$_POST[$index[1]] = array();
			}
			
			if ( !isset( $_POST[$index[1]][$index[2]] ) ) {
				$_POST[$index[1]][$index[2]] = array();
			}
			
			$_POST[$index[1]][$index[2]][$index[3]] = $val;
		} elseif ( preg_match( "/(.*)\[(.*)\]/", $idx, $index ) ) {
			if ( !isset( $_POST[$index[1]]) ) {
				$_POST[$index[1]] = array();
			}
			
			$_POST[$index[1]][(int)$index[2]] = $val;
		} else {
		
			if ( !isset( $_POST[$idx] ) ) {
				$_POST[$idx] = $val;
			} else {
				if ( !is_array( $_POST[$idx] ) ) {
					$oldval = $_POST[$idx];
					$_POST[$idx] = array( $oldval );
				}
				
				array_push( $_POST[$idx], $val );
			}
		}
	}
}

include( EK_ROOT . '/inc.config.php');
require_once('lib/error/class.ek_errorhandler.php');

$errorHandler = new EK_Error();
@set_error_handler(array( $errorHandler, 'EK_ErrorHandler'));

$handler = EK_Factory::getHandler( $_CONFIG );

$handler->init();

if ( $timer ) {
	$ekTimer = new EK_Timer();
	$ekTimer->setPercision(4);
	$ekTimer->startTimer();
}

$handler->initView();

if ( $timer ) {
	print $ekTimer->stopTimer();
}

if ( EK_DEBUG ) {
	$errorHandler->getTrace();
}

$handler->output();
?>