Fixed fatal error handling for php5.
[fa-stable.git] / includes / errors.inc
1 <?php
2
3 $messages = array(); // container for system messages
4 $before_box = ''; // temporary container for output html data before error box
5
6 $msg_colors = array( 
7         E_USER_ERROR => array('bg'=>'#ffcccc', 'txt'=>'#dd2200', 'bd'=>'#cc3300'),
8     E_USER_WARNING => array('bg'=>'#ffcccc', 'txt'=>'#dd2200', 'bd'=>'#cc3300'),
9     E_USER_NOTICE => array('bg'=>'#ccffcc', 'txt'=>'#007700', 'bd'=>'#33cc00'));
10
11 //-----------------------------------------------------------------------------
12 //    Error handler - collects all php/user messages for
13 //    display in message box.
14
15 function error_handler($errno, $errstr, $file, $line) {
16     global $messages;
17
18         // error_reporting==0 when messages are set off with @ 
19         if ($errno & error_reporting())
20                 $messages[] = array($errno, $errstr, $file, $line);
21
22     return true;
23 }
24 //------------------------------------------------------------------------------
25 //      Formats system messages before insert them into message <div>
26 // FIX center is unused now
27 function fmt_errors($center=true) {
28     global $messages, $msg_colors;
29   
30   $type = E_USER_NOTICE;
31   
32   if (count($messages)) {
33         $content = '';
34         foreach($messages as $msg) {
35                 if ($msg[0]>$type) continue;
36                 if ($msg[0]<$type && $type>E_USER_ERROR) {
37                         $content = ''; // clean notices when we have errors
38                         $type = E_USER_ERROR; // php or user errors 
39                 }
40             $str = $msg[1];
41                 $c = $msg_colors[$type];
42                 if ($msg[0]<E_USER_ERROR && $msg[2]!=null)
43                   $str .= ' '._('in file').': '.$msg[2].' '._('at line ').$msg[3];
44                 $content .= "<tr><td  " . ($center?"align='center' ":"").
45                   " width='100%' bgcolor='{$c['bg']}'><font color='{$c['txt']}'>"
46                   .$str."</font></td></tr>";
47         }
48
49         $str = "<center><table border='1' cellpadding='3' cellspacing='0' style='border-collapse: collapse' bordercolor='{$c['bd']}' width='98%'>"
50           . $content . "</table></center><br>\n";       
51                 
52   }
53      else
54         $str = '';
55         
56     return $str;
57 }
58 //-----------------------------------------------------------------------------
59 // Error box <div> element.
60 //
61 function error_box() {
62     global $before_box;
63     
64     echo "<div id='msgbox'>";
65     $before_box = ob_get_clean(); // save html content before error box 
66 // Necessary restart instead of get_contents/clean calls due to a bug in php 4.3.2
67         register_shutdown_function('ob_end_flush');
68     ob_start('output_html');
69     echo "</div>";
70 }
71
72 function display_db_error($msg, $sql_statement=null, $exit=true)
73 {
74         global $db, $debug;
75         $db_error = db_error_no();
76         
77         $str = "<span class='errortext'><b>" . _("DATABASE ERROR :") . "</b> $msg</span><br>";
78         
79         if ($db_error != 0) 
80         {
81                 $str .= "error code : " . $db_error . "<br>";
82                 $str .= "error message : " . db_error_msg($db) . "<br>";
83         }
84         
85         if ($debug == 1) 
86         {
87                 $str .= "sql that failed was : " . $sql_statement . "<br>";
88         }
89         
90         $str .= "<br><br>";
91
92         trigger_error($str, E_USER_ERROR);
93
94         if ($exit)
95                 exit;
96 }
97
98 function frindly_db_error($db_error)
99 {
100         global $db_duplicate_error_code;
101         
102         if ($db_error == $db_duplicate_error_code) 
103         {
104                 display_error(_("The entered information is a duplicate. Please go back and enter different values."));
105                 return true;
106         }
107         
108         return false;
109 }
110
111 function check_db_error($msg, $sql_statement, $exit_if_error=true, $rollback_if_error=true)
112 {
113         global $db;
114         $db_error = db_error_no();
115         
116         if ($db_error != 0) 
117         {
118                 
119                 if (!frindly_db_error($db_error))
120                         display_db_error($msg, $sql_statement, false);
121                 
122                 if ($rollback_if_error) 
123                 {
124                   $rollback_result = db_query("rollback","could not rollback");                 
125                 }
126                 
127                 if ($exit_if_error) 
128                 {
129                         echo "<br><br>";
130                         exit;
131                 }
132         }
133         return $db_error;               
134 }
135
136 ?>