Skip to content

Commit 644e5e5

Browse files
committed
Merge branch 'dev'
2 parents f1bae26 + 40a1f78 commit 644e5e5

File tree

7 files changed

+94
-11
lines changed

7 files changed

+94
-11
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 3.0.5 - 2021-11-23
2+
### Added
3+
- Add truncate / delete buttons
4+
15
## 3.0.4 - 2021-07-09 [CRITICAL]
26
### Fixed
37
- Fix security vulnerability

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "ether/logs",
33
"description": "Access logs from the CP",
4-
"version": "3.0.4",
4+
"version": "3.0.5",
55
"type": "craft-plugin",
66
"minimum-stability": "dev",
77
"require": {

src/Controller.php

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,54 @@
33
namespace ether\logs;
44

55
use Craft;
6+
use craft\helpers\UrlHelper;
7+
use Exception;
8+
use yii\web\HttpException;
69

710
class Controller extends \craft\web\Controller
811
{
912

1013
public function actionStream ()
1114
{
12-
$logsDir = Craft::getAlias('@storage/logs');
15+
try {
16+
$logsDir = Craft::getAlias('@storage/logs');
17+
$currentLog = $this->_getLogFile();
18+
$log = file_get_contents($logsDir . DIRECTORY_SEPARATOR . $currentLog);
19+
20+
exit($log);
21+
} catch (Exception $e) {
22+
if (strpos($e->getMessage(), 'failed to open stream') !== false)
23+
return '<p>Unable to find log file</p>';
24+
25+
return '<p>You can only access .log files!</p>';
26+
}
27+
}
28+
29+
public function actionTruncate ()
30+
{
31+
$this->requireAdmin(false);
32+
$logFile = $this->_getLogFile();
33+
Logs::getInstance()->service->truncate($logFile);
34+
}
35+
36+
public function actionDelete ()
37+
{
38+
$this->requireAdmin(false);
39+
$logFile = $this->_getLogFile();
40+
Logs::getInstance()->service->delete($logFile);
41+
42+
return $this->redirect(UrlHelper::cpUrl('utilities/logs'));
43+
}
44+
45+
private function _getLogFile (): string
46+
{
1347
$logFile = Craft::$app->request->getParam('log');
1448
$currentLog = basename(Craft::$app->request->get('log', $logFile));
1549

1650
if (strpos($currentLog, '.log') === false)
17-
return '<p>You can only access <code>.log</code> files!</p>';
18-
19-
$log = file_get_contents($logsDir . '/' . $currentLog);
51+
throw new HttpException(403);
2052

21-
exit($log);
53+
return $currentLog;
2254
}
2355

2456
}

src/Logs.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
use craft\services\Utilities;
88
use yii\base\Event;
99

10+
/**
11+
* @property Service $service
12+
*/
1013
class Logs extends Plugin
1114
{
1215

@@ -18,6 +21,10 @@ public function init ()
1821
{
1922
parent::init();
2023

24+
$this->setComponents([
25+
'service' => Service::class,
26+
]);
27+
2128
Event::on(
2229
Utilities::class,
2330
Utilities::EVENT_REGISTER_UTILITY_TYPES,

src/Service.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace ether\logs;
4+
5+
use Craft;
6+
use craft\base\Component;
7+
8+
class Service extends Component
9+
{
10+
11+
public function truncate ($log)
12+
{
13+
$logsDir = Craft::getAlias('@storage/logs');
14+
file_put_contents($logsDir . DIRECTORY_SEPARATOR . $log, '');
15+
Craft::$app->getSession()->setNotice($log . ' truncated.');
16+
}
17+
18+
public function delete ($log)
19+
{
20+
$logsDir = Craft::getAlias('@storage/logs');
21+
unlink($logsDir . DIRECTORY_SEPARATOR . $log);
22+
Craft::$app->getSession()->setNotice($log . ' deleted.');
23+
}
24+
25+
}

src/Utility.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
namespace ether\logs;
44

55
use Craft;
6+
use Twig\Error\LoaderError;
7+
use Twig\Error\RuntimeError;
8+
use Twig\Error\SyntaxError;
9+
use yii\base\Exception;
610

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

2529
/**
2630
* @return string
27-
* @throws \Twig_Error_Loader
28-
* @throws \yii\base\Exception
31+
* @throws LoaderError
32+
* @throws RuntimeError
33+
* @throws SyntaxError
34+
* @throws Exception
2935
*/
3036
public static function contentHtml (): string
3137
{

src/templates/view.twig

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,24 @@
1-
<form>
1+
<div style="display:flex;align-items:center;justify-content:space-between">
22
<label class="select">
33
<select id="__logSwitch">
44
{% for file in logFiles %}
55
<option{{ file == currentLog ? ' selected' }}>{{ file }}</option>
66
{% endfor %}
77
</select>
88
</label>
9-
</form>
9+
10+
{% if currentUser.admin %}
11+
<form class="btngroup" method="post">
12+
<button name="action" value="logs/logs/truncate" class="btn caution" title="Empty this log file">Truncate</button>
13+
{{ csrfInput() }}
14+
{{ hiddenInput('log', currentLog) }}
15+
<button name="action" value="logs/logs/delete" class="btn caution" title="Delete this log file">Delete</button>
16+
</form>
17+
{% endif %}
18+
</div>
1019

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

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

0 commit comments

Comments
 (0)