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); # apache 2.4.17
53 $basename = preg_replace('|.*/|', '', $basename);
54 $basename = preg_replace('|\.html?$|', '', $basename);
60 $GLOBALS['wfpl_basename'] = $basename;
62 $html_file = "$basename.html";
63 $php_file = "$basename.php";
65 $html_exists = file_exists($html_file);
66 $php_exists = file_exists($php_file);
68 if(file_exists('template.html')) {
69 $GLOBALS['wfpl_main_template'] = new tem();
70 $GLOBALS['wfpl_main_template']->load("template.html");
71 $GLOBALS['wfpl_main_template']->set('$basename', $basename);
74 # cms_get can return one of:
75 # 1) true to indicate that there is cms content (so no 404)
76 # 2) false to indicate that there's no cms content for this basename
77 # 3) a string to indicate a soft/full redirect just as foo_main()
78 if(function_exists('cms_display')) {
79 $cms_content = cms_display($basename, $GLOBALS['wfpl_main_template']);
80 if(is_string($cms_content)) {
81 wfpl_main($cms_content);
88 if(!$php_exists && !$html_exists && !$cms_content) {
89 header('HTTP/1.0 404 File Not Found');
90 if(file_exists('error_404.php') || file_exists('error_404.html')) {
91 $GLOBALS['error_basename'] = $basename;
92 wfpl_main('error_404');
95 echo '<!DOCTYPE html><html><head><title>404</title></head><body><h1>404 File Not Found</h1></body></html>';
100 # If there's no template.html we don't want to parse $html_file.
101 if($html_exists && !$php_exists && !file_exists('template.html')) {
102 readfile($html_file);
107 tem_load_new($html_file);
111 # files can return a basename or URL of a page to be run/displayed
112 $other = file_run($php_file);
117 } elseif($html_exists) {
118 $sub_names = tem_top_sub_names();
119 foreach($sub_names as $sub_name) {
124 # Check for $GLOBALS['wfpl_template'] because it might have been set (or unset) by the php script.
125 if(isset($GLOBALS['wfpl_template']) || isset($GLOBALS['wfpl_main_template'])) {
126 if(isset($GLOBALS['wfpl_main_template'])) {
127 # if there was a template for that page, and one for the whole
128 # site, copy all template sections that have been show()n to the
130 if(isset($GLOBALS['wfpl_template'])) {
131 $GLOBALS['wfpl_main_template']->merge($GLOBALS['wfpl_template']);
134 $GLOBALS['wfpl_template'] = $GLOBALS['wfpl_main_template'];
138 # You'll probably want to require_once(__DIR__.'/'.'lib/wfpl/messages.php') or
139 # require_once(__DIR__.'/'.'lib/wfpl/session_messages.php') in config.php
140 if(function_exists('display_messages')) {
141 if(function_exists('atexit_now')) {