<?php

error_reporting(E_ALL ^ E_NOTICE);

if (!function_exists('virtual')){
    die("<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">
    <html><body>

    <table width=60% height=\"90%\" align=center border=0>
    <tr valign=middle align=center><td>
    <table bgcolor=#ee0000 bordercolor=#660000 cellpadding=8 border=2 style='border-collapse: collapse'><tr><td>
    <b>Fatal error:</b> PHP must be compiled-in as Apache module to use this 
    method of protection. Looks like you cannot use this method of protection 
    (<b>mod_rewrite+php_include</b>) on your hosting.
    <br /><br />
    <div><small>This message generated by aMember Pro subscription management software.</small></div>
    </b></td></tr></table>
    </td></tr>
    </table>

    </body></html>
    ");    
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
put the following code to your .htaccess file:

RewriteEngine On
RewriteRule ^(.*)$ /amember/plugins/protect/php_include/rewrite.php [E=PRODUCT_ID:1;2;3]

If your content contains something except HTML, JPG, GIF and PNG images,
customize the code in bottom of this file to send correct headers to
client browser.
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
session_cache_limiter('none');

//$expires = (function_exists('session_cache_expire') ? session_cache_expire() : 60 ) * 60;
$expires = 0;
$gm_expires = gmdate('D, d M Y H:i:s', time() + $expires);
header("Cache-control: private, max-age=$expires, pre-check=$expires");
header("Expires: $gm_expires GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");

if ($s = $_SERVER['REDIRECT_PRODUCT_ID'])
    $_product_id = preg_split('/\D+/', $s, -1, PREG_SPLIT_NO_EMPTY);
else
    $_product_id = array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21);
$dir = dirname(__FILE__);

if (!array_intersect((array)$_product_id, (array)$_SESSION['_amember_product_ids'])){
    require "$dir/check.inc.php";
}

function rewrite_run_main(){
    global $_product_id;
    $uri = $_SERVER["REQUEST_URI"];
    if (preg_match('/(([\w\d\s\-]+)\.([a-zA-Z0-9]+))($|\?)/i', $uri, $regs)) {
        global $common_mime_types;
        $fn =  $regs[1];
        $ext = strtolower($regs[3]);
//            print_r($regs);
        if ($ext && ($mime=$common_mime_types[$ext])) {
//            print "$fn|$ext|$mime<br />";
            header("Content-type: $mime");
            if (preg_match('/^application/', $mime)){
                header($s="Content-Disposition: attachment; filename=\"$fn\"");
//                print "$s<br />";
            }
        }
    }
    return $uri;
}

$common_mime_types = array(
         "exe" => "application/octet-stream",
         "class" => "application/octet-stream",
         "so" => "application/octet-stream",
         "dll" => "application/octet-stream",
         "oda" => "application/oda",
         "hqx" => "application/mac-binhex40",
         "cpt" => "application/mac-compactpro",
         "doc" => "application/msword",
         "bin" => "application/octet-stream",
         "dms" => "application/octet-stream",
         "lha" => "application/octet-stream",
         "lzh" => "application/octet-stream",
         "pdf" => "application/pdf",
         "ai" => "application/postscript",
         "eps" => "application/postscript",
         "ps" => "application/postscript",
         "smi" => "application/smil",
         "smil" => "application/smil",
         "bcpio" => "application/x-bcpio",
         "wbxml" => "application/vnd.wap.wbxml",
         "wmlc" => "application/vnd.wap.wmlc",
         "wmlsc" => "application/vnd.wap.wmlscriptc",
         "vcd" => "application/x-cdlink",
         "pgn" => "application/x-chess-pgn",
         "cpio" => "application/x-cpio",
         "csh" => "application/x-csh",
         "dcr" => "application/x-director",
         "dir" => "application/x-director",
         "dxr" => "application/x-director",
         "dvi" => "application/x-dvi",
         "spl" => "application/x-futuresplash",
         "gtar" => "application/x-gtar",
         "hdf" => "application/x-hdf",
         "skp" => "application/x-koan",
         "skd" => "application/x-koan",
         "js" => "application/x-javascript",
         "skt" => "application/x-koan",
         "skm" => "application/x-koan",
         "latex" => "application/x-latex",
         "nc" => "application/x-netcdf",
         "cdf" => "application/x-netcdf",
         "sh" => "application/x-sh",
         "shar" => "application/x-shar",
         "swf" => "application/x-shockwave-flash",
         "sit" => "application/x-stuffit",
         "sv4cpio" => "application/x-sv4cpio",
         "sv4crc" => "application/x-sv4crc",
         "tar" => "application/x-tar",
         "tcl" => "application/x-tcl",
         "tex" => "application/x-tex",
         "texinfo" => "application/x-texinfo",
         "texi" => "application/x-texinfo",
         "t" => "application/x-troff",
         "tr" => "application/x-troff",
         "roff" => "application/x-troff",
         "man" => "application/x-troff-man",
         "me" => "application/x-troff-me",
         "ms" => "application/x-troff-ms",
         "ustar" => "application/x-ustar",
         "src" => "application/x-wais-source",
         "xhtml" => "application/xhtml+xml",
         "xht" => "application/xhtml+xml",
         "zip" => "application/zip",
         "au" => "audio/basic",
         "snd" => "audio/basic",
         "mid" => "audio/midi",
         "midi" => "audio/midi",
         "kar" => "audio/midi",
         "mpga" => "audio/mpeg",
         "mp2" => "audio/mpeg",
         "mp3" => "audio/mpeg",
         "aif" => "audio/x-aiff",
         "aiff" => "audio/x-aiff",
         "aifc" => "audio/x-aiff",
         "m3u" => "audio/x-mpegurl",
         "ram" => "audio/x-pn-realaudio",
         "rm" => "audio/x-pn-realaudio",
         "rpm" => "audio/x-pn-realaudio-plugin",
         "ra" => "audio/x-realaudio",
         "wav" => "audio/x-wav",
         "wmv" => "audio/x-ms-wma",
         "pdb" => "chemical/x-pdb",
         "xyz" => "chemical/x-xyz",
         "bmp" => "image/bmp",
         "gif" => "image/gif",
         "ief" => "image/ief",
         "jpeg" => "image/jpeg",
         "jpg" => "image/jpeg",
         "jpe" => "image/jpeg",
         "png" => "image/png",
         "tiff" => "image/tiff",
         "tif" => "image/tif",
         "djvu" => "image/vnd.djvu",
         "djv" => "image/vnd.djvu",
         "wbmp" => "image/vnd.wap.wbmp",
         "ras" => "image/x-cmu-raster",
         "pnm" => "image/x-portable-anymap",
         "pbm" => "image/x-portable-bitmap",
         "pgm" => "image/x-portable-graymap",
         "ppm" => "image/x-portable-pixmap",
         "rgb" => "image/x-rgb",
         "xbm" => "image/x-xbitmap",
         "xpm" => "image/x-xpixmap",
         "xwd" => "image/x-windowdump",
         "igs" => "model/iges",
         "iges" => "model/iges",
         "msh" => "model/mesh",
         "mesh" => "model/mesh",
         "silo" => "model/mesh",
         "wrl" => "model/vrml",
         "vrml" => "model/vrml",
         "mpeg" => "video/mpeg",
         "mpg" => "video/mpeg",
         "mpe" => "video/mpeg",
         "qt" => "video/quicktime",
         "mov" => "video/quicktime",
         "mxu" => "video/vnd.mpegurl",
         "avi" => "video/x-msvideo",
         "wmv" => "video/x-ms-wmv",
         "movie" => "video/x-sgi-movie",
         "asx" => "video/x-ms-asf",
         "asf" => "video/x-ms-asf",
         "css" => "text/css",
         "asc" => "text/plain",
         "txt" => "text/plain",
         "rtx" => "text/richtext",
         "rtf" => "text/rtf",
         "sgml" => "text/sgml",
         "sgm" => "text/sgml",
         "tsv" => "text/tab-seperated-values",
         "wml" => "text/vnd.wap.wml",
         "wmls" => "text/vnd.wap.wmlscript",
         "etx" => "text/x-setext",
         "xml" => "text/xml",
         "xsl" => "text/xml",
      );


function catch_rewrite_errors($errno, $errstr, $errfile, $errline){
    global $err_count;
    $err_count++;
}

if (($uri=rewrite_run_main())) {

    $err_count=0;    
    $is_php = preg_match('/\.(php|phtml|php3)[?$]/', $uri);
    if (!$is_php) {
        set_error_handler('catch_rewrite_errors');
        error_reporting(E_ALL ^ E_NOTICE);
    }
    virtual($uri);


    if ($err_count){
        header("HTTP/1.1 404 Not Found");
        header("Status: 404");
        print <<<CUT
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>404 Not Found</title>
</head><body>
<h1>Not Found</h1>
The requested URL $_SERVER[REQUEST_URI] was not found on this server.<p>
<hr>
</body></html>
CUT;
    }
} 
?>