|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348 |
- <?php
-
-
- namespace yii\debug;
-
- use Yii;
- use yii\base\Application;
- use yii\base\BootstrapInterface;
- use yii\helpers\Json;
- use yii\web\Response;
- use yii\helpers\Html;
- use yii\helpers\Url;
- use yii\web\View;
- use yii\web\ForbiddenHttpException;
-
-
- class Module extends \yii\base\Module implements BootstrapInterface
- {
- const DEFAULT_IDE_TRACELINE = '<a href="ide://open?url=file://{file}&line={line}">{text}</a>';
-
-
-
- public $allowedIPs = ['127.0.0.1', '::1'];
-
-
- public $allowedHosts = [];
-
-
- public $controllerNamespace = 'yii\debug\controllers';
-
-
- public $logTarget;
-
-
- public $panels = [];
-
-
- public $defaultPanel = 'log';
-
-
- public $dataPath = '@runtime/debug';
-
-
- public $fileMode;
-
-
- public $dirMode = 0775;
-
-
- public $historySize = 50;
-
-
- public $enableDebugLogs = false;
-
-
- public $traceLine = self::DEFAULT_IDE_TRACELINE;
-
-
-
- private static $_yiiLogo = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADwAAAA8CAMAAAANIilAAAAC7lBMVEUAAACl034Cb7HlcjGRyT/H34fyy5PxqlSfzjwQeb5PmtX71HAMdrWOxkDzmU3qcDSPx0HzhUGNxT+/2lX2olDmUy/Q1l+TyD7rgjq21k3ZRzDQ4GGFw0Ghzz6MwOkKdrTA2lTzzMVjo9mhzkCIxUPk1MLynU7qWS33vmbP1rm011Fwqsj123/r44tUltTyq1aCxEOo0EL1tFuCw0Npp9v7xGVHkM8Ddrza0pvC3FboczHmXSvE21h+wkRkpNHvjkS92FPW3avpeDT2t1zX5GefzUD6wGQReLtMltPN417oczPZ0L+62FF+tuJgqtXZUzNzrN3s4Y7n65y72FLwmk7xjESr0kYof8MQe8DY5Gc6jMnN32DoaDLbTiLulUo1hsni45vuwnIigMXC21dqq8vKzaaBt+XU4mUMd7wDdr7xlUrU4a7A2VTD0LbVx5vvpFP/0m9godp/tuTD0LVyrsfZVDUuhMjkPChsrMt3suK92VDd52oEc7un0EKjzj7D21e01EuSyD2fzDvH3Fqu0kcDdL641k+x00rmXy0EdLiayzzynU2XyTzxmUur0ETshD7lZDDvkUbtiUDrgTvqfjrkWS292FPujEKAuObQ4GH3vWH1slr0r1j0pVLulEiPxj7oeDRnptn4zWrM31/1t13A2lb1rFb1qVS72FKHw0CLxD/qdTfnazL4wGPJ3VzwpFLpcjKFveljo9dfn9ZbntUYfcEIdr35w2XyoFH0ok/pfDZ9tONUmNRPltJIj89Ais388IL85Hn82nL80W33uV72tFy611DxlUnujkSCwkGlz0DqeTnocDJ3r99yrN1Xm9RFjc42hsorgsYhgMQPer/81XD5yGbT4mTriD/lbS3laCvjTiluqN5NktAxhMf853v84He/2VTgVCnmVSg8h8sHcrf6633+3nb8zGr2xmR/wEGcyzt3r+T/6n7tm01tqNnfSCnfPyO4zLmFwkDVRDGOweLP1aX55nrZTTOaxdjuY9uiAAAAfHRSTlMABv7+9hAJ/vMyGP2CbV5DOA+NbyYeG/DV0sC/ubaonYN5blZRQT41MSUk/v797+zj49PR0MXEw8PDu6imppqYlpOGhYN+bldWVFJROjAM+fPy8fDw8O7t6+vp5+Lh4N7e3Nvb2NPQ0MW8urm2rqiimJKFg3t5amZTT0k1ewExHwAABPVJREFUSMed1Xc81HEYB/DvhaOUEe29995777333ntv2sopUTQ4F104hRBSl8ohldCwOqfuuEiKaPdfz/P7/u6Syuu+ff727vM8z+8bhDHNB3Tr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sTQmKxor1EXFcXEDKnJf6q9xOBMCPvyVQG6aDGZhw80x8ZwK1h5ISzsRwe1Wt2B1MPHPZgYnqa3b1+4gOUKhUl/sP0Z7ITJycmowz5q3oxrfMBvvYBh6O7ZKcnvqY7dZuPXR8hQvOXSJdQc/7hhTB8TBjs6Ivz6pezsbKobmggYbJWOT1ADT8HFGxKW9LwTjRp4CujbTHj007t37kRHhGP5h5Tk5K0MduLce0/vvoyOjoiIuH4ddMoeBrzz2WvUMDrMDvpDFQa89Pkr4KCBo+7OYEdFpqLGcqqbMuDVaZGpqc/1OjycYerKohtpkZFl9ECG4qoihxvA9aN3ZDlXL5GDXR7Vr56BZtlYcAOwnQMdHXRPlmdd2U5kh5gffRHL0GSUXR5gKBeJ0tIiZ1UmLKlqlydygHD1s8EyYYe8PBFMjulVhbClEdy6kohLVTaJGEYW4eBr6MhsY1fi0ggoe7a3a7d84O6J5L8iNOiX3U+uoa/p8UPtoQAAAABJRU5ErkJggg==';
-
-
-
-
- public static function getYiiLogo()
- {
- return self::$_yiiLogo;
- }
-
-
-
- public static function setYiiLogo($logo)
- {
- self::$_yiiLogo = $logo;
- }
-
-
-
- public function init()
- {
- parent::init();
- $this->dataPath = Yii::getAlias($this->dataPath);
- $this->initPanels();
- }
-
-
-
- protected function initPanels()
- {
-
- if (empty($this->panels)) {
- $this->panels = $this->corePanels();
- } else {
- $corePanels = $this->corePanels();
- foreach ($corePanels as $id => $config) {
- if (isset($this->panels[$id])) {
- unset($corePanels[$id]);
- }
- }
- $this->panels = array_filter(array_merge($corePanels, $this->panels));
- }
-
- foreach ($this->panels as $id => $config) {
- if (is_string($config)) {
- $config = ['class' => $config];
- }
- $config['module'] = $this;
- $config['id'] = $id;
- $this->panels[$id] = Yii::createObject($config);
- }
- }
-
-
-
- public function bootstrap($app)
- {
- $this->logTarget = Yii::$app->getLog()->targets['debug'] = new LogTarget($this);
-
-
- $app->on(Application::EVENT_BEFORE_REQUEST, function () use ($app) {
- $app->getView()->on(View::EVENT_END_BODY, [$this, 'renderToolbar']);
- $app->getResponse()->on(Response::EVENT_AFTER_PREPARE, [$this, 'setDebugHeaders']);
- });
-
- $app->getUrlManager()->addRules([
- [
- 'class' => 'yii\web\UrlRule',
- 'route' => $this->id,
- 'pattern' => $this->id,
- ],
- [
- 'class' => 'yii\web\UrlRule',
- 'route' => $this->id . '/<controller>/<action>',
- 'pattern' => $this->id . '/<controller:[\w\-]+>/<action:[\w\-]+>',
- ]
- ], false);
- }
-
-
-
- public function beforeAction($action)
- {
- if (!$this->enableDebugLogs) {
- foreach (Yii::$app->getLog()->targets as $target) {
- $target->enabled = false;
- }
- }
-
- if (!parent::beforeAction($action)) {
- return false;
- }
-
-
- Yii::$app->getView()->off(View::EVENT_END_BODY, [$this, 'renderToolbar']);
- Yii::$app->getResponse()->off(Response::EVENT_AFTER_PREPARE, [$this, 'setDebugHeaders']);
-
- if ($this->checkAccess()) {
- $this->resetGlobalSettings();
- return true;
- } elseif ($action->id === 'toolbar') {
-
- return false;
- } else {
- throw new ForbiddenHttpException('You are not allowed to access this page.');
- }
- }
-
-
-
- public function setDebugHeaders($event)
- {
- if (!$this->checkAccess() || !Yii::$app->getRequest()->getIsAjax()) {
- return;
- }
- $url = Url::toRoute(['/' . $this->id . '/default/view',
- 'tag' => $this->logTarget->tag,
- ]);
- $event->sender->getHeaders()
- ->set('X-Debug-Tag', $this->logTarget->tag)
- ->set('X-Debug-Duration', number_format((microtime(true) - YII_BEGIN_TIME) * 1000 + 1))
- ->set('X-Debug-Link', $url);
- }
-
-
-
- protected function resetGlobalSettings()
- {
- Yii::$app->assetManager->bundles = [];
- }
-
-
-
- public function getToolbarHtml()
- {
- $url = Url::toRoute(['/' . $this->id . '/default/toolbar',
- 'tag' => $this->logTarget->tag,
- ]);
- return '<div id="yii-debug-toolbar" data-url="' . Html::encode($url) . '" style="display:none" class="yii-debug-toolbar-bottom"></div>';
- }
-
-
-
- public function renderToolbar($event)
- {
- if (!$this->checkAccess() || Yii::$app->getRequest()->getIsAjax()) {
- return;
- }
-
-
- $view = $event->sender;
- echo $view->renderDynamic('return Yii::$app->getModule("debug")->getToolbarHtml();');
-
-
- echo '<style>' . $view->renderPhpFile(__DIR__ . '/assets/toolbar.css') . '</style>';
- echo '<script>' . $view->renderPhpFile(__DIR__ . '/assets/toolbar.js') . '</script>';
- }
-
-
-
- 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;
- }
- }
- foreach ($this->allowedHosts as $hostname) {
- $filter = gethostbyname($hostname);
- if ($filter === $ip) {
- return true;
- }
- }
- Yii::warning('Access to debugger is denied due to IP address restriction. The requesting IP address is ' . $ip, __METHOD__);
- return false;
- }
-
-
-
- protected function corePanels()
- {
- 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'],
- 'assets' => ['class' => 'yii\debug\panels\AssetPanel'],
- 'mail' => ['class' => 'yii\debug\panels\MailPanel'],
- 'timeline' => ['class' => 'yii\debug\panels\TimelinePanel']
- ];
- }
-
-
-
- protected function defaultVersion()
- {
- $packageInfo = Json::decode(file_get_contents(__DIR__ . DIRECTORY_SEPARATOR . 'composer.json'));
- $extensionName = $packageInfo['name'];
- if (isset(Yii::$app->extensions[$extensionName])) {
- return Yii::$app->extensions[$extensionName]['version'];
- }
- return parent::defaultVersion();
- }
- }
|