Module.php 3.73 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

Qiang Xue committed
53 54 55 56

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

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

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

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

	public function renderToolbar($event)
	{
89
		if (!$this->checkAccess() || Yii::$app->getRequest()->getIsAjax()) {
90 91
			return;
		}
Alexander Makarov committed
92
		$url = Yii::$app->getUrlManager()->createUrl($this->id . '/default/toolbar', [
93
			'tag' => $this->logTarget->tag,
Alexander Makarov committed
94
		]);
Qiang Xue committed
95
		echo '<div id="yii-debug-toolbar" data-url="' . $url . '" style="display:none"></div>';
96 97
		/** @var View $view */
		$view = $event->sender;
98 99
		echo '<style>' . $view->renderPhpFile(__DIR__ . '/assets/toolbar.css') . '</style>';
		echo '<script>' . $view->renderPhpFile(__DIR__ . '/assets/toolbar.js') . '</script>';
Qiang Xue committed
100
	}
Qiang Xue committed
101

102 103 104 105 106 107 108 109
	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;
			}
		}
110
		Yii::warning('Access to debugger is denied due to IP address restriction. The requested IP is ' . $ip, __METHOD__);
111 112 113
		return false;
	}

Qiang Xue committed
114 115
	protected function corePanels()
	{
Alexander Makarov committed
116 117 118 119 120 121 122
		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
123
	}
resurtm committed
124
}