JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
metaform's html type supports image uploads by default
[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 if(file_exists('code/config.php')) {
63         file_run('code/config.php');
64 }
65
66 # pass the basename of the page you want for normal execution
67 # pass ./page.html to redirect to page.html in this directory
68 # pass http://foo.com/bar.html to redirect to a full directory
69 function run_php($dest = false) {
70         if($dest) {
71                 # if it's got a : it must be a full URL, redirect
72                 if(strpos($dest, ':')) {
73                         redirect($dest);
74                         exit();
75                 }
76
77                 # if it starts with './' then it's a relative URL, redirect
78                 if(substr($dest, 0, 2) == './') {
79                         redirect(ereg_replace('/[^/]*$', substr($dest, 1), this_url()));
80                         exit();
81                 }
82
83                 # otherwise, it's a normal basename, display that content
84                 $basename = $dest;
85
86         } else { # no dest arg
87                 $basename = $_SERVER['REDIRECT_URL'];
88                 $basename = ereg_replace('.*/', '', $basename);
89                 $basename = ereg_replace('\.html$', '', $basename);
90                 if($basename == '') {
91                         $basename = 'index';
92                 }
93         }
94
95         $html_file = "$basename.html";
96         $php_file = "$basename.php";
97
98         $html_exists = file_exists($html_file);
99         $php_exists = file_exists($php_file);
100
101         # cms_get can return one of:
102         # 1) false to indicate that there's no cms content for this basename
103         # 2) a string to indicate a soft/full redirect just as foo_main()
104         # 3) a hash of key/value pairs to be tem_set(key,value) on the template
105         if(function_exists('cms_get')) {
106                 $cms_content = cms_get($basename);
107                 if(is_string($cms_content)) {
108                         run_php($cms_content);
109                         return;
110                 }
111         } else {
112                 $cms_content = false;
113         }
114
115         if(!$php_exists && !$html_exists && !$cms_content) {
116                 header('HTTP/1.0 404 File Not Found');
117                 if(file_exists('404.php') || file_exists('404.html')) {
118                         run_php('404');
119                         return;
120                 } else {
121                         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>';
122                 }
123         }
124
125         # If there's no template.html we don't want to parse $html_file.
126         if($html_exists && !$php_exists && !file_exists('template.html')) {
127                 readfile($html_file);
128                 exit();
129         }
130
131         if($html_exists) {
132                 tem_load_new($html_file);
133         }
134
135         if($php_exists) {
136                 # files can return a basename or URL of a page to be run/displayed
137                 $other = file_run($php_file);
138                 if($other) {
139                         run_php($other);
140                         return;
141                 }
142         } else {
143                 $sub_names = tem_top_sub_names();
144                 foreach($sub_names as $sub_name) {
145                         tem_sub($sub_name);
146                 }
147         }
148
149         # Check for $GLOBALS['wfpl_template'] because it might have been set (or unset) by the php script.
150         if($GLOBALS['wfpl_template']) {
151                 if(file_exists('template.html')) {
152                         $tem = new tem();
153                         $tem->load("template.html");
154                         $tem->set('basename', $basename);
155                         if($cms_content) foreach($cms_content as $name => $val) {
156                                 $tem->append($name, $val);
157                         }
158                         $sections = tem_top_subs();
159                         if($sections) foreach($sections as $name => $val) {
160                                 $tem->append($name, $val);
161                         }
162
163                         if(file_exists("$basename.css")) {
164                                 $tem->set('css_link', "$basename.css");
165                                 $tem->sub('css_links');
166                         }
167
168                         $GLOBALS['wfpl_template'] = $tem;
169                 }
170
171                 if(function_exists('display_messages')) {
172                         display_messages();
173                 }
174                 tem_output();
175         }
176 }
177
178 run_php();
179
180 ?>