<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
// +----------------------------------------------------------------------+
// | File name : application.php                                          |
// | PHP version >= 5.2                                                   |
// +----------------------------------------------------------------------+
// | Author: BINU CHANDRAN.E<binu.chandran@armiasystems.com>              |
// +----------------------------------------------------------------------+
// | Copyrights Armia Systems � 2010                                      |
// | All rights reserved                                                  |
// +----------------------------------------------------------------------+
// | This script may not be distributed, sold, given away for free to     |
// | third party, or used as a part of any internet services such as      |
// | webdesign etc.                                                       |
// +----------------------------------------------------------------------+
ob_start();
session_start();//exit;
require_once('settings.php');
require_once('globals.php');

/**
* Module name
**/
$module			= 'default';
/**
* Controller name
**/
$controllerName = 'index';
/**
* Function name
**/
$method 		= 'index';

/**
* Arguments
**/
$args	 		= '';
//Full url
$port = ($_SERVER["SERVER_PORT"] == "80") ? "" : (":".$_SERVER["SERVER_PORT"]);
$url = PROTOCOL  . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];

define('Current_Url',$url);
if(strpos($url, '?') !== false)
{
	$url = substr($url, 0,strpos($url, '?'));	
}


//Extract uri from full url
$url = str_replace(BASE_URL,'', $url);
$urlVars = explode("/", $url);


//The following code is 
//only for facebook app
//$te = array_shift($urlVars);


if(!empty($urlVars) && $urlVars[0] != '' && !isset($_GET['controller']))
{

	if(!(is_dir('modules/' . $urlVars[0])) && $urlVars[0] != '')
	{
		array_unshift($urlVars, 'default');
	}
	
	$module	= $urlVars[0];
	
	if(isset($urlVars[1]) && $urlVars[1] != '')
	{
		$controllerName = $urlVars[1];
	}
	
	if(isset($urlVars[2]) && $urlVars[2] != '')
	{
		$method = $urlVars[2];
	}
	
	if(isset($urlVars[3]) && $urlVars[3] != '')
	{
		$comma = '';
		for($i = 3; $i <= (count($urlVars) -1); $i++)
		{
			$args .= $comma ."'".$urlVars[$i]."'";
			$comma = ',';
		}
	}
}
else
{
	$module 		= isset($_GET['module']) 	? (trim($_GET['module']) !== '' 	? trim($_GET['module']) 	: 'default'): 'default';
	$controllerName = isset($_GET['controller'])? (trim($_GET['controller']) != '' 	? trim($_GET['controller']) : 'index') 	: 'index';
	$method 		= isset($_GET['method']) 	? (trim($_GET['method']) != ''		? trim($_GET['method']) 	: 'index')	: 'index';
}



/*
code to remove the index controller name from some links like  aboutus,contact us etc
*/
	$controllers = array ("aboutus" ,"contactus","forbusiness","help","howitworks","privacypolicy","termsofuse");
	if (in_array($controllerName, $controllers)) {
	
		$method = $controllerName;
		$controllerName = 'index';
	}
 
if ($controllerName=='')
	$controllerName = 'index';




/**
* Defining Constants
**/
define('MODULE', $module);
define('CONTROLLER', $controllerName);
define('METHOD', $method);
define('ARGS', $args);

