<?php
/*=======================================================================
 *
 * data_download.php
 *
 * Autor: Marc Vollmann
 *
 =======================================================================*/


include_once ("../include/mcglobal.inc.php");
include_once ("../include/auth.inc.php");

getSecHttpVars("1",array("f_fileName","f_path","cscIdRoot","customerId","cscIdActual","context"));

// Select user-type for mode of security check 
$userType = getFieldValueFromId("user","usr_id",$usr_id,"usr_type");
$userTypeName = getUserTypeName($userType);

// Check authentication verifying emmployee an his/her costcenter- and customer-association
if ($userType != "4" && $userType != "5") :
   if (!( authCheckHQ($hq_id,$usr_id,$emp_id) || authCheck($hq_id,$usr_id,$emp_id,$cscIdRoot,$customerId,$cscIdActual)) || $userTypeName == "") :
      die ("$PHP_SELF: Access denied!");
   endif;
endif;

if (!isset($context)) : $context = ""; endif;


if ($f_fileName != "") :

   // Log operation
   writeToLogDB("98",$hq_id,"",$usr_id,"","","","FILE=" . $f_fileName . "|CONTEXT=" . $context);

   $mimeType = "text/plain";
   if (substr($f_fileName,-4) == ".pdf") :
      $mimeType = "application/pdf";
   elseif (substr($f_fileName,-4) == ".png") :
      $mimeType = "image/png";
   elseif (substr($f_fileName,-4) == ".jpg" || substr($f_fileName,-5) == ".jpeg") :
      $mimeType = "image/jpeg";
   elseif (substr($f_fileName,-4) == ".csv") :
      $mimeType = "text/comma-separated-values";
   elseif (substr($f_fileName,-4) == ".doc" || substr($f_fileName,-5) == ".dot") :
      $mimeType = "application/msword";
   elseif (substr($f_fileName,-4) == ".xls" || substr($f_fileName,-5) == ".xla") :
      $mimeType = "application/msexcel";
   elseif (substr($f_fileName,-5) == ".docx") :
      $mimeType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
   elseif (substr($f_fileName,-5) == ".dotx") :
      $mimeType = "application/vnd.openxmlformats-officedocument.wordprocessingml.template";
   elseif (substr($f_fileName,-5) == ".xlsx") :
      $mimeType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
   elseif (substr($f_fileName,-5) == ".xltx") :
      $mimeType = "application/vnd.openxmlformats-officedocument.spreadsheetml.template";
   elseif (substr($f_fileName,-4) == ".xml") :
      $mimeType = "application/xml";
   endif;
   $mode = "r";
   if (ENCRYPT_EXPORTDATA == "1") : 
      $mimeType = "application/octet-stream";
      $mode = "rb";
   endif;

   $gmnow = gmdate('D, d M Y H:i:s') . ' GMT';
   header("Content-type: " . $mimeType);
   header('Expires: ' . $gmnow);
   header('Content-Disposition: attachment; filename="' . $f_fileName . '"');
   header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
   header('Pragma: public');

   if (substr($f_path, 0, 4) == "EMC2") : $f_path = ".." . substr($f_path, 4); endif;
   $fileContent = readFromFile($f_path . $f_fileName, "", $mode);
   echo $fileContent;

endif;
?>
