JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
Fix db_get_value after mysql->mysqli upgrade
[wfpl.git] / main.php
1 <?php  # Transitional; supports both template APIs
2
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/
7
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(preg_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 = preg_replace('|[?].*|', '', $basename); # apache 2.4.17
53                 $basename = preg_replace('|.*/|', '', $basename);
54                 $basename = preg_replace('|\.html?$|', '', $basename);
55                 if($basename == '') {
56                         $basename = 'index';
57                 }
58         }
59
60         $GLOBALS['wfpl_basename'] = $basename;
61
62         $html_file = "$basename.html";
63         $php_file = "$basename.php";
64
65         $html_exists = file_exists($html_file);
66         $php_exists = file_exists($php_file);
67
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);
72         }
73
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);
82                         return;
83                 }
84         } else {
85                 $cms_content = false;
86         }
87
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');
93                         return;
94                 } else {
95                         echo '<!DOCTYPE html><html><head><title>404</title></head><body><h1>404 File Not Found</h1></body></html>';
96                         exit();
97                 }
98         }
99
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);
103                 exit();
104         }
105
106         if($html_exists) {
107                 tem_load_new($html_file);
108         }
109
110         if($php_exists) {
111                 # files can return a basename or URL of a page to be run/displayed
112                 $other = file_run($php_file);
113                 if($other) {
114                         wfpl_main($other);
115                         return;
116                 }
117         } elseif($html_exists) {
118                 $sub_names = tem_top_sub_names();
119                 foreach($sub_names as $sub_name) {
120                         tem_sub($sub_name);
121                 }
122         }
123
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
129                         # site-wide template
130                         if(isset($GLOBALS['wfpl_template'])) {
131                                 $GLOBALS['wfpl_main_template']->merge($GLOBALS['wfpl_template']);
132                         }
133
134                         $GLOBALS['wfpl_template'] = $GLOBALS['wfpl_main_template'];
135                 }
136
137
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')) {
142                                 atexit_now();
143                         }
144                         display_messages();
145                 }
146
147                 tem_output();
148         }
149 }