<?php
//descargaFichero.php
error_reporting(0);

#ini_set('display_errors', 1);
#ini_set('display_startup_errors', 1);

$fichero = $_GET['file'];
$un=$_GET['dr'];
list($fname,$ext) = explode('.', $fichero);

$basePath = '/home/www/acfipressvideo/';

// Graba tracking
$tdt = "https://www.acfipress.com/sesionTrack.php?fichero=".$fichero.'&uname='.$un;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $tdt);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_exec($ch);
curl_close($ch);

switch(strtolower($ext))
{
	case 'mp4':

		$fname = $fichero;
		if (file_exists($basePath.'video/'.basename($fichero)))
		{
			$fg=$basePath.'video/'.basename($fichero);
      set_time_limit(0);
      header('Content-type: video/mp4');
      header('Content-Disposition: attachment; filename="'.basename($fichero).'"');
			header('Content-Length: ' . filesize($fg));
      $file = @fopen($fg,"rb");
      while(!feof($file))
      {
	print(@fread($file, 1024*8));
	ob_flush();
	flush();
      }
fclose($fd);
			exit;
		}

	break;

	case 'wmv':

		$fname = $fichero;
		if (file_exists($basePath.'video/'.$fname))
		{
      set_time_limit(0);
      header('Content-type: video/x-ms-wmv');
      header('Content-Disposition: attachment; filename="'.$fname.'"');
      $file = @fopen($basePath.'video/'.$fname,"rb");
      while(!feof($file))
      {
	print(@fread($file, 1024*8));
	ob_flush();
	flush();
      }
fclose($fd);
			exit;
		}

	break;

	case 'mpg':

		$fname = str_replace('BC_','',$fichero);
		$fname1 = str_replace('.mpg','.mp4',$fname);

		if (file_exists($basePath.'publico/'.$fname)||file_exists($basePath.'publico/'.$fname1))
		{
			if (file_exists($basePath.'publico/'.$fname)){$nn=$fname;}
			if (file_exists($basePath.'publico/'.$fname1)){$nn=$fname1;}
      set_time_limit(0);
      header("Content-Disposition: attachment; filename=\"$nn\"");
      header("Pragma: public");
      header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
      $file = @fopen($basePath.'publico/'.$nn,"rb");
      while(!feof($file))
      {
	print(@fread($file, 1024*8));
	ob_flush();
	flush();
      }
      fclose($fd);
	exit;
		}

	break;

	case 'm2t':

		$fname = $fichero;
		if (file_exists($basePath.'general/'.$fname))
		{
      set_time_limit(0);
      header("Content-Disposition: attachment; filename=\"$fname\"");
      header("Pragma: public");
      header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
      $file = @fopen($basePath.'general/'.$fname,"rb");
      while(!feof($file))
      {
	print(@fread($file, 1024*8));
	ob_flush();
	flush();
      }
      fclose($fd);
	exit;
		}

	break;

	case 'flv':

		$fname = $fichero;
		if (file_exists($basePath.'publico/'.$fname))
		{
      set_time_limit(0);
      header('Content-type: video/x-flv');
      header('Content-Disposition: attachment; filename="'.$fname.'"');
      $file = @fopen($basePath.'publico/'.$fname,"rb");
      while(!feof($file))
      {
	print(@fread($file, 1024*8));
	ob_flush();
	flush();
      }
fclose($fd);
			exit;
		}

	break;

	default:

		//none

	break;

}
?>
