<?php

// Check if the install folder exists - if so then show the installer app
if (is_dir(dirname(__FILE__).'/install') == true)
{
	header('Location: install');
	exit;
}

//Set the path here
$path = str_replace("public_html", "", getcwd());

// Set the current directory correctly for CLI requests
if (defined('STDIN')) {
    $callingfile = $_SERVER["PHP_SELF"];
    $fcpath = str_replace("index.php","",$callingfile);
    $path = str_replace("public_html/", "", $fcpath);
    chdir($fcpath);
}

/*
 * CHURCHBOX: Set a custom config VALUE based on BASE URL
 */	
	if (isset($_SERVER['HTTP_HOST']))
	{
		$cbox_base = $_SERVER['HTTP_HOST'];
		$cbox_base .= str_replace(basename($_SERVER['SCRIPT_NAME']), '', $_SERVER['SCRIPT_NAME']);
		$cbox_base = strtolower($cbox_base);
	}
	else
	{
		$cbox_base = 'http://localhost/';
	}


    //If we can't pick up the Base URL we should see if there is a "Base" file defined in the config folder.
    if($cbox_base == 'http://localhost/'):

        //See if an override file exists as this is a CLI request. This is a way of setting the cbox_base to
        //load alternative files as specified below
        if(file_exists("../config/override.php"))
            require_once "../config/override.php";

        define('CLI', 'TRUE');
    endif;

	$https_redirect = false;

    $additionaldir="";

	switch($cbox_base)
	{
		
		case 'dev.churchbox.co.uk/':
			define('CBOXKEY','devcbox87548256');
			$assign_to_config['cboxfolder'] = 'devcbox';
			define('ENVIRONMENT', 'development');
			$https_redirect = TRUE;
			break;
        case 'default.churchbox.co.uk/':
            define('CBOXKEY','defaultcc77dd');
            $assign_to_config['cboxfolder'] = 'default';
            //define('ENVIRONMENT', 'development');
            $https_redirect = TRUE;
            break;
        case 'gymea.churchbox.co.uk/':
            define('CBOXKEY','gymeacsfw3');
            $assign_to_config['cboxfolder'] = 'gymea';
            //define('ENVIRONMENT', 'development');
            $https_redirect = TRUE;
            break;
        
		case 'connect.localhost/':
		case 'cbox.localhost/':
			define('CBOXKEY','opefjhs732');
			$assign_to_config['cboxfolder'] = 'localhost';
			define('ENVIRONMENT', 'development');
			break;
		case 'drupal-demo.churchbox.co.uk/':
			define('CBOXKEY','drufhdte67');
			//define('CBOXKEY','12773hd83j');
			$assign_to_config['cboxfolder'] = 'drupaldemo';
			break;
		case 'belfrey.churchbox.co.uk/':
		case 'members.belfrey.org/':

			$https_redirect = TRUE;
            echo "This account has now been closed, please contact your church administrator for more information.";
            exit();

			break;

        case 'capp.localhost/':
        case 'uat.churchbox.co.uk/':
            define('ENVIRONMENT', 'development');
            $additionaldir = ":/home/coredev/:/opt/coredev/";

        default:
            //From Churchbox v0.6.5 start loading config files per church as each has it's own instance now.
            //The default folder will also just be "local" for these instances. They will also have the same cboxkey
            //"local"
            define('CBOXKEY','local');
            $assign_to_config['cboxfolder'] = 'local';
            $https_redirect = TRUE;

            //Tighten security on base dir
            $homeFolder = dirname($_SERVER['SCRIPT_FILENAME']);;
            $homeFolder = str_replace('public_html', '',$homeFolder);
            ini_set('open_basedir',"$homeFolder:/home/core/:/opt/core/:/home/base/tmp:/home/tmp$additionaldir");

            break;
	}


    $homeFolder = dirname($_SERVER['SCRIPT_FILENAME']);;
    $homeFolder = str_replace('public_html', '',$homeFolder);

    define('SITE_TMP', rtrim($homeFolder, '/') . '/tmp');

    //If undefined set to Production
	if (!defined('ENVIRONMENT')) {
		define('ENVIRONMENT', 'production');
	}
	


	//If redirection to HTTPS in require then do that here as long as it's not a CLI request
	if ($https_redirect && !defined('CLI')) :
		if(!isset($_SERVER['HTTPS']) || $_SERVER['HTTPS'] == ""){
			$redirect = "https://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
			header("Location: $redirect");
		}
	endif;


/*
 *---------------------------------------------------------------
 * ERROR REPORTING
 *---------------------------------------------------------------
 *
 * Different environments will require different levels of error reporting.
 * By default development will show errors but testing and live will hide them.
 */

	switch (ENVIRONMENT)
	{
		case 'development':
			error_reporting(E_ALL);
			if (!ini_get('display_errors'))
			{
				ini_set('display_errors', 1);
			}
			break;

		case 'testing':
		case 'production':
			error_reporting(0);
			break;

		default:
			exit('The application environment is not set correctly.');
	}

/*
|---------------------------------------------------------------
| DEFAULT INI SETTINGS
|---------------------------------------------------------------
|
| Necessary settings for a higher compatibility. Inspired by PyroCMS code.
|
*/

// PHP 5.3 requires this
if(ini_get('date.timezone') == '')
{
    date_default_timezone_set('GMT');
}


/*
 *---------------------------------------------------------------
* BONFIRE PATH
*---------------------------------------------------------------
*
* Simply change the first "path" variable, and the individual paths
* will be set accordingly.
*/
//$path = ".."; # THis is now set at the top of the page
$bonfire_path = "${path}/bonfire";

