alecpl
2008-08-29 53bd8fae60e1663447efba9f44fc6b79b3a5c1de
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;
4e17e6 70   $program = $arg_arr['type']=='xpath' ? 'XPath' : strtoupper($arg_arr['type']);
T 71
72   // write error to local log file
73   if ($CONFIG['debug_level'] & 1)
6d969b 74   {
T 75     $log_entry = sprintf(
76       "[%s] %s Error: %s in %s on line %d\n",
77       date("d-M-Y H:i:s O", mktime()),
78       $program,
79       $arg_arr['message'],
80       $arg_arr['file'],
81       $arg_arr['line']);
fd8c50 82                  
T 83     if (empty($CONFIG['log_dir']))
47124c 84       $CONFIG['log_dir'] = INSTALL_PATH.'logs';
fd8c50 85       
9fc381 86     // try to open specific log file for writing
b77d0d 87     if ($CONFIG['log_driver'] == 'syslog')
6d969b 88     {
b77d0d 89       syslog(LOG_ERR, $log_entry);
A 90     }
91     else if ($fp = @fopen($CONFIG['log_dir'].'/errors', 'a'))
92     {
93       // log_driver == 'file' is the default, assumed here.
4e17e6 94       fwrite($fp, $log_entry);
T 95       fclose($fp);
6d969b 96     }
9fc381 97     else
6d969b 98     {
9fc381 99       // send error to PHPs error handler
T 100       trigger_error($arg_arr['message']);
4e17e6 101     }
6d969b 102   }
4e17e6 103
T 104   // resport the bug to the global bug reporting system
105   if ($CONFIG['debug_level'] & 2)
6d969b 106   {
T 107     // TODO: Send error via HTTP
108   }
4e17e6 109
T 110   // show error if debug_mode is on
111   if ($CONFIG['debug_level'] & 4)
6d969b 112   {
31b2ce 113     print "<b>$program Error";
T 114
115     if (!empty($arg_arr['file']) && !empty($arg_arr['line']))
116       print " in $arg_arr[file] ($arg_arr[line])";
117
118     print ":</b>&nbsp;";
4e17e6 119     print nl2br($arg_arr['message']);
T 120     print '<br />';
121     flush();
122   }
6d969b 123 }
4e17e6 124
19bef0 125 ?>