<?php
date_default_timezone_set("Asia/Seoul");

if (isset($_SERVER['HTTP_ORIGIN'])) {
	header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}");
	header('Access-Control-Allow-Credentials: true');
	header('Access-Control-Max-Age: 86400');    // cache for 1 day
}

// Access-Control headers are received during OPTIONS requests
if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {

	if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD']))
		header("Access-Control-Allow-Methods: GET, POST, OPTIONS");

	if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']))
		header("Access-Control-Allow-Headers: {$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}");

	exit(0);
}
session_start();
require_once $_SERVER["DOCUMENT_ROOT"] . '/config/bslib_env.php';
//echo $_SERVER["DOCUMENT_ROOT"];

include_once ROOT . "/lib/Log.php";
$log = get_logger();
global $log;

$request = $_REQUEST;
$api = isset($request['api']) ? $request['api'] : null;

if (empty($api) != $api && $api != null) {
	include_once ROOT . '/api/' . $api . '.php';
}

include_once ROOT . "/lib/sk_common.php";
// include_once ROOT . "/lib/sk_define.php"; //string 값 적용을 위해서 아래 $string 밑으로 위치 이동
// include_once ROOT . "/api/development.php";

// 실행되면 안되는 항목들이 생기면 추가
$controller_exclude_filter = array(
	"favicon.ico",
	"public"	// bom관리의 jstree를 표시할 때 오류를 일으킴
);
$ar_url_cmd = explode("/", $_SERVER['REQUEST_URI']);
if (FALSE !== array_search($ar_url_cmd[1], $controller_exclude_filter)) {
	exit(1);
}

$BSPHP_CONTROLLER_NAME = null;
$BSPHP_VIEW_NAME = null;
$BSPHP_DONT_RENDER = false;

if (count($ar_url_cmd) > 1 && strlen($ar_url_cmd[1]) > 0) {
	$BSPHP_CONTROLLER_NAME = $ar_url_cmd[1];
	if (2 < count($ar_url_cmd)) {
		$BSPHP_VIEW_NAME = $ar_url_cmd[2];
	}
}

$ar_url_params = explode("?", $BSPHP_VIEW_NAME);
if (count($ar_url_params) > 1 && strlen($ar_url_params[1]) > 0) {
	$BSPHP_VIEW_NAME = $ar_url_params[0];
}

if (!isset($BSPHP_VIEW_NAME) || strlen($BSPHP_VIEW_NAME) < 1) {
	$BSPHP_VIEW_NAME = 'index';
}
$BSPHP_HEADER_NAME = 'header.php';
$BSPHP_FOOTER_NAME = 'footer.php';

/**
 * 문자열 치환 변수 $string 
 */
$string = '';
if ($BSPHP_CONTROLLER_NAME) {
	if ($BSPHP_DONT_RENDER != true) {
		$dbConn = bslib_db_connect();
		include_once ROOT . "/lib/bs_env_manager.php";
		$string = BSEnvManager::getStringEnvList($dbConn);
		include_once ROOT . "/views/common/mes_search_param.php";
		if(count($string)>0){ // string의 키를 변수키에 넣어서 바로 사용할수 있게 재 변환
			foreach($string as $key=>$val){
				$string_array[$val['env_key']] = $val['env_value'];
			}
		}
	}
	include_once ROOT . "/lib/sk_define.php";

	include_once $_SERVER["DOCUMENT_ROOT"] . '/controllers/' . $BSPHP_CONTROLLER_NAME . '_controller.php';

	if ($BSPHP_DONT_RENDER != true) {
		$viewPage = new BSPage($BSPHP_CONTROLLER_NAME, $BSPHP_VIEW_NAME, $BSPHP_HEADER_NAME, $BSPHP_FOOTER_NAME);
		$viewPage->renderPage();
	}
} else {
	BsAppController::redirect_to('/user/index');
	include_once ROOT . "/lib/sk_define.php";
}

function get_logger($ident = "", $priority = "all") {
	global $__logger;

	if (!isset($__logger)) {
		$log_filename = date("Ym") . ".log";
		$log_path = ROOT . "/_/logs";
		if (!file_exists($log_path)) {
			umask(0);
			@mkdir($log_path, 0777, true);
		}
		$__logger = Log::singleton("file", $log_path . "/${log_filename}", $ident);
		if (empty($priority)) {
			$priority = "all";
		}
		$n_priority = null;
		if ($priority == "all") {
			$n_priority = PEAR_LOG_ALL;
		} else if ($priority == "none") {
			$n_priority = PEAR_LOG_NONE;
		} else {
			$n_priority = $__logger->stringToPriority($priority);
		}

		if ($__logger->getPriority() != $n_priority) {
			$__logger->setPriority($n_priority);
		}
	} else if (!empty($ident)) {
		$__logger->setIdent($ident);
	}
	return $__logger;
}