/*
 *---------------------------------------------------------------
* SYSTEM FOLDER NAME
*---------------------------------------------------------------
*
* This variable must contain the name of your "system" folder.
* Include the path if the folder is not in the same  directory
* as this file.
*
*/
$system_path = "${path}/bonfire/codeigniter";

/*
 *---------------------------------------------------------------
* APPLICATION FOLDER NAME
*---------------------------------------------------------------
*
* If you want this front controller to use a different "application"
* folder then the default one you can set its name here. The folder
* can also be renamed or relocated anywhere on your server.  If
* you do, use a full server path. For more info please see the user guide:
* http://codeigniter.com/user_guide/general/managing_apps.html
*
* NO TRAILING SLASH!
*
*/
$application_folder = "${path}/application";

/*
 *---------------------------------------------------------------
* VIEW FOLDER NAME
*---------------------------------------------------------------
*
* If you want to move the view folder out of the application
* folder set the path to the folder here. The folder can be renamed
* and relocated anywhere on your server. If blank, it will default
* to the standard location inside your application folder.  If you
* do move this, use the full server path to this folder
*
* NO TRAILING SLASH!
*
*/
$view_folder = '';

/*
 * --------------------------------------------------------------------
* DEFAULT CONTROLLER
* --------------------------------------------------------------------
*
* Normally you will set your default controller in the routes.php file.
* You can, however, force a custom routing by hard-coding a
* specific controller class/function here.  For most applications, you
* WILL NOT set your routing here, but it's an option for those
* special instances where you might want to override the standard
* routing in a specific front controller that shares a common CI installation.
*
* IMPORTANT:  If you set the routing here, NO OTHER controller will be
* callable. In essence, this preference limits your application to ONE
* specific controller.  Leave the function name blank if you need
* to call functions dynamically via the URI.
*
* Un-comment the $routing array below to use this feature
*
*/
// The directory name, relative to the "controllers" folder.  Leave blank
// if your controller is not in a sub-folder within the "controllers" folder
// $routing['directory'] = '';

// The controller class file name.  Example:  Mycontroller.php
// $routing['controller'] = '';

// The controller function you wish to be called.
// $routing['function']	= '';


/*
 * -------------------------------------------------------------------
*  CUSTOM CONFIG VALUES
* -------------------------------------------------------------------
*
* The $assign_to_config array below will be passed dynamically to the
* config class when initialized. This allows you to set custom config
* items or override any default config values found in the config.php file.
* This can be handy as it permits you to share one application between
* multiple front controller files, with each file containing different
* config values.
*
* Un-comment the $assign_to_config array below to use this feature
*
*/
// $assign_to_config['name_of_config_item'] = 'value of config item';

//--------------------------------------------------------------------
// CSRF ByPASS
//--------------------------------------------------------------------
//
// By default, Bonfire ships with CSRF protection set to ON for all
// forms in the system. We also highly encourage it's use for security
// reasons in your own applications. On rare occasions, you may need
// to bypass the CSRF protection for a controller, such as within
// an API where the request is coming from an external controller and
// no CSRF token would be available.
//
// The controllers currently must either be in the root controllers
// folder(NOT in a subfolder), or in a module.
//
// Example:
//      array( 'users', 'activities/activity')
//
$assign_to_config['csrf_ignored_controllers'] = array();


// --------------------------------------------------------------------
// END OF USER CONFIGURABLE SETTINGS.  DO NOT EDIT BELOW THIS LINE
// --------------------------------------------------------------------


/*
 * ---------------------------------------------------------------
*  Resolve the system path for increased reliability
* ---------------------------------------------------------------
*/



if (realpath($system_path) !== false) {
    $system_path = realpath($system_path) . '/';
}

// Ensure there's a trailing slash
$system_path = rtrim($system_path, '/') . '/';

// Is the system path correct?
if ( ! is_dir($system_path)) {
    exit("Your system folder path does not appear to be set correctly. Please open the following file and correct this: " . pathinfo(__FILE__, PATHINFO_BASENAME));
}

/*
 * -------------------------------------------------------------------
*  Now that we know the path, set the main path constants
* -------------------------------------------------------------------
*/
// The name of THIS file
define('SELF', pathinfo(__FILE__, PATHINFO_BASENAME));

// The PHP file extension
// this global constant is deprecated.
define('EXT', '.php');

// Path to the system folder
define('BASEPATH', str_replace("\\", "/", $system_path));

// Path to the front controller (this file)
define('FCPATH', dirname($_SERVER['SCRIPT_FILENAME']) . '/');

// Name of the "system folder"
define('SYSDIR', trim(strrchr(trim(BASEPATH, '/'), '/'), '/'));

// Bonfire Path
define('BFPATH', $bonfire_path . '/');

// Bonfire Path
define('ROOTPATH', $bonfire_path . '/');

// The path to the "application" folder
if (is_dir($application_folder)) {
    define('APPPATH', $application_folder . '/');
} else {
    if ( ! is_dir(BASEPATH . $application_folder . '/')) {
        exit("Your application folder path does not appear to be set correctly. Please open the following file and correct this: ".SELF);
    }

    define('APPPATH', BASEPATH . $application_folder . '/');
}

// The path to the "views" folder
if (is_dir($view_folder)) {
    define('VIEWPATH', $view_folder . '/');
} else {
    if ( ! is_dir(APPPATH . 'views/')) {
        exit("Your view folder path does not appear to be set correctly. Please open the following file and correct this: ".SELF);
    }

    define('VIEWPATH', APPPATH . 'views/');
}

/*
 * --------------------------------------------------------------------
 * LOAD THE BOOTSTRAP FILE
 * --------------------------------------------------------------------
 *
 * And away we go...
 *
 */
require_once BASEPATH.'core/CodeIgniter.php';

/* End of file index.php */
/* Location: ./index.php */

