Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
137 changes: 137 additions & 0 deletions plugins/system/debug/Storage/FileStorage.php
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();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change to []

}

/**
* 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 = [], $max = 20, $offset = 0)
{
// Loop through all .php files and remember the modified time and id.
$files = [];
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 = [];
$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';
}
}
111 changes: 79 additions & 32 deletions plugins/system/debug/debug.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
Expand Down Expand Up @@ -116,6 +118,14 @@ class PlgSystemDebug extends CMSPlugin
*/
private $queryMonitor;

/**
* AJAX marker
*
* @var bool
* @since __DEPLOY_VERSION__
*/
protected $isAjax = false;

/**
* Constructor.
*
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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;
}
Expand All @@ -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();

Expand Down Expand Up @@ -288,14 +274,75 @@ 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;
}

// 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.
*
Expand Down