Skip to content

Commit

Permalink
Added reading from directories.
Browse files Browse the repository at this point in the history
Added reading content of file.
  • Loading branch information
andkirby committed Jun 5, 2018
1 parent c74e0e6 commit fa89917
Show file tree
Hide file tree
Showing 10 changed files with 284 additions and 31 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
# Created by .ignore support plugin (hsz.mobi)

6 changes: 3 additions & 3 deletions Block/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ public function getLogFiles()

public function downloadLogFiles($fileName)
{
return $this->getUrl('logviewer/download/getfile', [$fileName]);
return $this->getUrl('logviewer/download/getfile', ['file' => $fileName]);
}

public function previewLogFile($fileName)
{
return $this->getUrl('logviewer/view/index', [$fileName]);
return $this->getUrl('logviewer/view/index', ['file' => $fileName]);
}
}
}
105 changes: 101 additions & 4 deletions Block/View/Index.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
<?php
namespace PhilTurner\LogViewer\Block\View;

use PhilTurner\LogViewer\Helper\ReadLogFileTrait;

class Index extends \Magento\Framework\View\Element\Template
{
use ReadLogFileTrait {
fetch as fetchLogFileBlocks;
}

/**
* @var \PhilTurner\LogViewer\Helper\Data
*/
Expand All @@ -25,14 +31,105 @@ public function __construct(

public function getLogFile()
{
$params = $this->_request->getParams();
return $this->logDataHelper->getLastLinesOfFile($params[0], 10);
return $this->logDataHelper->getLastLinesOfFile($this->getFileName(), 10);
}

/**
* Get logs
*
* @return array
*/
public function getLogFileBlocks(): array
{
return $this->fetchLogFileBlocks($this->logFile(), $this->getLimit(), $this->getStart());
}

public function getLimit(): int
{
return (int)$this->getRequest()->getParam('limit', 10) ?: 10;
}

public function getStart(): int
{
return (int)$this->getRequest()->getParam('start', 0);
}

public function getFileName()
{
$params = $this->_request->getParams();
return $params[0];
return $this->getRequest()->getParam('file');
}

/**
* Get limit URL
*
* @param int $limit
* @return string
*/
public function getLimitUrl(int $limit): string
{
return $this->getUrl('*/*/*', ['_current' => true, 'limit' => $limit]);
}

/**
* Get start URL
*
* @param int $start
* @return string
*/
public function getStartUrl(int $start): string
{
return $this->getUrl('*/*/*', ['_current' => true, '_use_rewrite' => true, 'start' => $start]);
}

/**
* Get back URL
*
* @return string
*/
public function getBackUrl(): string
{
return $this->getUrl('*/grid/', ['_current' => true]);
}

/**
* Get full path to log file
*
* @return string
*/
private function logFile(): string
{
return $this->logDataHelper->getPath().DIRECTORY_SEPARATOR.$this->getFileName();
}

/**
* Get starts list
*
* @param int $max
* @return array
*/
public function getStarts($max = 10)
{
$start = $this->getStart() - $this->getLimit() * 2;
$start = $start > 0 ? $start : 0;
if ($start > $this->getLimit() * 3) {
$step = ceil($start / 4);
$step -= $step % $this->getLimit();
return array_merge(
range(0, $start - $this->getLimit(), $step),
range($start, $this->getLimit() * ($max - 1) + $start, $this->getLimit())
);
}
return range(0, $this->getLimit() * ($max - 1) + $start, $this->getLimit());
}

/**
* Get starts list
*
* @return array
*/
public function getLimits()
{
return [10, 20, 30, 50, 100, 500, 1000];
}

}
5 changes: 2 additions & 3 deletions Controller/Adminhtml/Download/AbstractLog.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ public function __construct(Context $context, FileFactory $fileFactory)

public function execute()
{
$param = $this->getRequest()->getParams();
$filePath = $this->getFilePathWithFile($param[0]);
$filePath = $this->getFilePathWithFile($this->getRequest()->getParam('file'));

$filter = new Zend_Filter_BaseName();
$fileName = $filter->filter($filePath);
Expand All @@ -44,4 +43,4 @@ public function execute()
* @return string
*/
abstract protected function getFilePathWithFile($filename);
}
}
11 changes: 11 additions & 0 deletions Exception.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php
namespace PhilTurner\LogViewer;

/**
* Class Exception
*
* @package PhilTurner\LogViewer
*/
class Exception extends \Exception
{
}
37 changes: 23 additions & 14 deletions Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,36 @@ public function __construct(
}

