2013-11-17 14:39:50 +01:00
|
|
|
<?php
|
|
|
|
class LogController
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @route /logs
|
|
|
|
*/
|
|
|
|
public function listAction()
|
|
|
|
{
|
|
|
|
PrivilegesHelper::confirmWithException(Privilege::ListLogs);
|
|
|
|
|
2013-11-23 20:52:41 +01:00
|
|
|
$path = TextHelper::absolutePath($this->config->main->logsPath);
|
2013-11-17 14:39:50 +01:00
|
|
|
|
|
|
|
$logs = [];
|
|
|
|
foreach (glob($path . DS . '*.log') as $log)
|
|
|
|
$logs []= basename($log);
|
|
|
|
|
|
|
|
usort($logs, function($a, $b)
|
|
|
|
{
|
|
|
|
return strnatcasecmp($b, $a); //reverse natcasesort
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->context->transport->logs = $logs;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @route /log/{name}
|
2014-01-27 09:17:36 +01:00
|
|
|
* @route /log/{name}/{page}
|
|
|
|
* @route /log/{name}/{page}/{filter}
|
2013-11-17 14:39:50 +01:00
|
|
|
* @validate name [0-9a-zA-Z._-]+
|
2014-01-27 09:17:36 +01:00
|
|
|
* @validate page \d*
|
|
|
|
* @validate filter .*
|
2013-11-17 14:39:50 +01:00
|
|
|
*/
|
2014-01-27 09:17:36 +01:00
|
|
|
public function viewAction($name, $page = 1, $filter = '')
|
2013-11-17 14:39:50 +01:00
|
|
|
{
|
2014-01-27 09:17:36 +01:00
|
|
|
//redirect requests in form of ?query=... to canonical address
|
|
|
|
$formQuery = InputHelper::get('query');
|
|
|
|
if ($formQuery !== null)
|
|
|
|
{
|
|
|
|
\Chibi\UrlHelper::forward(
|
|
|
|
\Chibi\UrlHelper::route(
|
|
|
|
'log',
|
|
|
|
'view',
|
|
|
|
[
|
|
|
|
'name' => $name,
|
|
|
|
'filter' => $formQuery,
|
|
|
|
'page' => 1
|
|
|
|
]));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-11-17 14:39:50 +01:00
|
|
|
PrivilegesHelper::confirmWithException(Privilege::ViewLog);
|
|
|
|
|
2014-01-27 09:17:36 +01:00
|
|
|
//parse input
|
|
|
|
$page = max(1, intval($page));
|
2013-11-17 14:39:50 +01:00
|
|
|
$name = str_replace(['/', '\\'], '', $name); //paranoia mode
|
2013-11-23 20:52:41 +01:00
|
|
|
$path = TextHelper::absolutePath($this->config->main->logsPath . DS . $name);
|
2013-11-17 14:39:50 +01:00
|
|
|
if (!file_exists($path))
|
|
|
|
throw new SimpleException('Specified log doesn\'t exist');
|
|
|
|
|
2014-01-27 09:17:36 +01:00
|
|
|
//load lines
|
2013-11-17 14:39:50 +01:00
|
|
|
$lines = file_get_contents($path);
|
|
|
|
$lines = explode(PHP_EOL, str_replace(["\r", "\n"], PHP_EOL, $lines));
|
|
|
|
$lines = array_reverse($lines);
|
2013-11-17 23:29:59 +01:00
|
|
|
|
2013-11-17 14:39:50 +01:00
|
|
|
if (!empty($filter))
|
|
|
|
$lines = array_filter($lines, function($line) use ($filter) { return stripos($line, $filter) !== false; });
|
2013-11-17 23:29:59 +01:00
|
|
|
|
2014-01-27 09:17:36 +01:00
|
|
|
$lineCount = count($lines);
|
|
|
|
$logsPerPage = intval($this->config->browsing->logsPerPage);
|
|
|
|
$pageCount = ceil($lineCount / $logsPerPage);
|
|
|
|
$page = min($pageCount, $page);
|
|
|
|
|
|
|
|
$lines = array_slice($lines, ($page - 1) * $logsPerPage, $logsPerPage);
|
|
|
|
|
2013-11-17 23:29:59 +01:00
|
|
|
//stylize important lines
|
|
|
|
foreach ($lines as &$line)
|
|
|
|
if (strpos($line, 'flag') !== false)
|
|
|
|
$line = '**' . $line . '**';
|
|
|
|
unset($line);
|
|
|
|
|
2013-11-17 14:39:50 +01:00
|
|
|
$lines = join(PHP_EOL, $lines);
|
2013-11-23 20:00:02 +01:00
|
|
|
$lines = TextHelper::parseMarkdown($lines, true);
|
2013-11-17 14:39:50 +01:00
|
|
|
$lines = trim($lines);
|
|
|
|
|
2014-01-27 09:17:36 +01:00
|
|
|
$this->context->transport->paginator = new StdClass;
|
|
|
|
$this->context->transport->paginator->page = $page;
|
|
|
|
$this->context->transport->paginator->pageCount = $pageCount;
|
|
|
|
$this->context->transport->paginator->entityCount = $lineCount;
|
|
|
|
$this->context->transport->paginator->entities = $lines;
|
|
|
|
$this->context->transport->lines = $lines;
|
2013-11-17 14:39:50 +01:00
|
|
|
$this->context->transport->filter = $filter;
|
|
|
|
$this->context->transport->name = $name;
|
|
|
|
}
|
|
|
|
}
|