Template/Update (Part 38)

* split Screenshot upload & management into separate endpoints
 * move shared functions to manager classes
 * cleanup javascript
 * move test for config screenshot min size to cfg class
This commit is contained in:
Sarjuuk
2025-08-13 23:19:57 +02:00
parent 3d3e2211e5
commit a369244908
35 changed files with 1546 additions and 1031 deletions

View File

@@ -236,129 +236,6 @@ class CommunityContent
return $replies;
}
public static function getScreenshotsForManager($type, $typeId, $userId = 0)
{
$screenshots = DB::Aowow()->select(
'SELECT s.`id`, a.`username` AS "user", s.`date`, s.`width`, s.`height`, s.`type`, s.`typeId`, s.`caption`, s.`status`, s.`status` AS "flags"
FROM ?_screenshots s
LEFT JOIN ?_account a ON s.`userIdOwner` = a.`id`
WHERE
{ s.`type` = ?d}
{ AND s.`typeId` = ?d}
{ s.`userIdOwner` = ?d}
LIMIT 100',
$userId ? DBSIMPLE_SKIP : $type,
$userId ? DBSIMPLE_SKIP : $typeId,
$userId ? $userId : DBSIMPLE_SKIP
);
$num = [];
foreach ($screenshots as $s)
{
if (empty($num[$s['type']][$s['typeId']]))
$num[$s['type']][$s['typeId']] = 1;
else
$num[$s['type']][$s['typeId']]++;
}
// format data to meet requirements of the js
foreach ($screenshots as $idx => &$s)
{
$s['date'] = date(Util::$dateFormatInternal, $s['date']);
$s['name'] = "Screenshot #".$s['id']; // what should we REALLY name it?
if (isset($screenshots[$idx - 1]))
$s['prev'] = $idx - 1;
if (isset($screenshots[$idx + 1]))
$s['next'] = $idx + 1;
// order gives priority for 'status'
if (!($s['flags'] & CC_FLAG_APPROVED))
{
$s['pending'] = 1;
$s['status'] = 0;
}
else
$s['status'] = 100;
if ($s['flags'] & CC_FLAG_STICKY)
{
$s['sticky'] = 1;
$s['status'] = 105;
}
if ($s['flags'] & CC_FLAG_DELETED)
{
$s['deleted'] = 1;
$s['status'] = 999;
}
// something todo with massSelect .. am i doing this right?
if ($num[$s['type']][$s['typeId']] == 1)
$s['unique'] = 1;
if (!$s['user'])
unset($s['user']);
}
return $screenshots;
}
public static function getScreenshotPagesForManager($all, &$nFound)
{
// i GUESS .. ss_getALL ? everything : pending
$nFound = 0;
$pages = DB::Aowow()->select(
'SELECT s.`type`, s.`typeId`, COUNT(1) AS "count", MIN(s.`date`) AS "date"
FROM ?_screenshots s
{ WHERE (s.`status` & ?d) = 0 }
GROUP BY s.`type`, s.`typeId`',
$all ? DBSIMPLE_SKIP : CC_FLAG_APPROVED | CC_FLAG_DELETED
);
if ($pages)
{
// limit to one actually existing type each
foreach (array_unique(array_column($pages, 'type')) as $t)
{
$ids = [];
foreach ($pages as $row)
if ($row['type'] == $t)
$ids[] = $row['typeId'];
if (!$ids)
continue;
$obj = Type::newList($t, [Cfg::get('SQL_LIMIT_NONE'), ['id', $ids]]);
if (!$obj || $obj->error)
continue;
foreach ($pages as &$p)
if ($p['type'] == $t)
if ($obj->getEntry($p['typeId']))
$p['name'] = $obj->getField('name', true);
}
foreach ($pages as &$p)
{
if (empty($p['name']))
{
trigger_error('Screenshot linked to nonexistent type/typeId combination: '.$p['type'].'/'.$p['typeId'], E_USER_NOTICE);
unset($p);
}
else
{
$nFound += $p['count'];
$p['date'] = date(Util::$dateFormatInternal, $p['date']);
}
}
}
return $pages;
}
public static function getComments(int $type, int $typeId) : array
{

View File

@@ -0,0 +1,306 @@
<?php
namespace Aowow;
if (!defined('AOWOW_REVISION'))
die('illegal access');
abstract class ImageUpload
{
public const /* int */ MIME_UNK = 0;
public const /* int */ MIME_JPG = 1;
public const /* int */ MIME_PNG = 2;
public const /* int */ MIME_WEBP = 3;
// scale img down if larger than crop screen
private const /* int */ CROP_W = 488;
private const /* int */ CROP_H = 325;
protected const /* int */ JPEG_QUALITY = 85;
protected static int $mimeType = self::MIME_UNK;
protected static ?\GdImage $img = null;
protected static string $fileName = '';
protected static string $uploadFormField;
protected static string $tmpPath;
public static bool $hasUpload = false;
public static string $error = '';
public static function init() : bool
{
// active screenshot upload
self::$hasUpload = $_FILES && !empty($_FILES[static::$uploadFormField]);
return true;
}
public static function validateUpload() : bool
{
if (!self::$hasUpload)
return false;
switch ($_FILES[static::$uploadFormField]['error']) // 0 is fine
{
case UPLOAD_ERR_INI_SIZE: // 1
case UPLOAD_ERR_FORM_SIZE: // 2
trigger_error('ImageUpload::validateUpload - the file exceeds the maximum size of '.ini_get('upload_max_filesize'), E_USER_WARNING);
self::$error = Lang::main('intError');
return false;
case UPLOAD_ERR_PARTIAL: // 3
trigger_error('ImageUpload::validateUpload - upload was interrupted', E_USER_WARNING);
self::$error = Lang::screenshot('error', 'selectSS');
return false;
case UPLOAD_ERR_NO_FILE: // 4
trigger_error('ImageUpload::validateUpload - no file was received', E_USER_WARNING);
self::$error = Lang::screenshot('error', 'selectSS');
return false;
case UPLOAD_ERR_NO_TMP_DIR: // 6
trigger_error('ImageUpload::validateUpload - temporary upload directory is not set', E_USER_ERROR);
self::$error = Lang::main('intError');
return false;
case UPLOAD_ERR_CANT_WRITE: // 7
trigger_error('ImageUpload::validateUpload - could not write temporary file to disk', E_USER_ERROR);
self::$error = Lang::main('intError');
return false;
case UPLOAD_ERR_EXTENSION: // 8
trigger_error('ImageUpload::validateUpload - a php extension stopped the file upload.', E_USER_ERROR);
self::$error = Lang::main('intError');
return false;
}
self::$fileName = $_FILES[static::$uploadFormField]['tmp_name'];
// points to invalid file
if (!is_uploaded_file(self::$fileName))
{
trigger_error('ImageUpload::validateUpload - uploaded file not in upload directory', E_USER_ERROR);
self::$error = Lang::main('intError');
self::$fileName = '';
return false;
}
// check if file is an image
if (!self::setMimeType())
{
self::$error = Lang::screenshot('error', 'unkFormat');
self::$fileName = '';
return false;
}
if (!self::$error)
return true;
self::$fileName = '';
return false;
}
public static function loadUpload() : bool
{
if (!self::$hasUpload)
return false;
return match (self::$mimeType)
{
self::MIME_JPG => self::loadFromJPG(),
self::MIME_PNG => self::loadFromPNG(),
self::MIME_WEBP => self::loadFromWEBP(),
default => false
};
}
public static function loadFile(string $path, string $nameBase) : bool
{
self::$fileName = sprintf($path, $nameBase);
if (!file_exists(self::$fileName))
{
trigger_error('ImageUpload::loadFile - image ('.self::$fileName.') not found', E_USER_ERROR);
self::$fileName = '';
return false;
}
// we are using only jpg internally
return self::loadFromJPG();
}
public static function calcImgDimensions() : array
{
if (!self::$img)
return [];
$oSize = $rSize = [imagesx(self::$img), imagesy(self::$img)];
$rel = $oSize[0] / $oSize[1];
// check for oversize and refit to crop-screen
if ($rel >= 1.5 && $oSize[0] > self::CROP_W)
$rSize = [self::CROP_W, self::CROP_W / $rel];
else if ($rel < 1.5 && $oSize[1] > self::CROP_H)
$rSize = [self::CROP_H * $rel, self::CROP_H];
// r: resized; o: original
// r: x <= 488 && y <= 325 while x proportional to y
return array(
'oWidth' => $oSize[0],
'rWidth' => $rSize[0],
'oHeight' => $oSize[1],
'rHeight' => $rSize[1]
);
}
public static function tempSaveUpload(array $tmpNameParts, ?string &$uid) : bool
{
if (!self::$img || !$tmpNameParts)
return false;
$uid = Util::createHash(16);
$nameBase = User::$username.'-'.implode('-', $tmpNameParts).'-'.$uid;
// use this image for work
if (!self::writeImage(static::$tmpPath, $nameBase.'_original'))
return false;
['oWidth' => $oW, 'rWidth' => $rW, 'oHeight' => $oH, 'rHeight' => $rH] = self::calcImgDimensions();
// use this image to display in cropper
$res = imagecreatetruecolor($rW, $rH);
if (!$res)
{
trigger_error('ImageUpload::tempSaveUpload - imagecreate failed', E_USER_ERROR);
return false;
}
if (!imagecopyresampled($res, self::$img, 0, 0, 0, 0, $rW, $rH, $oW, $oH))
{
trigger_error('ImageUpload::tempSaveUpload - imagecopy failed', E_USER_ERROR);
return false;
}
self::$img = $res;
unset($res);
return self::writeImage(static::$tmpPath, $nameBase);
}
public static function cropImg(float $scaleX, float $scaleY, float $scaleW, float $scaleH) : bool
{
if (!self::$img)
return false;
$x = (int)(imagesx(self::$img) * $scaleX);
$y = (int)(imagesy(self::$img) * $scaleY);
$w = (int)(imagesx(self::$img) * $scaleW);
$h = (int)(imagesy(self::$img) * $scaleH);
$destImg = imagecreatetruecolor($w, $h);
if (!$destImg)
return false;
// imagefill($destImg, 0, 0, imagecolorallocate($destImg, 255, 255, 255));
imagecopy($destImg, self::$img, 0, 0, $x, $y, $w, $h);
self::$img = $destImg;
imagedestroy($destImg);
return true;
}
public static function writeImage(string $path, string $file) : bool
{
if (!self::$img)
return false;
if (imagejpeg(self::$img, sprintf($path, $file), self::JPEG_QUALITY))
return true;
trigger_error('ImageUpload::writeImage - write failed', E_USER_ERROR);
return false;
}
private static function setMimeType() : bool
{
if (!self::$hasUpload)
return false;
$mime = (new \finfo(FILEINFO_MIME))?->file(self::$fileName);
if ($mime && stripos($mime, 'image/png') === 0)
self::$mimeType = self::MIME_PNG;
else if ($mime && stripos($mime, 'image/webp') === 0)
self::$mimeType = self::MIME_WEBP;
else if ($mime && preg_match('/^image\/jpe?g/i', $mime))
self::$mimeType = self::MIME_JPG;
else
trigger_error('ImageUpload::setMimeType - uploaded file is of type: '.$mime, E_USER_WARNING);
return self::$mimeType != self::MIME_UNK;
}
private static function loadFromPNG() : bool
{
// straight self::$img = imagecreatefrompng(self::$fileName); causes issues when transforming the alpha channel
// this roundabout way through imagealphablending() avoids that
$image = imagecreatefrompng(self::$fileName);
if (!$image)
return false;
self::$img = imagecreatetruecolor(imagesx($image), imagesy($image)) ?: null;
if (!self::$img)
return false;
imagealphablending(self::$img, true);
imagecopy(self::$img, $image, 0, 0, 0, 0, imagesx($image), imagesy($image));
imagedestroy($image);
return true;
}
private static function loadFromJPG() : bool
{
self::$img = imagecreatefromjpeg(self::$fileName) ?: null;
return !is_null(self::$img);
}
private static function loadFromWEBP() : bool
{
$image = imagecreatefromwebp(self::$fileName);
if (!$image)
return false;
self::$img = imagecreatetruecolor(imagesx($image), imagesy($image)) ?: null;
if (!self::$img)
return false;
imagealphablending(self::$img, true);
imagecopy(self::$img, $image, 0, 0, 0, 0, imagesx($image), imagesy($image));
imagedestroy($image);
return true;
}
protected static function resizeAndWrite(int $limitW, int $limitH, string $path, string $file) : bool
{
$srcW = imagesx(self::$img);
$srcH = imagesy(self::$img);
// already small enough
if ($srcW < $limitW && $srcH < $limitH)
return true;
$scale = min(1.0, $limitW / $srcW, $limitH / $srcH);
$destW = $srcW * $scale;
$destH = $srcH * $scale;
$destImg = imagecreatetruecolor($destW, $destH);
// imagefill($destImg, 0, 0, imagecolorallocate($destImg, 255, 255, 255));
imagecopyresampled($destImg, self::$img, 0, 0, 0, 0, $destW, $destH, $srcW, $srcH);
return imagejpeg($destImg, sprintf($path, $file), self::JPEG_QUALITY);
}
}
?>

View File

@@ -86,10 +86,7 @@ trait TrCommunityHelper
$caption = trim(preg_replace('/\s{2,}/', ' ', $caption));
// shorten to fit db
$caption = substr($caption, 0, 200);
// jsEscape just in case
return Util::jsEscape($caption);
return substr($caption, 0, 200);
}
}

