JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
run.php works even through symbolic links
[wfpl.git] / run.php
1 <?php
2
3 #  Copyright (C) 2006 Jason Woofenden
4 #
5 #  This file is part of wfpl.
6 #
7 #  wfpl is free software; you can redistribute it and/or modify it
8 #  under the terms of the GNU General Public License as published by
9 #  the Free Software Foundation; either version 2, or (at your option)
10 #  any later version.
11 #
12 #  wfpl is distributed in the hope that it will be useful, but
13 #  WITHOUT ANY WARRANTY; without even the implied warranty of
14 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 #  General Public License for more details.
16 #
17 #  You should have received a copy of the GNU General Public License
18 #  along with wfpl; see the file COPYING.  If not, write to the
19 #  Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
20 #  MA 02111-1307, USA.
21
22
23 # This file facilitates making a site with mixed PHP and html files. All URLs
24 # have the .html extension. One benefit of this is that you can change static
25 # html files to php scripts without having to update links.
26
27 # This script will pull the filename from the URL. It looks for a file with
28 # that name, and for one with the same basename, but with the .php extension
29 # and acts accordingly:
30
31 #--------------------------------------------#
32 #        | .php file exists   | no .php file #
33 #--------+--------------------+--------------#
34 # .html  | load html file as  | pass html    #
35 # file   | a template and run | file through #
36 # exists | run the php file   | as is        #
37 #--------+--------------------+--------------#
38 # no     |                    |              #
39 # .html  | run php file       | display 404  #
40 # file   |                    |              #
41 #--------------------------------------------#
42
43
44
45 # To activate this script in a directory, you'll need to put something like the
46 # following in you your .htaccess file (where /foo/ is the part of the url
47 # between the hostname and the filename.) The example below would work for this
48 # url: http://example.com/foo/bar.html
49
50 # RewriteEngine  on
51 # RewriteBase    /foo/
52 # RewriteRule    .*\.html$  /foo/code/wfpl/run.php
53
54 function run_php($basename = false) {
55         if($basename) {
56                 $html_file = "$basename.html";
57                 $php_file = "$basename.php";
58         } else {
59
60                 # first we must find the file and directory that was actually requested
61                 # (before we messed it up with mod_rewrite)
62                 $fs_path = $_SERVER['DOCUMENT_ROOT'] . $_SERVER['REDIRECT_URL'];
63                 $split = strrpos($fs_path, '/'); # find the last slash
64                 $directory = substr($fs_path, 0, $split);
65                 $file = substr($fs_path, $split + 1);
66
67                 if($file == '') {
68                         $file = 'index.html';
69                 }
70
71                 chdir($directory);
72                 $html_file = $file;
73                 $php_file = ereg_replace('\.html$', '.php', $html_file);
74         }
75         if($php_file != $html_file && file_exists($php_file)) {
76                 require_once('code/wfpl/template.php');
77                 if(file_exists($html_file)) tem_load($html_file);
78                 require $php_file;
79                 if(file_exists($html_file)) tem_output();
80         } else {
81                 if(file_exists($html_file)) {
82                         require $html_file;
83                 } else {
84                         header('HTTP/1.0 404 File Not Found');
85                         if(file_exists('404.php') || file_exists('404.html')) {
86                                 run_php('404');
87                         } else {
88                                 echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html><head><title>404</title></head><body><h1>404 File Not Found</h1></body></html>';
89                         }
90                 }
91         }
92 }
93
94 run_php();
95
96 ?>