/**
* Get path to var/log directory
*
* @return string
*/
public function getPath()
{
$rootPath = $this->directoryList->getRoot();
$path =
$rootPath . DIRECTORY_SEPARATOR . 'var' . DIRECTORY_SEPARATOR . 'log'. DIRECTORY_SEPARATOR;
return $path;
return $this->directoryList->getPath('log');
}

/**
* @return array
*/
protected function getLogFiles()
protected function getLogFiles($path)
{
$path = $this->getPath();
return scandir($path);
$list = scandir($path);
//remove rubbish from array
array_splice($list, 0, 2);

$output = [];
foreach ($list as $index => $file) {
if (is_dir($path.DIRECTORY_SEPARATOR.$file)) {
foreach ($this->getLogFiles($path.DIRECTORY_SEPARATOR.$file) as $childFile) {
$output[] = $file.DIRECTORY_SEPARATOR.$childFile;
}
} else {
$output[] = $file;
}
}

return $output;
}

/**
Expand Down Expand Up @@ -67,14 +80,10 @@ public function buildLogData()
{
$maxNumOfLogs = 30;
$logFileData = [];
$path = $this->getPath();
$files = $this->getLogFiles();

//remove rubbish from array
array_splice($files, 0, 2);
$path = $this->getPath().DIRECTORY_SEPARATOR;

//build log data into array
foreach ($files as $file) {
foreach ($this->getLogFiles($this->getPath()) as $file) {
$logFileData[$file]['name'] = $file;
$logFileData[$file]['filesize'] = $this->filesizeToReadableString((filesize($path . $file)));
$logFileData[$file]['modTime'] = filemtime($path . $file);
Expand All @@ -99,4 +108,4 @@ public function getLastLinesOfFile($fileName, $numOfLines)
exec('tail -'. $numOfLines . ' ' . $fullPath, $output);
return implode($output);
}
}
}
79 changes: 79 additions & 0 deletions Helper/ReadLogFileTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

namespace PhilTurner\LogViewer\Helper;

use PhilTurner\LogViewer\Exception;

/**
* Trait ReadLogFileTrait
*
* @package PhilTurner\LogViewer\Helper
*/
trait ReadLogFileTrait
{
/**
* Fetch block
*
* @param string $file
* @param int $start
* @param int $limit
* @return array
*/
public function fetch($file, $limit = 1, $start = 0): array
{
$output = [];
$block = 0;
$blockStamp = null;
$started = false;

foreach ($this->fileReader()($file) as $line) {
//match next log file text block by timestamp
if (preg_match('#^\[(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2})\]#', $line, $matches)) {
if ($start + $limit < ++$block) {
break;
}
if ($start + 1 > $block) {
continue;
}

$blockStamp = $matches[1]; // set new block timestamp
$i = 0;
while (isset($output[$blockStamp])) {
$blockStamp = $matches[1].'.'.++$i;
}
$line = str_replace($matches[0].' ', '', $line); // cut timestamp out
$output[$blockStamp] = $line.PHP_EOL;
} elseif ($started) {
$output[$blockStamp] .= $line.PHP_EOL;
}
}

return $output;
}

/**
* Get file reader
*
* @return \Closure
*/
private function fileReader(): \Closure
{
return function ($file) {
if (false !== strpos($file, '../')) {
throw new Exception('LFI protection. Parent directory is prohibited to use.');
}

$rs = fopen($file, 'r');

if (!$rs) {
throw new Exception('Cannot open file: '.$file);
}

while (($line = fgets($rs)) !== false) {
yield $line;
}

fclose($rs);
};
}
}
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"registration.php"
],
"psr-4": {
"\\PhilTurner\\LogViewer\\": ""
"PhilTurner\\LogViewer\\": ""
}
},
"authors": [
Expand Down
7 changes: 4 additions & 3 deletions view/adminhtml/templates/grid.phtml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<?php /** @var $block \PhilTurner\LogViewer\Block\Index */ ?>
<?php $logs = $block->getLogFiles(); ?>

<section class="admin__page-section">
Expand All @@ -16,7 +17,7 @@
</tr>
<?php foreach($logs as $log) { ?>
<tr>
<td><a href="<?php echo $block->downloadLogFiles($log['name']); ?>"><?php echo $log['name'] ?></a></td>
<td><a href="<?php echo $block->previewLogFile($log['name']); ?>"><?php echo $log['name'] ?></a></td>
<td>var/log</td>
<td><?php echo $log['filesize'] ?></td>
<td><?php echo $log['modTimeLong'] ?></td>
Expand All @@ -26,7 +27,7 @@
</td>
</tr>
<?php } ?>
</table><br>
</table>
</div>
</div>
</section>
Expand All @@ -53,4 +54,4 @@
.action-children a:link {
text-decoration: none;
}
</style>
</style>
Loading

0 comments on commit fa89917

Please sign in to comment.