<?php
class Page_SysLog extends Page
{
const PROP_ANON_DAYS = 'syslog.anon-days'; // Copy in cronjob
protected function doPreprocess()
{
User::load();
if (!User::isLoggedIn()) {
Message::addError('main.no-permission');
Util::redirect('?do=Main');
}
if (($days = Request::post('anondays', false, 'int')) !== false) {
User::assertPermission('configure-anonymization', NULL,'?do=syslog');
if ($days < 0 || $days > 180) {
Message::addError('anon-days-out-of-range', $days);
} else {
Property::set(self::PROP_ANON_DAYS, $days);
Message::addSuccess('anon-days-saved');
}
Util::redirect('?do=syslog');
}
if (Request::isPost()) {
$pairs = [];
foreach (['search', 'filter', 'not', 'machineuuid'] as $key) {
$val = Request::any($key, false, 'string');
if (!empty($val)) {
if ($key === 'not') {
$val = (bool)$val;
}
$pairs[$key] = $val;
}
Session::set('log_' . $key, $pairs[$key] ?? false, false);
}
Util::redirect('?do=syslog&' . http_build_query($pairs));
}
User::assertPermission('*');
}
protected function doRender()
{
$data = ['anondays' => Property::get(self::PROP_ANON_DAYS, 0)];
Permission::addGlobalTags($data['perms'], NULL, ['configure-anonymization', 'export-user-data']);
Render::addTemplate("heading", $data);
if (!User::hasPermission("view")) {
Message::addError('main.no-permission');
return;
}
$cutoff = strtotime('-1 month');
$res = Database::simpleQuery("SELECT logtypeid, Count(*) AS counter
FROM clientlog
WHERE dateline > $cutoff
GROUP BY logtypeid ORDER BY counter ASC");
$types = array();
foreach ($res as $row) {
$types[$row['logtypeid']] = $row;
}
if (Request::get('filter') !== false || Request::get('search') !== false) {
$search = Request::get('search');
$filter = Request::get('filter');
$not = Request::get('not', false, 'bool');
} else {
$search = Session::get('log_search');
$filter = Session::get('log_filter');
$not = (bool)Session::get('log_not');
}
$qArgs = [];
$whereClause = '1';
if (!empty($filter)) {
$whereClause .= ' AND ( ';
if ($not) {
$whereClause .= 'NOT ';
}
$filterList = array_unique(explode(',', $filter));
foreach ($filterList as $filterItem) {
if (!isset($types[$filterItem])) {
$types[$filterItem] = ['logtypeid' => $filterItem, 'counter' => ''];
}
}
$whereClause .= "logtypeid IN (:typeids) )";
$qArgs['typeids'] = $filterList;
}
if (!empty($search)) {
$qArgs['search'] = '%' . str_replace(array('=', '_', '%', '*', '?'), array('==', '=_', '=%', '%', '_'), $search) . '%';
$whereClause .= " AND description LIKE :search ESCAPE '='";
}
if (Request::get('machineuuid')) {
$whereClause .= " AND machineuuid = :uuid";
$qArgs['uuid'] = Request::get('machineuuid', '', 'string');
}
$allowedLocations = User::getAllowedLocations("view");
$joinClause = "";
if (!in_array(0, $allowedLocations)) {
$joinClause = "INNER JOIN machine USING (machineuuid)";
$whereClause .= ' AND locationid IN (:allowedLocations)';
$qArgs['allowedLocations'] = $allowedLocations;
}
$lines = array();
$paginate = new Paginate("SELECT logid, dateline, logtypeid, clientlog.clientip, clientlog.machineuuid, description, extra
FROM clientlog $joinClause
WHERE $whereClause
ORDER BY logid DESC", 50);
$res = $paginate->exec($qArgs);
foreach ($res as $row) {
$row['date'] = Util::prettyTime($row['dateline']);
$row['icon'] = $this->eventToIconName($row['logtypeid']);
$lines[] = $row;
}
$paginate->render('page-syslog', array(
'filter' => $filter,
'search' => $search,
'not' => $not,
'list' => $lines,
'types' => json_encode(array_values($types)),
'machineuuid' => Request::get('machineuuid'),
));
}
private function eventToIconName(string $event): string
{
switch ($event) {
case 'session-open':
return 'glyphicon-log-in';
case 'session-close':
return 'glyphicon-log-out';
case 'partition-swap':
return 'glyphicon-info-sign';
case 'partition-temp':
case 'smartctl-realloc':
return 'glyphicon-exclamation-sign';
default:
return 'glyphicon-minus';
}
}
}