5 # This file was placed into the public domain on November 16th, 2008 by it's
6 # sole author Jason Woofenden
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.
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:
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 #--------+--------------------+--------------#
24 # .html | run php file | display 404 #
26 #--------------------------------------------#
30 # To activate this script in a directory, you'll need to:
32 # 1) make a symbolic link to (or copy of) this file in your directory. and
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
41 # RewriteRule ^$ /foo/run.php
42 # RewriteRule ^foo/[^/]*\.html$ /foo/run.php
44 require_once('code/wfpl/file_run.php');
45 require_once('code/wfpl/http.php');
46 require_once('code/wfpl/template.php');
48 if(file_exists('code/config.php')) {
49 file_run('code/config.php');
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) {
57 # if it's got a : it must be a full URL, redirect
58 if(strpos($dest, ':')) {
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()));
69 # otherwise, it's a normal basename, display that content
72 } else { # no dest arg
73 $basename = $_SERVER['REDIRECT_URL'];
74 $basename = ereg_replace('.*/', '', $basename);
75 $basename = ereg_replace('\.html$', '', $basename);
81 $html_file = "$basename.html";
82 $php_file = "$basename.php";
84 $html_exists = file_exists($html_file);
85 $php_exists = file_exists($php_file);
87 # cms_get can return one of:
88 # 1) false to indicate that there's no cms content for this basename
89 # 2) a string to indicate a soft/full redirect just as foo_main()
90 # 3) a hash of key/value pairs to be tem_set(key,value) on the template
91 if(function_exists('cms_get')) {
92 $cms_content = cms_get($basename);
93 if(is_string($cms_content)) {
94 run_php($cms_content);
101 if(!$php_exists && !$html_exists && !$cms_content) {
102 header('HTTP/1.0 404 File Not Found');
103 if(file_exists('404.php') || file_exists('404.html')) {
107 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>';
111 # If there's no template.html we don't want to parse $html_file.
112 if($html_exists && !$php_exists && !file_exists('template.html')) {
113 readfile($html_file);
118 tem_load_new($html_file);
122 # files can return a basename or URL of a page to be run/displayed
123 $other = file_run($php_file);
129 $sub_names = tem_top_sub_names();
130 foreach($sub_names as $sub_name) {
135 # Check for $GLOBALS['wfpl_template'] because it might have been set (or unset) by the php script.
136 if($GLOBALS['wfpl_template']) {
137 if(file_exists('template.html')) {
139 $tem->load("template.html");
140 $tem->set('basename', $basename);
141 if($cms_content) foreach($cms_content as $name => $val) {
142 $tem->append($name, $val);
144 $sections = tem_top_subs();
145 if($sections) foreach($sections as $name => $val) {
146 $tem->append($name, $val);
149 if(file_exists("$basename.css")) {
150 $tem->set('css_link', "$basename.css");
151 $tem->sub('css_links');
154 $GLOBALS['wfpl_template'] = $tem;
157 if(function_exists('display_messages')) {