Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
Tam committed Nov 23, 2021
2 parents f1bae26 + 40a1f78 commit 644e5e5
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 11 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 3.0.5 - 2021-11-23
### Added
- Add truncate / delete buttons

## 3.0.4 - 2021-07-09 [CRITICAL]
### Fixed
- Fix security vulnerability
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "ether/logs",
"description": "Access logs from the CP",
"version": "3.0.4",
"version": "3.0.5",
"type": "craft-plugin",
"minimum-stability": "dev",
"require": {
Expand Down
42 changes: 37 additions & 5 deletions src/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,54 @@
namespace ether\logs;

use Craft;
use craft\helpers\UrlHelper;
use Exception;
use yii\web\HttpException;

class Controller extends \craft\web\Controller
{

public function actionStream ()
{
$logsDir = Craft::getAlias('@storage/logs');
try {
$logsDir = Craft::getAlias('@storage/logs');
$currentLog = $this->_getLogFile();
$log = file_get_contents($logsDir . DIRECTORY_SEPARATOR . $currentLog);

exit($log);
} catch (Exception $e) {
if (strpos($e->getMessage(), 'failed to open stream') !== false)
return '<p>Unable to find log file</p>';

return '<p>You can only access .log files!</p>';
}
}

public function actionTruncate ()
{
$this->requireAdmin(false);
$logFile = $this->_getLogFile();
Logs::getInstance()->service->truncate($logFile);
}

public function actionDelete ()
{
$this->requireAdmin(false);
$logFile = $this->_getLogFile();
Logs::getInstance()->service->delete($logFile);

return $this->redirect(UrlHelper::cpUrl('utilities/logs'));
}

private function _getLogFile (): string
{
$logFile = Craft::$app->request->getParam('log');
$currentLog = basename(Craft::$app->request->get('log', $logFile));

if (strpos($currentLog, '.log') === false)
return '<p>You can only access <code>.log</code> files!</p>';

$log = file_get_contents($logsDir . '/' . $currentLog);
throw new HttpException(403);

exit($log);
return $currentLog;
}

}
7 changes: 7 additions & 0 deletions src/Logs.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
use craft\services\Utilities;
use yii\base\Event;

/**
* @property Service $service
*/
class Logs extends Plugin
{

Expand All @@ -18,6 +21,10 @@ public function init ()
{
parent::init();

$this->setComponents([
'service' => Service::class,
]);

Event::on(
Utilities::class,
Utilities::EVENT_REGISTER_UTILITY_TYPES,
Expand Down
25 changes: 25 additions & 0 deletions src/Service.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace ether\logs;

use Craft;
use craft\base\Component;

class Service extends Component
{

public function truncate ($log)
{
$logsDir = Craft::getAlias('@storage/logs');
file_put_contents($logsDir . DIRECTORY_SEPARATOR . $log, '');
Craft::$app->getSession()->setNotice($log . ' truncated.');
}

public function delete ($log)
{
$logsDir = Craft::getAlias('@storage/logs');
unlink($logsDir . DIRECTORY_SEPARATOR . $log);
Craft::$app->getSession()->setNotice($log . ' deleted.');
}

}
10 changes: 8 additions & 2 deletions src/Utility.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
namespace ether\logs;

use Craft;
use Twig\Error\LoaderError;
use Twig\Error\RuntimeError;
use Twig\Error\SyntaxError;
use yii\base\Exception;

class Utility extends \craft\base\Utility
{
Expand All @@ -24,8 +28,10 @@ public static function iconPath ()

/**
* @return string
* @throws \Twig_Error_Loader
* @throws \yii\base\Exception
* @throws LoaderError
* @throws RuntimeError
* @throws SyntaxError
* @throws Exception
*/
public static function contentHtml (): string
{
Expand Down
15 changes: 12 additions & 3 deletions src/templates/view.twig
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
<form>
<div style="display:flex;align-items:center;justify-content:space-between">
<label class="select">
<select id="__logSwitch">
{% for file in logFiles %}
<option{{ file == currentLog ? ' selected' }}>{{ file }}</option>
{% endfor %}
</select>
</label>
</form>

{% if currentUser.admin %}
<form class="btngroup" method="post">
<button name="action" value="logs/logs/truncate" class="btn caution" title="Empty this log file">Truncate</button>
{{ csrfInput() }}
{{ hiddenInput('log', currentLog) }}
<button name="action" value="logs/logs/delete" class="btn caution" title="Delete this log file">Delete</button>
</form>
{% endif %}
</div>

<hr style="margin-bottom:0">

<pre style="flex-grow:1;overflow:auto;padding:24px 24px 48px;margin:0 -24px;"><!--
<pre style="flex-grow:1;overflow:auto;padding:24px;margin:0 -24px -24px;"><!--
--><code id="__log">Loading...</code>
</pre>

0 comments on commit 644e5e5

Please sign in to comment.