<?php

$_SERVER['VAS_PATH'] = pathinfo(dirname($_SERVER['SCRIPT_FILENAME']))['dirname'];

require_once("class/vitis_lib/VitisError.class.inc");
require_once("class/vmlib/cryptUtil.inc");
require_once("conf/properties.inc");
require_once("vmlib/logUtil.inc");
require_once("vmlib/Email.class.inc");

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
	header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE');    // For CORS
	header('Access-Control-Allow-Headers: Content-Type, Token');
}
if (isset($_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE'])) {
    $sMethod = $_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE'];
} else {
    $sMethod = $_SERVER['REQUEST_METHOD'];
}
$_PUTDATA = null;
$paramJson2 = null;
$params = file_get_contents('php://input');
$oJson = $params;
if (is_object(json_decode($oJson))) {
    $paramJson2 = get_object_vars(json_decode($oJson));
}
$boundary = substr($params, 0, strpos($params, "\r\n"));
// Fetch each part
if (!empty($boundary)) {
    $parts = array_slice(explode($boundary, $params), 1);
    foreach ($parts as $part) {
        // If this is the last part, break
        if ($part == "--\r\n")
            break;

        // Separate content from headers
        $part = ltrim($part, "\r\n");
        list($raw_headers, $body) = explode("\r\n\r\n", $part, 2);

        // Parse the headers list
        $raw_headers = explode("\r\n", $raw_headers);
        $headers = array();
        foreach ($raw_headers as $header) {
            list($name, $value) = explode(':', $header, 2);
            $headers[strtolower($name)] = ltrim($value, ' ');
        }
        // Parse the Content-Disposition to get the field name, etc.
        if (isset($headers['content-disposition'])) {
            $filename = null;
            preg_match(
                    '/^(.+); *name="([^"]+)"(; *filename="([^"]+)")?/', $headers['content-disposition'], $matches
            );
            list(, $type, $name) = $matches;
            isset($matches[4]) and $filename = $matches[4];
            // handle your fields here
            // Ce champ est un fichier ()?
            if (!empty($filename)) {
                // Internet Explorer passe le chemin complet vers le fichier.
                $aFilenamePathInfo = pathinfo($filename);
                $filename = $aFilenamePathInfo['filename'] . '.' . $aFilenamePathInfo['extension'];
                //
                $_PUTDATA[$name . "_file"] = substr($body, 0, strlen($body) - 2);
                $_PUTDATA[$name . "_name"] = $filename;
            } else
                $_PUTDATA[$name] = substr($body, 0, strlen($body) - 2);
            /*
              switch ($name) {
              // this is a file upload
              case 'userfile':
              file_put_contents($filename, $body);
              break;
              // default for all other files is to populate $data
              default:
              $_PUTDATA[$name."_file"] = substr($body, 0, strlen($body) - 2);
              $_PUTDATA[$name."_name"] = $filename;
              break;
              }
             */
        }
    }
}


$aParamsJson = json_decode(file_get_contents('php://input'), true);
if ($_PUTDATA != null) {
    $aValues = array_merge($_REQUEST, $_PUTDATA);
} else {
    $aValues = $_REQUEST;
}
if ($aParamsJson != null) {
    $aValues = array_merge($aValues, $aParamsJson);
}

if ($paramJson2 != null) {
    $aValues = array_merge($aValues, $paramJson2);
}
// Token depuis l'entête de la requête.
$aRequestHeaders = apache_request_headers();
if (!empty($aRequestHeaders['Token']))
    $aValues['token'] = $aRequestHeaders['Token'];

if (!empty($aValues['filter'])) {
    if (is_object($aValues['filter']) || is_array($aValues['filter'])) {
        $aValues['filter'] = json_encode($aValues['filter']);
    }
}

//Récupération des ressources
$aPath = explode('/', trim($_SERVER['PATH_INFO'], '/'));
//Définition du format de sortie
$sOutput = 'application/json';
if (!isset($aValues['output']))
    $aValues['output'] = null;
if ($aValues['output'] == "application/xml" || $_SERVER['HTTP_ACCEPT'] == 'application/xml') {
    $sOutput = 'application/xml';
} elseif ($aValues['output'] == "text/html" || $_SERVER['HTTP_ACCEPT'] == 'text/html') {
    $sOutput = 'text/html';
} elseif ($aValues['output'] == "application/x-vm-json" || $_SERVER['HTTP_ACCEPT'] == 'application/x-vm-json') {
    $sOutput = 'application/x-vm-json';
}
$aValues['output'] = $sOutput;
//Définition de l'encodage
if (empty($aValues["encoding"])) {
    $aValues['sEncoding'] = $properties["page_encoding"];
} else {
    $aValues['sEncoding'] = $aValues["encoding"];
}
$aValues['sSourceEncoding'] = $properties["page_encoding"];

if (empty($aValues['xslstylesheet'])) {
    $aValues['xslstylesheet'] = "";
}

//Vérification d'appel à une ressource
if ($_SERVER['PATH_INFO'] == "/") {
    $oError = new VitisError(5, 'Service missing.');
    $aXmlRacineAttribute['status'] = 0;
    $sMessage = $oError->asDocument($aValues['xslstylesheet'], 'vitis', $aValues['sEncoding'], True, $aXmlRacineAttribute, $aValues['sSourceEncoding'], $aValues['output']);
    $bXMLError = true;
} else {
    // set custom error handler to notify administrator if enabled
    if($properties["enable_error_notification"] === true && !empty($properties["admin_sign_up"])){
        set_error_handler("customErrorHandler");
    }
    //Instanciation de la classe
    $sModule = $aPath[0];
    $sClassName = $aPath[1];
    //$aValues['my_vitis_id'] = null;
    if (isset($aPath[2]))
        $aValues['my_vitis_id'] = $aPath[2];
    $aValues['module'] = $sModule;
    $files = glob("ws/$sModule/*.class.inc");
    foreach ($files as $file) {
        if (strtolower("ws/$sModule/$sClassName.class.inc") == strtolower($file)) {

            require($file);
            $sClass = new $sClassName($aPath, $aValues, $properties);
            //Appel de la méthode GET PUT POST ou DELETE de la classe
			if($sMethod !== 'OPTIONS')
				$sMessage = $sClass->$sMethod();
        }
    }
}

function customErrorHandler ($errno, $errstr, $errfile, $errline, $context){
    if($errno == E_USER_ERROR || $errno == E_CORE_ERROR || $errno == E_ERROR || $errno == E_RECOVERABLE_ERROR){

        $now = date('Y-m-d H:i:s');
        $now = new DateTime($now);
        $sNow = $now->format('d/m/Y H:i:s');

        $sErrorReturn  = $sNow . " " . $errfile . " LINE " . $errline . " BY " . $_SESSION["ses_Login"] . " --> " . $errstr;
        writeToErrorLog($sErrorReturn);

        $sMailStruct = array(
            "to" => $context["properties"]["admin_sign_up"],
            "cc" => "",
            "cci"=> "",
            "subject" => "[VITIS-ERROR] Une erreur est survenue dans votre application : " . $context["properties"]["web_server_name"],
            "body" => "<p>Une nouvelle erreur est survenue dans l'API de " . $context["properties"]["web_server_name"] . ". Retour d'erreur (consultez le fichier error.log de l'application pour plus d'informations):</p>
            <p>" . $sErrorReturn . "</p>
            <p> aPath : " . print_r($context["aPath"], true) . "</p>
            <p> aValues : " . print_r($context["aValues"], true) . "</p>
            <p> Chemin du vas : " . $context["properties"]['vas_home'] . "</p>",
            "aAttachments" => array()
        );
        $mail = new Email(null, null, $context["properties"], null);
        $mail->loadFromExernalDataStruct($sMailStruct);
        //error_log("between load and send");
        $mail->send();

        http_response_code(500);
        header('Content-type: '.$context["aValues"]['output'].'; charset='.$context["properties"]["page_encoding"]);
        echo $context["sMessage"];

        die();
    }
}

include ("index.vhtml");
?>
