forked from philaturner/LogViewer
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added reading content of file.
- Loading branch information
Showing
10 changed files
with
284 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
# Created by .ignore support plugin (hsz.mobi) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.