Module.php 4.06 KB
Newer Older
Qiang Xue committed
1 2 3 4 5 6 7 8 9
<?php
/**
 * @link http://www.yiiframework.com/
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */

namespace yii\debug;

Qiang Xue committed
10
use Yii;
11
use yii\base\Application;
Alexander Makarov committed
12
use yii\web\View;
13
use yii\web\ForbiddenHttpException;
Mark committed
14
use yii\helpers\ArrayHelper;
Qiang Xue committed
15

Qiang Xue committed
16
/**
17 18
 * The Yii Debug Module provides the debug toolbar and debugger
 *
Qiang Xue committed
19 20 21 22 23
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @since 2.0
 */
class Module extends \yii\base\Module
{
Qiang Xue committed
24 25 26 27
	/**
	 * @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.
Alexander Makarov committed
28
	 * The default value is `['127.0.0.1', '::1']`, which means the module can only be accessed
Qiang Xue committed
29 30
	 * by localhost.
	 */
Alexander Makarov committed
31
	public $allowedIPs = ['127.0.0.1', '::1'];
32 33 34
	/**
	 * @var string the namespace that controller classes are in.
	 */
Qiang Xue committed
35
	public $controllerNamespace = 'yii\debug\controllers';
Qiang Xue committed
36 37 38 39
	/**
	 * @var LogTarget
	 */
	public $logTarget;
Qiang Xue committed
40 41 42
	/**
	 * @var array|Panel[]
	 */
Alexander Makarov committed
43
	public $panels = [];
Qiang Xue committed
44 45 46 47
	/**
	 * @var string the directory storing the debugger data files. This can be specified using a path alias.
	 */
	public $dataPath = '@runtime/debug';
48 49 50 51
	/**
	 * @var integer the maximum number of debug data files to keep. If there are more files generated,
	 * the oldest ones will be removed.
	 */
Qiang Xue committed
52
	public $historySize = 50;
53

54 55 56
	/**
	 * @inheritdoc
	 */
Qiang Xue committed
57 58 59
	public function init()
	{
		parent::init();
Qiang Xue committed
60
		$this->dataPath = Yii::getAlias($this->dataPath);
Qiang Xue committed
61
		$this->logTarget = Yii::$app->getLog()->targets['debug'] = new LogTarget($this);
62
		// do not initialize view component before application is ready (needed when debug in preload)
63
		Yii::$app->on(Application::EVENT_BEFORE_REQUEST, function() {
Alexander Makarov committed
64
			Yii::$app->getView()->on(View::EVENT_END_BODY, [$this, 'renderToolbar']);
65
		});
Qiang Xue committed
66

Mark committed
67
		$this->panels = ArrayHelper::merge($this->corePanels(), $this->panels);
68
		foreach ($this->panels as $id => $config) {
Qiang Xue committed
69
			$config['module'] = $this;
Qiang Xue committed
70
			$config['id'] = $id;
Qiang Xue committed
71 72
			$this->panels[$id] = Yii::createObject($config);
		}
Qiang Xue committed
73 74
	}

75 76 77
	/**
	 * @inheritdoc
	 */
Qiang Xue committed
78 79
	public function beforeAction($action)
	{
Alexander Makarov committed
80
		Yii::$app->getView()->off(View::EVENT_END_BODY, [$this, 'renderToolbar']);
Qiang Xue committed
81
		unset(Yii::$app->getLog()->targets['debug']);
Qiang Xue committed
82
		$this->logTarget = null;
Qiang Xue committed
83

84
		if ($this->checkAccess()) {
Qiang Xue committed
85
			return parent::beforeAction($action);
86 87 88
		} elseif ($action->id === 'toolbar') {
			return false;
		} else {
89
			throw new ForbiddenHttpException('You are not allowed to access this page.');
Qiang Xue committed
90
		}
Qiang Xue committed
91 92
	}

93 94 95 96 97
	/**
	 * Renders mini-toolbar at the end of page body.
	 *
	 * @param \yii\base\Event $event
	 */
Qiang Xue committed
98 99
	public function renderToolbar($event)
	{
100
		if (!$this->checkAccess() || Yii::$app->getRequest()->getIsAjax()) {
101 102
			return;
		}
Alexander Makarov committed
103
		$url = Yii::$app->getUrlManager()->createUrl($this->id . '/default/toolbar', [
104
			'tag' => $this->logTarget->tag,
Alexander Makarov committed
105
		]);
Qiang Xue committed
106
		echo '<div id="yii-debug-toolbar" data-url="' . $url . '" style="display:none"></div>';
107 108
		/** @var View $view */
		$view = $event->sender;
109 110
		echo '<style>' . $view->renderPhpFile(__DIR__ . '/assets/toolbar.css') . '</style>';
		echo '<script>' . $view->renderPhpFile(__DIR__ . '/assets/toolbar.js') . '</script>';
Qiang Xue committed
111
	}
Qiang Xue committed
112

113 114 115 116
	/**
	 * Checks if current user is allowed to access the module
	 * @return boolean if access is granted
	 */
117 118 119 120 121 122 123 124
	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;
			}
		}
125
		Yii::warning('Access to debugger is denied due to IP address restriction. The requested IP is ' . $ip, __METHOD__);
126 127 128
		return false;
	}

129 130 131
	/**
	 * @return array default set of panels
	 */
Qiang Xue committed
132 133
	protected function corePanels()
	{
Alexander Makarov committed
134 135 136 137 138 139 140
		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'],
		];
Qiang Xue committed
141
	}
resurtm committed
142
}