1 <?php # Transitional; supports both template APIs
3 # This program is in the public domain within the United States. Additionally,
4 # we waive copyright and related rights in the work worldwide through the CC0
5 # 1.0 Universal public domain dedication, which can be found at
6 # http://creativecommons.org/publicdomain/zero/1.0/
9 # This file is meant to run for every page view. It finds PHP and HTML files
10 # that match the basename of the url, and loads/executes them. It also allows
11 # php files to specify redirects (both "soft" redirects which makes the server
12 # act as if a different url was requested, and real HTTP redirects).
16 # 1. Make config.php in your main directory with contents like:
18 # require_once(__DIR__.'/'.'inc/wfpl/run.php');
21 # 2. Include the following in your .htaccess file:
24 # RewriteRule ^[^/.]*$ /config.php
26 require_once(__DIR__.'/'.'file_run.php');
27 require_once(__DIR__.'/'.'http.php');
28 require_once(__DIR__.'/'.'template.php');
30 # pass the basename of the page you want for normal execution
31 # pass ./page.html to redirect to page.html in this directory
32 # pass http://foo.com/bar.html to redirect to a full directory
33 function wfpl_main($dest = false) {
35 # if it has a : it must be a full URL, redirect
36 if(strpos($dest, ':')) {
41 # if it starts with './' then it's a relative URL, redirect
42 if(substr($dest, 0, 2) == './') {
43 redirect(preg_replace('|/[^/]*$|', substr($dest, 1), this_url()));
47 # otherwise, it's a normal basename, display that content
50 } else { # no dest arg
51 $basename = $_SERVER['REDIRECT_URL'];
52 $basename = preg_replace('|.*/|', '', $basename);
53 $basename = preg_replace('|\.html?$|', '', $basename);
59 $GLOBALS['wfpl_basename'] = $basename;
61 $html_file = "$basename.html";
62 $php_file = "$basename.php";
64 $html_exists = file_exists($html_file);
65 $php_exists = file_exists($php_file);
67 if(file_exists('template.html')) {
68 $GLOBALS['wfpl_main_template'] = new tem();
69 $GLOBALS['wfpl_main_template']->load("template.html");
70 $GLOBALS['wfpl_main_template']->set('$basename', $basename);
73 # cms_get can return one of:
74 # 1) true to indicate that there is cms content (so no 404)
75 # 2) false to indicate that there's no cms content for this basename
76 # 3) a string to indicate a soft/full redirect just as foo_main()
77 if(function_exists('cms_display')) {
78 $cms_content = cms_display($basename, $GLOBALS['wfpl_main_template']);
79 if(is_string($cms_content)) {
80 wfpl_main($cms_content);
87 if(!$php_exists && !$html_exists && !$cms_content) {
88 header('HTTP/1.0 404 File Not Found');
89 if(file_exists('error_404.php') || file_exists('error_404.html')) {
90 $GLOBALS['error_basename'] = $basename;
91 wfpl_main('error_404');
94 echo '<!DOCTYPE html><html><head><title>404</title></head><body><h1>404 File Not Found</h1></body></html>';
99 # If there's no template.html we don't want to parse $html_file.
100 if($html_exists && !$php_exists && !file_exists('template.html')) {
101 readfile($html_file);
106 tem_load_new($html_file);
110 # files can return a basename or URL of a page to be run/displayed
111 $other = file_run($php_file);
116 } elseif($html_exists) {
117 $sub_names = tem_top_sub_names();
118 foreach($sub_names as $sub_name) {
123 # Check for $GLOBALS['wfpl_template'] because it might have been set (or unset) by the php script.
124 if(isset($GLOBALS['wfpl_template']) || isset($GLOBALS['wfpl_main_template'])) {
125 if(isset($GLOBALS['wfpl_main_template'])) {
126 # if there was a template for that page, and one for the whole
127 # site, copy all template sections that have been show()n to the
129 if(isset($GLOBALS['wfpl_template'])) {
130 $GLOBALS['wfpl_main_template']->merge($GLOBALS['wfpl_template']);
133 $GLOBALS['wfpl_template'] = $GLOBALS['wfpl_main_template'];
137 # You'll probably want to require_once(__DIR__.'/'.'lib/wfpl/messages.php') or
138 # require_once(__DIR__.'/'.'lib/wfpl/session_messages.php') in config.php
139 if(function_exists('display_messages')) {
140 if(function_exists('atexit_now')) {