JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
delete string_array.php (buggy, stupid)
[wfpl.git] / main.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, so that if you need similar behavior, you can
7 # copy this file into your website, and change it as needed.
8
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).
13
14 # INSTRUCTIONS
15 #
16 # 1.    Make config.php in your main directory with contents like:
17 #
18 #       require_once(__DIR__.'/'.'inc/wfpl/run.php');
19 #       wfpl_main();
20 #
21 # 2.    Include the following in your .htaccess file:
22 #
23 #       RewriteEngine  on
24 #       RewriteRule    ^[^/.]*$  /config.php
25
26 require_once(__DIR__.'/'.'file_run.php');
27 require_once(__DIR__.'/'.'http.php');
28 require_once(__DIR__.'/'.'template.php');
29
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) {
34         if($dest) {
35                 # if it has a : it must be a full URL, redirect
36                 if(strpos($dest, ':')) {
37                         redirect($dest);
38                         exit();
39                 }
40
41                 # if it starts with './' then it's a relative URL, redirect
42                 if(substr($dest, 0, 2) == './') {
43                         redirect(ereg_replace('/[^/]*$', substr($dest, 1), this_url()));
44                         exit();
45                 }
46
47                 # otherwise, it's a normal basename, display that content
48                 $basename = $dest;
49
50         } else { # no dest arg
51                 $basename = $_SERVER['REDIRECT_URL'];
52                 $basename = ereg_replace('.*/', '', $basename);
53                 $basename = ereg_replace('\.html?$', '', $basename);
54                 if($basename == '') {
55                         $basename = 'index';
56                 }
57         }
58
59         $GLOBALS['wfpl_basename'] = $basename;
60
61         $html_file = "$basename.html";
62         $php_file = "$basename.php";
63
64         $html_exists = file_exists($html_file);
65         $php_exists = file_exists($php_file);
66
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);
71         }
72
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);
81                         return;
82                 }
83         } else {
84                 $cms_content = false;
85         }
86
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');
92                         return;
93                 } else {
94                         echo '<!DOCTYPE html><html><head><title>404</title></head><body><h1>404 File Not Found</h1></body></html>';
95                         exit();
96                 }
97         }
98
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);
102                 exit();
103         }
104
105         if($html_exists) {
106                 tem_load_new($html_file);
107         }
108
109         if($php_exists) {
110                 # files can return a basename or URL of a page to be run/displayed
111                 $other = file_run($php_file);
112                 if($other) {
113                         wfpl_main($other);
114                         return;
115                 }
116         } elseif($html_exists) {
117                 $sub_names = tem_top_sub_names();
118                 foreach($sub_names as $sub_name) {
119                         tem_sub($sub_name);
120                 }
121         }
122
123         # Check for $GLOBALS['wfpl_template'] because it might have been set (or unset) by the php script.
124         if($GLOBALS['wfpl_template'] || $GLOBALS['wfpl_main_template']) {
125                 if($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
128                         # site-wide template
129                         if($GLOBALS['wfpl_template']) {
130                                 $GLOBALS['wfpl_main_template']->merge($GLOBALS['wfpl_template']);
131                         }
132
133                         $GLOBALS['wfpl_template'] = $GLOBALS['wfpl_main_template'];
134                 }
135
136
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')) {
141                                 atexit_now();
142                         }
143                         display_messages();
144                 }
145
146                 tem_output();
147         }
148 }