Improve posting error handling

- if spamnoticer was contacted, add cleanup callback to error handler if
  something goes wrong during the rest of the process (like thumbnail
  creation for example)

- add another allowed warning (string) that gm may safely report back
  and don't call error if we see it.
This commit is contained in:
towards-a-new-leftypol 2024-12-22 05:21:37 +00:00
parent f5a073f0b1
commit 221faac75b
4 changed files with 94 additions and 18 deletions

View File

@ -24,6 +24,42 @@ register_shutdown_function('fatal_error_handler');
$error_recursion=false;
/*
* Global anything is always a bad idea, but since all of this website's error handling comes
* down to calling this error function and quitting, we have no way of catching exceptions, for example
* during thumbnail creation.
*
* So push things to run in case of a crash into a list, and then run all of them in error.
*
* This will be exclusive to callbacks for posting a post callflow, not mod actions or anything else.
*/
function global_post_cleanup() {
global $post_cleanup_list;
foreach ($post_cleanup_list as $f) {
$f();
}
unset($post_cleanup_list);
}
function push_global_post_cleanup($f) {
global $post_cleanup_list;
if (!isset($post_cleanup_list)) {
$post_cleanup_list = array($f);
} else {
array_push($post_cleanup_list, $f);
}
}
function init_global_post_cleanup() {
global $post_cleanup_list;
$post_cleanup_list = array();
}
function error($message, $priority = true, $debug_stuff = false) {
global $board, $mod, $config, $db_error, $error_recursion;
@ -75,11 +111,14 @@ function error($message, $priority = true, $debug_stuff = false) {
$data['debug']=$debug_stuff;
}
print json_encode($data);
global_post_cleanup();
exit();
}
header($_SERVER['SERVER_PROTOCOL'] . ' 500 Internal Server Error');
global_post_cleanup();
die(Element('page.html', array(
'config' => $config,
'title' => _('Error'),

View File

@ -171,12 +171,13 @@ class ImageBase extends ImageGD {
$this->width = $width;
$this->height = $height;
if (method_exists($this, 'resize'))
if (method_exists($this, 'resize')) {
$this->resize();
else
} else {
// use default GD functions
$this->GD_resize();
}
}
}
class ImageImagick extends ImageBase {
@ -339,6 +340,24 @@ class ImageConvert extends ImageBase {
$this->temp = false;
}
// Returns true if there is a real error and false if it's just a warning
// (appilcable to the return text of the `gm convert` command only)
public static function actualErrorOrJustWarning($message_string) {
$warnings = array(
"known incorrect sRGB profile",
"iCCP: Not recognizing known sRGB profile that has been edited",
"sRGB: cHRM chunk does not match sRGB"
);
foreach ($warnings as $w) {
if (strpos($message_string, $w) !== False) {
return False;
}
}
return True;
}
public function resize() {
global $config;
@ -390,20 +409,25 @@ class ImageConvert extends ImageBase {
$convert_args = str_replace('-auto-orient', '', $config['convert_args']);
else
$convert_args = &$config['convert_args'];
if (($error = shell_exec_error(($this->gm ? 'gm ' : '') . 'convert ' .
sprintf($convert_args,
$full_convert_cmd =
($this->gm ? 'gm ' : '') . 'convert ' .
sprintf(
$convert_args,
$this->width,
$this->height,
escapeshellarg($this->src . '[0]'),
$this->width,
$this->height,
escapeshellarg($this->temp)))) || !file_exists($this->temp)) {
escapeshellarg($this->temp)
);
if (strpos($error, "known incorrect sRGB profile") === false &&
strpos($error, "iCCP: Not recognizing known sRGB profile that has been edited") === false) {
if (($error = shell_exec_error($full_convert_cmd)) || !file_exists($this->temp)) {
if (ImageConvert::actualErrorOrJustWarning($error)) {
$this->destroy();
error(_('Failed to resize image!')." "._('Details: ').nl2br(htmlspecialchars($error)), null, array('convert_error' => $error));
}
if (!file_exists($this->temp)) {
$this->destroy();
error(_('Failed to resize image!'), null, $error);

View File

@ -138,14 +138,14 @@ $config['spam']['enabled'] = true;
$config['spam']['hidden_inputs_expire'] = 60 * 60 * 24 * 120; //keep hashes for 120 days in the database just in case someone posts on a slow board.
$config['spam_noticer']['enabled'] = true;
$config['spam_noticer']['base_url'] = 'http://localhost:8300';
$config['spam_noticer']['ui_url'] = 'https://spamnoticer.leftychan.net/static/index.html';
$config['spam_noticer']['ui_url'] = 'http://localhost:8081/static/index.html';
$config['spam_noticer']['imageboard_root'] = 'http://leftychan.net/';
$config['spam_noticer']['website_name'] = "leftychan";
/*
* Basic captcha. See also: captchaconfig.php
*/
$config['securimage'] = true;
$config['securimage'] = false;
$config['captcha_tor_only'] = true;
/*
@ -597,5 +597,5 @@ $config['filters'][] = array(
'message' => 'New threads are being created too quickly. Wait [at most] 10 minutes'
);
$config['global_message'] = '<span><a href="https://talk.leftychan.net/#/room/#welcome:matrix.leftychan.net">Matrix</a></span> &nbsp; <span><a href="ircs://irc.leftychan.net:6697/#leftychan">IRC Chat</a></span> &nbsp; <span><a href="mumble://leftychan.net">Mumble</a></span> &nbsp; <span><a href="https://t.me/+RegtyzzrE0M1NDMx">Telegram</a></span> &nbsp; <span><a href="https://discord.gg/AcZeFKXPmZ">Discord</a></span><br/><br/><span>We will be performing scheduled maintenance from 2-3am UTC, May 30th. The board will be read only during this time.</span>';
$config['debug'] = false;
$config['global_message'] = '<span><a href="https://talk.leftychan.net/#/room/#welcome:matrix.leftychan.net">Matrix</a></span> &nbsp; <span><a href="ircs://irc.leftychan.net:6697/#leftychan">IRC Chat</a></span> &nbsp; <span><a href="mumble://leftychan.net">Mumble</a></span> &nbsp; <span><a href="https://t.me/+RegtyzzrE0M1NDMx">Telegram</a></span> &nbsp; <span><a href="https://discord.gg/AcZeFKXPmZ">Discord</a></span>';
$config['debug'] = true;

View File

@ -457,6 +457,8 @@ function validate_images(array $post_array) {
function handle_post(){
global $config,$dropped_post,$board, $mod,$pdo;
init_global_post_cleanup();
if (!isset($_POST['body'], $_POST['board']) && !$dropped_post) {
error($config['error']['bot']);
}
@ -1036,6 +1038,17 @@ function handle_post(){
$spam_noticer_result = checkWithSpamNoticer($config, $post, $board['uri']);
/*
* If we have an error with posting this later, send back the
* delete token to spamnoticer to remove the post from the recent
* posts table. (see error.php for the error cleanup function)
*/
$f_spamnoticer_cleanup_on_err = function() use ($config, $delete_token) {
removeRecentPostFromSpamnoticer($config, array($delete_token));
};
push_global_post_cleanup($f_spamnoticer_cleanup_on_err);
if ($spam_noticer_result->succeeded && $spam_noticer_result->noticed) {
error($config['error']['spam_noticer'] . $spam_noticer_result->reason);
}