forked from avdg/ppi-framework-old
-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors.php
149 lines (129 loc) · 4.32 KB
/
errors.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
<?php
/**
*
* @version 1.0
* @author Paul Dragoonis <dragoonis@php.net>
* @license http://opensource.org/licenses/mit-license.php MIT
* @copyright Digiflex Development
* @package PPI
*/
/**
* The default PPI error handler, will play with some data then throw an exception, thus the set_exception_handler callback is ran
*
* @param string $errno The error level (number)
* @param string $errstr The error message
* @param string $errfile The error filename
* @param string $errline The error line
* @throws PPI_Exception
*/
function ppi_error_handler($errno = '', $errstr = "", $errfile = "", $errline = "") {
$ppi_exception_thrown = true;
$error = array ();
$error ['code'] = $errno;
$error ['message'] = $errstr;
$error ['file'] = $errfile;
$error ['line'] = $errline;
/* throw exception to user */
$oException = new PPI_Exception();
if(property_exists($oException, '_traceString')) {
$error['backtrace'] = $oException->_traceString;
}
// this function has the exit() call in it, so we must put it last
$oException->show_exceptioned_error($error);
}
/**
* The default exception handler
*
* @param object $oException The exception object
* @return void
*/
function ppi_exception_handler($oException) {
if(!$oException instanceof Exception) {
return false;
}
$error = array();
foreach(array('code', 'message', 'file', 'line', 'traceString') as $field) {
$fieldName = "_$field";
if(!property_exists($oException, $fieldName)) { continue; }
if($field == 'traceString') {
$error['backtrace'] = $oException->$fieldName;
} else {
$error[$field] = $oException->$fieldName;
}
}
try {
if(!PPI_Registry::getInstance()->exists('PPI_Config')) {
$oException->show_exceptioned_error($error);
return;
}
$oConfig = PPI_Helper::getConfig();
$error['sql'] = PPI_Helper::getRegistry()->get('PPI_Model::PPI_Model_Queries', array());
// email the error with the backtrace information to the developer
if(!isset($oConfig->system->log_errors) || $oConfig->system->log_errors != false) {
// get the email contents
$emailContent = $oException instanceof PPI_Exception ? $oException->getErrorForEmail($error) : '';
$oLog = new PPI_Model_Log();
$oLog->addExceptionLog(array(
'code' => $oException->_code,
'message' => $oException->_message,
'file' => $oException->_file,
'line' => $oException->_line,
'backtrace' => $error['backtrace'],
'post' => serialize($_POST),
'cookie' => serialize($_COOKIE),
'get' => serialize($_GET),
'session' => serialize($_SESSION),
'content' => $emailContent
));
if($oConfig->system->email_errors) {
//@mail($oConfig->system->developer_email, 'PHP Exception For '.getHostname(), $emailContent);
//include CORECLASSPATH.'mail.php';
//$mail = new Mail();
//$mail->send();
}
// write the error to the php error log
writeErrorToLog($error['message'] . ' in file: '.$error['file'] . ' on line: '.$error['line']);
$oException->show_exceptioned_error($error);
}
}
catch(PPI_Exception $e) {
writeErrorToLog($e->getMessage());
}
catch(Exception $e) {
writeErrorToLog($e->getMessage());
}
catch(PDOException $e) {
writeErrorToLog($e->getMessage());
}
$oException->show_exceptioned_error($error);
// @todo This should go to an internal error page which doesn't use framework components and show the error code
// ppi_show_exceptioned_error($error);
}
function writeErrorToLog($message) {
if(ini_get('log_errors') !== 'On') {
return false;
}
$oConfig = PPI_Helper::getConfig();
if( ($sErrorLog = ini_get('error_log')) == 'syslog') {
syslog(LOG_ALERT, "\n$message\n");
} elseif($sErrorLog !== '' && is_writable($sErrorLog)) {
file_put_contents($sErrorLog, "\n$message\n", FILE_APPEND);
}
}
function ppi_show_exceptioned_error() {
}
/**
* Set the error and exception handlers
*
* @param string $p_sErrorHandler The error handler function name
* @param string $p_sExceptionHandler The exception handler function name
* @return void
*/
function setErrorHandlers($p_sErrorHandler = null, $p_sExceptionHandler = null) {
if($p_sErrorHandler !== null) {
set_error_handler($p_sErrorHandler, E_ALL);
}
if($p_sExceptionHandler !== null) {
set_exception_handler($p_sExceptionHandler);
}
}