<?php
/**
 * 2007-2017 PrestaShop
 *
 * NOTICE OF LICENSE
 *
 * @author    Innovadeluxe SL
 * @copyright 2017 Innovadeluxe SL

 * @license   INNOVADELUXE
 */

include_once('../../config/config.inc.php');
include_once('../../init.php');
//include_once('idxrcustomproduct.php');

//$module = new IdxrCustomProduct();
//
$context = Context::getContext();

$file_key = Tools::getValue('key');

$sql = 'select * from ' . _DB_PREFIX_ . 'idxrcustomproduct_files where target_name = "' . pSQL($file_key) . '"';
$file_info = Db::getInstance()->getRow($sql);

$url = _PS_ROOT_DIR_ . '/upload/' . $file_info['target_name'];

$mime = mime_content_type($url);

$name = $file_info['original_name'];

// Fetch and serve
if ($url) {
    $size= filesize($url);
    
    // Generate the server headers
    if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== false) {
        header('Content-Type: "' . $mime . '"');
        header('Content-Disposition: attachment; filename="' . $name . '"');
        header('Expires: 0');
        header('Content-Length: '.$size);
        header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
        header("Content-Transfer-Encoding: binary");
        header('Pragma: public');
    } else {
        header('Content-Type: "' . $mime . '"');
        header('Content-Disposition: attachment; filename="' . $name . '"');
        header("Content-Transfer-Encoding: binary");
        header('Expires: 0');
        header('Content-Length: '.$size);
        header('Pragma: no-cache');
    }
    
    readfile($url);
    exit;
}

// Not found
exit('File not found');
