misuzu/public-legacy/manage/general/emoticon.php

108 lines
3.5 KiB
PHP
Raw Normal View History

2022-09-13 13:14:49 +00:00
<?php
namespace Misuzu;
use RuntimeException;
use Index\XArray;
2022-09-13 13:14:49 +00:00
if(!$msz->authInfo->getPerms('global')->check(Perm::G_EMOTES_MANAGE))
Template::throwError(403);
2022-09-13 13:14:49 +00:00
$emoteId = (string)filter_input(INPUT_GET, 'e', FILTER_SANITIZE_NUMBER_INT);
2023-11-26 22:23:47 +00:00
$emoteInfo = [];
$emoteStrings = [];
2022-09-13 13:14:49 +00:00
if(empty($emoteId))
$isNew = true;
else
try {
$isNew = false;
$emoteInfo = $msz->emotes->getEmote($emoteId);
$emoteStrings = iterator_to_array($msz->emotes->getEmoteStrings($emoteInfo));
} catch(RuntimeException $ex) {
Template::throwError(404);
}
2022-09-13 13:14:49 +00:00
// make errors not echos lol
while($_SERVER['REQUEST_METHOD'] === 'POST' && CSRF::validateRequest()) {
$order = (int)filter_input(INPUT_POST, 'em_order', FILTER_SANITIZE_NUMBER_INT);
$minRank = (int)filter_input(INPUT_POST, 'em_minrank', FILTER_SANITIZE_NUMBER_INT);
$url = trim((string)filter_input(INPUT_POST, 'em_url'));
$strings = explode(' ', trim((string)filter_input(INPUT_POST, 'em_strings')));
2022-09-13 13:14:49 +00:00
if($isNew || $url !== $emoteInfo->url) {
$checkUrl = $msz->emotes->checkEmoteUrl($url);
if($checkUrl !== '') {
echo match($checkUrl) {
'empty' => 'URL may not be empty.',
'spaces' => 'URL may not end or start with spaces.',
'used' => 'This URL already belongs to another emoticon.',
default => 'URL cannot be accepted: ' . $checkUrl,
};
break;
2022-09-13 13:14:49 +00:00
}
}
if($order == 0)
$order = null;
if($isNew) {
$emoteInfo = $msz->emotes->createEmote($url, $minRank, $order);
} else {
if($order === $emoteInfo->order)
$order = null;
if($minRank === $emoteInfo->minRank)
$minRank = null;
if($url === $emoteInfo->url)
$url = null;
2022-09-13 13:14:49 +00:00
2023-07-15 02:05:49 +00:00
if($order !== null || $minRank !== null || $url !== null)
$msz->emotes->updateEmote($emoteInfo, $order, $minRank, $url);
2022-09-13 13:14:49 +00:00
}
$sCurrent = XArray::select($emoteStrings, fn($stringInfo) => $stringInfo->string);
$sApply = $strings;
$sRemove = [];
foreach($sCurrent as $string)
if(!in_array($string, $sApply)) {
$sRemove[] = $string;
$msz->emotes->removeEmoteString($string);
}
$sCurrent = array_diff($sCurrent, $sRemove);
foreach($sApply as $string)
if(!in_array($string, $sCurrent)) {
$checkString = $msz->emotes->checkEmoteString($string);
if($checkString === '') {
$msz->emotes->addEmoteString($emoteInfo, $string);
} else {
echo match($checkString) {
'empty' => 'String may not be empty.',
'spaces' => 'String may not end or start with spaces.',
'case' => 'String must be lowercase.',
'format' => 'String must follow proper formatting.',
'used' => 'This string has already been used for another emoticon.',
default => 'String cannot be accepted: ' . $checkString,
};
break;
}
$sCurrent[] = $string;
}
$msz->createAuditLog(
$isNew ? 'EMOTICON_CREATE' : 'EMOTICON_EDIT',
[$emoteInfo->id]
2023-07-15 02:05:49 +00:00
);
Tools::redirect($msz->urls->format('manage-general-emoticon', ['emote' => $emoteInfo->id]));
return;
2022-09-13 13:14:49 +00:00
}
Template::render('manage.general.emoticon', [
'emote_new' => $isNew,
2023-11-26 22:23:47 +00:00
'emote_info' => $emoteInfo,
'emote_strings' => $emoteStrings,
2022-09-13 13:14:49 +00:00
]);