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