cvs: docweb /www redirect.php

From: Date: Mon, 23 May 2005 17:31:00 +0000
Subject: cvs: docweb /www redirect.php
Groups: php.doc.web 
Request: Send a blank email to doc-web+get-664@lists.php.net to get a copy of this message
nlopess Mon May 23 13:31:00 2005 EDT Modified files: /docweb/www redirect.php Log: fix bug that prevented files from beeing downloaded, because we were including them as PHP files. cache the sanitized URI add a minimal mime types table http://cvs.php.net/diff.php/docweb/www/redirect.php?r1=1.4&r2=1.5&ty=u Index: docweb/www/redirect.php diff -u docweb/www/redirect.php:1.4 docweb/www/redirect.php:1.5 --- docweb/www/redirect.php:1.4 Fri Apr 15 14:07:42 2005 +++ docweb/www/redirect.php Mon May 23 13:30:58 2005 @@ -15,11 +15,20 @@ +----------------------------------------------------------------------+ | Authors: Sean Coates <sean@php.net> | +----------------------------------------------------------------------+ -$Id: redirect.php,v 1.4 2005/04/15 18:07:42 nlopess Exp $ +$Id: redirect.php,v 1.5 2005/05/23 17:30:58 nlopess Exp $ */ require_once('../include/lib_proj_lang.inc.php'); +/* mime types for downloading files */ +$mime_types = array( + 'gz' => 'application/x-gunzip', + 'tgz' => 'application/x-tar-gz', + 'tar.gz' => 'application/x-tar-gz', + 'zip' => 'application/x-zip-compressed' +); + + function part_is_project($part) { return isset($GLOBALS['PROJECTS'][$part]); } @@ -49,7 +58,9 @@ } function part_is_valid_uri($part) { - return file_exists(part_get_filename($part)); + if (file_exists($uri = part_get_filename($part))) + return $uri; + return false; } $parts = explode('/', $_SERVER['REQUEST_URI'], 4); @@ -63,20 +74,35 @@ } elseif (part_is_language($part)) { $language = $part; - } else { + } elseif ($part) { $uri .= "/$part"; } } -if (!$uri || !part_is_valid_uri($uri)) { +if (!$uri || !($uri = part_is_valid_uri($uri))) { unset($uri); } // we have a valid URI, answer the request if (isset($uri)) { - header($_SERVER['SERVER_PROTOCOL']." 200 Found (magic redirect)"); - require(part_get_filename($uri)); + header($_SERVER['SERVER_PROTOCOL']." 200 Found (magic redirect)"); + + // If it's a PHP file include it, otherwise pass it through + if (substr($uri, -4) == '.php') { + require(part_get_filename($uri)); + } else { + // get the file mime type + foreach ($mime_types as $ext => $type) { + if (substr($uri, -strlen(ext)) == $ext) { + $mime = $type; + break; + } + } + + header("Content-Type: $mime"); + readfile($uri); + } } else { // no resource found: header($_SERVER['SERVER_PROTOCOL']." 404 Not Found");

« previous php.doc.web (#664) next »