1bbc71ff3ea917dc1af36380dd72109958100804
[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, $go_debug;
21
22         // skip well known warnings we don't care about.
23         // Please use restrainedly to not risk loss of important messages
24         $excluded_warnings = array('html_entity_decode', 'htmlspecialchars');
25         foreach($excluded_warnings as $ref) {
26                 if (strpos($errstr, $ref) !== false) {
27                         return true;
28                 }
29         }
30
31         // error_reporting==0 when messages are set off with @ 
32         if ($errno & error_reporting())
33                         $messages[] = array($errno, $errstr, $file, $line);
34         else if($errno&~E_NOTICE)// log all not displayed messages 
35                 error_log(user_company() . ':' . $_SESSION["wa_current_user"]->loginname.':'
36                          . basename($file) .":$line: $errstr");
37         
38     return true;
39 }
40 //------------------------------------------------------------------------------
41 //      Formats system messages before insert them into message <div>
42 // FIX center is unused now
43 function fmt_errors($center=false) {
44     global $messages, $path_to_root;
45   
46   $msg_class = array(
47         E_USER_ERROR => 'err_msg',
48         E_USER_WARNING =>'warn_msg', 
49         E_USER_NOTICE => 'note_msg');
50
51   $type = E_USER_NOTICE;
52   $content = '';
53 //  $class = 'no_msg';
54   if (count($messages)) {
55         foreach($messages as $cnt=>$msg) {
56                 if ($msg[0]>$type) continue;
57
58                 if ($msg[0]<$type) { 
59                         if ($msg[0] == E_USER_WARNING) {
60                                 $type = E_USER_WARNING; // user warnings 
61                                 $content = '';                  // clean notices when we have errors
62                         } else  {
63                                 $type = E_USER_ERROR;   // php or user errors 
64                                 if($type == E_USER_WARNING)
65                                         $content = '';                  // clean other messages
66                         }
67                 }
68             $str = $msg[1];
69                 if ($msg[0] < E_USER_ERROR && $msg[2] != null)
70                   $str .= ' '._('in file').': '.$msg[2].' '._('at line ').$msg[3];
71                 $content .= ($cnt ? '<hr>' : '').$str;
72         }               
73         $class = $msg_class[$type];
74     $content = "<div class='$class'>$content</div>";
75   } else
76   if ($path_to_root=='.')
77         return '';
78   return $content;
79 }
80 //-----------------------------------------------------------------------------
81 // Error box <div> element.
82 //
83 function error_box() {
84     global $before_box;
85     
86     echo "<div id='msgbox'>";
87         $before_box = ob_get_clean(); // save html content before error box 
88 // Necessary restart instead of get_contents/clean calls due to a bug in php 4.3.2
89         register_shutdown_function('end_flush');
90     ob_start('output_html');
91     echo "</div>";
92 }
93 /*
94         Helper to avoid sparse log notices.
95 */
96 function end_flush () {
97                 if (ob_get_level()) ob_end_flush();
98 }
99
100 function display_db_error($msg, $sql_statement=null, $exit=true)
101 {
102         global $db, $debug;
103
104         $warning = $msg==null;
105         $db_error = db_error_no();
106         
107 //      $str = "<span class='errortext'><b>" . _("DATABASE ERROR :") . "</b> $msg</span><br>";
108         if($warning)
109                 $str = "<b>" . _("Debug mode database warning:") . "</b><br>";
110         else
111                 $str = "<b>" . _("DATABASE ERROR :") . "</b> $msg<br>";
112         
113         if ($db_error != 0) 
114         {
115                 $str .= "error code : " . $db_error . "<br>";
116                 $str .= "error message : " . db_error_msg($db) . "<br>";
117         }
118         
119         if ($debug == 1) 
120         {
121                 $str .= "sql that failed was : " . $sql_statement . "<br>";
122         }
123         
124         $str .= "<br><br>";
125         if($msg)
126                 trigger_error($str, E_USER_ERROR);
127         else    // $msg can be null here only in debug mode, otherwise the error is ignored
128                 trigger_error($str, E_USER_WARNING);
129         if ($exit)
130                 exit;
131 }
132
133 function frindly_db_error($db_error, $sql)
134 {
135         global $db_duplicate_error_code, $go_debug;
136         
137         if ($db_error == $db_duplicate_error_code) 
138         {       $msg = _("The entered information is a duplicate. Please go back and enter different values.");
139                 if ($go_debug)
140                         $msg .= '<br>Sql that failed was :<br>'.$sql;
141                 display_error($msg);
142                 return true;
143         }
144         
145         return false;
146 }
147
148 function check_db_error($msg, $sql_statement, $exit_if_error=true, $rollback_if_error=true)
149 {
150         global $db;
151         $db_error = db_error_no();
152         
153         if ($db_error != 0) 
154         {
155                 
156                 if (!frindly_db_error($db_error, $sql_statement)) {
157                                 display_db_error($msg, $sql_statement, false);
158                 }
159                 
160                 if ($rollback_if_error) 
161                 {
162                   $rollback_result = db_query("rollback","could not rollback");                 
163                 }
164                 
165                 if ($exit_if_error) 
166                 {
167                         end_page(); exit;
168                 }
169         }
170         return $db_error;               
171 }
172
173 ?>