<?php

header('Access-Control-Allow-Origin: *');
header("Access-Control-Allow-Headers: Authorization, Origin, X-Requested-With, Content-Type, Accept");

include('needed_functions.php');

$file = $_GET["f"];
$img = $_GET["i"];
// fake here
    // $file = 'certificate1.json';
    // $file = 'eventfull_icon.png';
    // $img = true;
// end fake

$extension = substr($file, -4);

// all images (.jpg and .png) are stored in the /i folder
// all templates (json) are stored in the /t folder

$url_base = 'https://api.eventfull-us.com/';
// $url = $url_base . 'i/eventfull_icon.png';
$out = '';

// echo($extension);


switch ($extension) {
    case ".jpg":
        $url = 'i/' . $file;
        $imageData = base64_encode(file_get_contents($url));
        $out = 'data:image/jpeg;base64,'.$imageData;
        // echo '<img src="', $out , '">';
        echo $out; 
        break;
    case ".png":
        $url = 'i/' . $file;
        if ($img){
            $out = file_get_contents($url);
            header('content-type: image/png');
        } else {
            $imageData = base64_encode(file_get_contents($url));
            $out = 'data:image/png;base64,'.$imageData;
        }
        echo $out; 
        
        break;
    case "json":
        $url = 't/' . $file;
        // $out = file_getcontents($url);
        // $out = file_get_contents($url_base . $url); // this USED to work but failed ~8/1/19.  Doesnt' seem to like including the url base (https://...)
        $out = file_get_contents($url);
        
        header("Content-Type: application/json");
        // $out = preg_replace('/\s+/S', " ", $out);
        echo $out;
		// echo json_encode($out);
        break;
}

// echo $out;    
// $out = file_get_contents("t/certificate1.json"); // this works
// echo $out . '<--';

// echo $url_base . $url;
// echo file_get_contents($url_base . $url);

