<?php
// docsecure.php - Grant Root - 12/2/04
//
// This snippet of code demonstrates how to secure a collection of documents
// by assigning a PHP program as a "handler" for a file type in Apache.
//
// Copyright 2004 Grant Root (grant@rootcentral.org)
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details (www.gnu.org).


// ************************************
// Apache Configuration
// ************************************
// You must set up the following directives in the Apache config file in
// order to activate this code when a document is requested. It requires
// the mod_actions module for Apache.
//
//LoadModule action_module /usr/lib/apache/1.3/mod_actions.so
//...
//<Directory "/var/www/documents">
//    <IfModule mod_actions.c>
//        Action secure-doc /docsecure.php
//        <IFModule mod_mime.c>
//            AddHandler secure-doc .pdf .PDF  # Specify file extensions here.
//        </IfModule>
//    </IfModule>
//</Directory>


// Require a customized function library for access control
require("./functions/security_functions.php");

// Initialize some variables
$URIparts = explode('?', $_SERVER['REQUEST_URI']); // Get URI of requested document w/o parameters
$errorpage = 'http://www.mydomain.com/myerror.php'; // Full path preferred for redirect
$$docpath = 'documents/'; // Pre-specified document path

// Start or re-start the session, for access to authentication credentials
session_start();  // Must be placed before any output to the browser

// Call some custom function to control access to document
secure_page(array('mygroup')); // Limit access to members of group "mygroup"

// Specify available documents (This could be read from a database table, and
// possibly customized according to the authenticated username.)
$allowedDocs = array ('documentone.pdf',
					 'documenttwo.pdf',
					 'documentthree.pdf',
					 'documentfour.pdf',
					 );

foreach($allowedDocs as $thisDoc)
{
   if($docpath . $thisDoc == $URIparts[0]) // Using pre-specified path for added security
   {
      $filename = $_SERVER['DOCUMENT_ROOT'] . $docpath . $thisDoc;
      if (file_exists($filename))
      {
         $download_size = filesize($filename);
         if (isset($_SERVER["HTTPS"]))
         {
             // We need to set the following headers to make downloads work
             // using IE in HTTPS mode.
             header("Pragma: ");
             header("Cache-Control: ");
             header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
             header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
             header("Cache-Control: no-store, no-cache, must-revalidate"); // HTTP/1.1
             header("Cache-Control: post-check=0, pre-check=0", false);
         }
         else
         {
             header("Cache-Control: no-cache, must-revalidate");
             header("Pragma: no-cache");
         }
         header('Content-type: application/pdf');
         header('Content-Disposition: inline; filename="' . $thisDoc . '"');
         header("Accept-Ranges: bytes");
         header("Content-Length: $download_size");
         readfile($filename); 
         exit;
      }
   }
} // End foreach

$errorMessage = 'Sorry; you do not have permission to access that document.';
header("Location: $errorpage?errorMessage=$errorMessage");
exit;
