Module.php 4.02 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;
Qiang Xue committed
14

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

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

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

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

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

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

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

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