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