From 221faac75b3a93f74db87634e26bcf9c220e134f Mon Sep 17 00:00:00 2001 From: towards-a-new-leftypol Date: Sun, 22 Dec 2024 05:21:37 +0000 Subject: [PATCH] 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. --- inc/error.php | 39 +++++++++++++++++++++++++++++++ inc/image.php | 52 ++++++++++++++++++++++++++++++----------- inc/instance-config.php | 8 +++---- post.php | 13 +++++++++++ 4 files changed, 94 insertions(+), 18 deletions(-) diff --git a/inc/error.php b/inc/error.php index de69db35..ff0881fe 100644 --- a/inc/error.php +++ b/inc/error.php @@ -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'), diff --git a/inc/image.php b/inc/image.php index 0237da21..b4c17b24 100644 --- a/inc/image.php +++ b/inc/image.php @@ -171,11 +171,12 @@ 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(); + } } } @@ -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,24 +409,29 @@ 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) { - $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); - } + 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); + } } if ($size = $this->get_size($this->temp)) { $this->width = $size[0]; diff --git a/inc/instance-config.php b/inc/instance-config.php index e54e770d..1ce6b89a 100644 --- a/inc/instance-config.php +++ b/inc/instance-config.php @@ -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'] = 'Matrix   IRC Chat   Mumble   Telegram   Discord

We will be performing scheduled maintenance from 2-3am UTC, May 30th. The board will be read only during this time.'; -$config['debug'] = false; +$config['global_message'] = 'Matrix   IRC Chat   Mumble   Telegram   Discord'; +$config['debug'] = true; diff --git a/post.php b/post.php index 22932166..0f12ab4f 100644 --- a/post.php +++ b/post.php @@ -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); }