<?php
class SubPage
{
public static function doPreprocess()
{
}
private static function loadExpiredImages(): array
{
$res = Database::simpleQuery("SELECT b.displayname,
own.firstname, own.lastname, own.userid,
v.imageversionid, v.createtime, v.filesize, v.deletestate,
lat.expiretime AS latexptime, lat.deletestate AS latdelstate
FROM sat.imageversion v
INNER JOIN sat.imagebase b ON (b.imagebaseid = v.imagebaseid)
INNER JOIN sat.user own ON (b.ownerid = own.userid)
LEFT JOIN sat.imageversion lat ON (b.latestversionid = lat.imageversionid)
WHERE v.deletestate <> 'KEEP'
ORDER BY b.displayname ASC, v.createtime ASC");
$NOW = time();
$rows = array();
foreach ($res as $row) {
if ($row['latexptime'] > $NOW && $row['latdelstate'] === 'KEEP') {
$row['hasNewerClass'] = 'glyphicon-ok green';
$row['checked'] = 'checked';
} else {
$row['hasNewerClass'] = 'glyphicon-remove red';
}
if ($row['deletestate'] === 'WANT_DELETE') {
$row['name_extra_class'] = 'slx-strike';
}
$row['version'] = date('d.m.Y H:i:s', $row['createtime']);
$row['rawfilesize'] = $row['filesize'];
$row['filesize'] = Util::readableFileSize($row['filesize']);
$rows[] = $row;
}
return $rows;
}
public static function doRender()
{
$expiredImages = self::loadExpiredImages();
$data = ['images' => $expiredImages];
Permission::addGlobalTags($data['perm'], null, ['expiredimages.delete', 'orphaned.scan']);
Render::addTemplate('images-delete', $data);
}
public static function doAjax()
{
$action = Request::post('action');
if ($action === 'delimages') {
self::handleDeleteImages();
} elseif ($action === 'orphaned') {
self::handleOrphaned();
} else {
echo 'Huh?';
}
}
private static function handleDeleteImages()
{
User::assertPermission("expiredimages.delete");
$images = Request::post('images', false);
$result = false;
if (is_array($images)) {
foreach ($images as $image => $val) {
if (strtolower($val) !== 'on')
continue;
Database::exec("UPDATE sat.imageversion SET deletestate = 'WANT_DELETE'"
. " WHERE deletestate = 'SHOULD_DELETE' AND imageversionid = :imageversionid", array(
'imageversionid' => $image
));
}
if (!empty($images)) {
$result = Download::asStringPost('http://127.0.0.1:9080/do/delete-images', false, 10, $code);
if ($code == 999) {
$result .= "\nConnection to DMSD failed.";
}
}
}
if (!empty($result)) {
echo $result;
}
}
private static function handleOrphaned()
{
if (Request::post('delete', 0, 'int') !== 0) {
User::assertPermission("orphaned.delete");
$action = 'delete';
} else {
User::assertPermission("orphaned.scan");
$action = 'scan';
}
// Talk to dmsd
$result = Download::asStringPost('http://127.0.0.1:9080/do/scan-orphaned-files', ['action' => $action],
10, $code);
if ($code == 999) {
$result = '<div class="alert alert-warning">'
. $result . ' - Connection to DMSD failed.</div>';
} else {
$json = json_decode($result, true);
if (is_array($json)) {
$result = [];
$showDelete = false;
foreach ($json as $k => $v) {
$result[] = ['file' => $k, 'status' => $v];
if ($v === 'EXISTS') {
$showDelete = true;
}
}
$data = ['files' => $result, 'show_delete' => $showDelete];
Permission::addGlobalTags($data['perm'], null, ['orphaned.delete']);
$result = Render::parse('images-orphaned', $data);
}
}
echo $result;
}
}