-
Notifications
You must be signed in to change notification settings - Fork 824
/
Debug.php
243 lines (219 loc) · 6.99 KB
/
Debug.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
<?php
namespace SilverStripe\Dev;
use SilverStripe\Control\Director;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\ORM\DB;
use SilverStripe\Security\Permission;
use SilverStripe\Security\Security;
/**
* Supports debugging and core error handling.
*
* Attaches custom methods to the default error handling hooks
* in PHP. Currently, two levels of error are supported:
*
* - Notice
* - Warning
* - Error
*
* Uncaught exceptions are currently passed to the debug
* reporter as standard PHP errors.
*
* Errors handled by this class are passed along to {@link SS_Log}.
* For configuration information, see the {@link SS_Log}
* class documentation.
*/
class Debug
{
/**
* Show the contents of val in a debug-friendly way.
* Debug::show() is intended to be equivalent to dprintr()
* Does not work on live mode.
*
* @param mixed $val
* @param bool $showHeader
* @param HTTPRequest|null $request
*/
public static function show($val, $showHeader = true, ?HTTPRequest $request = null)
{
// Don't show on live
if (Director::isLive()) {
return;
}
echo static::create_debug_view($request)
->debugVariable($val, static::caller(), $showHeader);
}
/**
* Returns the caller for a specific method
*
* @return array
*/
public static function caller()
{
$bt = debug_backtrace();
$caller = isset($bt[2]) ? $bt[2] : [];
$caller['line'] = $bt[1]['line'];
$caller['file'] = $bt[1]['file'];
if (!isset($caller['class'])) {
$caller['class'] = '';
}
if (!isset($caller['type'])) {
$caller['type'] = '';
}
if (!isset($caller['function'])) {
$caller['function'] = '';
}
return $caller;
}
/**
* Close out the show dumper.
* Does not work on live mode
*
* @param mixed $val
* @param bool $showHeader
* @param HTTPRequest $request
*/
public static function endshow($val, $showHeader = true, ?HTTPRequest $request = null)
{
// Don't show on live
if (Director::isLive()) {
return;
}
echo static::create_debug_view($request)
->debugVariable($val, static::caller(), $showHeader);
die();
}
/**
* Quick dump of a variable.
* Note: This method will output in live!
*
* @param mixed $val
* @param HTTPRequest $request Current request to influence output format
*/
public static function dump($val, ?HTTPRequest $request = null)
{
echo Debug::create_debug_view($request)
->renderVariable($val, Debug::caller());
}
/**
* Get debug text for this object
*
* @param mixed $val
* @param HTTPRequest $request
* @return string
*/
public static function text($val, ?HTTPRequest $request = null)
{
return static::create_debug_view($request)
->debugVariableText($val);
}
/**
* Show a debugging message.
* Does not work on live mode
*
* @param string $message
* @param bool $showHeader
* @param HTTPRequest|null $request
*/
public static function message($message, $showHeader = true, ?HTTPRequest $request = null)
{
// Don't show on live
if (Director::isLive()) {
return;
}
echo static::create_debug_view($request)
->renderMessage($message, static::caller(), $showHeader);
}
/**
* Create an instance of an appropriate DebugView object.
*
* @param HTTPRequest $request Optional request to target this view for
* @return DebugView
*/
public static function create_debug_view(?HTTPRequest $request = null)
{
$service = static::supportsHTML($request)
? DebugView::class
: CliDebugView::class;
return Injector::inst()->get($service);
}
/**
* Determine if the given request supports html output
*
* @param HTTPRequest $request
* @return bool
*/
protected static function supportsHTML(?HTTPRequest $request = null)
{
// No HTML output in CLI
if (Director::is_cli()) {
return false;
}
// Get current request if registered
if (!$request && Injector::inst()->has(HTTPRequest::class)) {
$request = Injector::inst()->get(HTTPRequest::class);
}
if (!$request) {
return false;
}
// Request must include text/html
$accepted = $request->getAcceptMimetypes(false);
// Explicit opt in
if (in_array('text/html', $accepted ?? [])) {
return true;
};
// Implicit opt-out
if (in_array('application/json', $accepted ?? [])) {
return false;
}
// Fallback to wildcard comparison
if (in_array('*/*', $accepted ?? [])) {
return true;
}
return false;
}
/**
* Check if the user has permissions to run URL debug tools,
* else redirect them to log in.
*/
public static function require_developer_login()
{
// Don't require login for dev mode
if (Director::isDev()) {
return;
}
if (isset($_SESSION['loggedInAs'])) {
// We have to do some raw SQL here, because this method is called in Object::defineMethods().
// This means we have to be careful about what objects we create, as we don't want Object::defineMethods()
// being called again.
// This basically calls Permission::checkMember($_SESSION['loggedInAs'], 'ADMIN');
$memberID = $_SESSION['loggedInAs'];
$permission = DB::prepared_query(
'
SELECT "ID" FROM "Permission"
INNER JOIN "Group_Members" ON "Permission"."GroupID" = "Group_Members"."GroupID"
WHERE "Permission"."Code" = ?
AND "Permission"."Type" = ?
AND "Group_Members"."MemberID" = ?',
[
'ADMIN', // Code
Permission::GRANT_PERMISSION, // Type
$memberID // MemberID
]
)->value();
if ($permission) {
return;
}
}
// This basically does the same as
// Security::permissionFailure(null, "You need to login with developer access to make use of debugging tools.")
// We have to do this because of how early this method is called in execution.
$_SESSION['SilverStripe\\Security\\Security']['Message']['message']
= "You need to login with developer access to make use of debugging tools.";
$_SESSION['SilverStripe\\Security\\Security']['Message']['type'] = 'warning';
$_SESSION['BackURL'] = $_SERVER['REQUEST_URI'];
header($_SERVER['SERVER_PROTOCOL'] . " 302 Found");
header("Location: " . Director::baseURL() . Security::login_url());
die();
}
}