Merged changes up to version 2.3.4 into unstable.
[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         );
67         foreach($excluded_warnings as $ref) {
68                 if (strpos($errstr, $ref) !== false) {
69                         return true;
70                 }
71         }
72
73         if ($go_debug>1) {
74                 $bt = get_backtrace(true, 1);
75         }
76
77         // error_reporting==0 when messages are set off with @ 
78         if ($errno & error_reporting()) {
79                 $messages[] = array($errno, $errstr, $file, $line, @$bt);
80         }
81         else if($errno&~E_NOTICE) { // log all not displayed messages 
82                 $user = @$_SESSION["wa_current_user"]->loginname;
83                 $context = isset($SysPrefs) && !$SysPrefs->db_ok ? '[before upgrade]' : '';
84                 error_log(user_company() . ":$user:". basename($file) .":$line:$context $errstr");
85         }
86         
87     return true;
88 }
89 //------------------------------------------------------------------------------
90 //      Formats system messages before insert them into message <div>
91 // FIX center is unused now
92 function fmt_errors($center=false) {
93     global $messages, $path_to_root, $go_debug;
94
95   $msg_class = array(
96         E_USER_ERROR => 'err_msg',
97         E_USER_WARNING =>'warn_msg', 
98         E_USER_NOTICE => 'note_msg');
99
100   $type = E_USER_NOTICE;
101   $content = '';
102 //  $class = 'no_msg';
103   if (count($messages)) {
104         foreach($messages as $cnt=>$msg) {
105                 if ($msg[0]>$type) continue;
106
107                 if ($msg[0]<$type) { 
108                         if ($msg[0] == E_USER_WARNING) {
109                                 $type = E_USER_WARNING; // user warnings 
110                                 $content = '';                  // clean notices when we have errors
111                         } else  {
112                                 $type = E_USER_ERROR;   // php or user errors 
113                                 if($type == E_USER_WARNING)
114                                         $content = '';                  // clean other messages
115                         }
116                 }
117             $str = $msg[1];
118                 if ($msg[0] < E_USER_ERROR && $msg[2] != null)
119                   $str .= ' '._('in file').': '.$msg[2].' '._('at line ').$msg[3];
120                 if ($go_debug>1 && $type!=E_USER_NOTICE && $type!=E_USER_WARNING)
121                   $str .= '<br>'.$msg[4];
122                 $content .= ($cnt ? '<hr>' : '').$str;
123         }
124         $class = $msg_class[$type];
125     $content = "<div class='$class'>$content</div>";
126   } else
127   if ($path_to_root=='.')
128         return '';
129   return $content;
130 }
131 //-----------------------------------------------------------------------------
132 // Error box <div> element.
133 //
134 function error_box() {
135     global $before_box;
136     
137     echo "<div id='msgbox'>";
138
139 // Necessary restart instead of get_contents/clean calls due to a bug in php 4.3.2
140         $before_box = ob_get_clean(); // save html content before error box 
141     ob_start('output_html');
142     echo "</div>";
143 }
144 /*
145         Helper to avoid sparse log notices.
146 */
147 function end_flush() {
148         global $Ajax, $transaction_level;
149
150         if (isset($Ajax))
151                 $Ajax->run();
152         // flush all output buffers (works also with exit inside any div levels)
153         while(ob_get_level()) ob_end_flush();
154
155         // if any transaction was aborted unexpectedly rollback changes
156         cancel_transaction();
157 }
158
159 function display_db_error($msg, $sql_statement=null, $exit=true)
160 {
161         global $db, $debug, $go_debug, $db_connections;
162
163         $warning = $msg==null;
164         $db_error = db_error_no();
165         
166 //      $str = "<span class='errortext'><b>" . _("DATABASE ERROR :") . "</b> $msg</span><br>";
167         if($warning)
168                 $str = "<b>" . _("Debug mode database warning:") . "</b><br>";
169         else
170                 $str = "<b>" . _("DATABASE ERROR :") . "</b> $msg<br>";
171         
172         if ($db_error != 0) 
173         {
174                 $str .= "error code : " . $db_error . "<br>";
175                 $str .= "error message : " . db_error_msg($db) . "<br>";
176         }
177         
178         if ($debug == 1) 
179         {
180                 $cur_prefix = $db_connections[$_SESSION["wa_current_user"]->cur_con]['tbpref'];
181
182                 $str .= "sql that failed was : ".str_replace(TB_PREF, $cur_prefix, $sql_statement)."<br>";
183 //              if ($go_debug > 1) display_backtrace();
184         }
185         
186         $str .= "<br><br>";
187         if($msg)
188                 trigger_error($str, E_USER_ERROR);
189         else    // $msg can be null here only in debug mode, otherwise the error is ignored
190                 trigger_error($str, E_USER_WARNING);
191         if ($exit)
192                 exit;
193 }
194
195 function frindly_db_error($db_error)
196 {
197         global $db_duplicate_error_code;
198         
199         if ($db_error == $db_duplicate_error_code) 
200         {       
201                 display_error(_("The entered information is a duplicate. Please go back and enter different values."));
202                 return true;
203         }
204         
205         return false;
206 }
207
208 function check_db_error($msg, $sql_statement, $exit_if_error=true, $rollback_if_error=true)
209 {
210         global $db, $go_debug;
211         $db_error = db_error_no();
212         
213         if ($db_error != 0) 
214         {
215                 
216                 if ($go_debug || !frindly_db_error($db_error)) {
217                                 display_db_error($msg, $sql_statement, false);
218                 }
219                 
220                 if ($rollback_if_error) 
221                 {
222                   $rollback_result = db_query("rollback");
223                 }
224                 
225                 if ($exit_if_error) 
226                 {
227                         end_page(); exit;
228                 }
229         }
230         return $db_error;               
231 }
232
233 ?>