/* To load setting values */
if(!defined(THEME))
{ 
    $connection=mysqli_connect(MYSQL_HOST,MYSQL_USERNAME,MYSQL_PASSWORD);
    mysqli_select_db($connection,MYSQL_DB);
    $result=mysqli_query($connection,"SELECT `value` FROM `".MYSQL_TABLE_PREFIX."settings` WHERE `settingfield`='siteStyle'");
    $row=mysqli_fetch_row($result);
    define('THEME', $row[0]);

    $result=mysqli_query($connection,"SELECT `value` FROM `".MYSQL_TABLE_PREFIX."settings` WHERE `settingfield`='banner_link'");
    $row=mysqli_fetch_row($result);
    define('LOGO_URL', $row[0]);

    $result=mysqli_query($connection,"SELECT `value` FROM `".MYSQL_TABLE_PREFIX."settings` WHERE `settingfield`='siteListingMode'");
    $row=mysqli_fetch_row($result);
    define('SITE_MODE', $row[0]);
    $_SESSION['SITELISTINGMODE']=$row[0];

     $result=mysqli_query($connection,"SELECT `value` FROM `".MYSQL_TABLE_PREFIX."settings` WHERE `settingfield`='admin_currency'");
    $row=mysqli_fetch_row($result);
    if($row[0]=='USD'){
        define('CURRENCY', '$');
    }
    else{
        define('CURRENCY', $row[0]);
    }

    $result=mysqli_query($connection,"SELECT `value` FROM `".MYSQL_TABLE_PREFIX."settings` WHERE `settingfield`='operationMode'");
    $row=mysqli_fetch_row($result);
    define('SITE_PAYMENT_MODE', $row[0]);

    $result=mysqli_query($connection,"SELECT `value` FROM `".MYSQL_TABLE_PREFIX."settings` WHERE `settingfield`='listing_mode'");
    $row=mysqli_fetch_row($result);
    define('LISTINGMODE', $row[0]);

    $result=mysqli_query($connection,"SELECT `value` FROM `".MYSQL_TABLE_PREFIX."settings` WHERE `settingfield`='enableRating'");
    $row=mysqli_fetch_row($result);
    define('PRODUCT_RATE_MODE', $row[0]);

    $result=mysqli_query($connection,"SELECT `value` FROM `".MYSQL_TABLE_PREFIX."settings` WHERE `settingfield`='enableComments'");
    $row=mysqli_fetch_row($result);
    define('PRODUCT_COMMENT_MODE', $row[0]);

     $result=mysqli_query($connection,"SELECT `value` FROM `".MYSQL_TABLE_PREFIX."settings` WHERE `settingfield`='enable_forum'");
    $row=mysqli_fetch_row($result);
    define('PRODUCT_FORUM_MODE', $row[0]);
    
    $result=mysqli_query($connection,"SELECT `value` FROM `".MYSQL_TABLE_PREFIX."settings` WHERE `settingfield`='enable_captcha'");
    $row=mysqli_fetch_row($result);
    define('PRODUCT_CAPTCHA_MODE', $row[0]);
    
    
    $result=mysqli_query($connection,"SELECT `value` FROM `".MYSQL_TABLE_PREFIX."settings` WHERE `settingfield`='siteLogo'");
    $row=mysqli_fetch_row($result);
    define('PRODUCT_LOGO', $row[0]);

    
    $result=mysqli_query($connection,"SELECT `value` FROM `".MYSQL_TABLE_PREFIX."settings` WHERE `settingfield`='siteName'");
    $row=mysqli_fetch_row($result);
    define('SITE_NAME',$row[0]);
    
    $result=mysqli_query($connection,"SELECT `value` FROM `".MYSQL_TABLE_PREFIX."settings` WHERE `settingfield`='siteTitle'");
    $row=mysqli_fetch_row($result);
    define('META_TITLE',$row[0]);
    
    $result=mysqli_query($connection,"SELECT `value` FROM `".MYSQL_TABLE_PREFIX."settings` WHERE `settingfield`='metaKeywords'");
    $row=mysqli_fetch_row($result);
     define('META_KEYWORDS',$row[0]);
    
    $result=mysqli_query($connection,"SELECT `value` FROM `".MYSQL_TABLE_PREFIX."settings` WHERE `settingfield`='metaDescription'");
    $row=mysqli_fetch_row($result);
    define('META_DES',$row[0]);
    
    $result=mysqli_query($connection,"SELECT `value` FROM `".MYSQL_TABLE_PREFIX."settings` WHERE `settingfield`='secureURL'");
    $row=mysqli_fetch_row($result);
    define('SITE_SECURE_URL',$row[0]);
    
    $result=mysqli_query($connection,"SELECT `value` FROM `".MYSQL_TABLE_PREFIX."settings` WHERE `settingfield`='nFeatured_Amount'");
    $row=mysqli_fetch_row($result);
    define('FEATURED_AMOUNT',$row[0]);

    define('SITEPATH',getcwd());
    
    $result=mysqli_query($connection,"SELECT `value` FROM `".MYSQL_TABLE_PREFIX."settings` WHERE `settingfield`='pageListCount'");
    $row=mysqli_fetch_row($result);
    define('PAGE_LIST_COUNT', $row[0] ); 

}


/**
* Method to auto load classes
* Controller, Model and views 
* will Load from modules
**/
function __autoload($class)
{

 	//Split class name from camel case
	$classArray = preg_split('/(?<=\\w)(?=[A-Z])/', $class);
	$isValidRequest = true;
	/**
	* Controller, Model and views 
	* will Load from modules
	**/
	$nameSpace = array('Controller', 'Model', 'View');
        
	if($isValidRequest == !empty($classArray))
	{
		$module = '';
		if(in_array($classArray[0], $nameSpace))
		{
			$module = 'modules/' . MODULE . '/';
		}
		
		$classPath  = $module . implode($classArray, '/');
		$classPath  = strtolower($classPath)  . '.php' ;;
		//Check for class file existance
		if(file_exists($classPath)) 
		{
			include_once  $classPath;
		}
		else
		{
		   $isValidRequest = $isValidRequest && class_exists($class);
		}
	}
    
	if(!$isValidRequest)
	{ 
              
        ob_start();
//        header('location:' . BASE_URL . 'error.html' );
        header('location:' . BASE_URL . 'index/error_404' );

        exit;
    }

}
?>