/
ErrorMessage.fnc.php
192 lines (156 loc) · 4.37 KB
/
ErrorMessage.fnc.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
<?php
/**
* Error Message function
*
* @package RosarioSIS
* @subpackage functions
*/
// Declare Error & Note & Warning global arrays.
$note = $error = $warning = array();
/**
* Error Message
*
* Use 'fatal' code to exit program.
* Use 'note' code for Notes and Update messages.
*
* If there are missing vals or similar, show them a msg.
* Pass in an array with error messages and this will display them
* in a standard fashion.
* In a program you may have:
*
* @example if ( ! $sch ) $error[] = _( 'School not provided.' );
* @example if ( $count === 0 ) $error[] = _( 'Number of students is zero.' ); ErrorMessage( $error );
*
* Why use this? It will tell the user if they have multiple errors
* without them having to re-run the program each time finding new
* problems. Also, the error display will be standardized.
*
* @global string $print_data PDF print data
*
* @param array $errors Array of errors or notes.
* @param string $code error|fatal|warning|note (optional). Defaults to 'error'.
* @return string Error / Note Message, exits if 'fatal' code
*/
function ErrorMessage( $errors, $code = 'error' )
{
if ( ! $errors
|| ! is_array( $errors ) )
{
return '';
}
$return = '';
// Error.
if ( $code === 'error'
|| $code === 'fatal' )
{
$return .= '<div class="error"><p>' . button( 'x', _( 'Error' ) );
}
// Warning.
elseif ( $code === 'warning' )
{
$return .= '<div class="error"><p>' . button( 'warning', _( 'Warning' ) );
}
// Note / Update.
else
{
$return .= '<div class="updated"><p><b>' . _( 'Note' ) . '</b>';
}
if ( count( $errors ) === 1 )
{
// Note: do not use issetVal() here.
$return .= ': ' . ( isset( $errors[0] ) ? $errors[0] : $errors[1] ) . '</p>';
}
// More than one error: list.
else
{
$return .= '</p><ul>';
foreach ( (array) $errors as $error )
{
$return .= '<li>' . $error . '</li>';
}
$return .= '</ul>';
}
$return .= '</div>';
// Fatal error, display error and exit.
if ( $code === 'fatal' )
{
echo $return;
if ( ! isset( $_REQUEST['_ROSARIO_PDF'] ) )
{
Warehouse( 'footer' );
}
else
{
// FJ force PDF on fatal error.
global $print_data;
PDFStop( $print_data );
}
exit;
}
return $return;
}
/**
* Send Error by Email
* Send email to $RosarioErrorsAddress if set {@see config.inc.php}.
*
* @since 4.0
*
* @param array $error Error messages. Optional.
* @param $title string Email title. Optional.
*/
function ErrorSendEmail( $error = array(), $title = 'PHP Fatal error' )
{
global $RosarioErrorsAddress;
chdir( dirname( __FILE__ ) . '/../' );
require_once 'ProgramFunctions/SendEmail.fnc.php';
if ( ! $error )
{
$last_error = error_get_last();
if ( $last_error['type'] !== 1 )
{
return false;
}
// Fatal error.
$error = array(
'PHP Fatal error: ' . $last_error['message'],
'File: ' . $last_error['file'],
'Line: ' . $last_error['line'],
);
if ( ROSARIO_DEBUG )
{
print_r( $error );
}
}
// Send email to $RosarioErrorsAddress if set {@see config.inc.php}.
if ( ! filter_var( $RosarioErrorsAddress, FILTER_VALIDATE_EMAIL ) )
{
return false;
}
$ip = issetVal( $_SERVER['HTTP_X_FORWARDED_FOR'], $_SERVER['REMOTE_ADDR'] );
$debug_backtrace = debug_backtrace();
// User function would call ErrorMessage in an infinite loop if we are not logged in yet.
$username = empty( $_SESSION['STAFF_ID'] ) && empty( $_SESSION['STUDENT_ID'] ) ?
'[no user in session yet]' :
User( 'USERNAME' );
$message = 'System: ' . ParseMLField( Config( 'TITLE' ) ) . "\n";
$message .= 'IP: ' . $ip . "\n";
$message .= 'Date: ' . date( 'Y-m-d H:i:s' ) . "\n";
$message .= 'User: ' . $username . "\n";
$message .= 'Page: ' . $_SERVER['PHP_SELF'] . "\n";
$message .= 'Query string: ' . $_SERVER['QUERY_STRING'] . "\n";
if ( ! empty( $_SERVER['HTTP_REFERER'] ) )
{
$message .= 'Referrer: ' . $_SERVER['HTTP_REFERER'] . "\n";
}
$message .= "\n\n" . 'Error: ' . "\n" . print_r( $error, true );
$message .= "\n\n" . 'Request Array: ' . "\n" . print_r( $_REQUEST, true );
$message .= "\n\n" . 'Session Array: ' . "\n" . print_r( $_SESSION, true );
$message .= "\n\n" . 'Debug Backtrace: ' . "\n" . print_r( $debug_backtrace, true );
if ( mb_strlen( $message ) !== mb_strlen( strip_tags( $message ) ) )
{
// Format message if has HTML tags.
$message = nl2br( $message );
}
SendEmail( $RosarioErrorsAddress, $title, $message );
return true;
}