Added hook support for localized functions.
[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 //-----------------------------------------------------------------------------
16 //    Error handler - collects all php/user messages for
17 //    display in message box.
18
19 function error_handler($errno, $errstr, $file, $line) {
20     global $messages;
21
22         // error_reporting==0 when messages are set off with @ 
23         if ($errno & error_reporting()) {
24                 $messages[] = array($errno, $errstr, $file, $line);
25         }
26     return true;
27 }
28 //------------------------------------------------------------------------------
29 //      Formats system messages before insert them into message <div>
30 // FIX center is unused now
31 function fmt_errors($center=false) {
32     global $messages, $path_to_root;
33   
34   $msg_class = array(
35         E_USER_ERROR => 'err_msg',
36         E_USER_WARNING =>'warn_msg', 
37         E_USER_NOTICE => 'note_msg');
38
39   $type = E_USER_NOTICE;
40   $content = '';
41 //  $class = 'no_msg';
42   if (count($messages)) {
43         foreach($messages as $cnt=>$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                 if ($msg[0] < E_USER_ERROR && $msg[2] != null)
58                   $str .= ' '._('in file').': '.$msg[2].' '._('at line ').$msg[3];
59                 $content .= ($cnt ? '<hr>' : '').$str;
60         }               
61         $class = $msg_class[$type];
62     $content = "<div class='$class'>$content</div>";
63   } else
64   if ($path_to_root=='.')
65         return '';
66   return $content;
67 }
68 //-----------------------------------------------------------------------------
69 // Error box <div> element.
70 //
71 function error_box() {
72     global $before_box;
73     
74     echo "<div id='msgbox'>";
75     $before_box = ob_get_clean(); // save html content before error box 
76 // Necessary restart instead of get_contents/clean calls due to a bug in php 4.3.2
77         register_shutdown_function('ob_end_flush');
78     ob_start('output_html');
79     echo "</div>";
80 }
81
82 function display_db_error($msg, $sql_statement=null, $exit=true)
83 {
84         global $db, $debug;
85
86         $warning = $msg==null;
87         $db_error = db_error_no();
88         
89 //      $str = "<span class='errortext'><b>" . _("DATABASE ERROR :") . "</b> $msg</span><br>";
90         if($warning)
91                 $str = "<b>" . _("Debug mode database warning:") . "</b><br>";
92         else
93                 $str = "<b>" . _("DATABASE ERROR :") . "</b> $msg<br>";
94         
95         if ($db_error != 0) 
96         {
97                 $str .= "error code : " . $db_error . "<br>";
98                 $str .= "error message : " . db_error_msg($db) . "<br>";
99         }
100         
101         if ($debug == 1) 
102         {
103                 $str .= "sql that failed was : " . $sql_statement . "<br>";
104         }
105         
106         $str .= "<br><br>";
107         if($msg)
108                 trigger_error($str, E_USER_ERROR);
109         else    // $msg can be null here only in debug mode, otherwise the error is ignored
110                 trigger_error($str, E_USER_WARNING);
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                 
140                 if ($rollback_if_error) 
141                 {
142                   $rollback_result = db_query("rollback","could not rollback");                 
143                 }
144                 
145                 if ($exit_if_error) 
146                 {
147                         echo "<br><br>";
148                         exit;
149                 }
150         }
151         return $db_error;               
152 }
153
154 ?>