<?php
///////////////////////////////////////////////////////////////////////////////////
//////////////// DO NOT MODIFY ////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////

// pdthtml.php -> DOWNLOADMANAGER.php
error_reporting(0); //disable error reportings

ob_start(); //important else header is set !
include '../settings.php';
ob_end_clean(); //important else header is set !

$file_path = $SettingsDownload['file_path'];
$referer_url = $SettingsDownload['referer_url']; 
$enable_download_counter = $SettingsDownload['enable_download_counter']; 
$Max_downloads = $SettingsDownload['Max_downloads'];
$enable_log = $SettingsDownload['enable_log']; 
$log_file_name = $SettingsDownload['log_file_name']; 

$tx_token = $_GET['tx'];
$log_file = $file_path . $log_file_name;

//functions
function GetDownloadCount($file_path){
  global $tx_token;
  
  //Create a new file if this does not exist
  if (!file_exists($file_path . $tx_token . '.txt')) {
    $file = fopen($file_path . $tx_token . '.txt', 'w') or die("Failed to open file for writing! CHMOD the secret directory and all subdirectories to 755 or 777");
  }
  
  //Open File
  $file = fopen( $file_path . $tx_token . '.txt','r+');
  $count = fread($file,100);
  fclose($file);
  return $count;
}

function SetDownloadCount($file_path,$count){
  global $tx_token;
  $count++;
  $file = fopen( $file_path . $tx_token . '.txt','w'); // opens file again with 'w'-parameter
  fwrite($file, $count);
  fclose($file);   
}

function DownloadFile ($file_name , $file_size , $full_file_path){
  //Set Headers
  header('Pragma: public');   // required  
  header('Expires: 0');       // no cache  
  header('Cache-Control: must-revalidate, post-check=0, pre-check=0');  
  header("Cache-Control: public");
  header("Content-Description: File Transfer");
  header("Content-type: application/zip");
  header("Content-Disposition: attachment; filename=\"$file_name\"");
  header('Content-Transfer-Encoding: binary');
  header("Content-Length: " . $file_size);

  //Download File
  readfile($full_file_path);
}

function CreateLog ($log_file,$file_name){
  global $tx_token;
  $f = fopen($log_file, 'a+');
    if ($f) {
      fputs($f, date("m.d.Y g:ia")." ".$_SERVER['REMOTE_ADDR']." ".$tx_token." ".$file_name."\n");
      fclose($f);
    }
}
  	     


// Make sure program execution doesn't time out Set maximum script execution time in seconds (0 means no limit)
set_time_limit(0);

// If hotlinking not allowed then invent server problems 
if (
    $referer_url !== '' && 
   (!isset($_SERVER['HTTP_REFERER']) || strpos(strtoupper($_SERVER['HTTP_REFERER']),strtoupper("$referer_url")) === false)
    ) {
        die("Internal server error. Please contact the system administrator.");
}


//Check Input
if (!isset($_GET['f']) || empty($_GET['f'])) {
  die("Please specify the file name for download.");
} else {
  $file_name = $_GET['f'];
  $full_file_path = $file_path . $file_name;
}

if (!is_file($full_file_path)) {
  die("File does not exist. Make sure you specified the correct file name."); 
} else {
  $file_size = filesize($full_file_path);
}

if ($enable_download_counter==False) { 
  DownloadFile ($file_name , $file_size , $full_file_path);
} else {
  $current_counter=GetDownloadCount($file_path);
  if ($current_counter<$Max_downloads) {
    SetDownloadCount($file_path , $current_counter);
    DownloadFile ($file_name , $file_size , $full_file_path);
  } else {
    die("Max download limit reached for the $tx_token ID transaction");
  }
}

if ($enable_log==False) {
  die();
} else {
  CreateLog ($log_file,$file_name);
}

?>