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