This repository has been archived by the owner on Sep 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 92
/
YiiDebugToolbarRoute.php
274 lines (235 loc) · 7.43 KB
/
YiiDebugToolbarRoute.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
<?php
/**
* YiiDebugToolbarRouter class file.
*
* @author Sergey Malyshev <malyshev.php@gmail.com>
*/
/**
* YiiDebugToolbarRouter represents an ...
*
* Description of YiiDebugToolbarRouter
*
* @author Sergey Malyshev <malyshev.php@gmail.com>
* @version $Id$
* @package YiiDebugToolbar
* @since 1.1.7
*/
class YiiDebugToolbarRoute extends CLogRoute
{
private $_panels = array(
//'YiiDebugToolbarPanelServer',
'YiiDebugToolbarPanelRequest',
'YiiDebugToolbarPanelSettings',
'YiiDebugToolbarPanelViews',
'YiiDebugToolbarPanelSql',
'YiiDebugToolbarPanelLogging',
);
/**
* The filters are given in an array, each filter being:
* - a normal IP (192.168.0.10 or '::1')
* - an incomplete IP (192.168.0.* or 192.168.0.)
* - a CIDR mask (192.168.0.0/24)
* - "*" for everything.
*/
public $ipFilters=array('127.0.0.1','::1');
/**
* Whitelist for response content types. DebugToolbarRoute won't write any
* output if the server generates output that isn't listed here (json, xml,
* files, ...)
* @var array of content type strings (in lower case)
*/
public $contentTypeWhitelist = array(
// Yii framework doesn't seem to send content-type header by default.
'',
'text/html',
'application/xhtml+xml',
);
private $_toolbarWidget,
$_startTime,
$_endTime;
private $_proxyMap = array(
'viewRenderer' => 'YiiDebugViewRenderer'
);
public function setPanels(array $pannels)
{
$selfPanels = array_fill_keys($this->_panels, array());
$this->_panels = array_merge($selfPanels, $pannels);
}
public function getPanels()
{
return $this->_panels;
}
public function getStartTime()
{
return $this->_startTime;
}
public function getEndTime()
{
return $this->_endTime;
}
public function getLoadTime()
{
return ($this->endTime-$this->startTime);
}
protected function getToolbarWidget()
{
if (null === $this->_toolbarWidget)
{
$this->_toolbarWidget = Yii::createComponent(array(
'class'=>'YiiDebugToolbar',
'panels'=> $this->panels
), $this);
}
return $this->_toolbarWidget;
}
public function init()
{
Yii::app()->controllerMap = array_merge(array(
'debug' => array(
'class' => 'YiiDebugController'
)
), Yii::app()->controllerMap);
Yii::setPathOfAlias('yii-debug-toolbar', dirname(__FILE__));
Yii::app()->setImport(array(
'yii-debug-toolbar.*'
));
$route = Yii::app()->getUrlManager()->parseUrl(Yii::app()->getRequest());
$this->enabled = strpos(trim($route, '/'), 'debug') !== 0;
$this->enabled && $this->enabled = ($this->allowIp(Yii::app()->request->userHostAddress)
&& !Yii::app()->getRequest()->getIsAjaxRequest() && (Yii::app() instanceof CWebApplication))
&& $this->checkContentTypeWhitelist();
if ($this->enabled) {
Yii::app()->attachEventHandler('onBeginRequest', array($this, 'onBeginRequest'));
Yii::app()->attachEventHandler('onEndRequest', array($this, 'onEndRequest'));
$this->categories = '';
$this->levels='';
}
$this->_startTime = microtime(true);
parent::init();
}
protected function onBeginRequest(CEvent $event)
{
$this->initComponents();
$this->getToolbarWidget()
->init();
}
protected function initComponents()
{
foreach ($this->_proxyMap as $name=>$class) {
$instance = Yii::app()->getComponent($name);
if (null !== ($instance)) {
Yii::app()->setComponent($name, null);
}
$this->_proxyMap[$name] = array(
'class'=>$class,
'instance' => $instance
);
}
Yii::app()->setComponents($this->_proxyMap, false);
}
/**
* Processes the current request.
* It first resolves the request into controller and action,
* and then creates the controller to perform the action.
*/
private function processRequest()
{
if (is_array(Yii::app()->catchAllRequest) && isset(Yii::app()->catchAllRequest[0])) {
$route = Yii::app()->catchAllRequest[0];
foreach(array_splice(Yii::app()->catchAllRequest,1) as $name=>$value)
$_GET[$name] = $value;
} else {
$route = Yii::app()->getUrlManager()->parseUrl(Yii::app()->getRequest());
}
Yii::app()->runController($route);
}
protected function onEndRequest(CEvent $event)
{
$this->_endTime = microtime(true);
}
public function collectLogs($logger, $processLogs=false)
{
$logs = $logger->getLogs();
$this->logs = empty($this->logs) ? $logs : array_merge($this->logs, $logs);
$this->processLogs($this->logs);
$this->logs = array();
}
protected function processLogs($logs)
{
$this->getToolbarWidget()->run();
}
private function checkContentTypeWhitelist()
{
$contentType = '';
foreach (headers_list() as $header) {
list($key, $value) = explode(':', $header);
$value = ltrim($value, ' ');
if (strtolower($key) === 'content-type') {
// Split encoding if exists
$contentType = explode(";", strtolower($value));
$contentType = current($contentType);
break;
}
}
return in_array( $contentType, $this->contentTypeWhitelist );
}
/**
* Checks to see if the user IP is allowed by {@link ipFilters}.
* @param string $ip the user IP
* @return boolean whether the user IP is allowed by {@link ipFilters}.
*/
protected function allowIp($ip)
{
foreach ($this->ipFilters as $filter)
{
$filter = trim($filter);
// normal or incomplete IPv4
if (preg_match('/^[\d\.]*\*?$/', $filter)) {
$filter = rtrim($filter, '*');
if (strncmp($ip, $filter, strlen($filter)) === 0)
{
return true;
}
}
// CIDR
else if (preg_match('/^([\d\.]+)\/(\d+)$/', $filter, $match))
{
if (self::matchIpMask($ip, $match[1], $match[2]))
{
return true;
}
}
// IPv6
else if ($ip === $filter)
{
return true;
}
}
return false;
}
/**
* Check if an IP matches a CIDR mask.
*
* @param integer|string $ip IP to check.
* @param integer|string $matchIp Radical of the mask (e.g. 192.168.0.0).
* @param integer $maskBits Size of the mask (e.g. 24).
*/
protected static function matchIpMask($ip, $maskIp, $maskBits)
{
$mask =~ (pow(2, 32-$maskBits)-1);
if (false === is_int($ip))
{
$ip = ip2long($ip);
}
if (false === is_int($maskIp))
{
$maskIp = ip2long($maskIp);
}
if (($ip & $mask) === ($maskIp & $mask))
{
return true;
} else {
return false;
}
}
}