<?php

/***
 ***
 *** INITIAL PHP SETUP
 ***
 **/


// session_start();

/*** INITIAL TIMEZONE SETUP ***/
date_default_timezone_set("Asia/Taipei");

/*** DEFINE THE SITE PATH AS CONSTANT ***/
define('__SITE_PATH', realpath(dirname(dirname(dirname(dirname(__FILE__))))));

/*** DEFINE THE DIRECTORY SEPARATOR AS CONSTANT ***/
define("DS", DIRECTORY_SEPARATOR);

/*** 正式上線的production請務必設定此項，避免錯誤回報 ***/
ini_set("display_errors", "0");

/*** 程式串寫時建議將 error_report 全部開啟，已嚴謹方式進行撰寫 ***/
ini_set('error_reporting', E_ALL);

/*** 啟用錯誤 log 紀錄 ***/
ini_set("log_errors", "On");

/*** LOG THE FILE ***/
ini_set('error_log', __SITE_PATH . "/app/log/ErrorLog-" . date("Y-m") . ".log");

/*** 將 POST/GET/SESSION/COOKIE 變數分開，PHP5以後預設為關閉的 ***/
ini_set("register_globals", "0");

/***
 ***
 *** CONSTANT SETUP :: DEFINE RELATED VARIABLE
 ***
 **/

/*** DEFINE PROJECT NAME AS CONSTANT ***/
define("PROJECT_NAME", "Hanyu");

/*** DEFINE MAX IDLE TIME AS CONSTANT :: SECONDS ***/
define("MAX_IDLE_TIME", 900);

/*** DEFINE THE DAYS OF PASSWORD OUTDATED CHECK ***/
define("PASSWORD_OUTDATED_CHECK", 90);

/*** DEFINE THE RECOMMEND API PREFIX ***/
// define( "RECOMMEND_API_PREFIX", "http://localhost:16888/" );
//define("RECOMMEND_API_PREFIX", "http://localhost/mooddeer/mooddeer_backend");
/***
 ***
 *** CONSTANT SETUP :: HANDLE COMPOSED URL
 ***
 **/

/*** DEFINE DOMAIN AS CONSTANT ***/
//define( 'DOMAIN', "192.168.10.230" );
// define('DOMAIN', "localhost:8000");
/*** CHECK CONNECTION TYPE AND SET AS CONSTANT ***/
if ((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') || $_SERVER['SERVER_PORT'] == 443) {

    define("CONNECTION_TYPE", 'https://');
} else {

    define("CONNECTION_TYPE", 'http://');
}

/*** GET THE CURRENT HOST NAME ***/
$current_host  = $_SERVER['HTTP_HOST'];

// if ($current_host === DOMAIN) {

//     /*** LOAD RELATED FILES ***/
//     include_once __SITE_PATH . '/src/inc/system/get_request_uri_route.php';

//     /*** GET THE REQUEST URI ***/
//     $request_uri = $_SERVER['REQUEST_URI'];
//     // echo "request_uri:" . $request_uri;
//     $request_uri_parse = parse_url($request_uri, PHP_URL_PATH);
//     $request_uri_elements = explode('/', $request_uri_parse);
    
//     /*** GET THE REQUEST URI ROUTE ***/
//     $uri_route = getRequestUriRoute(DOMAIN, $request_uri_elements);
  
//     $composed_url = CONNECTION_TYPE . DOMAIN . $uri_route;
//     $composed_url = str_replace("\\", "/", $composed_url );
//     define('__COMPOSED_URL', $composed_url);
//     //  echo "__SITE_PATH=" . __SITE_PATH;
    
// }
class APICache {
    private $redis;
    private $ttl; // Time To Live for cache items192.168.10.119

    public function __construct($host = '125.229.116.28', $port = 56379, $ttl = 3600) {
        $this->redis = new Redis();
        $this->redis->connect($host, $port);
        $this->ttl = $ttl;
    }

    public function get($key) {
        return $this->redis->get($key);
    }

    public function set($key, $value) {
        return $this->redis->setex($key, $this->ttl, $value);
    }

    public function delete($key) {
        return $this->redis->del($key);
    }
}
