JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
metaform: only require_once(email.php) if needed
[wfpl.git] / run.php
1 <?php  # Transitional; supports both template APIs
2
3 #  2006 Public Domain
4 #
5 #  This file was placed into the public domain on November 16th, 2008 by its
6 #  sole author Jason Woofenden
7
8 # This file facilitates making a site with mixed PHP and html files. All URLs
9 # have the .html extension. One benefit of this is that you can change static
10 # html files to php scripts without having to update links.
11
12 # This script will pull the filename from the URL. It looks for a file with
13 # that name, and for one with the same basename, but with the .php extension
14 # and acts accordingly:
15
16 #--------------------------------------------#
17 #        | .php file exists   | no .php file #
18 #--------+--------------------+--------------#
19 # .html  | load html file as  | pass html    #
20 # file   | a template and run | file through #
21 # exists | run the php file   | as is        #
22 #--------+--------------------+--------------#
23 # no     |                    |              #
24 # .html  | run php file       | display 404  #
25 # file   |                    |              #
26 #--------------------------------------------#
27
28
29
30 # To activate this script in a directory, you'll need to:
31 #
32 # 1) make a symbolic link to (or copy of) this file in your directory. and
33 #
34 # 3) Set your webserver to run this script instead of html files. Here's how to
35 # do that with apache: put something like the following in you your .htaccess
36 # file (where /foo/ is the part of the url between the hostname and the
37 # filename.) The example below would work for this url:
38 # http://example.com/foo/bar.html
39
40 # RewriteEngine  on
41 # RewriteRule    ^$  /foo/run.php
42 # RewriteRule    ^foo/[^/]*\.html$  /foo/run.php
43
44 require_once(__DIR__.'/'.'code/wfpl/file_run.php');
45 require_once(__DIR__.'/'.'code/wfpl/http.php');
46 require_once(__DIR__.'/'.'code/wfpl/template.php');
47
48 if(file_exists(__DIR__.'/'.'code/config.php')) {
49         file_run(__DIR__.'/'.'code/config.php');
50 }
51
52 # pass the basename of the page you want for normal execution
53 # pass ./page.html to redirect to page.html in this directory
54 # pass http://foo.com/bar.html to redirect to a full directory
55 function run_php($dest = false) {
56         if($dest) {
57                 # if it has a : it must be a full URL, redirect
58                 if(strpos($dest, ':')) {
59                         redirect($dest);
60                         exit();
61                 }
62
63                 # if it starts with './' then it's a relative URL, redirect
64                 if(substr($dest, 0, 2) == './') {
65                         redirect(ereg_replace('/[^/]*$', substr($dest, 1), this_url()));
66                         exit();
67                 }
68
69                 # otherwise, it's a normal basename, display that content
70                 $basename = $dest;
71
72         } else { # no dest arg
73                 $basename = $_SERVER['REDIRECT_URL'];
74                 $basename = ereg_replace('.*/', '', $basename);
75                 $basename = ereg_replace('\.html?$', '', $basename);
76                 if($basename == '') {
77                         $basename = 'index';
78                 }
79         }
80
81         $GLOBALS['wfpl_basename'] = $basename;
82
83         $html_file = "$basename.html";
84         $php_file = "$basename.php";
85
86         $html_exists = file_exists($html_file);
87         $php_exists = file_exists($php_file);
88
89         if(file_exists('template.html')) {
90                 $GLOBALS['wfpl_main_template'] = new tem();
91                 $GLOBALS['wfpl_main_template']->load("template.html");
92                 $GLOBALS['wfpl_main_template']->set('$basename', $basename);
93         }
94
95         # cms_get can return one of:
96         # 1) true to indicate that there is cms content (so no 404)
97         # 2) false to indicate that there's no cms content for this basename
98         # 3) a string to indicate a soft/full redirect just as foo_main()
99         if(function_exists('cms_display')) {
100                 $cms_content = cms_display($basename, $GLOBALS['wfpl_main_template']);
101                 if(is_string($cms_content)) {
102                         run_php($cms_content);
103                         return;
104                 }
105         } else {
106                 $cms_content = false;
107         }
108
109         if(!$php_exists && !$html_exists && !$cms_content) {
110                 header('HTTP/1.0 404 File Not Found');
111                 if(file_exists('error_404.php') || file_exists('error_404.html')) {
112                         $GLOBALS['error_basename'] = $basename;
113                         run_php('error_404');
114                         return;
115                 } else {
116                         echo '<!DOCTYPE html><html><head><title>404</title></head><body><h1>404 File Not Found</h1></body></html>';
117                         exit();
118                 }
119         }
120
121         # If there's no template.html we don't want to parse $html_file.
122         if($html_exists && !$php_exists && !file_exists('template.html')) {
123                 readfile($html_file);
124                 exit();
125         }
126
127         if($html_exists) {
128                 tem_load_new($html_file);
129         }
130
131         if($php_exists) {
132                 # files can return a basename or URL of a page to be run/displayed
133                 $other = file_run($php_file);
134                 if($other) {
135                         run_php($other);
136                         return;
137                 }
138         } elseif($html_exists) {
139                 $sub_names = tem_top_sub_names();
140                 foreach($sub_names as $sub_name) {
141                         tem_sub($sub_name);
142                 }
143         }
144
145         # Check for $GLOBALS['wfpl_template'] because it might have been set (or unset) by the php script.
146         if($GLOBALS['wfpl_template'] || $GLOBALS['wfpl_main_template']) {
147                 if($GLOBALS['wfpl_main_template']) {
148                         # if there was a template for that page, and one for the whole
149                         # site, copy all template sections that have been show()n to the
150                         # site-wide template
151                         if($GLOBALS['wfpl_template']) {
152                                 $GLOBALS['wfpl_main_template']->merge($GLOBALS['wfpl_template']);
153                         }
154
155                         $GLOBALS['wfpl_template'] = $GLOBALS['wfpl_main_template'];
156                 }
157
158
159                 # You'll probably want to require_once(__DIR__.'/'.'code/wfpl/messages.php') or
160                 # require_once(__DIR__.'/'.'code/wfpl/session_messages.php') in code/config.php
161                 if(function_exists('display_messages')) {
162                         if(function_exists('atexit_now')) {
163                                 atexit_now();
164                         }
165                         display_messages();
166                 }
167
168                 tem_output();
169         }
170 }
171
172 run_php();
173
174 ?>