]> git.donarmstrong.com Git - roundcube.git/blob - program/include/bugs.inc
Imported Upstream version 0.2~stable
[roundcube.git] / program / include / bugs.inc
1 <?php
2
3 /*
4  +-----------------------------------------------------------------------+
5  | program/include/bugs.inc                                              |
6  |                                                                       |
7  | This file is part of the RoudCube Webmail client                      |
8  | Copyright (C) 2005-2007, RoudCube Dev - Switzerland                   |
9  | Licensed under the GNU GPL                                            |
10  |                                                                       |
11  | PURPOSE:                                                              |
12  |   Provide error handling and logging functions                        |
13  |                                                                       |
14  +-----------------------------------------------------------------------+
15  | Author: Thomas Bruederli <roundcube@gmail.com>                        |
16  +-----------------------------------------------------------------------+
17
18  $Id: bugs.inc 2025 2008-10-30 09:15:01Z alec $
19
20 */
21
22
23 /**
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)
43   {
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
61 /**
62  * Report error according to configured debug_level
63  *
64  * @param array Named parameters
65  * @see raise_error()
66  */
67 function log_bug($arg_arr)
68 {
69   global $CONFIG;
70   $program = strtoupper($arg_arr['type']);
71
72   // write error to local log file
73   if ($CONFIG['debug_level'] & 1)
74   {
75     $post_query = ($_SERVER['REQUEST_METHOD'] == 'POST' ? '?_task='.urlencode($_POST['_task']).'&_action='.urlencode($_POST['_action']) : '');
76     $log_entry = sprintf("[%s] %s Error: %s%s (%s %s)\n",
77       date("d-M-Y H:i:s O", mktime()),
78       $program,
79       $arg_arr['message'],
80       $arg_arr['file'] ? sprintf(' in %s on line %d', $arg_arr['file'], $arg_arr['line']) : '',
81       $_SERVER['REQUEST_METHOD'],
82       $_SERVER['REQUEST_URI'] . $post_query);
83       
84     if (empty($CONFIG['log_dir']))
85       $CONFIG['log_dir'] = INSTALL_PATH.'logs';
86       
87     // try to open specific log file for writing
88     if ($CONFIG['log_driver'] == 'syslog')
89     {
90       syslog(LOG_ERR, $log_entry);
91     }
92     else if ($fp = @fopen($CONFIG['log_dir'].'/errors', 'a'))
93     {
94       // log_driver == 'file' is the default, assumed here.
95       fwrite($fp, $log_entry);
96       fclose($fp);
97     }
98     else
99     {
100       // send error to PHPs error handler
101       trigger_error($arg_arr['message']);
102     }
103   }
104
105   // resport the bug to the global bug reporting system
106   if ($CONFIG['debug_level'] & 2)
107   {
108     // TODO: Send error via HTTP
109   }
110
111   // show error if debug_mode is on
112   if ($CONFIG['debug_level'] & 4)
113   {
114     print "<b>$program Error";
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;";
120     print nl2br($arg_arr['message']);
121     print '<br />';
122     flush();
123   }
124 }
125
126 ?>