thomascube
2008-09-19 42e328a85f5880e3e767eebaaa88b55a2b49d2ff
commit | author | age
4e17e6 1 <?php
T 2
3 /*
4  +-----------------------------------------------------------------------+
5  | program/include/bugs.inc                                              |
6  |                                                                       |
6d969b 7  | This file is part of the RoudCube Webmail client                      |
T 8  | Copyright (C) 2005-2007, RoudCube Dev - Switzerland                   |
30233b 9  | Licensed under the GNU GPL                                            |
4e17e6 10  |                                                                       |
T 11  | PURPOSE:                                                              |
12  |   Provide error handling and logging functions                        |
13  |                                                                       |
14  +-----------------------------------------------------------------------+
15  | Author: Thomas Bruederli <roundcube@gmail.com>                        |
16  +-----------------------------------------------------------------------+
17
18  $Id$
19
20 */
21
22
6d969b 23 /**
T 24  * Error handling and logging functions
25  *
26  * @package Core
27  */
28
29
30 /**
31  * Throw system error and show error page
32  *
33  * @param array Named parameters
34  *  - code: Error code (required)
35  *  - type: Error type [php|db|imap|javascript] (required)
36  *  - message: Error message
37  *  - file: File where error occured
38  *  - line: Line where error occured
39  * @param boolean True to log the error
40  * @param boolean Terminate script execution
41  */
42 function raise_error($arg=array(), $log=false, $terminate=false)
4e17e6 43   {
T 44   global $__page_content, $CONFIG, $OUTPUT, $ERROR_CODE, $ERROR_MESSAGE;
45   
46   // report bug (if not incompatible browser)
47   if ($log && $arg['type'] && $arg['message'])
48     log_bug($arg);
49
50   // display error page and terminate script
51   if ($terminate)
52     {
53     $ERROR_CODE = $arg['code'];
54     $ERROR_MESSAGE = $arg['message'];
55     include("program/steps/error.inc");
56     exit;
57     }
58   }
59
60
6d969b 61 /**
T 62  * Report error according to configured debug_level
63  *
64  * @param array Named parameters
65  * @see raise_error()
66  */
4e17e6 67 function log_bug($arg_arr)
6d969b 68 {
47124c 69   global $CONFIG;
c5e2d8 70   $program = strtoupper($arg_arr['type']);
4e17e6 71
T 72   // write error to local log file
73   if ($CONFIG['debug_level'] & 1)
6d969b 74   {
c5e2d8 75     $log_entry = sprintf("[%s] %s Error: %s in %s on line %d (%s %s)\n",
6d969b 76       date("d-M-Y H:i:s O", mktime()),
T 77       $program,
78       $arg_arr['message'],
79       $arg_arr['file'],
c5e2d8 80       $arg_arr['line'],
T 81       $_SERVER['REQUEST_METHOD'],
82       $_SERVER['REQUEST_URI']);
83       
fd8c50 84     if (empty($CONFIG['log_dir']))
47124c 85       $CONFIG['log_dir'] = INSTALL_PATH.'logs';
fd8c50 86       
9fc381 87     // try to open specific log file for writing
b77d0d 88     if ($CONFIG['log_driver'] == 'syslog')
6d969b 89     {
b77d0d 90       syslog(LOG_ERR, $log_entry);
A 91     }
92     else if ($fp = @fopen($CONFIG['log_dir'].'/errors', 'a'))
93     {
94       // log_driver == 'file' is the default, assumed here.
4e17e6 95       fwrite($fp, $log_entry);
T 96       fclose($fp);
6d969b 97     }
9fc381 98     else
6d969b 99     {
9fc381 100       // send error to PHPs error handler
T 101       trigger_error($arg_arr['message']);
4e17e6 102     }
6d969b 103   }
4e17e6 104
T 105   // resport the bug to the global bug reporting system
106   if ($CONFIG['debug_level'] & 2)
6d969b 107   {
T 108     // TODO: Send error via HTTP
109   }
4e17e6 110
T 111   // show error if debug_mode is on
112   if ($CONFIG['debug_level'] & 4)
6d969b 113   {
31b2ce 114     print "<b>$program Error";
T 115
116     if (!empty($arg_arr['file']) && !empty($arg_arr['line']))
117       print " in $arg_arr[file] ($arg_arr[line])";
118
119     print ":</b>&nbsp;";
4e17e6 120     print nl2br($arg_arr['message']);
T 121     print '<br />';
122     flush();
123   }
6d969b 124 }
4e17e6 125
19bef0 126 ?>