szurubooru/src/Controllers/LogController.php
Marcin Kurczewski 9ab961985d Refactor to logging
- Centralized use of TextHelper::repr..() instead of hardcoded markdown
- Centralized processing of highlighting instead of hardcoded markdown
- Highlighted items are marked with color, not just bold
2013-11-17 23:46:31 +01:00

67 lines
1.8 KiB
PHP

<?php
class LogController
{
/**
* @route /logs
*/
public function listAction()
{
$this->context->subTitle = 'latest logs';
PrivilegesHelper::confirmWithException(Privilege::ListLogs);
$path = $this->context->rootDir . DS . $this->config->main->logsPath;
$path = TextHelper::cleanPath($path);
$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}
* @validate name [0-9a-zA-Z._-]+
*/
public function viewAction($name)
{
$this->context->subTitle = 'logs (' . $name . ')';
$this->context->stylesheets []= 'logs.css';
$this->context->scripts []= 'logs.js';
PrivilegesHelper::confirmWithException(Privilege::ViewLog);
$name = str_replace(['/', '\\'], '', $name); //paranoia mode
$path = $this->context->rootDir . DS . $this->config->main->logsPath . DS . $name;
$path = TextHelper::cleanPath($path);
if (!file_exists($path))
throw new SimpleException('Specified log doesn\'t exist');
$filter = InputHelper::get('filter');
$lines = file_get_contents($path);
$lines = explode(PHP_EOL, str_replace(["\r", "\n"], PHP_EOL, $lines));
$lines = array_reverse($lines);
if (!empty($filter))
$lines = array_filter($lines, function($line) use ($filter) { return stripos($line, $filter) !== false; });
//stylize important lines
foreach ($lines as &$line)
if (strpos($line, 'flag') !== false)
$line = '**' . $line . '**';
unset($line);
$lines = join(PHP_EOL, $lines);
$lines = TextHelper::parseMarkdown($lines);
$lines = trim($lines);
$this->context->transport->filter = $filter;
$this->context->transport->name = $name;
$this->context->transport->log = $lines;
}
}