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