View File

@@ -0,0 +1,230 @@
<?php
namespace Aowow;
if (!defined('AOWOW_REVISION'))
die('illegal access');
class ScreenshotMgr extends ImageUpload
{
// config value
public static int $MIN_SIZE; // 200
// 4k resolution
private const /* int */ MAX_W = 4096;
private const /* int */ MAX_H = 2160;
// as expected by js - this also makes the CC-flags functionally exclusive with each other
private const /* int */ STATUS_PENDING = 0;
private const /* int */ STATUS_DELETED = 999;
private const /* int */ STATUS_APPROVED = 100;
private const /* int */ STATUS_STICKY = 105;
private const /* array */ DIMS_RESIZED = [772, 618];
private const /* array */ DIMS_THUMB = [150, 150];
protected static string $uploadFormField = 'screenshotfile';
protected static string $tmpPath = self::PATH_TEMP;
public const /* string */ PATH_TEMP = 'static/uploads/screenshots/temp/%s.jpg';
public const /* string */ PATH_PENDING = 'static/uploads/screenshots/pending/%d.jpg';
public const /* string */ PATH_THUMB = 'static/uploads/screenshots/thumb/%d.jpg';
public const /* string */ PATH_RESIZED = 'static/uploads/screenshots/resized/%d.jpg';
public const /* string */ PATH_NORMAL = 'static/uploads/screenshots/normal/%d.jpg';
public static function init() : bool
{
self::$MIN_SIZE = Cfg::get('SCREENSHOT_MIN_SIZE');
$dirErr = false;
foreach (['TEMP', 'PENDING', 'THUMB', 'RESIZED', 'NORMAL'] as $p)
{
$path = constant('self::PATH_' . $p);
if (!is_writable(substr($path, 0, strrpos($path, '/'))))
{
trigger_error('ScreenshotMgr::init - directory '.substr($path, 0, strrpos($path, '/')).' not writable', E_USER_ERROR);
$dirErr = true;
}
}
if ($dirErr)
return false;
return parent::init();
}
public static function validateUpload() : bool
{
if (!parent::validateUpload())
return false;
// invalid file
if ($is = getimagesize(self::$fileName))
{
// image size out of bounds
if ($is[0] < self::$MIN_SIZE || $is[1] < self::$MIN_SIZE)
self::$error = Lang::screenshot('error', 'tooSmall');
else if ($is[0] > self::MAX_W || $is[1] > self::MAX_H)
self::$error = Lang::screenshot('error', 'selectSS');
}
else
self::$error = Lang::screenshot('error', 'selectSS');
if (!self::$error)
return true;
self::$fileName = '';
return false;
}
public static function createThumbnail(string $fileName) : bool
{
if (!self::$img)
return false;
return static::resizeAndWrite(self::DIMS_THUMB[0], self::DIMS_THUMB[1], self::PATH_THUMB, $fileName);
}
public static function createResized(string $fileName) : bool
{
if (!self::$img)
return false;
return self::resizeAndWrite(self::DIMS_RESIZED[0], self::DIMS_RESIZED[1], self::PATH_RESIZED, $fileName);
}
/*************/
/* Admin Mgr */
/*************/
public static function getScreenshots(int $type = 0, int $typeId = 0, $userId = 0, ?int &$nFound = 0) : array
{
$screenshots = DB::Aowow()->select(
'SELECT s.`id`, a.`username` AS "user", s.`date`, s.`width`, s.`height`, s.`type`, s.`typeId`, s.`caption`, s.`status`, s.`status` AS "flags"
FROM ?_screenshots s
LEFT JOIN ?_account a ON s.`userIdOwner` = a.`id`
WHERE
{ s.`type` = ?d }
{ AND s.`typeId` = ?d }
{ s.`userIdOwner` = ?d }
{ LIMIT ?d }',
$userId ? DBSIMPLE_SKIP : $type,
$userId ? DBSIMPLE_SKIP : $typeId,
$userId ? $userId : DBSIMPLE_SKIP,
$userId || $type ? DBSIMPLE_SKIP : 100
);
$num = [];
foreach ($screenshots as $s)
{
if (empty($num[$s['type']][$s['typeId']]))
$num[$s['type']][$s['typeId']] = 1;
else
$num[$s['type']][$s['typeId']]++;
}
$nFound = 0;
// format data to meet requirements of the js
foreach ($screenshots as $i => &$s)
{
$nFound++;
$s['date'] = date(Util::$dateFormatInternal, $s['date']);
$s['name'] = "Screenshot #".$s['id']; // what should we REALLY name it?
if ($i > 0)
$s['prev'] = $i - 1;
if (($i + 1) < count($screenshots))
$s['next'] = $i + 1;
// order gives priority for 'status'
if (!($s['flags'] & CC_FLAG_APPROVED))
{
$s['pending'] = 1;
$s['status'] = self::STATUS_PENDING;
}
else
$s['status'] = self::STATUS_APPROVED;
if ($s['flags'] & CC_FLAG_STICKY)
{
$s['sticky'] = 1;
$s['status'] = self::STATUS_STICKY;
}
if ($s['flags'] & CC_FLAG_DELETED)
{
$s['deleted'] = 1;
$s['status'] = self::STATUS_DELETED;
}
// something todo with massSelect .. am i doing this right?
if ($num[$s['type']][$s['typeId']] == 1)
$s['unique'] = 1;
if (!$s['user'])
unset($s['user']);
}
return $screenshots;
}
public static function getPages(?bool $all, ?int &$nFound) : array
{
// i GUESS .. ss_getALL ? everything : pending
$nFound = 0;
$pages = DB::Aowow()->select(
'SELECT s.`type`, s.`typeId`, COUNT(1) AS "count", MIN(s.`date`) AS "date"
FROM ?_screenshots s
{ WHERE (s.`status` & ?d) = 0 }
GROUP BY s.`type`, s.`typeId`',
$all ? DBSIMPLE_SKIP : CC_FLAG_APPROVED | CC_FLAG_DELETED
);
if ($pages)
{
// limit to one actually existing type each
foreach (array_unique(array_column($pages, 'type')) as $t)
{
$ids = [];
foreach ($pages as $row)
if ($row['type'] == $t)
$ids[] = $row['typeId'];
if (!$ids)
continue;
$obj = Type::newList($t, [Cfg::get('SQL_LIMIT_NONE'), ['id', $ids]]);
if (!$obj || $obj->error)
continue;
foreach ($pages as &$p)
if ($p['type'] == $t)
if ($obj->getEntry($p['typeId']))
$p['name'] = $obj->getField('name', true);
}
foreach ($pages as &$p)
{
if (empty($p['name']))
{
trigger_error('ScreenshotMgr::getPages - screenshot linked to nonexistent type/typeId combination: '.$p['type'].'/'.$p['typeId'], E_USER_NOTICE);
unset($p);
}
else
{
$nFound += $p['count'];
$p['date'] = date(Util::$dateFormatInternal, $p['date']);
}
}
}
return $pages;
}
}
?>