Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

added full 404 handling logic

  • Loading branch information...
commit 7e32eec5997515f1b48fb1dc740ef98679b3ffa7 1 parent f0fd723
@iamcal iamcal authored Jacques Marneweck committed
View
19 404.php
@@ -1,22 +1,5 @@
<?php
include('include/init.php');
-
- #
- # This is where you redirect users to a different page incase you've moved stuff or you can use .htaccess
- # do this for you.
- #
-
- if ($_SERVER['REQUEST_URI'] == '/home'){
-
- header("Location: /");
- exit;
- }
-
-
- #
- # output
- #
-
- $smarty->display('page_404.txt');
+ error_404();
?>
View
2  include/init.php
@@ -98,7 +98,7 @@ function handle_error_notices($errno, $errstr){
loadlib('log'); # logging comes first, so that other modules can log during startup
loadlib('smarty'); # smarty comes next, since other libs register smarty modules
- #loadlib('error');
+ loadlib('error');
loadlib('db');
#loadlib('cache');
#loadlib('login');
View
190 include/lib_error.php
@@ -0,0 +1,190 @@
+<?
+ ###############################################################################
+
+ #
+ # we can call this function anywhere from code where we want to make
+ # the current request 404. that can be inside 404.php which gets requests
+ # that apache can't map, or within a page handler that has checked QS
+ # variables and can't find a matching object. or whatever else you want.
+ #
+
+ function error_404($msg=null){
+
+ $url = $_SERVER['REQUEST_URI'];
+ $orig = $_SERVER['REDIRECT_URL'];
+
+
+ #
+ # try adding a slash at the end if:
+ # 1) we've not already mapped it through a RewriteRule
+ # 2) it doesn't look like a filename
+ # 3) it doesn't already have a slash at the end
+ #
+
+ if ($url == $orig){
+ $last_part = array_pop((explode('/', $url)));
+ if (preg_match('!^[^\.]+$!', $last_part)){
+
+ header("location: $url/");
+ exit;
+ }
+ }
+
+
+ #
+ # static redirect map. add things here if you know you moved them.
+ #
+
+ $redirs = array(
+ '/foo/' => '/bar/',
+ );
+
+ if ($redirs[$url]){
+ header("location: $redirs[$url]");
+ exit;
+ }
+
+
+ #
+ # give up
+ #
+
+ $GLOBALS['no_cache'] = 1;
+
+
+ #
+ # build debug block
+ #
+
+ $debug_block = '';
+ if (!is_null($msg)){
+ $debug_block .= "Message:\n";
+ $debug_block .= error_format_pre($msg)."\n\n";
+ }
+
+ $debug_block .= "Args:\n";
+ $args = array(
+ 'SERVER_REQUEST_URI' => $_SERVER['REQUEST_URI'],
+ 'SERVER_REDIRECT_URL' => $_SERVER['REDIRECT_URL'],
+ );
+ $debug_block .= error_format_hash($args)."\n\n";
+
+ $debug_block .= "Backtrace:\n";
+ $debug_block .= error_format_indent(error_smart_trace());
+
+ $GLOBALS['smarty']->assign('debug_block', $debug_block);
+
+
+ #
+ # output
+ #
+
+ $GLOBALS['smarty']->display('page_error_404.txt');
+ exit;
+ }
+
+ ###############################################################################
+
+ function error_smart_trace(){
+
+ $root_path = realpath(dirname(__FILE__)."/..");
+
+ $trace = debug_backtrace();
+ $pairs = array();
+
+ foreach ($trace as $item){
+
+ $function = "$item[function]($args)";
+
+ if (preg_match('!^error_!', $item['function'])){
+ $pairs = array();
+ $function = "ERROR";
+ }
+
+
+ $file = str_replace($root_path, '', $item['file']);
+
+ $args = array();
+ foreach ($item['args'] as $arg){
+ if (is_object($arg)){
+ $args[] = "Object()";
+ }else{
+ $args[] = "$arg"; # this will just string-ify the arg
+ }
+ }
+ $args = implode(', ', $args);
+
+ $pairs[] = array(
+ $function,
+ "$file:$item[line]",
+ );
+ }
+
+ return error_format_table($pairs, 4);
+ }
+
+ ###############################################################################
+
+ function error_format_pre($data){
+
+ if (is_string($data)) return error_format_indent($data);
+
+ return error_format_indent(var_export($data, 1));
+ }
+
+ function error_format_indent($data){
+
+ $lines = explode("\n", trim(HtmlSpecialChars($data)));
+
+ $out = '';
+
+ foreach ($lines as $line){
+ $out .= "\t$line\n";
+ }
+
+ return $out;
+ }
+
+ function error_format_table($pairs, $padding=4){
+
+ #
+ # get section lengths
+ #
+
+ $lengths = array();
+ foreach ($pairs as $pair){
+ foreach ($pair as $k => $str){
+ $lengths[$k] = max(intval($lengths[$k]), strlen($str));
+ }
+ }
+
+
+ #
+ # build lines
+ #
+
+ $pad = str_repeat(' ', $padding);
+ $out = '';
+
+ foreach ($pairs as $kp => $pair){
+ foreach ($lengths as $k => $len){
+ $pairs[$kp][$k] = str_pad($pairs[$kp][$k], $lengths[$k], ' ', STR_PAD_RIGHT);
+ }
+ $out .= implode($pad, $pairs[$kp])."\n";
+ }
+
+ return $out;
+ }
+
+ function error_format_hash($hash){
+
+ $pairs = array();
+ foreach ($hash as $k => $v){
+ $pairs[] = array($k, $v);
+ }
+
+ return error_format_indent(error_format_table($pairs));
+ }
+
+ ###############################################################################
+?>
View
11 templates/page_error_404.txt
@@ -1,8 +1,11 @@
-{"HTTP/1.1 404 Not Found"|header}
-{include file='inc_head.txt'}
+{"HTTP/1.1 404 Not Found"|header}{include file='inc_head.txt'}
-<h2>four-oh-four</h2>
+<h1>Page not found</h1>
-<p>{$smarty.server.REQUEST_URI|escape} does not exist.</p>
+<p>We can't find the page you requested. Sorry :(</p>
+
+<div class="admin-section">
+ <pre class="admin-debug">{$debug_block}</pre>
+</div>
{include file='inc_foot.txt'}
Please sign in to comment.
Something went wrong with that request. Please try again.