1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverStripe\Logging; |
4
|
|
|
|
5
|
|
|
use SilverStripe\Core\Convert; |
6
|
|
|
use SilverStripe\Dev\Debug; |
7
|
|
|
use SilverStripe\Control\Director; |
8
|
|
|
use SilverStripe\Control\Email\Email; |
9
|
|
|
use Monolog\Formatter\FormatterInterface; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Produce a friendly error message |
13
|
|
|
*/ |
14
|
|
|
class DebugViewFriendlyErrorFormatter implements FormatterInterface |
15
|
|
|
{ |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Default status code |
19
|
|
|
* |
20
|
|
|
* @var int |
21
|
|
|
*/ |
22
|
|
|
protected $statusCode = 500; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Default friendly error |
26
|
|
|
* |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
protected $friendlyErrorMessage = 'Error'; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Default error body |
33
|
|
|
* |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
protected $friendlyErrorDetail; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Get default status code |
40
|
|
|
* |
41
|
|
|
* @return int |
42
|
|
|
*/ |
43
|
|
|
public function getStatusCode() |
44
|
|
|
{ |
45
|
|
|
return $this->statusCode; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Set default status code |
50
|
|
|
* |
51
|
|
|
* @param int $statusCode |
52
|
|
|
* @return $this |
53
|
|
|
*/ |
54
|
|
|
public function setStatusCode($statusCode) |
55
|
|
|
{ |
56
|
|
|
$this->statusCode = $statusCode; |
57
|
|
|
return $this; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Get friendly title |
62
|
|
|
* |
63
|
|
|
* @return string |
64
|
|
|
*/ |
65
|
|
|
public function getTitle() |
66
|
|
|
{ |
67
|
|
|
return $this->friendlyErrorMessage; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Set friendly title |
72
|
|
|
* |
73
|
|
|
* @param string $title |
74
|
|
|
* @return $this |
75
|
|
|
*/ |
76
|
|
|
public function setTitle($title) |
77
|
|
|
{ |
78
|
|
|
$this->friendlyErrorMessage = $title; |
79
|
|
|
return $this; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Get default error body |
84
|
|
|
* |
85
|
|
|
* @return string |
86
|
|
|
*/ |
87
|
|
|
public function getBody() |
88
|
|
|
{ |
89
|
|
|
return $this->friendlyErrorDetail; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Set default error body |
94
|
|
|
* |
95
|
|
|
* @param string $body |
96
|
|
|
* @return $this |
97
|
|
|
*/ |
98
|
|
|
public function setBody($body) |
99
|
|
|
{ |
100
|
|
|
$this->friendlyErrorDetail = $body; |
101
|
|
|
return $this; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function format(array $record) |
105
|
|
|
{ |
106
|
|
|
// Get error code |
107
|
|
|
$code = empty($record['code']) ? $this->getStatusCode() : $record['code']; |
108
|
|
|
return $this->output($code); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function formatBatch(array $records) |
112
|
|
|
{ |
113
|
|
|
$message = ''; |
114
|
|
|
foreach ($records as $record) { |
115
|
|
|
$message .= $this->format($record); |
116
|
|
|
} |
117
|
|
|
return $message; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Return the appropriate error content for the given status code |
122
|
|
|
* |
123
|
|
|
* @param int $statusCode |
124
|
|
|
* @return string Content in an appropriate format for the current request |
125
|
|
|
*/ |
126
|
|
|
public function output($statusCode) |
|
|
|
|
127
|
|
|
{ |
128
|
|
|
// TODO: Refactor into a content-type option |
129
|
|
|
if (Director::is_ajax()) { |
130
|
|
|
return $this->getTitle(); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
$renderer = Debug::create_debug_view(); |
134
|
|
|
$output = $renderer->renderHeader(); |
135
|
|
|
$output .= $renderer->renderInfo("Website Error", $this->getTitle(), $this->getBody()); |
136
|
|
|
|
137
|
|
|
if (!is_null($contactInfo = $this->addContactAdministratorInfo())) { |
138
|
|
|
$output .= $renderer->renderParagraph($contactInfo); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
$output .= $renderer->renderFooter(); |
142
|
|
|
return $output; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Generate the line with admin contact info |
147
|
|
|
* |
148
|
|
|
* @return string|null |
149
|
|
|
*/ |
150
|
|
|
private function addContactAdministratorInfo() |
151
|
|
|
{ |
152
|
|
|
if (!$adminEmail = Email::config()->admin_email) { |
|
|
|
|
153
|
|
|
return null; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
if (is_string($adminEmail)) { |
157
|
|
|
return 'Contact an administrator: ' . Email::obfuscate($adminEmail); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
if (!is_array($adminEmail) || !count($adminEmail)) { |
161
|
|
|
return null; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
$email = array_keys($adminEmail)[0]; |
165
|
|
|
$name = array_values($adminEmail)[0]; |
166
|
|
|
|
167
|
|
|
return sprintf('Contact %s: %s', Convert::raw2xml($name), Email::obfuscate($email)); |
|
|
|
|
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.