Skip to content

Commit

Permalink
- Fixed making url on view page.
Browse files Browse the repository at this point in the history
- Fixed getting whole log block.
  • Loading branch information
andkirby committed Jun 6, 2018
1 parent 3249cc9 commit 843bae6
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 19 deletions.
43 changes: 27 additions & 16 deletions Block/View/Index.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace PhilTurner\LogViewer\Block\View;

use PhilTurner\LogViewer\Helper\ReadLogFileTrait;
Expand All @@ -16,8 +17,8 @@ class Index extends \Magento\Framework\View\Element\Template

/**
* @param \Magento\Framework\View\Element\Template\Context $context
* @param \PhilTurner\LogViewer\Helper\Data $logDataHelper
* @param array $data
* @param \PhilTurner\LogViewer\Helper\Data $logDataHelper
* @param array $data
*/
public function __construct(
\Magento\Framework\View\Element\Template\Context $context,
Expand Down Expand Up @@ -46,12 +47,12 @@ public function getLogFileBlocks(): array

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

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

public function getFileName()
Expand All @@ -67,7 +68,11 @@ public function getFileName()
*/
public function getLimitUrl(int $limit): string
{
return $this->getUrl('*/*/*', ['_current' => true, 'limit' => $limit]);
return $this->getUrl('*/*/*', [
'_current' => true,
'limit' => $limit,
'file' => $this->getFileName(),
]);
}

/**
Expand All @@ -78,7 +83,11 @@ public function getLimitUrl(int $limit): string
*/
public function getStartUrl(int $start): string
{
return $this->getUrl('*/*/*', ['_current' => true, '_use_rewrite' => true, 'start' => $start]);
return $this->getUrl('*/*/*', [
'_current' => true,
'start' => $start,
'file' => $this->getFileName(),
]);
}

/**
Expand All @@ -91,16 +100,6 @@ 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
*
Expand All @@ -114,11 +113,13 @@ public function getStarts($max = 10)
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());
}

Expand All @@ -132,4 +133,14 @@ public function getLimits()
return [10, 20, 30, 50, 100, 500, 1000];
}

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

}
6 changes: 3 additions & 3 deletions Helper/ReadLogFileTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ 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
Expand All @@ -41,9 +40,10 @@ public function fetch($file, $limit = 1, $start = 0): array
while (isset($output[$blockStamp])) {
$blockStamp = $matches[1].'.'.++$i;
}

$line = str_replace($matches[0].' ', '', $line); // cut timestamp out
$output[$blockStamp] = $line.PHP_EOL;
} elseif ($started) {
} elseif ($output) {
$output[$blockStamp] .= $line.PHP_EOL;
}
}
Expand All @@ -63,7 +63,7 @@ private function fileReader(): \Closure
throw new Exception('LFI protection. Parent directory is prohibited to use.');
}

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

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

0 comments on commit 843bae6

Please sign in to comment.