forked from yiisoft/yii2-debug
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModule.php
348 lines (321 loc) · 14.3 KB
/
Module.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\debug;
use Yii;
use yii\base\Application;
use yii\base\BootstrapInterface;
use yii\helpers\Json;
use yii\web\Response;
use yii\helpers\Html;
use yii\helpers\Url;
use yii\web\View;
use yii\web\ForbiddenHttpException;
/**
* The Yii Debug Module provides the debug toolbar and debugger
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class Module extends \yii\base\Module implements BootstrapInterface
{
const DEFAULT_IDE_TRACELINE = '<a href="ide://open?url=file://{file}&line={line}">{text}</a>';
/**
* @var array the list of IPs that are allowed to access this module.
* Each array element represents a single IP filter which can be either an IP address
* or an address with wildcard (e.g. 192.168.0.*) to represent a network segment.
* The default value is `['127.0.0.1', '::1']`, which means the module can only be accessed
* by localhost.
*/
public $allowedIPs = ['127.0.0.1', '::1'];
/**
* @var array the list of hosts that are allowed to access this module.
* Each array element is a hostname that will be resolved to an IP address that is compared
* with the IP address of the user. A use case is to use a dynamic DNS (DDNS) to allow access.
* The default value is `[]`.
*/
public $allowedHosts = [];
/**
* @inheritdoc
*/
public $controllerNamespace = 'yii\debug\controllers';
/**
* @var LogTarget
*/
public $logTarget;
/**
* @var array|Panel[] list of debug panels. The array keys are the panel IDs, and values are the corresponding
* panel class names or configuration arrays. This will be merged with [[corePanels()]].
* You may reconfigure a core panel via this property by using the same panel ID.
* You may also disable a core panel by setting it to be false in this property.
*/
public $panels = [];
/**
* @var string the name of the panel that should be visible when opening the debug panel.
* The default value is 'log'.
* @since 2.0.7
*/
public $defaultPanel = 'log';
/**
* @var string the directory storing the debugger data files. This can be specified using a path alias.
*/
public $dataPath = '@runtime/debug';
/**
* @var integer the permission to be set for newly created debugger data files.
* This value will be used by PHP [[chmod()]] function. No umask will be applied.
* If not set, the permission will be determined by the current environment.
* @since 2.0.6
*/
public $fileMode;
/**
* @var integer the permission to be set for newly created directories.
* This value will be used by PHP [[chmod()]] function. No umask will be applied.
* Defaults to 0775, meaning the directory is read-writable by owner and group,
* but read-only for other users.
* @since 2.0.6
*/
public $dirMode = 0775;
/**
* @var integer the maximum number of debug data files to keep. If there are more files generated,
* the oldest ones will be removed.
*/
public $historySize = 50;
/**
* @var boolean whether to enable message logging for the requests about debug module actions.
* You normally do not want to keep these logs because they may distract you from the logs about your applications.
* You may want to enable the debug logs if you want to investigate how the debug module itself works.
*/
public $enableDebugLogs = false;
/**
* @var mixed the string with placeholders to be be substituted or an anonymous function that returns the trace line string.
* The placeholders are {file}, {line} and {text} and the string should be as follows:
*
* `File: {file} - Line: {line} - Text: {text}`
*
* The signature of the anonymous function should be as follows:
*
* ```php
* function($trace, $panel) {
* // compute line string
* return $line;
* }
* ```
* @since 2.0.7
*/
public $traceLine = self::DEFAULT_IDE_TRACELINE;
/**
* @var string Yii logo URL
*/
private static $_yiiLogo = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADwAAAA8CAMAAAANIilAAAAC7lBMVEUAAACl034Cb7HlcjGRyT/H34fyy5PxqlSfzjwQeb5PmtX71HAMdrWOxkDzmU3qcDSPx0HzhUGNxT+/2lX2olDmUy/Q1l+TyD7rgjq21k3ZRzDQ4GGFw0Ghzz6MwOkKdrTA2lTzzMVjo9mhzkCIxUPk1MLynU7qWS33vmbP1rm011Fwqsj123/r44tUltTyq1aCxEOo0EL1tFuCw0Npp9v7xGVHkM8Ddrza0pvC3FboczHmXSvE21h+wkRkpNHvjkS92FPW3avpeDT2t1zX5GefzUD6wGQReLtMltPN417oczPZ0L+62FF+tuJgqtXZUzNzrN3s4Y7n65y72FLwmk7xjESr0kYof8MQe8DY5Gc6jMnN32DoaDLbTiLulUo1hsni45vuwnIigMXC21dqq8vKzaaBt+XU4mUMd7wDdr7xlUrU4a7A2VTD0LbVx5vvpFP/0m9godp/tuTD0LVyrsfZVDUuhMjkPChsrMt3suK92VDd52oEc7un0EKjzj7D21e01EuSyD2fzDvH3Fqu0kcDdL641k+x00rmXy0EdLiayzzynU2XyTzxmUur0ETshD7lZDDvkUbtiUDrgTvqfjrkWS292FPujEKAuObQ4GH3vWH1slr0r1j0pVLulEiPxj7oeDRnptn4zWrM31/1t13A2lb1rFb1qVS72FKHw0CLxD/qdTfnazL4wGPJ3VzwpFLpcjKFveljo9dfn9ZbntUYfcEIdr35w2XyoFH0ok/pfDZ9tONUmNRPltJIj89Ais388IL85Hn82nL80W33uV72tFy611DxlUnujkSCwkGlz0DqeTnocDJ3r99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1vOVRj5wLVwY7U2dvR/vGRy1BB7TsgMH/HKAQzfVZlZEF0sjwHgtLC7GbySjvWCjojYS0vjIEcpBH8WTmwmIPmON4GEChksXF8MnotYX7NuMDGkb0vbaEeQ50E11A1R67SOnUzsjlsjgzvHx8cFRQKUFvQmpd/kaaD+sPoiYrqyfvDY39QPYOMTU1F8shn09g98WSOPi4szbEBuPy8BRY7V9l3L/34VDy2AvsdgXLfTGmZun9yY1PTw8Ll+DwenWI0j52A6awWGJzNQLj0VtenpsbHshWZXpQasTYO6ZJuTPCC3WQjFeix5LKpWap8dqNJohZHgmaA5DtQ35e6wtNnXS4wwojn2jUSimkH2ZtBpxnYp+67ce1pX7xBkF1KrV+S3IHIrxYuNJxbEd2SM4qoDDim/5+THrSD09bmzIn5eRPTiMNmYqLM2PDUMblNabzaE5PwbSZowHPdi0tsTQmKxor1EXFcXEDKnJf6q9xOBMCPvyVQG6aDGZhw80x8ZwK1h5ISzsRwe1Wt2B1MPHPZgYnqa3b1+4gOUKhUl/sP0Z7ITJycmowz5q3oxrfMBvvYBh6O7ZKcnvqY7dZuPXR8hQvOXSJdQc/7hhTB8TBjs6Ivz6pezsbKobmggYbJWOT1ADT8HFGxKW9LwTjRp4CujbTHj007t37kRHhGP5h5Tk5K0MduLce0/vvoyOjoiIuH4ddMoeBrzz2WvUMDrMDvpDFQa89Pkr4KCBo+7OYEdFpqLGcqqbMuDVaZGpqc/1OjycYerKohtpkZFl9ECG4qoihxvA9aN3ZDlXL5GDXR7Vr56BZtlYcAOwnQMdHXRPlmdd2U5kh5gffRHL0GSUXR5gKBeJ0tIiZ1UmLKlqlydygHD1s8EyYYe8PBFMjulVhbClEdy6kohLVTaJGEYW4eBr6MhsY1fi0ggoe7a3a7d84O6J5L8iNOiX3U+uoa/p8UPtoQAAAABJRU5ErkJggg==';
/**
* Returns the logo URL to be used in `<img src="`
*
* @return string the logo URL
*/
public static function getYiiLogo()
{
return self::$_yiiLogo;
}
/**
* Sets the logo URL to be used in `<img src="`
*
* @param string $logo the logo URL
*/
public static function setYiiLogo($logo)
{
self::$_yiiLogo = $logo;
}
/**
* @inheritdoc
*/
public function init()
{
parent::init();
$this->dataPath = Yii::getAlias($this->dataPath);
$this->initPanels();
}
/**
* Initializes panels.
*/
protected function initPanels()
{
// merge custom panels and core panels so that they are ordered mainly by custom panels
if (empty($this->panels)) {
$this->panels = $this->corePanels();
} else {
$corePanels = $this->corePanels();
foreach ($corePanels as $id => $config) {
if (isset($this->panels[$id])) {
unset($corePanels[$id]);
}
}
$this->panels = array_filter(array_merge($corePanels, $this->panels));
}
foreach ($this->panels as $id => $config) {
if (is_string($config)) {
$config = ['class' => $config];
}
$config['module'] = $this;
$config['id'] = $id;
$this->panels[$id] = Yii::createObject($config);
}
}
/**
* @inheritdoc
*/
public function bootstrap($app)
{
$this->logTarget = Yii::$app->getLog()->targets['debug'] = new LogTarget($this);
// delay attaching event handler to the view component after it is fully configured
$app->on(Application::EVENT_BEFORE_REQUEST, function () use ($app) {
$app->getView()->on(View::EVENT_END_BODY, [$this, 'renderToolbar']);
$app->getResponse()->on(Response::EVENT_AFTER_PREPARE, [$this, 'setDebugHeaders']);
});
$app->getUrlManager()->addRules([
[
'class' => 'yii\web\UrlRule',
'route' => $this->id,
'pattern' => $this->id,
],
[
'class' => 'yii\web\UrlRule',
'route' => $this->id . '/<controller>/<action>',
'pattern' => $this->id . '/<controller:[\w\-]+>/<action:[\w\-]+>',
]
], false);
}
/**
* @inheritdoc
*/
public function beforeAction($action)
{
if (!$this->enableDebugLogs) {
foreach (Yii::$app->getLog()->targets as $target) {
$target->enabled = false;
}
}
if (!parent::beforeAction($action)) {
return false;
}
// do not display debug toolbar when in debug view mode
Yii::$app->getView()->off(View::EVENT_END_BODY, [$this, 'renderToolbar']);
Yii::$app->getResponse()->off(Response::EVENT_AFTER_PREPARE, [$this, 'setDebugHeaders']);
if ($this->checkAccess()) {
$this->resetGlobalSettings();
return true;
} elseif ($action->id === 'toolbar') {
// Accessing toolbar remotely is normal. Do not throw exception.
return false;
} else {
throw new ForbiddenHttpException('You are not allowed to access this page.');
}
}
/**
* Setting headers to transfer debug data in AJAX requests
* without interfering with the request itself.
*
* @param \yii\base\Event $event
* @since 2.0.7
*/
public function setDebugHeaders($event)
{
if (!$this->checkAccess() || !Yii::$app->getRequest()->getIsAjax()) {
return;
}
$url = Url::toRoute(['/' . $this->id . '/default/view',
'tag' => $this->logTarget->tag,
]);
$event->sender->getHeaders()
->set('X-Debug-Tag', $this->logTarget->tag)
->set('X-Debug-Duration', number_format((microtime(true) - YII_BEGIN_TIME) * 1000 + 1))
->set('X-Debug-Link', $url);
}
/**
* Resets potentially incompatible global settings done in app config.
*/
protected function resetGlobalSettings()
{
Yii::$app->assetManager->bundles = [];
}
/**
* Gets the Toolbars html
* @since 2.0.7
*/
public function getToolbarHtml()
{
$url = Url::toRoute(['/' . $this->id . '/default/toolbar',
'tag' => $this->logTarget->tag,
]);
return '<div id="yii-debug-toolbar" data-url="' . Html::encode($url) . '" style="display:none" class="yii-debug-toolbar-bottom"></div>';
}
/**
* Renders mini-toolbar at the end of page body.
*
* @param \yii\base\Event $event
*/
public function renderToolbar($event)
{
if (!$this->checkAccess() || Yii::$app->getRequest()->getIsAjax()) {
return;
}
/* @var $view View */
$view = $event->sender;
echo $view->renderDynamic('return Yii::$app->getModule("debug")->getToolbarHtml();');
// echo is used in order to support cases where asset manager is not available
echo '<style>' . $view->renderPhpFile(__DIR__ . '/assets/toolbar.css') . '</style>';
echo '<script>' . $view->renderPhpFile(__DIR__ . '/assets/toolbar.js') . '</script>';
}
/**
* Checks if current user is allowed to access the module
* @return boolean if access is granted
*/
protected function checkAccess()
{
$ip = Yii::$app->getRequest()->getUserIP();
foreach ($this->allowedIPs as $filter) {
if ($filter === '*' || $filter === $ip || (($pos = strpos($filter, '*')) !== false && !strncmp($ip, $filter, $pos))) {
return true;
}
}
foreach ($this->allowedHosts as $hostname) {
$filter = gethostbyname($hostname);
if ($filter === $ip) {
return true;
}
}
Yii::warning('Access to debugger is denied due to IP address restriction. The requesting IP address is ' . $ip, __METHOD__);
return false;
}
/**
* @return array default set of panels
*/
protected function corePanels()
{
return [
'config' => ['class' => 'yii\debug\panels\ConfigPanel'],
'request' => ['class' => 'yii\debug\panels\RequestPanel'],
'log' => ['class' => 'yii\debug\panels\LogPanel'],
'profiling' => ['class' => 'yii\debug\panels\ProfilingPanel'],
'db' => ['class' => 'yii\debug\panels\DbPanel'],
'assets' => ['class' => 'yii\debug\panels\AssetPanel'],
'mail' => ['class' => 'yii\debug\panels\MailPanel'],
'timeline' => ['class' => 'yii\debug\panels\TimelinePanel']
];
}
/**
* @inheritdoc
* @since 2.0.7
*/
protected function defaultVersion()
{
$packageInfo = Json::decode(file_get_contents(__DIR__ . DIRECTORY_SEPARATOR . 'composer.json'));
$extensionName = $packageInfo['name'];
if (isset(Yii::$app->extensions[$extensionName])) {
return Yii::$app->extensions[$extensionName]['version'];
}
return parent::defaultVersion();
}
}