<?php

require "firebase_jwt.php";
require dirname(__DIR__) . "/autoload.smart.php";
//require dirname(__DIR__) . "/Includes/constants.php";
require LIBS_PATH . 'Common/db_standalone_class.php';
require __DIR__ . "/app_class.php";
if (!defined('CMS__APP_TYPE'))
    define('CMS__APP_TYPE', 'distribution'); //default
if (!file_exists(__DIR__ . '/' . CMS__APP_TYPE . '/app_class.php')) {
    header("HTTP/1.0 500 Internal Server Error");
    die(json_encode(array('error_info' => "Application Type [" . CMS__APP_TYPE . "] Not Available..")));
}
require __DIR__ . '/' . CMS__APP_TYPE . '/app_class.php';
//
if (!function_exists('apache_request_headers')) {
    $arh = array();
    $rx_http = '/\AHTTP_/';
    foreach ($_SERVER as $key => $val) {
        if (preg_match($rx_http, $key)) {
            $arh_key = preg_replace($rx_http, '', $key);
            $rx_matches = array();
            // do some nasty string manipulations to restore the original letter case
            // this should work in most cases
            $rx_matches = explode('_', $arh_key);
            if (count($rx_matches) > 0 and strlen($arh_key) > 2) {
                foreach ($rx_matches as $ak_key => $ak_val)
                    $rx_matches[$ak_key] = ucfirst($ak_val);
                $arh_key = implode('-', $rx_matches);
            }
            $arh[$arh_key] = $val;
        }
    }
    $_HEADER = $arh;
} else
    $_HEADER = apache_request_headers();
//$_HEADER = apache_request_headers();
//note: header field name on some servers not read it if there is special characters like underscore, dot,..
//and also capitalize first letter also letter after the dash or capitalize all letters and since some 
//servers send it as all upper case and some just first letter and.. for that we always here make upper 
//case and depend on it
if (!empty($_HEADER)) {
    foreach ($_HEADER as $key => $val)
        $_HEADER[strtoupper($key)] = $val;
}
//
$app = new app();
//
$_BODY = json_decode(file_get_contents('php://input'), true);
if (empty($_BODY))
    $_BODY = array();
if (!empty($_POST))
    $_BODY = array_merge($_BODY, $_POST);
$app->set_body_data($_BODY);
//
if (empty($_REQUEST['action']) || $_REQUEST['action'] == 'login')
    $app->login();
//here before check for access token, app. here check if url is success or not by call this
if ($_REQUEST['action'] == 'check_connection')
    die(json_encode(array('is_valid' => 'ok')));
//
$tmp_access_token = '';
if (!empty($_HEADER['ACCESSTOKEN']))
    $tmp_access_token = $_HEADER['ACCESSTOKEN'];
elseif (isset($_BODY['ACCESSTOKEN'])) {
    $tmp_access_token = $_BODY['ACCESSTOKEN'];
    unset($_BODY['ACCESSTOKEN']);
}
//
my_jwt::$is_error_die = false;   //to don't die, so here die and send error we want
my_jwt::$error_msg = null;  //clear error message
$login_data = my_jwt::my_decode($tmp_access_token);
if (my_jwt::$error_msg)
    $app->die_error('Access Token Error: ' . my_jwt::$error_msg);
$app->set_login_data($login_data);
//
$tmp_space = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
$log_data = "<br/>====================================================================================><br/>";
$log_data .= "Time: " . date('Y-m-d H:i:s u') . "{$tmp_space}User: {$app->get_login_data('User_Name')}[{$app->get_login_user_id()}]{$tmp_space}Action: {$_REQUEST['action']}\r\n<br/>";
file_put_contents(tmp_dir_path . '/app-' . CMS__APP_TYPE . '-' . date('Y-m-d') . '-log.html', $log_data, FILE_APPEND);
if (method_exists($app, $_REQUEST['action'])) {
    $app->init_app_unique();    //to prevent same request at same time.
    call_user_func(array($app, $_REQUEST['action']));
} else
    $app->die_error("Method [{$_REQUEST['action']}] Not Available..");
//
