This repository has been archived on 2025-02-26. You can view files and clone it, but cannot push or open issues or pull requests.
szurubooru/src/Controllers/LogController.php

90 lines
2.3 KiB
PHP
Raw Normal View History

2013-11-17 14:39:50 +01:00
<?php
class LogController
{
public function listAction()
{
2014-04-29 21:35:29 +02:00
$context = getContext();
Access::assert(Privilege::ListLogs);
2013-11-17 14:39:50 +01:00
2014-04-29 21:35:29 +02:00
$path = TextHelper::absolutePath(getConfig()->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
});
2014-04-29 21:35:29 +02:00
$context->transport->logs = $logs;
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-04-29 21:35:29 +02:00
$context = getContext();
2014-01-27 09:17:36 +01:00
//redirect requests in form of ?query=... to canonical address
$formQuery = InputHelper::get('query');
if ($formQuery !== null)
{
2014-04-29 21:35:29 +02:00
\Chibi\Util\Url::forward(
\Chibi\Router::linkTo(
['LogController', 'viewAction'],
2014-01-27 09:17:36 +01:00
[
'name' => $name,
'filter' => $formQuery,
'page' => 1
]));
return;
}
Access::assert(Privilege::ViewLog);
2013-11-17 14:39:50 +01:00
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
2014-04-29 21:35:29 +02:00
$path = TextHelper::absolutePath(getConfig()->main->logsPath . DS . $name);
2013-11-17 14:39:50 +01:00
if (!file_exists($path))
2014-02-05 08:32:19 +01:00
throw new SimpleNotFoundException('Specified log doesn\'t exist');
2013-11-17 14:39:50 +01:00
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 14:39:50 +01:00
if (!empty($filter))
{
$lines = array_filter($lines, function($line) use ($filter)
{
return stripos($line, $filter) !== false;
});
}
2014-01-27 09:17:36 +01:00
$lineCount = count($lines);
2014-04-29 21:35:29 +02:00
$logsPerPage = intval(getConfig()->browsing->logsPerPage);
2014-01-27 09:17:36 +01:00
$pageCount = ceil($lineCount / $logsPerPage);
$page = min($pageCount, $page);
$lines = array_slice($lines, ($page - 1) * $logsPerPage, $logsPerPage);
//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);
$lines = TextHelper::parseMarkdown($lines, true);
2013-11-17 14:39:50 +01:00
$lines = trim($lines);
2014-04-29 21:35:29 +02:00
$context->transport->paginator = new StdClass;
$context->transport->paginator->page = $page;
$context->transport->paginator->pageCount = $pageCount;
$context->transport->paginator->entityCount = $lineCount;
$context->transport->paginator->entities = $lines;
$context->transport->lines = $lines;
$context->transport->filter = $filter;
$context->transport->name = $name;
2013-11-17 14:39:50 +01:00
}
}