function IM_Entry($datasource, $options, $dbspecification, $debug = false)
{
    global $g_dbInstance, $g_serverSideCall;
    // check required PHP extensions
    $requiredFunctions = array('mbstring' => 'mb_internal_encoding');
    if (isset($options) && is_array($options)) {
        foreach ($options as $key => $option) {
            if ($key == 'authentication' && isset($option['user']) && is_array($option['user']) && array_search('database_native', $option['user']) !== false) {
                // Native Authentication requires BC Math functions
                $requiredFunctions = array_merge($requiredFunctions, array('bcmath' => 'bcadd'));
                break;
            }
        }
    }
    foreach ($requiredFunctions as $key => $value) {
        if (!function_exists($value)) {
            $generator = new GenerateJSCode();
            $generator->generateInitialJSCode($datasource, $options, $dbspecification, $debug);
            $generator->generateErrorMessageJS("PHP extension \"" . $key . "\" is required for running INTER-Mediator.");
            return;
        }
    }
    if ($debug) {
        $dc = new DefinitionChecker();
        $defErrorMessage = $dc->checkDefinitions($datasource, $options, $dbspecification);
        if (strlen($defErrorMessage) > 0) {
            $generator = new GenerateJSCode();
            $generator->generateInitialJSCode($datasource, $options, $dbspecification, $debug);
            $generator->generateErrorMessageJS($defErrorMessage);
            return;
        }
    }
    //    file_put_contents("/tmp/php2.log", "POST: " . var_export($_POST, true), FILE_APPEND);
    //    file_put_contents("/tmp/php2.log", "GET: " . var_export($_GET, true), FILE_APPEND);
    //    file_put_contents("/tmp/php2.log", "FILES: " . var_export($_FILES, true), FILE_APPEND);
    //    file_put_contents("/tmp/php2.log", "SERVER: " . var_export($_SERVER, true), FILE_APPEND);
    if (isset($g_serverSideCall) && $g_serverSideCall) {
        $dbInstance = new DB_Proxy();
        $dbInstance->initialize($datasource, $options, $dbspecification, $debug);
        $dbInstance->processingRequest("NON");
        $g_dbInstance = $dbInstance;
    } else {
        if (!isset($_POST['access']) && isset($_GET['uploadprocess'])) {
            $fileUploader = new FileUploader();
            $fileUploader->processInfo();
        } else {
            if (!isset($_POST['access']) && isset($_GET['media'])) {
                $dbProxyInstance = new DB_Proxy();
                $dbProxyInstance->initialize($datasource, $options, $dbspecification, $debug);
                $mediaHandler = new MediaAccess();
                if (isset($_GET['attach'])) {
                    $mediaHandler->asAttachment();
                }
                $mediaHandler->processing($dbProxyInstance, $options, $_GET['media']);
            } else {
                if (isset($_POST['access']) && $_POST['access'] == 'uploadfile' || isset($_GET['access']) && $_GET['access'] == 'uploadfile') {
                    $fileUploader = new FileUploader();
                    if (IMUtil::guessFileUploadError()) {
                        $fileUploader->processingAsError($datasource, $options, $dbspecification, $debug);
                    } else {
                        $fileUploader->processing($datasource, $options, $dbspecification, $debug);
                    }
                } else {
                    if (!isset($_POST['access']) && !isset($_GET['media'])) {
                        $generator = new GenerateJSCode();
                        $generator->generateInitialJSCode($datasource, $options, $dbspecification, $debug);
                    } else {
                        $dbInstance = new DB_Proxy();
                        $dbInstance->initialize($datasource, $options, $dbspecification, $debug);
                        $util = new IMUtil();
                        if ($util->protectCSRF() === TRUE) {
                            $dbInstance->processingRequest();
                            $dbInstance->finishCommunication(false);
                        } else {
                            $dbInstance->addOutputData('debugMessages', 'Invalid Request Error.');
                            $dbInstance->addOutputData('errorMessages', array('Invalid Request Error.'));
                        }
                        $dbInstance->exportOutputDataAsJSON();
                    }
                }
            }
        }
    }
}
function IM_Entry($datasource, $options, $dbspecification, $debug = false)
{
    global $g_dbInstance, $g_serverSideCall;
    spl_autoload_register('loadClass');
    // check required PHP extensions
    $requiredFunctions = array('mbstring' => 'mb_internal_encoding');
    if (isset($options) && is_array($options)) {
        foreach ($options as $key => $option) {
            if ($key == 'authentication' && isset($option['user']) && is_array($option['user']) && array_search('database_native', $option['user']) !== false) {
                // Native Authentication requires BC Math functions
                $requiredFunctions = array_merge($requiredFunctions, array('bcmath' => 'bcadd'));
                break;
            }
        }
    }
    foreach ($requiredFunctions as $key => $value) {
        if (!function_exists($value)) {
            $generator = new GenerateJSCode();
            $generator->generateInitialJSCode($datasource, $options, $dbspecification, $debug);
            $generator->generateErrorMessageJS("PHP extension \"" . $key . "\" is required for running INTER-Mediator.");
            return;
        }
    }
    if ($debug) {
        $dc = new DefinitionChecker();
        $defErrorMessage = $dc->checkDefinitions($datasource, $options, $dbspecification);
        if (strlen($defErrorMessage) > 0) {
            $generator = new GenerateJSCode();
            $generator->generateInitialJSCode($datasource, $options, $dbspecification, $debug);
            $generator->generateErrorMessageJS($defErrorMessage);
            return;
        }
    }
    if (isset($g_serverSideCall) && $g_serverSideCall) {
        $dbInstance = new DB_Proxy();
        $dbInstance->initialize($datasource, $options, $dbspecification, $debug);
        $dbInstance->processingRequest($options, "NON");
        $g_dbInstance = $dbInstance;
    } else {
        if (!isset($_POST['access']) && isset($_GET['uploadprocess'])) {
            $fileUploader = new FileUploader();
            $fileUploader->processInfo();
        } else {
            if (!isset($_POST['access']) && isset($_GET['media'])) {
                $dbProxyInstance = new DB_Proxy();
                $dbProxyInstance->initialize($datasource, $options, $dbspecification, $debug);
                $mediaHandler = new MediaAccess();
                if (isset($_GET['attach'])) {
                    $mediaHandler->asAttachment();
                }
                $mediaHandler->processing($dbProxyInstance, $options, $_GET['media']);
            } else {
                if (isset($_POST['access']) && $_POST['access'] == 'uploadfile' || isset($_GET['access']) && $_GET['access'] == 'uploadfile') {
                    $fileUploader = new FileUploader();
                    $fileUploader->processing($datasource, $options, $dbspecification, $debug);
                } else {
                    if (!isset($_POST['access']) && !isset($_GET['media'])) {
                        $generator = new GenerateJSCode();
                        $generator->generateInitialJSCode($datasource, $options, $dbspecification, $debug);
                    } else {
                        $dbInstance = new DB_Proxy();
                        $dbInstance->initialize($datasource, $options, $dbspecification, $debug);
                        if ($_SERVER['HTTP_X_REQUESTED_WITH'] === 'XMLHttpRequest') {
                            $dbInstance->processingRequest($options);
                            $dbInstance->finishCommunication(false);
                        } else {
                            $dbInstance->addOutputData('debugMessages', 'Invalid Request Error.');
                            $dbInstance->addOutputData('errorMessages', array('Invalid Request Error.'));
                        }
                        $dbInstance->exportOutputDataAsJSON();
                    }
                }
            }
        }
    }
}
function IM_Entry($datasource, $options, $dbspecification, $debug = false)
{
    global $g_dbInstance, $g_serverSideCall;
    spl_autoload_register('loadClass');
    // check required PHP extensions
    $requiredFunctions = array('mbstring' => 'mb_internal_encoding');
    foreach ($requiredFunctions as $key => $value) {
        if (!function_exists($value)) {
            $generator = new GenerateJSCode();
            $generator->generateInitialJSCode($datasource, $options, $dbspecification, $debug);
            $generator->generateErrorMessageJS("PHP extension \"" . $key . "\" is required for running INTER-Mediator.");
            return;
        }
    }
    if ($debug) {
        $dc = new DefinitionChecker();
        $defErrorMessage = $dc->checkDefinitions($datasource, $options, $dbspecification);
        if (strlen($defErrorMessage) > 0) {
            $generator = new GenerateJSCode();
            $generator->generateInitialJSCode($datasource, $options, $dbspecification, $debug);
            $generator->generateErrorMessageJS($defErrorMessage);
            return;
        }
    }
    if (isset($g_serverSideCall) && $g_serverSideCall) {
        $dbInstance = new DB_Proxy();
        $dbInstance->initialize($datasource, $options, $dbspecification, $debug);
        $dbInstance->processingRequest($options, "NON");
        $g_dbInstance = $dbInstance;
    } else {
        if (!isset($_POST['access']) && isset($_GET['uploadprocess'])) {
            $fileUploader = new FileUploader();
            $fileUploader->processInfo();
        } else {
            if (!isset($_POST['access']) && isset($_GET['media'])) {
                $dbProxyInstance = new DB_Proxy();
                $dbProxyInstance->initialize($datasource, $options, $dbspecification, $debug);
                $mediaHandler = new MediaAccess();
                if (isset($_GET['attach'])) {
                    $mediaHandler->asAttachment();
                }
                $mediaHandler->processing($dbProxyInstance, $options, $_GET['media']);
            } else {
                if (isset($_POST['access']) && $_POST['access'] == 'uploadfile' || isset($_GET['access']) && $_GET['access'] == 'uploadfile') {
                    $fileUploader = new FileUploader();
                    $fileUploader->processing($datasource, $options, $dbspecification, $debug);
                } else {
                    if (!isset($_POST['access']) && !isset($_GET['media'])) {
                        $generator = new GenerateJSCode();
                        $generator->generateInitialJSCode($datasource, $options, $dbspecification, $debug);
                    } else {
                        $dbInstance = new DB_Proxy();
                        $dbInstance->initialize($datasource, $options, $dbspecification, $debug);
                        $dbInstance->processingRequest($options);
                        $dbInstance->finishCommunication(false);
                    }
                }
            }
        }
    }
}