Module.php 3.46 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 11
use Yii;
use yii\base\View;
12
use yii\web\HttpException;
Qiang Xue committed
13

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

Qiang Xue committed
52 53 54 55

	public function init()
	{
		parent::init();
Qiang Xue committed
56
		$this->dataPath = Yii::getAlias($this->dataPath);
Qiang Xue committed
57 58 59
		$this->logTarget = Yii::$app->getLog()->targets['debug'] = new LogTarget($this);
		Yii::$app->getView()->on(View::EVENT_END_BODY, array($this, 'renderToolbar'));

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

	public function beforeAction($action)
	{
		Yii::$app->getView()->off(View::EVENT_END_BODY, array($this, 'renderToolbar'));
Qiang Xue committed
70
		unset(Yii::$app->getLog()->targets['debug']);
Qiang Xue committed
71
		$this->logTarget = null;
Qiang Xue committed
72

73
		if ($this->checkAccess($action)) {
Qiang Xue committed
74
			return parent::beforeAction($action);
75 76 77 78
		} elseif ($action->id === 'toolbar') {
			return false;
		} else {
			throw new HttpException(403, 'You are not allowed to access this page.');
Qiang Xue committed
79
		}
Qiang Xue committed
80 81 82 83
	}

	public function renderToolbar($event)
	{
84 85 86
		if (!$this->checkAccess()) {
			return;
		}
Qiang Xue committed
87
		$url = Yii::$app->getUrlManager()->createUrl($this->id . '/default/toolbar', array(
88
			'tag' => $this->logTarget->tag,
Qiang Xue committed
89
		));
Qiang Xue committed
90
		echo '<div id="yii-debug-toolbar" data-url="' . $url . '" style="display:none"></div>';
91 92
		/** @var View $view */
		$view = $event->sender;
Qiang Xue committed
93 94
		echo '<style>' . $view->renderPhpFile(__DIR__ . '/views/default/toolbar.css') . '</style>';
		echo '<script>' . $view->renderPhpFile(__DIR__ . '/views/default/toolbar.js') . '</script>';
Qiang Xue committed
95
	}
Qiang Xue committed
96

97 98 99 100 101 102 103 104 105 106 107
	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;
			}
		}
		return false;
	}

Qiang Xue committed
108 109 110
	protected function corePanels()
	{
		return array(
Qiang Xue committed
111 112 113
			'config' => array(
				'class' => 'yii\debug\panels\ConfigPanel',
			),
Qiang Xue committed
114 115 116 117 118 119
			'request' => array(
				'class' => 'yii\debug\panels\RequestPanel',
			),
			'log' => array(
				'class' => 'yii\debug\panels\LogPanel',
			),
Qiang Xue committed
120 121 122 123 124 125
			'profiling' => array(
				'class' => 'yii\debug\panels\ProfilingPanel',
			),
			'db' => array(
				'class' => 'yii\debug\panels\DbPanel',
			),
Qiang Xue committed
126 127
		);
	}
resurtm committed
128
}