From 799072d692179a70d5960c63ced88d58b06ff3c8 Mon Sep 17 00:00:00 2001 From: Zankaria Date: Tue, 16 Jan 2024 18:01:13 +0000 Subject: [PATCH] Refactor post.php: split upload file hashing to a function --- post.php | 49 ++++++++++++++++++++++++++++++++++--------------- 1 file changed, 34 insertions(+), 15 deletions(-) diff --git a/post.php b/post.php index 31f4f0bf..33ed030b 100644 --- a/post.php +++ b/post.php @@ -5,6 +5,39 @@ require_once 'inc/bootstrap.php'; +/** + * Utility functions + */ + +/** + * Get the md5 hash of the file. + * + * @param [type] $config instance configuration. + * @param [type] $file file to the the md5 of. + * @return string|false + */ +function md5_hash_of_file($config, $file) { + $cmd = false; + if ($config['bsd_md5']) { + $cmd = '/sbin/md5 -r'; + } + if ($config['gnu_md5']) { + $cmd = 'md5sum'; + } + + if ($cmd) { + $output = shell_exec_error($cmd . " " . escapeshellarg($file)); + $output = explode(' ', $output); + return $output[0]; + } else { + return md5_file($file); + } +} + +/** + * Method handling functions + */ + $dropped_post = false; function handle_nntpchan() @@ -1007,14 +1040,6 @@ function handle_post() $post['tracked_cites'] = markup($post['body'], true); if ($post['has_file']) { - $md5cmd = false; - if ($config['bsd_md5']) { - $md5cmd = '/sbin/md5 -r'; - } - if ($config['gnu_md5']) { - $md5cmd = 'md5sum'; - } - $allhashes = ''; foreach ($post['files'] as $key => &$file) { @@ -1037,13 +1062,7 @@ function handle_post() error($config['error']['nomove']); } - if ($md5cmd) { - $output = shell_exec_error($md5cmd . " " . escapeshellarg($upload)); - $output = explode(' ', $output); - $hash = $output[0]; - } else { - $hash = md5_file($upload); - } + $hash = md5_hash_of_file($config, $upload); $file['hash'] = $hash; $allhashes .= $hash;