<?php
/* ======================================================================
 * 
 * A P I   F O R   C I   S E R V E R
 * 
 * GET  /packages/[phase]/[ID]/[filename]
 * 
 * ----------------------------------------------------------------------
 * 2021-03-31  v0.0  <axel.hahn@iml.unibe.ch>  init
 * ======================================================================
 */

    $bDebug=true;
    ini_set('display_errors', 1);
    ini_set('display_startup_errors', 1);
    error_reporting(E_ALL);

    require('../inc_functions.php');
    $aConfig=require_once("../inc_config.php");
    
    $iMaxAge=60;
    

    // ----------------------------------------------------------------------
    // MAIN
    // ----------------------------------------------------------------------

    _wd('Start: '.date('Y-m-d H:i:s').'<style>body{background:#eee; color:#456;}
            .debug{background:#ddd; margin-bottom: 2px;}
         </style>');

    _wd('request uri is '.$_SERVER["REQUEST_URI"]); 
    _wd('<pre>GET: '.print_r($_GET, 1).'</pre>');

    
    _checkAuth($aConfig['apikey']);

    // if I am here then authentication was successful.

    // ---------- SPLIT URL
    
    $sRelfile=preg_replace('#'.$aConfig['cutfromrequest'].'#', '', $_SERVER["REQUEST_URI"]);
    _wd('$sRelfile: '.$sRelfile);  
    
    
    $sMyFile=$aConfig['packagedir'].$sRelfile;
    _wd('full path of file: '.$sMyFile);
    
    if (!file_exists($sMyFile)){
        _quit('File not found.', 404);
    }
    
    // let the webserver deliver a given file 
    header('X-Sendfile: ' . $sMyFile);

// ======================================================================