Added error logging to file or syslog.
[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 $msg_colors = array( 
16         E_USER_ERROR => array('bg'=>'#ffcccc', 'txt'=>'#dd2200', 'bd'=>'#cc3300'),
17     E_USER_WARNING => array('bg'=>'#ffff00', 'txt'=>'#ff5500', 'bd'=>'#ff9900'),
18     E_USER_NOTICE => array('bg'=>'#ccffcc', 'txt'=>'#007700', 'bd'=>'#33cc00'));
19
20 //-----------------------------------------------------------------------------
21 //    Error handler - collects all php/user messages for
22 //    display in message box.
23
24 function error_handler($errno, $errstr, $file, $line) {
25     global $messages;
26
27         // error_reporting==0 when messages are set off with @ 
28         if ($errno) {
29                 if(error_reporting())
30                         $messages[] = array($errno, $errstr, $file, $line);
31
32                 $ignored = E_USER_WARNING|E_USER_ERROR|E_USER_NOTICE;
33                  // don't log notices hidden with @ unless in debug mode
34                 if (!$go_debug) $ignored |= E_NOTICE;
35                 
36                 if (!($errno & $ignored))
37                          error_log(
38                          user_company().':'.$_SESSION["wa_current_user"]->loginname.':'.
39                          basename($file).":$line: $errstr");
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=true) {
47     global $messages, $msg_colors;
48   
49   $type = E_USER_NOTICE;
50   
51   if (count($messages)) {
52         $content = '';
53         foreach($messages as $msg) {
54                 if ($msg[0]>$type) continue;
55
56                 if ($msg[0]<$type) { 
57                         if ($msg[0] == E_USER_WARNING) {
58                                 $type = E_USER_WARNING; // user warnings 
59                                 $content = '';                  // clean notices when we have errors
60                         } else  {
61                                 $type = E_USER_ERROR;   // php or user errors 
62                                 if($type == E_USER_WARNING)
63                                         $content = '';                  // clean other messages
64                         }
65                 }
66             $str = $msg[1];
67                 $c = $msg_colors[$type];
68                 if ($msg[0] < E_USER_ERROR && $msg[2] != null)
69                   $str .= ' '._('in file').': '.$msg[2].' '._('at line ').$msg[3];
70
71                 $content .= "<tr><td  " . ($center?"align='center' ":"").
72                   " width='100%' bgcolor='{$c['bg']}'><font color='{$c['txt']}'>"
73                   .$str."</font></td></tr>";
74         }
75
76         $str = "<center><table border='1' cellpadding='3' cellspacing='0' style='border-collapse: collapse' bordercolor='{$c['bd']}' width='98%'>"
77           . $content . "</table></center><br>\n";       
78                 
79   }
80      else
81         $str = '';
82         
83     return $str;
84 }
85 //-----------------------------------------------------------------------------
86 // Error box <div> element.
87 //
88 function error_box() {
89     global $before_box;
90     
91     echo "<div id='msgbox'>";
92     $before_box = ob_get_clean(); // save html content before error box 
93 // Necessary restart instead of get_contents/clean calls due to a bug in php 4.3.2
94         register_shutdown_function('ob_end_flush');
95     ob_start('output_html');
96     echo "</div>";
97 }
98
99 function display_db_error($msg, $sql_statement=null, $exit=true)
100 {
101         global $db, $debug;
102         $db_error = db_error_no();
103         
104         $str = "<span class='errortext'><b>" . _("DATABASE ERROR :") . "</b> $msg</span><br>";
105         
106         if ($db_error != 0) 
107         {
108                 $str .= "error code : " . $db_error . "<br>";
109                 $str .= "error message : " . db_error_msg($db) . "<br>";
110         }
111         
112         if ($debug == 1) 
113         {
114                 $str .= "sql that failed was : " . $sql_statement . "<br>";
115         }
116         
117         $str .= "<br><br>";
118
119         trigger_error($str, E_USER_ERROR);
120
121         if ($exit)
122                 exit;
123 }
124
125 function frindly_db_error($db_error)
126 {
127         global $db_duplicate_error_code;
128         
129         if ($db_error == $db_duplicate_error_code) 
130         {
131                 display_error(_("The entered information is a duplicate. Please go back and enter different values."));
132                 return true;
133         }
134         
135         return false;
136 }
137
138 function check_db_error($msg, $sql_statement, $exit_if_error=true, $rollback_if_error=true)
139 {
140         global $db;
141         $db_error = db_error_no();
142         
143         if ($db_error != 0) 
144         {
145                 
146                 if (!frindly_db_error($db_error))
147                         display_db_error($msg, $sql_statement, false);
148                 
149                 if ($rollback_if_error) 
150                 {
151                   $rollback_result = db_query("rollback","could not rollback");                 
152                 }
153                 
154                 if ($exit_if_error) 
155                 {
156                         echo "<br><br>";
157                         exit;
158                 }
159         }
160         return $db_error;               
161 }
162
163 ?>