Implemented customizable authentication timeout.
[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;
21
22         // error_reporting==0 when messages are set off with @ 
23         if ($errno & error_reporting())
24                         $messages[] = array($errno, $errstr, $file, $line);
25         else if($errno&~E_NOTICE)// log all not displayed messages 
26                 error_log(user_company() . ':' . $_SESSION["wa_current_user"]->loginname.':'
27                          . basename($file) .":$line: $errstr");
28         
29     return true;
30 }
31 //------------------------------------------------------------------------------
32 //      Formats system messages before insert them into message <div>
33 // FIX center is unused now
34 function fmt_errors($center=false) {
35     global $messages, $path_to_root;
36   
37   $msg_class = array(
38         E_USER_ERROR => 'err_msg',
39         E_USER_WARNING =>'warn_msg', 
40         E_USER_NOTICE => 'note_msg');
41
42   $type = E_USER_NOTICE;
43   $content = '';
44 //  $class = 'no_msg';
45   if (count($messages)) {
46         foreach($messages as $cnt=>$msg) {
47                 if ($msg[0]>$type) continue;
48
49                 if ($msg[0]<$type) { 
50                         if ($msg[0] == E_USER_WARNING) {
51                                 $type = E_USER_WARNING; // user warnings 
52                                 $content = '';                  // clean notices when we have errors
53                         } else  {
54                                 $type = E_USER_ERROR;   // php or user errors 
55                                 if($type == E_USER_WARNING)
56                                         $content = '';                  // clean other messages
57                         }
58                 }
59             $str = $msg[1];
60                 if ($msg[0] < E_USER_ERROR && $msg[2] != null)
61                   $str .= ' '._('in file').': '.$msg[2].' '._('at line ').$msg[3];
62                 $content .= ($cnt ? '<hr>' : '').$str;
63         }               
64         $class = $msg_class[$type];
65     $content = "<div class='$class'>$content</div>";
66   } else
67   if ($path_to_root=='.')
68         return '';
69   return $content;
70 }
71 //-----------------------------------------------------------------------------
72 // Error box <div> element.
73 //
74 function error_box() {
75     global $before_box;
76     
77     echo "<div id='msgbox'>";
78         $before_box = ob_get_clean(); // save html content before error box 
79 // Necessary restart instead of get_contents/clean calls due to a bug in php 4.3.2
80         register_shutdown_function('end_flush');
81     ob_start('output_html');
82     echo "</div>";
83 }
84 /*
85         Helper to avoid sparse log notices.
86 */
87 function end_flush () {
88                 if (ob_get_level()) ob_end_flush();
89 }
90
91 function display_db_error($msg, $sql_statement=null, $exit=true)
92 {
93         global $db, $debug;
94
95         $warning = $msg==null;
96         $db_error = db_error_no();
97         
98 //      $str = "<span class='errortext'><b>" . _("DATABASE ERROR :") . "</b> $msg</span><br>";
99         if($warning)
100                 $str = "<b>" . _("Debug mode database warning:") . "</b><br>";
101         else
102                 $str = "<b>" . _("DATABASE ERROR :") . "</b> $msg<br>";
103         
104         if ($db_error != 0) 
105         {
106                 $str .= "error code : " . $db_error . "<br>";
107                 $str .= "error message : " . db_error_msg($db) . "<br>";
108         }
109         
110         if ($debug == 1) 
111         {
112                 $str .= "sql that failed was : " . $sql_statement . "<br>";
113         }
114         
115         $str .= "<br><br>";
116         if($msg)
117                 trigger_error($str, E_USER_ERROR);
118         else    // $msg can be null here only in debug mode, otherwise the error is ignored
119                 trigger_error($str, E_USER_WARNING);
120         if ($exit)
121                 exit;
122 }
123
124 function frindly_db_error($db_error)
125 {
126         global $db_duplicate_error_code;
127         
128         if ($db_error == $db_duplicate_error_code) 
129         {
130                 display_error(_("The entered information is a duplicate. Please go back and enter different values."));
131                 return true;
132         }
133         
134         return false;
135 }
136
137 function check_db_error($msg, $sql_statement, $exit_if_error=true, $rollback_if_error=true)
138 {
139         global $db;
140         $db_error = db_error_no();
141         
142         if ($db_error != 0) 
143         {
144                 
145                 if (!frindly_db_error($db_error)) {
146                                 display_db_error($msg, $sql_statement, false);
147                 }
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 ?>