]> git.donarmstrong.com Git - roundcube.git/blob - program/include/bugs.inc
Merge commit 'debian/0.3-1' into debian
[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-2009, 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 2862 2009-08-17 09:54:53Z thomasb $
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 Error: %s%s (%s %s)",
77       $program,
78       $arg_arr['message'],
79       $arg_arr['file'] ? sprintf(' in %s on line %d', $arg_arr['file'], $arg_arr['line']) : '',
80       $_SERVER['REQUEST_METHOD'],
81       $_SERVER['REQUEST_URI'] . $post_query);
82     
83     if (!write_log('errors', $log_entry))
84     {
85       // send error to PHPs error handler if write_log didn't succeed
86       trigger_error($arg_arr['message']);
87     }
88   }
89
90   // resport the bug to the global bug reporting system
91   if ($CONFIG['debug_level'] & 2)
92   {
93     // TODO: Send error via HTTP
94   }
95
96   // show error if debug_mode is on
97   if ($CONFIG['debug_level'] & 4)
98   {
99     print "<b>$program Error";
100
101     if (!empty($arg_arr['file']) && !empty($arg_arr['line']))
102       print " in $arg_arr[file] ($arg_arr[line])";
103
104     print ":</b>&nbsp;";
105     print nl2br($arg_arr['message']);
106     print '<br />';
107     flush();
108   }
109 }
110
111 ?>