This commit is contained in:
Marcin Kurczewski 2013-11-17 14:39:50 +01:00
parent 4fd25b10c6
commit da63c0fd19
11 changed files with 130 additions and 4 deletions

View file

@ -114,3 +114,6 @@ listTags=anonymous
mergeTags=moderator
renameTags=moderator
massTag=moderator
listLogs=moderator
viewLog=moderator

View file

@ -0,0 +1,9 @@
#content input {
margin: 0 1em;
height: 25px;
vertical-align: middle;
}
pre {
font-size: 11pt;
}

View file

@ -0,0 +1,4 @@
$(function()
{
$('#content form input').eq(0).focus().select();
});

View file

@ -0,0 +1,59 @@
<?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; });
$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;
}
}

View file

@ -5,6 +5,7 @@ class CustomMarkdown extends \Michelf\Markdown
{
$this->no_markup = true;
$this->span_gamut += ['doSpoilers' => 71];
$this->span_gamut += ['doUsers' => 7];
$this->span_gamut += ['doPosts' => 8];
$this->span_gamut += ['doTags' => 9];
$this->span_gamut += ['doAutoLinks2' => 29];
@ -66,4 +67,12 @@ class CustomMarkdown extends \Michelf\Markdown
return $this->hashPart('<a href="' . \Chibi\UrlHelper::route('post', 'list', ['query' => $x[1]]) . '">') . $x[0] . $this->hashPart('</a>');
}, $text);
}
protected function doUsers($text)
{
return preg_replace_callback('/(?:(?<!\w))\+([a-zA-Z0-9_-]+)/', function($x)
{
return $this->hashPart('<a href="' . \Chibi\UrlHelper::route('user', 'view', ['name' => $x[1]]) . '">') . $x[0] . $this->hashPart('</a>');
}, $text);
}
}

View file

@ -197,4 +197,14 @@ class TextHelper
$iv = mcrypt_create_iv(mcrypt_get_iv_size($alg, $mode), MCRYPT_RAND);
return trim(mcrypt_decrypt($alg, $salt, base64_decode($text), $mode, $iv));
}
public static function cleanPath($path)
{
$path = str_replace(['/', '\\'], DS, $path);
$path = preg_replace('{[^' . DS . ']+' . DS . '\.\.(' . DS . '|$)}', '', $path);
$path = preg_replace('{(' . DS . '|^)\.' . DS . '}', '\1', $path);
$path = preg_replace('{' . DS . '{2,}}', DS, $path);
$path = rtrim($path, DS);
return $path;
}
}

View file

@ -36,4 +36,7 @@ class Privilege extends Enum
const MergeTags = 27;
const RenameTags = 27;
const MassTag = 29;
const ListLogs = 32;
const ViewLog = 33;
}

View file

@ -102,6 +102,9 @@
<pre class="debug"><?php echo join('<br>', array_map(function($x) { return preg_replace('/\s+/', ' ', $x); }, queryLogger()->getLogs())) ?></pre>
<?php endif ?>
<span><a href="<?php echo SZURU_LINK ?>">szurubooru v<?php echo SZURU_VERSION ?></a></span>
<?php if (PrivilegesHelper::confirm(Privilege::ListLogs)): ?>
<span><a href="<?php echo \Chibi\UrlHelper::route('log', 'list') ?>">Logs</span>
<?php endif ?>
</div>
</footer>
</body>

13
src/Views/log-list.phtml Normal file
View file

@ -0,0 +1,13 @@
<?php if (empty($this->context->transport->logs)): ?>
<p class="alert alert-warning">No logs to show.</p>
<?php else: ?>
<ul>
<?php foreach ($this->context->transport->logs as $log): ?>
<li>
<a href="<?php echo \Chibi\UrlHelper::route('log', 'view', ['name' => $log]) ?>">
<?php echo $log ?>
</a>
</li>
<?php endforeach ?>
</ul>
<?php endif ?>

11
src/Views/log-view.phtml Normal file
View file

@ -0,0 +1,11 @@
<?php if (empty($this->context->transport->log)): ?>
<p class="alert alert-warning">This log is empty. <a href="<?php echo \Chibi\UrlHelper::route('log', 'list') ?>">Go back</a></p>
<?php else: ?>
<form action="<?php echo \Chibi\UrlHelper::route('log', 'view', ['name' => $this->context->transport->name]) ?>" method="get">
Keep only lines that contain:
<input type="text" name="filter" value="<?php echo $this->context->transport->filter ?>" placeholder="any text&hellip;"/>
</form>
<pre><?php echo $this->context->transport->log ?></pre>
<?php endif ?>

View file

@ -2,7 +2,9 @@
define('SZURU_VERSION', '0.3.0');
define('SZURU_LINK', 'http://github.com/rr-/szurubooru');
define('DS', DIRECTORY_SEPARATOR);
$startTime = microtime(true);
$rootDir = __DIR__ . DS . '..' . DS;
$requiredExtensions = ['pdo', 'pdo_sqlite', 'gd', 'openssl'];
foreach ($requiredExtensions as $ext)
@ -12,17 +14,17 @@ foreach ($requiredExtensions as $ext)
date_default_timezone_set('UTC');
setlocale(LC_CTYPE, 'en_US.UTF-8');
ini_set('memory_limit', '128M');
define('DS', DIRECTORY_SEPARATOR);
require_once __DIR__ . '/../lib/php-markdown/Michelf/Markdown.php';
require_once __DIR__ . '/../lib/redbean/RedBean/redbean.inc.php';
require_once __DIR__ . '/../lib/chibi-core/Facade.php';
require_once $rootDir . 'lib' . DS . 'php-markdown' . DS . 'Michelf' . DS . 'Markdown.php';
require_once $rootDir . 'lib' . DS . 'redbean' . DS . 'RedBean' . DS . 'redbean.inc.php';
require_once $rootDir . 'lib' . DS . 'chibi-core' . DS . 'Facade.php';
\Chibi\AutoLoader::init(__DIR__);
\Chibi\Facade::init();
$config = \Chibi\Registry::getConfig();
$context = \Chibi\Registry::getContext();
$context->startTime = $startTime;
$context->rootDir = $rootDir;
R::setup('sqlite:' . $config->main->dbPath);
R::freeze(true);