Changed license type to GPLv3 in top of files
[fa-stable.git] / includes / errors.inc
1 <?php
2 /**********************************************************************
3     Copyright (C) FrontAccounting, LLC.
4         Released under the terms of the GNU General Public License, GPL, 
5         as published by the Free Software Foundation, either version 3 
6         of the License, or (at your option) any later version.
7     This program is distributed in the hope that it will be useful,
8     but WITHOUT ANY WARRANTY; without even the implied warranty of
9     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
10     See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
11 ***********************************************************************/
12 $messages = array(); // container for system messages
13 $before_box = ''; // temporary container for output html data before error box
14
15 $msg_colors = array( 
16         E_USER_ERROR => array('bg'=>'#ffcccc', 'txt'=>'#dd2200', 'bd'=>'#cc3300'),
17     E_USER_WARNING => array('bg'=>'#ffff00', 'txt'=>'#ff5500', 'bd'=>'#ff9900'),
18     E_USER_NOTICE => array('bg'=>'#ccffcc', 'txt'=>'#007700', 'bd'=>'#33cc00'));
19
20 //-----------------------------------------------------------------------------
21 //    Error handler - collects all php/user messages for
22 //    display in message box.
23
24 function error_handler($errno, $errstr, $file, $line) {
25     global $messages;
26
27         // error_reporting==0 when messages are set off with @ 
28         if ($errno & error_reporting()) {
29                 $messages[] = array($errno, $errstr, $file, $line);
30         }
31     return true;
32 }
33 //------------------------------------------------------------------------------
34 //      Formats system messages before insert them into message <div>
35 // FIX center is unused now
36 function fmt_errors($center=true) {
37     global $messages, $msg_colors;
38   
39   $type = E_USER_NOTICE;
40   
41   if (count($messages)) {
42         $content = '';
43         foreach($messages as $msg) {
44                 if ($msg[0]>$type) continue;
45
46                 if ($msg[0]<$type) { 
47                         if ($msg[0] == E_USER_WARNING) {
48                                 $type = E_USER_WARNING; // user warnings 
49                                 $content = '';                  // clean notices when we have errors
50                         } else  {
51                                 $type = E_USER_ERROR;   // php or user errors 
52                                 if($type == E_USER_WARNING)
53                                         $content = '';                  // clean other messages
54                         }
55                 }
56             $str = $msg[1];
57                 $c = $msg_colors[$type];
58                 if ($msg[0] < E_USER_ERROR && $msg[2] != null)
59                   $str .= ' '._('in file').': '.$msg[2].' '._('at line ').$msg[3];
60
61                 $content .= "<tr><td  " . ($center?"align='center' ":"").
62                   " width='100%' bgcolor='{$c['bg']}'><font color='{$c['txt']}'>"
63                   .$str."</font></td></tr>";
64         }
65
66         $str = "<center><table border='1' cellpadding='3' cellspacing='0' style='border-collapse: collapse' bordercolor='{$c['bd']}' width='98%'>"
67           . $content . "</table></center><br>\n";       
68                 
69   }
70      else
71         $str = '';
72         
73     return $str;
74 }
75 //-----------------------------------------------------------------------------
76 // Error box <div> element.
77 //
78 function error_box() {
79     global $before_box;
80     
81     echo "<div id='msgbox'>";
82     $before_box = ob_get_clean(); // save html content before error box 
83 // Necessary restart instead of get_contents/clean calls due to a bug in php 4.3.2
84         register_shutdown_function('ob_end_flush');
85     ob_start('output_html');
86     echo "</div>";
87 }
88
89 function display_db_error($msg, $sql_statement=null, $exit=true)
90 {
91         global $db, $debug;
92         $db_error = db_error_no();
93         
94         $str = "<span class='errortext'><b>" . _("DATABASE ERROR :") . "</b> $msg</span><br>";
95         
96         if ($db_error != 0) 
97         {
98                 $str .= "error code : " . $db_error . "<br>";
99                 $str .= "error message : " . db_error_msg($db) . "<br>";
100         }
101         
102         if ($debug == 1) 
103         {
104                 $str .= "sql that failed was : " . $sql_statement . "<br>";
105         }
106         
107         $str .= "<br><br>";
108
109         trigger_error($str, E_USER_ERROR);
110
111         if ($exit)
112                 exit;
113 }
114
115 function frindly_db_error($db_error)
116 {
117         global $db_duplicate_error_code;
118         
119         if ($db_error == $db_duplicate_error_code) 
120         {
121                 display_error(_("The entered information is a duplicate. Please go back and enter different values."));
122                 return true;
123         }
124         
125         return false;
126 }
127
128 function check_db_error($msg, $sql_statement, $exit_if_error=true, $rollback_if_error=true)
129 {
130         global $db;
131         $db_error = db_error_no();
132         
133         if ($db_error != 0) 
134         {
135                 
136                 if (!frindly_db_error($db_error))
137                         display_db_error($msg, $sql_statement, false);
138                 
139                 if ($rollback_if_error) 
140                 {
141                   $rollback_result = db_query("rollback","could not rollback");                 
142                 }
143                 
144                 if ($exit_if_error) 
145                 {
146                         echo "<br><br>";
147                         exit;
148                 }
149         }
150         return $db_error;               
151 }
152
153 ?>