0e8b2578b1978d0e92f3fa5aca9bba8d93f2a67d
[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 function get_backtrace($html = false, $skip=0)
16 {
17         $str = '';
18         if ($html) $str .= '<table border=0>';
19         $trace = debug_backtrace();
20
21         foreach($trace as $trn => $tr) {
22                 if ($trn <= $skip) continue;
23                 if ($html) $str .= '<tr><td>';
24                 $str .= $tr['file'].':'.$tr['line'].': ';
25                 if ($html) $str .= '</td><td>';
26                 if (isset($tr['type'])) {
27                         if($tr['type'] == '::') {
28                                 $str .= $tr['class'].'::';
29                         } else if($tr['type'] == '->') {
30                                 $str .= '('.$tr['class'].' Object)'.'->';
31                         }
32                 }
33                 $str .= $tr['function'].'(';
34                 
35                 if(is_array($tr['args'])) {
36                         $args = array();
37                         foreach($tr['args'] as $n=>$a) {
38                                 if (is_object($tr['args'][$n]))
39                                         $args[$n] = "(".get_class($tr['args'][$n])." Object)";
40                                 elseif (is_array($tr['args'][$n]))
41                                         $args[$n] = "(Array[".count($tr['args'][$n])."])";
42                                 else
43                                         $args[$n] = "'".$tr['args'][$n]."'";
44                         }
45                         $str .= implode(',',$args);
46                 }
47                 $str .= ')</td>';
48         }
49
50         if ($html) $str .= '</tr></table>';
51         return $str;
52 }
53
54 //-----------------------------------------------------------------------------
55 //    Error handler - collects all php/user messages for
56 //    display in message box.
57
58 function error_handler($errno, $errstr, $file, $line) {
59     global $messages, $go_debug, $SysPrefs;
60
61         // skip well known warnings we don't care about.
62         // Please use restrainedly to not risk loss of important messages
63         $excluded_warnings = array(
64                 'html_entity_decode', 'htmlspecialchars',       // nevermind encodings, special chars are processed anyway
65                 'should be compatible with that',                       // ignore cpdf/frontreport wrapper warnings
66                 'mysql extension is deprecated'                         // ignore strict warning in 5.4
67         );
68         foreach($excluded_warnings as $ref) {
69                 if (strpos($errstr, $ref) !== false) {
70                         return true;
71                 }
72         }
73
74         if ($go_debug>1) {
75                 $bt = get_backtrace(true, 1);
76         }
77
78         // error_reporting==0 when messages are set off with @ 
79         if ($errno & error_reporting()) {
80                 // suppress duplicated errors
81                 if (!in_array(array($errno, $errstr, $file, $line, @$bt), $messages))
82                         $messages[] = array($errno, $errstr, $file, $line, @$bt);
83         }
84         else if($errno&~E_NOTICE) { // log all not displayed messages 
85                 $user = @$_SESSION["wa_current_user"]->loginname;
86                 $context = isset($SysPrefs) && !$SysPrefs->db_ok ? '[before upgrade]' : '';
87                 error_log(user_company() . ":$user:". basename($file) .":$line:$context $errstr");
88         }
89         
90     return true;
91 }
92 //------------------------------------------------------------------------------
93 //      Formats system messages before insert them into message <div>
94 // FIX center is unused now
95 function fmt_errors($center=false) {
96     global $messages, $path_to_root, $go_debug;
97   
98   $msg_class = array(
99         E_USER_ERROR => 'err_msg',
100         E_USER_WARNING =>'warn_msg', 
101         E_USER_NOTICE => 'note_msg');
102
103   $type = E_USER_NOTICE;
104   $content = '';
105 //  $class = 'no_msg';
106   if (count($messages)) {
107         foreach($messages as $cnt=>$msg) {
108                 if ($go_debug && $msg[0]>E_USER_NOTICE)
109                         $msg[0] = E_ERROR;
110
111                 if ($msg[0]>$type) continue;
112
113                 if ($msg[0]<$type) { 
114                         if ($msg[0] == E_USER_WARNING) {
115                                 $type = E_USER_WARNING; // user warnings 
116                                 $content = '';                  // clean notices when we have errors
117                         } else  {
118                                 $type = E_USER_ERROR;   // php or user errors 
119                                 if($type == E_USER_WARNING)
120                                         $content = '';                  // clean other messages
121                         }
122                 }
123                 
124             $str = $msg[1];
125                 if (!in_array($msg[0], array(E_USER_NOTICE, E_USER_ERROR, E_USER_WARNING)) && $msg[2] != null)
126                   $str .= ' '._('in file').': '.$msg[2].' '._('at line ').$msg[3];
127
128                 if ($go_debug>1 && $type!=E_USER_NOTICE && $type!=E_USER_WARNING)
129                   $str .= '<br>'.$msg[4];
130                 $content .= ($cnt ? '<hr>' : '').$str;
131         }
132         $class = $msg_class[$type];
133     $content = "<div class='$class'>$content</div>";
134   } else
135   if ($path_to_root=='.')
136         return '';
137   return $content;
138 }
139 //-----------------------------------------------------------------------------
140 // Error box <div> element.
141 //
142 function error_box() {
143     global $before_box;
144     
145     echo "<div id='msgbox'>";
146
147 // Necessary restart instead of get_contents/clean calls due to a bug in php 4.3.2
148         $before_box = ob_get_clean(); // save html content before error box 
149     ob_start('output_html');
150     echo "</div>";
151 }
152 /*
153         Helper to avoid sparse log notices.
154 */
155 function end_flush() {
156         global $Ajax;
157
158         if (isset($Ajax))
159                 $Ajax->run();
160
161          // on some (but not all) php versions zlib extension adds 1 additional level of buffering, 
162          // so flush the last buffer outside the loop to be on safe side 
163         while(ob_get_level() > 1)
164                 ob_end_flush();
165         @ob_end_flush();
166
167         // if any transaction was aborted unexpectedly rollback changes
168         cancel_transaction();
169 }
170
171 function display_db_error($msg, $sql_statement=null, $exit=true)
172 {
173         global $db, $debug, $go_debug, $db_connections;
174
175         $warning = $msg==null;
176         $db_error = db_error_no();
177         
178 //      $str = "<span class='errortext'><b>" . _("DATABASE ERROR :") . "</b> $msg</span><br>";
179         if($warning)
180                 $str = "<b>" . _("Debug mode database warning:") . "</b><br>";
181         else
182                 $str = "<b>" . _("DATABASE ERROR :") . "</b> $msg<br>";
183         
184         if ($db_error != 0) 
185         {
186                 $str .= "error code : " . $db_error . "<br>";
187                 $str .= "error message : " . db_error_msg($db) . "<br>";
188         }
189         
190         if ($debug == 1) 
191         {
192                 $cur_prefix = $db_connections[$_SESSION["wa_current_user"]->cur_con]['tbpref'];
193
194                 $str .= "sql that failed was : ".str_replace(TB_PREF, $cur_prefix, $sql_statement)."<br>";
195 //              if ($go_debug > 1) display_backtrace();
196         }
197         
198         $str .= "<br><br>";
199         if ($go_debug)
200                 error_log($str);
201         else {
202                 if($msg)
203                         trigger_error($str, E_USER_ERROR);
204                 else    // $msg can be null here only in debug mode, otherwise the error is ignored
205                         trigger_error($str, E_USER_WARNING);
206         }
207         if ($exit)
208                 exit;
209 }
210
211 function frindly_db_error($db_error)
212 {
213         global $db_duplicate_error_code;
214         
215         if ($db_error == $db_duplicate_error_code) 
216         {       
217                 display_error(_("The entered information is a duplicate. Please go back and enter different values."));
218                 return true;
219         }
220         
221         return false;
222 }
223
224 function check_db_error($msg, $sql_statement, $exit_if_error=true, $rollback_if_error=true)
225 {
226         global $db, $go_debug;
227         $db_error = db_error_no();
228         
229         if ($db_error != 0) 
230         {
231                 
232                 if ($go_debug || !frindly_db_error($db_error)) {
233                                 display_db_error($msg, $sql_statement, false);
234                 }
235                 
236                 if ($rollback_if_error) 
237                 {
238                   $rollback_result = db_query("rollback");
239                 }
240                 
241                 if ($exit_if_error) 
242                 {
243                         end_page(); exit;
244                 }
245         }
246         return $db_error;               
247 }
248
249 ?>