<?php
// Get file name (presumed to be in this same folder)
if (isset($_GET["f"])) $strFile = trim($_GET["f"]);
else $strFile = "";
if (isset($_GET["ct"])) $strCT = trim($_GET["ct"]);
else $strCT = "application/pdf";
if (isset($_GET["nf"])) $strNF = true;
else $strNF = false;
$strPathToSelf = $_SERVER["SCRIPT_FILENAME"];
if (!empty($strFile)){
  // Compute full path to PDF
  $strPathToPDF = str_replace("servepdf.php", "", $strPathToSelf) . $strFile;
  // Check PDF path is valid
  if (file_exists($strPathToPDF) === FALSE) {
    echo "File not found";
  } else {
    // Check user agent and set disposition
    $ua = $_SERVER["HTTP_USER_AGENT"] . "";
    if(strpos($ua, "Firefox") === false){
      // Not Firefox: Redirect to actual file
      // Compute URL
      $url = $_SERVER['SERVER_NAME'] . str_replace("servepdf.php?f=", "", $_SERVER['REQUEST_URI']);
      if (!empty($_SERVER['HTTPS'])) {
        $url = "https://" . $url;
      } else {
        $url = "http://" . $url;
      }
      // Send redirect
      header("location: " . $url);
    } else {
      // Firefox: Serve file with attachment disposition to force download
      // Read in file as a string
      $strPDFData = file_get_contents("./" . $strFile);
      // Set headers
      header("Content-type: " . $strCT);
	  if ($strNF === true) header("Content-Disposition: attachment");
      else header("Content-Disposition: attachment; filename=" . $strFile);
      // Serve blob
      echo $strPDFData;
    }
  }
} else {
  echo "<pre>" . htmlspecialchars(file_get_contents($strPathToSelf)) . "</pre>";
}
?>