JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
API CHANGE s/run_php/wfpl_main/
[wfpl.git] / main.php
diff --git a/main.php b/main.php
new file mode 100644 (file)
index 0000000..e088d67
--- /dev/null
+++ b/main.php
@@ -0,0 +1,148 @@
+<?php  # Transitional; supports both template APIs
+
+# 2006 Public Domain
+#
+# This file was placed into the public domain on November 16th, 2008 by its
+# sole author Jason Woofenden, so that if you need similar behavior, you can
+# copy this file into your website, and change it as needed.
+
+# This file is meant to run for every page view. It finds PHP and HTML files
+# that match the basename of the url, and loads/executes them. It also allows
+# php files to specify redirects (both "soft" redirects which makes the server
+# act as if a different url was requested, and real HTTP redirects).
+
+# INSTRUCTIONS
+#
+# 1.   Make config.php in your main directory with contents like:
+#
+#      require_once(__DIR__.'/'.'inc/wfpl/run.php');
+#      wfpl_main();
+#
+# 2.   Include the following in your .htaccess file:
+#
+#      RewriteEngine  on
+#      RewriteRule    ^[^/.]*$  /config.php
+
+require_once(__DIR__.'/'.'file_run.php');
+require_once(__DIR__.'/'.'http.php');
+require_once(__DIR__.'/'.'template.php');
+
+# pass the basename of the page you want for normal execution
+# pass ./page.html to redirect to page.html in this directory
+# pass http://foo.com/bar.html to redirect to a full directory
+function wfpl_main($dest = false) {
+       if($dest) {
+               # if it has a : it must be a full URL, redirect
+               if(strpos($dest, ':')) {
+                       redirect($dest);
+                       exit();
+               }
+
+               # if it starts with './' then it's a relative URL, redirect
+               if(substr($dest, 0, 2) == './') {
+                       redirect(ereg_replace('/[^/]*$', substr($dest, 1), this_url()));
+                       exit();
+               }
+
+               # otherwise, it's a normal basename, display that content
+               $basename = $dest;
+
+       } else { # no dest arg
+               $basename = $_SERVER['REDIRECT_URL'];
+               $basename = ereg_replace('.*/', '', $basename);
+               $basename = ereg_replace('\.html?$', '', $basename);
+               if($basename == '') {
+                       $basename = 'index';
+               }
+       }
+
+       $GLOBALS['wfpl_basename'] = $basename;
+
+       $html_file = "$basename.html";
+       $php_file = "$basename.php";
+
+       $html_exists = file_exists($html_file);
+       $php_exists = file_exists($php_file);
+
+       if(file_exists('template.html')) {
+               $GLOBALS['wfpl_main_template'] = new tem();
+               $GLOBALS['wfpl_main_template']->load("template.html");
+               $GLOBALS['wfpl_main_template']->set('$basename', $basename);
+       }
+
+       # cms_get can return one of:
+       # 1) true to indicate that there is cms content (so no 404)
+       # 2) false to indicate that there's no cms content for this basename
+       # 3) a string to indicate a soft/full redirect just as foo_main()
+       if(function_exists('cms_display')) {
+               $cms_content = cms_display($basename, $GLOBALS['wfpl_main_template']);
+               if(is_string($cms_content)) {
+                       run_php($cms_content);
+                       return;
+               }
+       } else {
+               $cms_content = false;
+       }
+
+       if(!$php_exists && !$html_exists && !$cms_content) {
+               header('HTTP/1.0 404 File Not Found');
+               if(file_exists('error_404.php') || file_exists('error_404.html')) {
+                       $GLOBALS['error_basename'] = $basename;
+                       run_php('error_404');
+                       return;
+               } else {
+                       echo '<!DOCTYPE html><html><head><title>404</title></head><body><h1>404 File Not Found</h1></body></html>';
+                       exit();
+               }
+       }
+
+       # If there's no template.html we don't want to parse $html_file.
+       if($html_exists && !$php_exists && !file_exists('template.html')) {
+               readfile($html_file);
+               exit();
+       }
+
+       if($html_exists) {
+               tem_load_new($html_file);
+       }
+
+       if($php_exists) {
+               # files can return a basename or URL of a page to be run/displayed
+               $other = file_run($php_file);
+               if($other) {
+                       run_php($other);
+                       return;
+               }
+       } elseif($html_exists) {
+               $sub_names = tem_top_sub_names();
+               foreach($sub_names as $sub_name) {
+                       tem_sub($sub_name);
+               }
+       }
+
+       # Check for $GLOBALS['wfpl_template'] because it might have been set (or unset) by the php script.
+       if($GLOBALS['wfpl_template'] || $GLOBALS['wfpl_main_template']) {
+               if($GLOBALS['wfpl_main_template']) {
+                       # if there was a template for that page, and one for the whole
+                       # site, copy all template sections that have been show()n to the
+                       # site-wide template
+                       if($GLOBALS['wfpl_template']) {
+                               $GLOBALS['wfpl_main_template']->merge($GLOBALS['wfpl_template']);
+                       }
+
+                       $GLOBALS['wfpl_template'] = $GLOBALS['wfpl_main_template'];
+               }
+
+
+               # You'll probably want to require_once(__DIR__.'/'.'code/wfpl/messages.php') or
+               # require_once(__DIR__.'/'.'code/wfpl/session_messages.php') in code/config.php
+               if(function_exists('display_messages')) {
+                       if(function_exists('atexit_now')) {
+                               atexit_now();
+                       }
+                       display_messages();
+               }
+
+               tem_output();
+       }
+}