-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
[4.0] Improve DebugBar: view history, allow debug POST and non HTML requests, replace json storage to own #22327
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 7 commits
40d993f
fba8387
d7f5977
4f8a47f
ad06e1b
7bfc904
0e51812
6fda29a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,137 @@ | ||
| <?php | ||
| /** | ||
| * @package Joomla.Plugin | ||
| * @subpackage System.Debug | ||
| * | ||
| * @copyright Copyright (C) 2017 Open Source Matters, Inc. All rights reserved. | ||
| * @license GNU General Public License version 2 or later; see LICENSE.txt | ||
| */ | ||
|
|
||
| namespace Joomla\Plugin\System\Debug\Storage; | ||
|
|
||
| use Joomla\Filesystem\Folder; | ||
|
|
||
| /** | ||
| * Stores collected data into files | ||
| * | ||
| * @since __DEPLOY_VERSION__ | ||
| */ | ||
| class FileStorage extends \DebugBar\Storage\FileStorage | ||
| { | ||
| /** | ||
| * Saves collected data | ||
| * | ||
| * @param string $id The log id | ||
| * @param string $data The log data | ||
| * | ||
| * @return void | ||
| * | ||
| * @since __DEPLOY_VERSION__ | ||
| */ | ||
| public function save($id, $data) | ||
| { | ||
| if (!file_exists($this->dirname)) | ||
| { | ||
| Folder::create($this->dirname); | ||
| } | ||
|
|
||
| $dataStr = '<?php die(); ?>#(^-^)#' . json_encode($data); | ||
|
|
||
| file_put_contents($this->makeFilename($id), $dataStr); | ||
| } | ||
|
|
||
| /** | ||
| * Returns collected data with the specified id | ||
| * | ||
| * @param string $id The log id | ||
| * | ||
| * @return array | ||
| * | ||
| * @since __DEPLOY_VERSION__ | ||
| */ | ||
| public function get($id) | ||
| { | ||
| $dataStr = file_get_contents($this->makeFilename($id)); | ||
| $dataStr = str_replace('<?php die(); ?>#(^-^)#', '', $dataStr); | ||
|
|
||
| return json_decode($dataStr, true) ?: array(); | ||
| } | ||
|
|
||
| /** | ||
| * Returns a metadata about collected data | ||
| * | ||
| * @param array $filters Filtering options | ||
| * @param integer $max The limit, items per page | ||
| * @param integer $offset The offset | ||
| * | ||
| * @return array | ||
| * | ||
| * @since __DEPLOY_VERSION__ | ||
| */ | ||
| public function find(array $filters = array(), $max = 20, $offset = 0) | ||
| { | ||
| // Loop through all .php files and remember the modified time and id. | ||
| $files = array(); | ||
|
||
| foreach (new \DirectoryIterator($this->dirname) as $file) | ||
| { | ||
| if ($file->getExtension() == 'php') | ||
| { | ||
| $files[] = array( | ||
| 'time' => $file->getMTime(), | ||
| 'id' => $file->getBasename('.php') | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| // Sort the files, newest first | ||
| usort( | ||
| $files, | ||
| function ($a, $b) { | ||
| return $a['time'] < $b['time']; | ||
| } | ||
| ); | ||
|
|
||
| // Load the metadata and filter the results. | ||
| $results = array(); | ||
| $i = 0; | ||
| foreach ($files as $file) | ||
| { | ||
| // When filter is empty, skip loading the offset | ||
| if ($i++ < $offset && empty($filters)) | ||
| { | ||
| $results[] = null; | ||
| continue; | ||
| } | ||
|
|
||
| $data = $this->get($file['id']); | ||
| $meta = $data['__meta']; | ||
| unset($data); | ||
|
|
||
| if ($this->filter($meta, $filters)) | ||
| { | ||
| $results[] = $meta; | ||
| } | ||
|
|
||
| if (count($results) >= ($max + $offset)) | ||
| { | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| return array_slice($results, $offset, $max); | ||
| } | ||
|
|
||
| /** | ||
| * Get a full path to the file | ||
| * | ||
| * @param string $id The log id | ||
| * | ||
| * @return string | ||
| * | ||
| * @since __DEPLOY_VERSION__ | ||
| */ | ||
| public function makeFilename($id) | ||
| { | ||
| return $this->dirname . basename($id) . '.php'; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,12 +13,13 @@ | |
| use DebugBar\DataCollector\MessagesCollector; | ||
| use DebugBar\DataCollector\RequestDataCollector; | ||
| use DebugBar\DebugBar; | ||
| use DebugBar\Storage\FileStorage; | ||
| use DebugBar\OpenHandler; | ||
| use Joomla\CMS\Factory; | ||
| use Joomla\CMS\Log\Log; | ||
| use Joomla\CMS\Log\LogEntry; | ||
| use Joomla\CMS\HTML\HTMLHelper; | ||
| use Joomla\CMS\Plugin\CMSPlugin; | ||
| use Joomla\CMS\Session\Session; | ||
| use Joomla\Event\DispatcherInterface; | ||
| use Joomla\Plugin\System\Debug\DataCollector\InfoCollector; | ||
| use Joomla\Plugin\System\Debug\DataCollector\LanguageErrorsCollector; | ||
|
|
@@ -30,6 +31,7 @@ | |
| use Joomla\Plugin\System\Debug\DebugMonitor; | ||
| use Joomla\Database\DatabaseDriver; | ||
| use Joomla\Database\Event\ConnectionEvent; | ||
| use Joomla\Plugin\System\Debug\Storage\FileStorage; | ||
|
|
||
| /** | ||
| * Joomla! Debug plugin. | ||
|
|
@@ -116,6 +118,14 @@ class PlgSystemDebug extends CMSPlugin | |
| */ | ||
| private $queryMonitor; | ||
|
|
||
| /** | ||
| * AJAX marker | ||
| * | ||
| * @var bool | ||
| * @since __DEPLOY_VERSION__ | ||
| */ | ||
| protected $isAjax = false; | ||
|
|
||
| /** | ||
| * Constructor. | ||
| * | ||
|
|
@@ -160,8 +170,13 @@ public function __construct(&$subject, $config) | |
|
|
||
| $this->db->setMonitor($this->queryMonitor); | ||
|
|
||
| $storagePath = JPATH_CACHE . '/plg_system_debug_' . $this->app->getClientId(); | ||
|
|
||
| $this->debugBar = new DebugBar; | ||
| $this->debugBar->setStorage(new FileStorage($this->app->get('tmp_path'))); | ||
| $this->debugBar->setStorage(new FileStorage($storagePath)); | ||
|
|
||
| $this->isAjax = $this->app->input->get('option') === 'com_ajax' | ||
| && $this->app->input->get('plugin') === 'debug' && $this->app->input->get('group') === 'system'; | ||
|
|
||
| $this->setupLogging(); | ||
| } | ||
|
|
@@ -200,7 +215,7 @@ public function onAfterDispatch() | |
| public function onAfterRespond() | ||
| { | ||
| // Do not render if debugging or language debug is not enabled. | ||
| if (!JDEBUG && !$this->debugLang) | ||
| if (!JDEBUG && !$this->debugLang || $this->isAjax) | ||
| { | ||
| return; | ||
| } | ||
|
|
@@ -211,35 +226,6 @@ public function onAfterRespond() | |
| return; | ||
| } | ||
|
|
||
| // Only render for HTML output. | ||
| if (Factory::getDocument()->getType() !== 'html') | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| if ('com_content' === $this->app->input->get('option') && 'debug' === $this->app->input->get('view')) | ||
| { | ||
| // Com_content debug view - @since 4.0 | ||
| return; | ||
| } | ||
|
|
||
| // Capture output. | ||
| $contents = ob_get_contents(); | ||
|
|
||
| if ($contents) | ||
| { | ||
| ob_end_clean(); | ||
| } | ||
|
|
||
| // No debug for Safari and Chrome redirection. | ||
| if (strpos($contents, '<html><head><meta http-equiv="refresh" content="0;') === 0 | ||
| && strpos(strtolower($_SERVER['HTTP_USER_AGENT'] ?? ''), 'webkit') !== false) | ||
| { | ||
| echo $contents; | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| // Load language. | ||
| $this->loadLanguage(); | ||
|
|
||
|
|
@@ -288,14 +274,83 @@ public function onAfterRespond() | |
| } | ||
|
|
||
| $debugBarRenderer = $this->debugBar->getJavascriptRenderer(); | ||
| $openHandlerUrl = JUri::base(true) . '/index.php?option=com_ajax&plugin=debug&group=system&format=raw&action=openhandler'; | ||
| $openHandlerUrl .= '&' . Session::getFormToken() . '=1'; | ||
|
|
||
| $debugBarRenderer->setOpenHandlerUrl($openHandlerUrl); | ||
| $debugBarRenderer->setBaseUrl(JUri::root(true) . '/media/vendor/debugbar/'); | ||
|
|
||
| // Only render for HTML output. | ||
| if (Factory::getDocument()->getType() !== 'html') | ||
| { | ||
| $this->debugBar->stackData(); | ||
| return; | ||
| } | ||
|
|
||
| if ('com_content' === $this->app->input->get('option') && 'debug' === $this->app->input->get('view')) | ||
|
||
| { | ||
| $this->debugBar->stackData(); | ||
|
|
||
| // Com_content debug view - @since 4.0 | ||
| return; | ||
| } | ||
|
|
||
| // Capture output. | ||
| $contents = ob_get_contents(); | ||
|
|
||
| if ($contents) | ||
| { | ||
| ob_end_clean(); | ||
| } | ||
|
|
||
| // No debug for Safari and Chrome redirection. | ||
| if (strpos($contents, '<html><head><meta http-equiv="refresh" content="0;') === 0 | ||
| && strpos(strtolower($_SERVER['HTTP_USER_AGENT'] ?? ''), 'webkit') !== false) | ||
| { | ||
| $this->debugBar->stackData(); | ||
|
|
||
| echo $contents; | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| $contents = str_replace('</head>', $debugBarRenderer->renderHead() . '</head>', $contents); | ||
|
|
||
| echo str_replace('</body>', $debugBarRenderer->render() . '</body>', $contents); | ||
| } | ||
|
|
||
| /** | ||
| * AJAX handler | ||
| * | ||
| * @return string | ||
| * | ||
| * @since __DEPLOY_VERSION__ | ||
| */ | ||
| public function onAjaxDebug() | ||
| { | ||
| // Do not render if debugging or language debug is not enabled. | ||
| if (!JDEBUG && !$this->debugLang) | ||
| { | ||
| return ''; | ||
| } | ||
|
|
||
| // User has to be authorised to see the debug information. | ||
| if (!$this->isAuthorisedDisplayDebug() || !Session::checkToken('request')) | ||
| { | ||
| return ''; | ||
| } | ||
|
|
||
| switch ($this->app->input->get('action')) | ||
| { | ||
| case 'openhandler': | ||
| $handler = new OpenHandler($this->debugBar); | ||
| return $handler->handle($this->app->input->request->getArray(), false, false); | ||
| break; | ||
| default: | ||
| return ''; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Setup logging functionality. | ||
| * | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Change to
[]