<?
$webhost='smpt.vbox.com';
$wshost='smpt.vbox.com';
$wsport=80;
$zmqport=5555;
$homedir='C:\\wamp64\\www\\smpt';
$webdir='\\pegasus';

$root=$homedir . $webdir;
$basedir=$homedir . '\\pegasus';
$homepage='./';

$libdir=$root."/lib";
set_include_path(get_include_path() . PATH_SEPARATOR . $libdir);

// LOG
$GLOBALS['_LOGFILE_INFO'] = "$root/log/info.log";
$GLOBALS['_LOGFILE_DEBUG'] = "$root/log/debug.log";
$GLOBALS['_LOGFILE_ERROR'] = "$root/log/error.log";
$GLOBALS['_LOGFILE_TRACK'] = "$root/log/tracking.log";

require_once "lib/Logger.php";
/* Log verbose */
$GLOBALS['_LOGLEVEL'] = LogLevel::$ALL;
//$GLOBALS['_LOGLEVEL'] = LogLevel::$INFO;
$logger = Logger::getInstance($_SERVER['PHP_SELF']);
//$logger->debug(print_r($_SERVER, 1));


// memcached integrated
require_once $libdir."/Memcached_pool.php";
$mcache = new Memcached();
$mcache->addServers($GLOBALS['memcached-sets'][DEFAULT_MEMCACHED_SET]);


//+++ site profile +++
$WebSocket = "ws://".$wshost.":".$wsport;
$DomainDir = "http://".$webhost."/".$webdir;

if (PHP_SAPI !== 'cli') {
  if (session_status() == PHP_SESSION_NONE) session_start() or die("cannot create session");
}

DEFINE('MCACHE_SERVER', 'smpt');

DEFINE('MCACHE_SUFFIX', '-smpt_user');

DEFINE('MCACHE_EXPIRE', 600);
DEFINE('MCACHE_KEY_USER', session_id() . 'db_user');
DEFINE('MCACHE_KEY_LAST_ACCESS', session_id() . 'last_access_time');
DEFINE('DB_SERVER_TIMEZONE', 'Asia/Bangkok');

//$db_user = $mcache->get(session_id() . MCACHE_SUFFIX);

// set last access time
if (!defined('__CHECK_SESSION_EXPIRE__'))
  $mcache->setByKey(MCACHE_SERVER, MCACHE_KEY_LAST_ACCESS, new DateTime(), MCACHE_EXPIRE);

$db_user = $mcache->getByKey(MCACHE_SERVER, MCACHE_KEY_USER);

require_once $libdir."/Util.php";

if (PHP_SAPI !== 'cli') {
DEFINE('SIGNIN_PAGE', 'signin.php');
DEFINE('SIGNOUT_PAGE', 'signout.php');
$curPageName = substr($_SERVER["SCRIPT_NAME"],strrpos($_SERVER["SCRIPT_NAME"],"/")+1);


// if ($_SERVER['HTTP_USER_AGENT'] == "use-for-webservice-only" && $_SERVER["HTTP_X_REQUESTED_WITH"] == "XMLHttpRequest") {
//   DEFINE('__NO_AUTHENTICATE_REQUIRED__', '');
// }


if (
  (
  $_SERVER['HTTP_USER_AGENT'] == "use-for-webservice-only"
    && $_SERVER["HTTP_X_REQUESTED_WITH"] == "XMLHttpRequest"
    ) || $_SERVER["HTTP_X_REQUESTED_WITH"] == "application/web-service"
    || getallheaders()['X-Requested-With'] == "application/web-service"
) {
  DEFINE('__NO_AUTHENTICATE_REQUIRED__', '');
}

DEFINE('DB_SERVER_TIMEZONE', 'UTC');

if (!$db_user
	&& !defined("__NO_AUTHENTICATE_REQUIRED__")
    && $curPageName !== SIGNIN_PAGE // prevent infinite loop
	) {
    header("Locaion: " . SIGNOUT_PAGE); /* Redirect browser */
    exit();
}
}

const __LIB_DIR 				= 'C:\\wamp64\\www\\smpt\\pegasus\\lib\\';

$PATH_PARTS = pathinfo($_SERVER['PHP_SELF']);
$PATH_PARTS['MCACHE_KEY'] = strtok($PATH_PARTS['filename'], '.');