Support for debugging sql queries in page footer.
[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, $SysPrefs;
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:" . ( $SysPrefs->db_ok ? '':'[before upgrade]')
37                          . " $errstr");
38         
39     return true;
40 }
41 //------------------------------------------------------------------------------
42 //      Formats system messages before insert them into message <div>
43 // FIX center is unused now
44 function fmt_errors($center=false) {
45     global $messages, $path_to_root;
46   
47   $msg_class = array(
48         E_USER_ERROR => 'err_msg',
49         E_USER_WARNING =>'warn_msg', 
50         E_USER_NOTICE => 'note_msg');
51
52   $type = E_USER_NOTICE;
53   $content = '';
54 //  $class = 'no_msg';
55   if (count($messages)) {
56         foreach($messages as $cnt=>$msg) {
57                 if ($msg[0]>$type) continue;
58
59                 if ($msg[0]<$type) { 
60                         if ($msg[0] == E_USER_WARNING) {
61                                 $type = E_USER_WARNING; // user warnings 
62                                 $content = '';                  // clean notices when we have errors
63                         } else  {
64                                 $type = E_USER_ERROR;   // php or user errors 
65                                 if($type == E_USER_WARNING)
66                                         $content = '';                  // clean other messages
67                         }
68                 }
69             $str = $msg[1];
70                 if ($msg[0] < E_USER_ERROR && $msg[2] != null)
71                   $str .= ' '._('in file').': '.$msg[2].' '._('at line ').$msg[3];
72                 $content .= ($cnt ? '<hr>' : '').$str;
73         }               
74         $class = $msg_class[$type];
75     $content = "<div class='$class'>$content</div>";
76   } else
77   if ($path_to_root=='.')
78         return '';
79   return $content;
80 }
81 //-----------------------------------------------------------------------------
82 // Error box <div> element.
83 //
84 function error_box() {
85     global $before_box;
86     
87     echo "<div id='msgbox'>";
88
89 // Necessary restart instead of get_contents/clean calls due to a bug in php 4.3.2
90         $before_box = ob_get_clean(); // save html content before error box 
91     ob_start('output_html');
92     echo "</div>";
93 }
94 /*
95         Helper to avoid sparse log notices.
96 */
97 function end_flush() {
98         global $Ajax, $transaction_level;
99
100         if (isset($Ajax))
101                 $Ajax->run();
102         // flush all output buffers (works also with exit inside any div levels)
103         while(ob_get_level()) ob_end_flush();
104
105         // if any transaction was aborted unexpectedly rollback changes
106         cancel_transaction();
107 }
108
109 function display_db_error($msg, $sql_statement=null, $exit=true)
110 {
111         global $db, $debug, $go_debug;
112
113         $warning = $msg==null;
114         $db_error = db_error_no();
115         
116 //      $str = "<span class='errortext'><b>" . _("DATABASE ERROR :") . "</b> $msg</span><br>";
117         if($warning)
118                 $str = "<b>" . _("Debug mode database warning:") . "</b><br>";
119         else
120                 $str = "<b>" . _("DATABASE ERROR :") . "</b> $msg<br>";
121         
122         if ($db_error != 0) 
123         {
124                 $str .= "error code : " . $db_error . "<br>";
125                 $str .= "error message : " . db_error_msg($db) . "<br>";
126         }
127         
128         if ($debug == 1) 
129         {
130                 $str .= "sql that failed was : " . $sql_statement . "<br>";
131                 if ($go_debug > 1) display_backtrace();
132         }
133         
134         $str .= "<br><br>";
135         if($msg)
136                 trigger_error($str, E_USER_ERROR);
137         else    // $msg can be null here only in debug mode, otherwise the error is ignored
138                 trigger_error($str, E_USER_WARNING);
139         if ($exit)
140                 exit;
141 }
142
143 function frindly_db_error($db_error)
144 {
145         global $db_duplicate_error_code;
146         
147         if ($db_error == $db_duplicate_error_code) 
148         {       
149                 display_error(_("The entered information is a duplicate. Please go back and enter different values."));
150                 return true;
151         }
152         
153         return false;
154 }
155
156 function check_db_error($msg, $sql_statement, $exit_if_error=true, $rollback_if_error=true)
157 {
158         global $db, $go_debug;
159         $db_error = db_error_no();
160         
161         if ($db_error != 0) 
162         {
163                 
164                 if ($go_debug || !frindly_db_error($db_error)) {
165                                 display_db_error($msg, $sql_statement, false);
166                 }
167                 
168                 if ($rollback_if_error) 
169                 {
170                   $rollback_result = db_query("rollback","could not rollback");                 
171                 }
172                 
173                 if ($exit_if_error) 
174                 {
175                         end_page(); exit;
176                 }
177         }
178         return $db_error;               
179 }
180
181 ?>