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

95 lines
2.6 KiB
PHP
Raw Normal View History

2013-11-17 14:39:50 +01:00
<?php
class LogController
{
/**
* @route /logs
*/
public function listAction()
{
PrivilegesHelper::confirmWithException(Privilege::ListLogs);
$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
$path = TextHelper::absolutePath($this->config->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);
$logsPerPage = intval($this->config->browsing->logsPerPage);
$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-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;
}
}