| [ Index ] |
PHP Cross Reference of Web Application Component Toolkit |
[Summary view] [Print] [Text view]
1 <?php 2 //-------------------------------------------------------------------------------- 3 // Copyright 2003 Procata, Inc. 4 // Released under the LGPL license (http://www.gnu.org/copyleft/lesser.html) 5 //-------------------------------------------------------------------------------- 6 /** 7 * @package WACT_UTIL 8 * @version $Id: debug.inc.php,v 1.2 2004/10/16 20:43:49 jeffmoore Exp $ 9 * @TODO Change to use a template 10 */ 11 require_once WACT_ROOT . 'error/error.inc.php'; 12 /** 13 * Include template engine (currently used only for importVarFile()) 14 */ 15 require_once WACT_ROOT . 'template/template.inc.php'; 16 /** 17 * Current error object placed here, for sharing by HandleFrameworkError and 18 * BareBonesErrorHandler 19 */ 20 $GLOBALS['CurrentErrorObject'] = NULL; 21 /** 22 * Putting high-level operations in an error handler leads to the dreaded 23 * recursive error problem that is the bane of non exception based error handling. 24 * Yet its nice to allow error message pages to have the look and feel of the rest 25 * of the site. 26 * that said, this function directly outputs HTML, and instead should probably 27 * format its output using a template. That way, the template can be customized 28 * to match the look at feel of the site using the framework. 29 * The price of this is by using a template, there is an increased risk of a new 30 * error occurring while handling an error. A bad situation. 31 */ 32 function HandleError($ErrorNumber, $ErrorMessage, $FileName, $LineNumber) { 33 // Handle framework errors 34 if ($ErrorNumber & E_USER_ERROR) { 35 HandleFrameworkError($ErrorMessage); 36 } else { 37 HandleGeneralError($ErrorNumber, $ErrorMessage, $FileName, $LineNumber); 38 } 39 } 40 41 /** 42 * Handles WACT specific framework errors. Currently "hacked" to disable 43 * multiple generation of error messages with compileall. BareBonesErrorHandler 44 * should be declared before calling importVarFile 45 * @TODO fix hack on BareBonesErrorHandler (related to compileall) 46 */ 47 function HandleFrameworkError($ErrorMessage) { 48 $Error = @unserialize($ErrorMessage); 49 if (is_object($Error)) { 50 $GLOBALS['CurrentErrorObject'] =& $Error; 51 52 if ( !defined('WACT_ERROR_CONTINUE') ) { 53 $OldHandler = set_error_handler('BareBonesErrorHandler'); 54 } 55 56 $Group = $Error->group; 57 $MessageList = importVarFile("/errormessages/$Group.vars"); 58 $ErrorMessage = $MessageList[$Error->id]; 59 60 if ( defined('WACT_ERROR_CONTINUE') ) { 61 $OldHandler = set_error_handler('BareBonesErrorHandler'); 62 } 63 64 foreach($Error->info as $key => $replacement) { 65 $ErrorMessage = str_replace('{' . $key . '}', $replacement, $ErrorMessage); 66 } 67 68 echo "<br><hr>\n"; 69 echo "<h3>Error:</h3>$ErrorMessage\n"; 70 echo "<br><hr>\n"; 71 72 echo "<ul>"; 73 DisplayTraceBack(); 74 echo "</ul>"; 75 76 if ( !defined('WACT_ERROR_CONTINUE') ) { 77 exit; 78 } 79 } else { 80 // avoid silently swallowing this error if there is a problem. 81 echo "Could not unserialize object for framework error<BR>"; 82 echo "Probably because message string exceeded 1023 byte charater limit<BR>"; 83 exit; 84 // we should do something smarter than this here. 85 } 86 } 87 88 function DisplayTraceBack() { 89 // based on PHP manual page for debug_backtrace() 90 if (version_compare(PHPVERSION(), '4.3', '>=')) { 91 $Trace = debug_backtrace(); 92 93 foreach ($Trace as $line) { 94 95 if (in_array($line['function'], array( 96 'raiseerror', 'raiseerrorhandler', 'trigger_error', 'errorhandlerdispatch', 97 'handleerror', 'handleframeworkerror', 'displaytraceback' ))) { 98 continue; 99 } 100 101 echo '<li>'; 102 echo '<font face="Courier New,Courier"><B>'; 103 if (isset($line['class'])) { 104 echo $line['class']; 105 echo "."; 106 } 107 echo $line['function']; 108 echo "("; 109 if (isset($line['args'])) { 110 $sep = ''; 111 foreach ($line['args'] as $arg) { 112 echo $sep; 113 $sep = ', '; 114 115 if (is_null($arg)) { 116 echo 'NULL'; 117 } else if (is_array($arg)) { 118 echo 'ARRAY[' . sizeof($arg) . ']'; 119 } else if (is_object($arg)) { 120 echo 'OBJECT:' . get_class($arg); 121 } else if (is_bool($arg)) { 122 echo $arg ? 'TRUE' : 'FALSE'; 123 } else { 124 echo '"'; 125 echo htmlspecialchars(substr((string) @$arg, 0, 32)); 126 if (strlen($arg) > 32) { 127 echo '...'; 128 } 129 echo '"'; 130 } 131 } 132 } 133 echo ")"; 134 echo "</b><br>\n"; 135 if (isset($line['file'])) { 136 echo $line['file']; 137 echo " line <b>"; 138 echo $line['line']; 139 echo '</b></font>'; 140 } 141 } 142 } 143 } 144 145 /** 146 * Handles general erros (e.g. PHP errors) 147 */ 148 function HandleGeneralError($ErrorNumber, $ErrorMessage, $FileName, $LineNumber) { 149 $ErrorTime = date("Y-m-d H:i:s (T)"); 150 151 // define an assoc array of error string 152 // in reality the only entries we should 153 // consider are 2,8,256,512 and 1024 154 $ErrorType = array ( 155 1 => "Error", 156 2 => "Warning", 157 4 => "Parsing Error", 158 8 => "Notice", 159 16 => "Core Error", 160 32 => "Core Warning", 161 64 => "Compile Error", 162 128 => "Compile Warning", 163 256 => "User Error", 164 512 => "User Warning", 165 1024=> "User Notice" 166 ); 167 168 if ($ErrorNumber & (E_NOTICE | E_USER_NOTICE)) { 169 $Level = 'NOTICE'; 170 } else if ($ErrorNumber & (E_WARNING | E_USER_WARNING | E_CORE_WARNING | E_COMPILE_WARNING)) { 171 $Level = 'WARNING'; 172 } else if ($ErrorNumber & (E_ERROR | E_PARSE | E_CORE_ERROR | E_COMPILE_ERROR | E_USER_ERROR)) { 173 $Level = 'ERROR'; 174 } else { 175 $Level = 'Unknown'; 176 } 177 178 echo "<br><hr>\n"; 179 echo "<h3>$Level:</h3>$ErrorMessage\n"; 180 echo "<ul><li>"; 181 echo '<font face="Courier New,Courier">'; 182 echo "$FileName line $LineNumber\n"; 183 184 DisplayTraceBack(); 185 186 echo "</ul>"; 187 echo "<hr>\n"; 188 if ( !defined('WACT_ERROR_CONTINUE') ) { 189 exit; 190 } 191 } 192 193 /** 194 * This function is called only when an error has occured in the error handler. 195 * Usually this is because there is a problem accessing the template system. 196 * Here we heroicly attempt to print out something intelligable for the original error. 197 */ 198 function BareBonesErrorHandler($ErrorNumber, $ErrorMessage, $FileName, $LineNumber) { 199 $Error =& $GLOBALS['CurrentErrorObject']; 200 201 $filename = WACT_ROOT . 'default/errormessages/' . $Error->group . '.vars'; 202 203 $MessageList = array(); 204 205 if (FALSE !== ($RawLines = file($filename)) ) { 206 while (list(,$Line) = each($RawLines)) { 207 $EqualPos = strpos($Line, '='); 208 if ($EqualPos === FALSE) { 209 $MessageList[trim($Line)] = NULL; 210 } else { 211 $Key = trim(substr($Line, 0, $EqualPos)); 212 if (strlen($Key) > 0) { 213 $MessageList[$Key] = trim(substr($Line, $EqualPos+1)); 214 } 215 } 216 } 217 218 $ErrorMessage = $MessageList[$Error->id]; 219 } else { 220 $ErrorMessage = "An resolvable WACT framework error occured. 221 <br>Group: {$Error->group} 222 <br>ID: {$Error->id} 223 <br>Info: {$Error->info} 224 <br>File: $FileName 225 <br>Line: $LineNumber"; 226 } 227 228 foreach($Error->info as $key => $replacement) { 229 $ErrorMessage = str_replace('{' . $key . '}', $replacement, $ErrorMessage); 230 } 231 232 echo "<br><hr>\n"; 233 echo "<h3>Error:</h3>$ErrorMessage\n"; 234 if ( !defined('WACT_ERROR_CONTINUE') ) { 235 exit; 236 } 237 } 238 ?>
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Sun Nov 28 19:36:09 2004 | Cross-referenced by PHPXref 0.5 |