JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
run.php css thing uses 'css_link' instead of 'basename' as template tag
[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 under the
8 #  terms of the GNU Lesser General Public License as published by the Free
9 #  Software Foundation; either version 2.1 of the License, or (at your option)
10 #  any later version.
11 #
12 #  wfpl is distributed in the hope that it will be useful, but WITHOUT ANY
13 #  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 #  FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for
15 #  more details.
16 #
17 #  You should have received a copy of the GNU Lesser General Public License
18 #  along with wfpl; if not, write to the Free Software Foundation, Inc., 51
19 #  Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
20
21
22 # This file facilitates making a site with mixed PHP and html files. All URLs
23 # have the .html extension. One benefit of this is that you can change static
24 # html files to php scripts without having to update links.
25
26 # This script will pull the filename from the URL. It looks for a file with
27 # that name, and for one with the same basename, but with the .php extension
28 # and acts accordingly:
29
30 #--------------------------------------------#
31 #        | .php file exists   | no .php file #
32 #--------+--------------------+--------------#
33 # .html  | load html file as  | pass html    #
34 # file   | a template and run | file through #
35 # exists | run the php file   | as is        #
36 #--------+--------------------+--------------#
37 # no     |                    |              #
38 # .html  | run php file       | display 404  #
39 # file   |                    |              #
40 #--------------------------------------------#
41
42
43
44 # To activate this script in a directory, you'll need to:
45 #
46 # 1) make a symbolic link to (or copy of) this file in your directory. and
47 #
48 # 3) Set your webserver to run this script instead of html files. Here's how to
49 # do that with apache: put something like the following in you your .htaccess
50 # file (where /foo/ is the part of the url between the hostname and the
51 # filename.) The example below would work for this url:
52 # http://example.com/foo/bar.html
53
54 # RewriteEngine  on
55 # RewriteRule    ^$  /foo/run.php
56 # RewriteRule    ^foo/[^/]*\.html$  /foo/run.php
57
58 require_once('code/wfpl/file_run.php');
59 require_once('code/wfpl/http.php');
60 require_once('code/wfpl/template.php');
61
62 function run_php($basename = false) {
63         if(!$basename) {
64                 $basename = $_SERVER['REDIRECT_URL'];
65                 $basename = ereg_replace('.*/', '', $basename);
66                 $basename = ereg_replace('\.html$', '', $basename);
67                 if($basename == '') {
68                         $basename = 'index';
69                 }
70         }
71
72         $html_file = "$basename.html";
73         $php_file = "$basename.php";
74
75         if(!file_exists($php_file)) {
76                 if(file_exists($html_file)) {
77                         readfile($html_file);
78                 } else {
79                         header('HTTP/1.0 404 File Not Found');
80                         if(file_exists('404.php') || file_exists('404.html')) {
81                                 run_php('404');
82                         } else {
83                                 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>';
84                         }
85                 }
86                 exit();
87         }
88
89         if(file_exists($html_file)) {
90                 $GLOBALS['wfpl_template'] = new tem();
91                 tem_load($html_file);
92         }
93
94         $other = file_run($php_file);
95         if($other) {
96                 if(strpos($other, ':')) {
97                         redirect($other);
98                         exit();
99                 }
100                 run_php($other);
101                 return;
102         }
103
104         if($GLOBALS['wfpl_template']) {
105                 if(file_exists('template.html')) {
106                         $tem = new tem();
107                         $tem->load("template.html");
108                         $sections = tem_top_subs();
109                         if($sections) foreach($sections as $name => $val) {
110                                 $tem->set($name, $val);
111                         }
112
113                         if(file_exists("$basename.css")) {
114                                 $tem->set('css_link', "$basename.css");
115                                 $tem->sub('css_links');
116                         }
117
118                         $GLOBALS['wfpl_template'] = $tem;
119                 }
120
121                 if(function_exists('display_messages')) {
122                         display_messages();
123                 }
124                 tem_output();
125         }
126 }
127
128 run_php();
129
130 ?>