Javascript/ajax code reorganizations
[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 // FIX: fatal errors ?
15
16 function error_handler($errno, $errstr, $file, $line) {
17     global $messages;
18
19         // error_reporting==0 when messages are set off with @ 
20         if ($errno & error_reporting())
21                 $messages[] = array($errno, $errstr, $file, $line);
22
23     return true;
24 }
25 //------------------------------------------------------------------------------
26 //      Formats system messages before insert them into message <div>
27 // FIX center is unused now
28 function fmt_errors($center=true) {
29     global $messages, $msg_colors;
30   
31   $type = E_USER_NOTICE;
32   
33   if (count($messages)) {
34         $content = '';
35         foreach($messages as $msg) {
36                 if ($msg[0]>$type) continue;
37                 if ($msg[0]<$type && $type>E_USER_ERROR) {
38                         $content = ''; // clean notices when we have errors
39                         $type = E_USER_ERROR; // php or user errors 
40                 }
41             $str = $msg[1];
42                 $c = $msg_colors[$type];
43                 if ($msg[0]<E_USER_ERROR && $msg[2]!=null)
44                   $str .= ' '._('in file').': '.$msg[2].' '._('at line ').$msg[3];
45                 $content .= "<tr><td  " . ($center?"align='center' ":"").
46                   " width='100%' bgcolor='{$c['bg']}'><font color='{$c['txt']}'>"
47                   .$str."</font></td></tr>";
48         }
49
50         $str = "<center><table border='1' cellpadding='3' cellspacing='0' style='border-collapse: collapse' bordercolor='{$c['bd']}' width='98%'>"
51           . $content . "</table></center><br>\n";       
52                 
53   }
54      else
55         $str = '';
56         
57     return $str;
58 }
59 //-----------------------------------------------------------------------------
60 // Error box <div> element.
61 //
62 function error_box() {
63     global $before_box;
64     
65     echo "<div id='msgbox'>";
66     $before_box = ob_get_clean(); // save html content before error box 
67 // Necessary restart instead of get_contents/clean calls due to a bug in php 4.3.2
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                 . "<br><a href='javascript:history.go(-1)'>" . _("Back") . "</a>", true);
106                 return true;
107         }
108         
109         return false;
110 }
111
112 function check_db_error($msg, $sql_statement, $exit_if_error=true, $rollback_if_error=true)
113 {
114         global $db;
115         $db_error = db_error_no();
116         
117         if ($db_error != 0) 
118         {
119                 
120                 if (!frindly_db_error($db_error))
121                         display_db_error($msg, $sql_statement, false);
122                 
123                 if ($rollback_if_error) 
124                 {
125                   $rollback_result = db_query("rollback","could not rollback");                 
126                 }
127                 
128                 if ($exit_if_error) 
129                 {
130                         echo "<br><br>";
131                         exit;
132                 }
133         }
134         return $db_error;               
135 }
136
137 ?>