From 15cce3a31ffb4883287461f25381a966d2dc4bbf Mon Sep 17 00:00:00 2001 From: Zankaria Date: Mon, 29 Jan 2024 22:57:45 +0100 Subject: [PATCH 1/4] Fix leading whitespace in post.php --- post.php | 334 +++++++++++++++++++++++++++---------------------------- 1 file changed, 166 insertions(+), 168 deletions(-) diff --git a/post.php b/post.php index 9ad8f844..ebb048fe 100644 --- a/post.php +++ b/post.php @@ -48,8 +48,8 @@ if (isset($_GET['Newsgroups']) && $config['nntpchan']['enabled']) { $ref = $refs[0]; $query = prepare("SELECT `board`,`id` FROM ``nntp_references`` WHERE `message_id` = :ref"); - $query->bindValue(':ref', $ref); - $query->execute() or error(db_error($query)); + $query->bindValue(':ref', $ref); + $query->execute() or error(db_error($query)); $ary = $query->fetchAll(PDO::FETCH_ASSOC); @@ -134,9 +134,9 @@ if (isset($_GET['Newsgroups']) && $config['nntpchan']['enabled']) { $query = prepare("SELECT `board`,`id` FROM ``nntp_references`` WHERE `message_id_digest` LIKE :rule"); $idx = $id . "%"; - $query->bindValue(':rule', $idx); - $query->execute() or error(db_error($query)); - + $query->bindValue(':rule', $idx); + $query->execute() or error(db_error($query)); + $ary = $query->fetchAll(PDO::FETCH_ASSOC); if (count($ary) == 0) { return ">>>>$id"; @@ -176,47 +176,47 @@ if (!isset($_POST['captcha_cookie']) && isset($_SESSION['captcha_cookie'])) { if (isset($_POST['delete'])) { // Delete - + if (!isset($_POST['board'], $_POST['password'])) error($config['error']['bot']); - + $password = &$_POST['password']; - + if ($password == '') error($config['error']['invalidpassword']); - + $delete = array(); foreach ($_POST as $post => $value) { if (preg_match('/^delete_(\d+)$/', $post, $m)) { $delete[] = (int)$m[1]; } } - + checkDNSBL(); - + // Check if board exists if (!openBoard($_POST['board'])) error($config['error']['noboard']); if ((!isset($_POST['mod']) || !$_POST['mod']) && $config['board_locked']) { - error("Board is locked"); + error("Board is locked"); } - + // Check if banned checkBan($board['uri']); // Check if deletion enabled if (!$config['allow_delete']) error(_('Post deletion is not allowed!')); - + if (empty($delete)) error($config['error']['nodelete']); - + foreach ($delete as &$id) { $query = prepare(sprintf("SELECT `id`,`thread`,`time`,`password` FROM ``posts_%s`` WHERE `id` = :id", $board['uri'])); $query->bindValue(':id', $id, PDO::PARAM_INT); $query->execute() or error(db_error($query)); - + if ($post = $query->fetch(PDO::FETCH_ASSOC)) { $thread = false; if ($config['user_moderation'] && $post['thread']) { @@ -224,20 +224,20 @@ if (isset($_POST['delete'])) { $thread_query->bindValue(':id', $post['thread'], PDO::PARAM_INT); $thread_query->execute() or error(db_error($query)); - $thread = $thread_query->fetch(PDO::FETCH_ASSOC); + $thread = $thread_query->fetch(PDO::FETCH_ASSOC); } if ($post['time'] < time() - $config['max_delete_time'] && $config['max_delete_time'] != false) { error(sprintf($config['error']['delete_too_late'], until($post['time'] + $config['max_delete_time']))); } - + if ($password != '' && $post['password'] != $password && (!$thread || $thread['password'] != $password)) error($config['error']['invalidpassword']); - + if ($post['time'] > time() - $config['delete_time'] && (!$thread || $thread['password'] != $password)) { error(sprintf($config['error']['delete_too_soon'], until($post['time'] + $config['delete_time']))); } - + if (isset($_POST['file'])) { // Delete just the file deleteFile($id); @@ -253,7 +253,7 @@ if (isset($_POST['delete'])) { ); } } - + buildIndex(); $is_mod = isset($_POST['mod']) && $_POST['mod']; @@ -266,39 +266,39 @@ if (isset($_POST['delete'])) { echo json_encode(array('success' => true)); } - // We are already done, let's continue our heavy-lifting work in the background (if we run off FastCGI) - if (function_exists('fastcgi_finish_request')) - @fastcgi_finish_request(); + // We are already done, let's continue our heavy-lifting work in the background (if we run off FastCGI) + if (function_exists('fastcgi_finish_request')) + @fastcgi_finish_request(); rebuildThemes('post-delete', $board['uri']); } elseif (isset($_POST['report'])) { if (!isset($_POST['board'], $_POST['reason'])) error($config['error']['bot']); - + $report = array(); foreach ($_POST as $post => $value) { if (preg_match('/^delete_(\d+)$/', $post, $m)) { $report[] = (int)$m[1]; } } - + checkDNSBL(); - + // Check if board exists if (!openBoard($_POST['board'])) error($config['error']['noboard']); if ((!isset($_POST['mod']) || !$_POST['mod']) && $config['board_locked']) { - error("Board is locked"); + error("Board is locked"); } - + // Check if banned checkBan($board['uri']); - + if (empty($report)) error($config['error']['noreport']); - + if (count($report) > $config['report_limit']) error($config['error']['toomanyreports']); @@ -317,10 +317,10 @@ if (isset($_POST['delete'])) { $resp = curl_exec($ch); if ($resp !== '1') { - error($config['error']['captcha']); + error($config['error']['captcha']); } } - + $reason = escape_markup_modifiers($_POST['reason']); markup($reason); @@ -328,14 +328,13 @@ if (isset($_POST['delete'])) { $query = prepare(sprintf("SELECT `id`, `thread` FROM ``posts_%s`` WHERE `id` = :id", $board['uri'])); $query->bindValue(':id', $id, PDO::PARAM_INT); $query->execute() or error(db_error($query)); - - $post = $query->fetch(PDO::FETCH_ASSOC); - - $error = event('report', array('ip' => $_SERVER['REMOTE_ADDR'], 'board' => $board['uri'], 'post' => $post, 'reason' => $reason, 'link' => link_for($post))); - if ($error) { - error($error); - } + $post = $query->fetch(PDO::FETCH_ASSOC); + + $error = event('report', array('ip' => $_SERVER['REMOTE_ADDR'], 'board' => $board['uri'], 'post' => $post, 'reason' => $reason, 'link' => link_for($post))); + if ($error) { + error($error); + } if ($config['syslog']) _syslog(LOG_INFO, 'Reported post: ' . @@ -350,10 +349,10 @@ if (isset($_POST['delete'])) { $query->bindValue(':reason', $reason, PDO::PARAM_STR); $query->execute() or error(db_error($query)); } - + $is_mod = isset($_POST['mod']) && $_POST['mod']; $root = $is_mod ? $config['root'] . $config['file_mod'] . '?/' : $config['root']; - + if (!isset($_POST['json_response'])) { $index = $root . $board['dir'] . $config['file_index']; echo Element($config['file_page_template'], array('config' => $config, 'body' => '
[ ' . _('Close window') ." ] [ " . _('Return') . ' ]
', 'title' => _('Report submitted!'))); @@ -372,21 +371,21 @@ if (isset($_POST['delete'])) { error($config['error']['noboard']); if ((!isset($_POST['mod']) || !$_POST['mod']) && $config['board_locked']) { - error("Board is locked"); + error("Board is locked"); } - + if (!isset($_POST['name'])) $_POST['name'] = $config['anonymous']; - + if (!isset($_POST['email'])) $_POST['email'] = ''; - + if (!isset($_POST['subject'])) $_POST['subject'] = ''; - + if (!isset($_POST['password'])) - $_POST['password'] = ''; - + $_POST['password'] = ''; + if (isset($_POST['thread'])) { $post['op'] = false; $post['thread'] = round($_POST['thread']); @@ -402,7 +401,7 @@ if (isset($_POST['delete'])) { // Check for CAPTCHA right after opening the board so the "return" link is in there if ($config['recaptcha']) { if (!isset($_POST['g-recaptcha-response'])) - error($config['error']['bot']); + error($config['error']['bot']); // Check what reCAPTCHA has to say... $resp = json_decode(file_get_contents(sprintf('https://www.recaptcha.net/recaptcha/api/siteverify?secret=%s&response=%s&remoteip=%s', @@ -451,22 +450,22 @@ if (isset($_POST['delete'])) { $resp = curl_exec($ch); if ($resp !== '1') { - error($config['error']['captcha'] . - ''); + error($config['error']['captcha'] . + ''); } } if (!(($post['op'] && $_POST['post'] == $config['button_newtopic']) || (!$post['op'] && $_POST['post'] == $config['button_reply']))) error($config['error']['bot']); - + // Check the referrer if ($config['referer_match'] !== false && (!isset($_SERVER['HTTP_REFERER']) || !preg_match($config['referer_match'], rawurldecode($_SERVER['HTTP_REFERER'])))) error($config['error']['referer']); - + checkDNSBL(); - + if ($post['mod'] = isset($_POST['mod']) && $_POST['mod']) { check_login(false); @@ -474,11 +473,11 @@ if (isset($_POST['delete'])) { // Liar. You're not a mod. error($config['error']['notamod']); } - + $post['sticky'] = $post['op'] && isset($_POST['sticky']); $post['locked'] = $post['op'] && isset($_POST['lock']); $post['raw'] = isset($_POST['raw']); - + if ($post['sticky'] && !hasPermission($config['mod']['sticky'], $board['uri'])) error($config['error']['noaccess']); if ($post['locked'] && !hasPermission($config['mod']['lock'], $board['uri'])) @@ -486,13 +485,13 @@ if (isset($_POST['delete'])) { if ($post['raw'] && !hasPermission($config['mod']['rawhtml'], $board['uri'])) error($config['error']['noaccess']); } - + if (!$post['mod']) { $post['antispam_hash'] = checkSpam(array($board['uri'], isset($post['thread']) ? $post['thread'] : ($config['try_smarter'] && isset($_POST['page']) ? 0 - (int)$_POST['page'] : null))); if ($post['antispam_hash'] === true) error($config['error']['spam']); } - + if ($config['robot_enable'] && $config['robot_mute']) { checkMute(); } @@ -500,13 +499,13 @@ if (isset($_POST['delete'])) { else { $mod = $post['mod'] = false; } - + //Check if thread exists if (!$post['op']) { $query = prepare(sprintf("SELECT `sticky`,`locked`,`cycle`,`sage`,`slug` FROM ``posts_%s`` WHERE `id` = :id AND `thread` IS NULL LIMIT 1", $board['uri'])); $query->bindValue(':id', $post['thread'], PDO::PARAM_INT); $query->execute() or error(db_error()); - + if (!$thread = $query->fetch(PDO::FETCH_ASSOC)) { // Non-existant error($config['error']['nonexistant']); @@ -515,8 +514,8 @@ if (isset($_POST['delete'])) { else { $thread = false; } - - + + // Check for an embed field if ($config['enable_embedding'] && isset($_POST['embed']) && !empty($_POST['embed'])) { // yep; validate it @@ -534,26 +533,26 @@ if (isset($_POST['delete'])) { error($config['error']['invalid_embed']); } } - + if (!hasPermission($config['mod']['bypass_field_disable'], $board['uri'])) { if ($config['field_disable_name']) $_POST['name'] = $config['anonymous']; // "forced anonymous" - + if ($config['field_disable_email']) $_POST['email'] = ''; - + if ($config['field_disable_password']) $_POST['password'] = ''; - + if ($config['field_disable_subject'] || (!$post['op'] && $config['field_disable_reply_subject'])) $_POST['subject'] = ''; } - + if ($config['allow_upload_by_url'] && isset($_POST['file_url']) && !empty($_POST['file_url'])) { $post['file_url'] = $_POST['file_url']; if (!preg_match('@^https?://@', $post['file_url'])) error($config['error']['invalidimg']); - + if (mb_strpos($post['file_url'], '?') !== false) $url_without_params = mb_substr($post['file_url'], 0, mb_strpos($post['file_url'], '?')); else @@ -574,9 +573,9 @@ if (isset($_POST['delete'])) { fatal_error_handler(); } register_shutdown_function('unlink_tmp_file', $post['file_tmp']); - + $fp = fopen($post['file_tmp'], 'w'); - + $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $post['file_url']); curl_setopt($curl, CURLOPT_FAILONERROR, true); @@ -587,12 +586,12 @@ if (isset($_POST['delete'])) { curl_setopt($curl, CURLOPT_BINARYTRANSFER, true); curl_setopt($curl, CURLOPT_FILE, $fp); curl_setopt($curl, CURLOPT_PROTOCOLS, CURLPROTO_HTTP | CURLPROTO_HTTPS); - + if (curl_exec($curl) === false) error($config['error']['nomove'] . '
Curl says: ' . curl_error($curl)); - + curl_close($curl); - + fclose($fp); $_FILES['file'] = array( @@ -603,14 +602,14 @@ if (isset($_POST['delete'])) { 'size' => filesize($post['file_tmp']) ); } - + $post['name'] = $_POST['name'] != '' ? $_POST['name'] : $config['anonymous']; $post['subject'] = $_POST['subject']; $post['email'] = str_replace(' ', '%20', htmlspecialchars($_POST['email'])); $post['body'] = $_POST['body']; $post['password'] = $_POST['password']; $post['has_file'] = (!isset($post['embed']) && (($post['op'] && !isset($post['no_longer_require_an_image_for_op']) && $config['force_image_op']) || count($_FILES) > 0)); - + if (!$dropped_post) { if (!($post['has_file'] || isset($post['embed'])) || (($post['op'] && $config['force_body_op']) || (!$post['op'] && $config['force_body']))) { @@ -619,28 +618,28 @@ if (isset($_POST['delete'])) { error($config['error']['tooshort_body']); } } - + if (!$post['op']) { // Check if thread is locked // but allow mods to post if ($thread['locked'] && !hasPermission($config['mod']['postinlocked'], $board['uri'])) error($config['error']['locked']); - + $numposts = numPosts($post['thread']); - + if ($config['reply_hard_limit'] != 0 && $config['reply_hard_limit'] <= $numposts['replies']) error($config['error']['reply_hard_limit']); - + if ($post['has_file'] && $config['image_hard_limit'] != 0 && $config['image_hard_limit'] <= $numposts['images']) error($config['error']['image_hard_limit']); } } else { if (!$post['op']) { - $numposts = numPosts($post['thread']); + $numposts = numPosts($post['thread']); } } - + if ($post['has_file']) { // Determine size sanity $size = 0; @@ -666,30 +665,30 @@ if (isset($_POST['delete'])) { ))); $post['filesize'] = $size; } - - + + $post['capcode'] = false; - + if ($mod && preg_match('/^((.+) )?## (.+)$/', $post['name'], $matches)) { $name = $matches[2] != '' ? $matches[2] : $config['anonymous']; $cap = $matches[3]; - + if (isset($config['mod']['capcode'][$mod['type']])) { if ( $config['mod']['capcode'][$mod['type']] === true || (is_array($config['mod']['capcode'][$mod['type']]) && in_array($cap, $config['mod']['capcode'][$mod['type']]) )) { - + $post['capcode'] = utf8tohtml($cap); $post['name'] = $name; } } } - + $trip = generate_tripcode($post['name']); $post['name'] = $trip[0]; $post['trip'] = isset($trip[1]) ? $trip[1] : ''; // XX: Dropped posts and tripcodes - + $noko = false; if (strtolower($post['email']) == 'noko') { $noko = true; @@ -698,7 +697,7 @@ if (isset($_POST['delete'])) { $noko = false; $post['email'] = ''; } else $noko = $config['always_noko']; - + if ($post['has_file']) { $i = 0; foreach ($_FILES as $key => $file) { @@ -719,7 +718,7 @@ if (isset($_POST['delete'])) { if (sizeof($_FILES) > 1) $file['file_id'] .= "-$i"; - + $file['file'] = $board['dir'] . $config['dir']['img'] . $file['file_id'] . '.' . $file['extension']; $file['thumb'] = $board['dir'] . $config['dir']['thumb'] . $file['file_id'] . '.' . ($config['thumb_ext'] ? $config['thumb_ext'] : $file['extension']); $post['files'][] = $file; @@ -748,11 +747,11 @@ if (isset($_POST['delete'])) { $post['subject'] = strip_combining_chars($post['subject']); $post['body'] = strip_combining_chars($post['body']); } - + if (!$dropped_post) { // Check string lengths if (mb_strlen($post['name']) > 35) - error(sprintf($config['error']['toolong'], 'name')); + error(sprintf($config['error']['toolong'], 'name')); if (mb_strlen($post['email']) > 40) error(sprintf($config['error']['toolong'], 'email')); if (mb_strlen($post['subject']) > 100) @@ -765,13 +764,13 @@ if (isset($_POST['delete'])) { error(sprintf($config['error']['toolong'], 'password')); } wordfilters($post['body']); - + $post['body'] = escape_markup_modifiers($post['body']); - + if ($mod && isset($post['raw']) && $post['raw']) { $post['body'] .= "\n1"; } - + if (!$dropped_post) if (($config['country_flags'] && !$config['allow_no_country']) || ($config['country_flags'] && $config['allow_no_country'] && !isset($_POST['no_country']))) { $gi=geoip_open('inc/lib/geoip/GeoIPv6.dat', GEOIP_STANDARD); @@ -787,7 +786,7 @@ if (isset($_POST['delete'])) { $part8 = base_convert(($iparr[2] * 256) + $iparr[3], 10, 16); return '::ffff:'.$part7.':'.$part8; } - + if ($country_code = geoip_country_code_by_addr_v6($gi, ipv4to6($_SERVER['REMOTE_ADDR']))) { if (!in_array(strtolower($country_code), array('eu', 'ap', 'o1', 'a1', 'a2'))) $post['body'] .= "\n".strtolower($country_code)."". @@ -795,18 +794,17 @@ if (isset($_POST['delete'])) { } } - if ($config['user_flag'] && isset($_POST['user_flag'])) - if (!empty($_POST['user_flag']) ){ - + if ($config['user_flag'] && isset($_POST['user_flag']) && !empty($_POST['user_flag'])) { $user_flag = $_POST['user_flag']; - - if (!isset($config['user_flags'][$user_flag])) + + if (!isset($config['user_flags'][$user_flag])) { error(_('Invalid flag selection!')); + } $flag_alt = isset($user_flag_alt) ? $user_flag_alt : $config['user_flags'][$user_flag]; $post['body'] .= "\n" . strtolower($user_flag) . "" . - "\n" . $flag_alt . ""; + "\n" . $flag_alt . ""; } if ($config['allowed_tags'] && $post['op'] && isset($_POST['tag']) && isset($config['allowed_tags'][$_POST['tag']])) { @@ -814,17 +812,17 @@ if (isset($_POST['delete'])) { } if (!$dropped_post) - if ($config['proxy_save'] && isset($_SERVER['HTTP_X_FORWARDED_FOR'])) { + if ($config['proxy_save'] && isset($_SERVER['HTTP_X_FORWARDED_FOR'])) { $proxy = preg_replace("/[^0-9a-fA-F.,: ]/", '', $_SERVER['HTTP_X_FORWARDED_FOR']); $post['body'] .= "\n".$proxy.""; } - + if (mysql_version() >= 50503) { $post['body_nomarkup'] = $post['body']; // Assume we're using the utf8mb4 charset } else { // MySQL's `utf8` charset only supports up to 3-byte symbols // Remove anything >= 0x010000 - + $chars = preg_split('//u', $post['body'], -1, PREG_SPLIT_NO_EMPTY); $post['body_nomarkup'] = ''; foreach ($chars as $char) { @@ -835,11 +833,11 @@ if (isset($_POST['delete'])) { $post['body_nomarkup'] .= $char; } } - + $post['tracked_cites'] = markup($post['body'], true); - - + + if ($post['has_file']) { $md5cmd = false; if ($config['bsd_md5']) $md5cmd = '/sbin/md5 -r'; @@ -854,14 +852,14 @@ if (isset($_POST['delete'])) { } elseif (!in_array($file['extension'], $config['allowed_ext']) && !in_array($file['extension'], $config['allowed_ext_files'])) error($config['error']['unknownext']); - + $file['is_an_image'] = !in_array($file['extension'], $config['allowed_ext_files']); - + // Truncate filename if it is too long $file['filename'] = mb_substr($file['filename'], 0, $config['max_filename_len']); - + $upload = $file['tmp_name']; - + if (!is_readable($upload)) error($config['error']['nomove']); @@ -903,9 +901,9 @@ if (isset($_POST['delete'])) { error($config['error']['mime_exploit']); } } - + require_once 'inc/image.php'; - + // find dimensions of an image using GD if (!$size = @getimagesize($file['tmp_name'])) { error($config['error']['invalidimg']); @@ -916,8 +914,8 @@ if (isset($_POST['delete'])) { if ($size[0] > $config['max_width'] || $size[1] > $config['max_height']) { error($config['error']['maxsize']); } - - + + if ($config['convert_auto_orient'] && ($file['extension'] == 'jpg' || $file['extension'] == 'jpeg')) { // The following code corrects the image orientation. // Currently only works with the 'convert' option selected but it could easily be expanded to work with the rest if you can be bothered. @@ -956,20 +954,20 @@ if (isset($_POST['delete'])) { } } } - + // create image object $image = new Image($file['tmp_name'], $file['extension'], $size); if ($image->size->width > $config['max_width'] || $image->size->height > $config['max_height']) { $image->delete(); error($config['error']['maxsize']); } - + $file['width'] = $image->size->width; $file['height'] = $image->size->height; - + if ($config['spoiler_images'] && isset($_POST['spoiler'])) { $file['thumb'] = 'spoiler'; - + $size = @getimagesize($config['spoiler_image']); $file['thumbwidth'] = $size[0]; $file['thumbheight'] = $size[1]; @@ -977,10 +975,10 @@ if (isset($_POST['delete'])) { $image->size->width <= $config['thumb_width'] && $image->size->height <= $config['thumb_height'] && $file['extension'] == ($config['thumb_ext'] ? $config['thumb_ext'] : $file['extension'])) { - + // Copy, because there's nothing to resize copy($file['tmp_name'], $file['thumb']); - + $file['thumbwidth'] = $image->size->width; $file['thumbheight'] = $image->size->height; } else { @@ -989,17 +987,17 @@ if (isset($_POST['delete'])) { $post['op'] ? $config['thumb_op_width'] : $config['thumb_width'], $post['op'] ? $config['thumb_op_height'] : $config['thumb_height'] ); - + $thumb->to($file['thumb']); - + $file['thumbwidth'] = $thumb->width; $file['thumbheight'] = $thumb->height; - + $thumb->_destroy(); } $dont_copy_file = false; - + if ($config['redraw_image'] || (!@$file['exif_stripped'] && $config['strip_exif'] && ($file['extension'] == 'jpg' || $file['extension'] == 'jpeg'))) { if (!$config['redraw_image'] && $config['use_exiftool']) { if($error = shell_exec_error('exiftool -overwrite_original -ignoreMinorErrors -q -q -all= ' . @@ -1038,7 +1036,7 @@ if (isset($_POST['delete'])) { // Preprocess command is an ImageMagick b/w quantization $error = shell_exec_error(sprintf($config['tesseract_preprocess_command'], escapeshellarg($fname)) . " | " . - 'tesseract stdin '.escapeshellarg($tmpname).' '.$config['tesseract_params']); + 'tesseract stdin '.escapeshellarg($tmpname).' '.$config['tesseract_params']); $tmpname .= ".txt"; $value = @file_get_contents($tmpname); @@ -1051,7 +1049,7 @@ if (isset($_POST['delete'])) { } } } - + if (!$dont_copy_file) { if (isset($file['file_tmp'])) { if (!@rename($file['tmp_name'], $file['file'])) @@ -1065,7 +1063,7 @@ if (isset($_POST['delete'])) { if ($config['image_reject_repost']) { if ($p = getPostByHash($post['filehash'])) { undoImage($post); - error(sprintf($config['error']['fileexists'], + error(sprintf($config['error']['fileexists'], ($post['mod'] ? $config['root'] . $config['file_mod'] . '?/' : $config['root']) . ($board['dir'] . $config['dir']['res'] . ($p['thread'] ? @@ -1078,7 +1076,7 @@ if (isset($_POST['delete'])) { } else if (!$post['op'] && $config['image_reject_repost_in_thread']) { if ($p = getPostByHashInThread($post['filehash'], $post['thread'])) { undoImage($post); - error(sprintf($config['error']['fileexistsinthread'], + error(sprintf($config['error']['fileexistsinthread'], ($post['mod'] ? $config['root'] . $config['file_mod'] . '?/' : $config['root']) . ($board['dir'] . $config['dir']['res'] . ($p['thread'] ? @@ -1090,7 +1088,7 @@ if (isset($_POST['delete'])) { } } } - + // Do filters again if OCRing if ($config['tesseract_ocr'] && !hasPermission($config['mod']['bypass_filters'], $board['uri']) && !$dropped_post) { do_filters($post); @@ -1104,7 +1102,7 @@ if (isset($_POST['delete'])) { error($config['error']['unoriginal']); } } - + // Remove board directories before inserting them into the database. if ($post['has_file']) { foreach ($post['files'] as $key => &$file) { @@ -1115,7 +1113,7 @@ if (isset($_POST['delete'])) { $file['thumb'] = mb_substr($file['thumb'], mb_strlen($board['dir'] . $config['dir']['thumb'])); } } - + $post = (object)$post; $post->files = array_map(function($a) { return (object)$a; }, $post->files); @@ -1131,14 +1129,14 @@ if (isset($_POST['delete'])) { if ($post['files']) $post['files'] = $post['files']; $post['num_files'] = sizeof($post['files']); - + $post['id'] = $id = post($post); $post['slug'] = slugify($post); - + if ($dropped_post && $dropped_post['from_nntp']) { - $query = prepare("INSERT INTO ``nntp_references`` (`board`, `id`, `message_id`, `message_id_digest`, `own`, `headers`) VALUES ". - "(:board , :id , :message_id , :message_id_digest , false, :headers)"); + $query = prepare("INSERT INTO ``nntp_references`` (`board`, `id`, `message_id`, `message_id_digest`, `own`, `headers`) VALUES ". + "(:board , :id , :message_id , :message_id_digest , false, :headers)"); $query->bindValue(':board', $dropped_post['board']); $query->bindValue(':id', $id); @@ -1157,15 +1155,15 @@ if (isset($_POST['delete'])) { $message = gen_nntp($headers, $files); - $query = prepare("INSERT INTO ``nntp_references`` (`board`, `id`, `message_id`, `message_id_digest`, `own`, `headers`) VALUES ". - "(:board , :id , :message_id , :message_id_digest , true , :headers)"); + $query = prepare("INSERT INTO ``nntp_references`` (`board`, `id`, `message_id`, `message_id_digest`, `own`, `headers`) VALUES ". + "(:board , :id , :message_id , :message_id_digest , true , :headers)"); $query->bindValue(':board', $post['board']); - $query->bindValue(':id', $post['id']); - $query->bindValue(':message_id', $msgid); - $query->bindValue(':message_id_digest', sha1($msgid)); - $query->bindValue(':headers', json_encode($headers)); - $query->execute() or error(db_error($query)); + $query->bindValue(':id', $post['id']); + $query->bindValue(':message_id', $msgid); + $query->bindValue(':message_id_digest', sha1($msgid)); + $query->bindValue(':headers', json_encode($headers)); + $query->execute() or error(db_error($query)); // Let's broadcast it! nntp_publish($message, $msgid); @@ -1181,11 +1179,11 @@ if (isset($_POST['delete'])) { $query->bindValue(':limit', $config['cycle_limit'], PDO::PARAM_INT); $query->execute() or error(db_error($query)); } - + if (isset($post['antispam_hash'])) { incrementSpamHash($post['antispam_hash']); } - + if (isset($post['tracked_cites']) && !empty($post['tracked_cites'])) { $insert_rows = array(); foreach ($post['tracked_cites'] as $cite) { @@ -1195,11 +1193,11 @@ if (isset($_POST['delete'])) { } query('INSERT INTO ``cites`` VALUES ' . implode(', ', $insert_rows)) or error(db_error()); } - + if (!$post['op'] && strtolower($post['email']) != 'sage' && !$thread['sage'] && ($config['reply_limit'] == 0 || $numposts['replies']+1 < $config['reply_limit'])) { bumpThread($post['thread']); } - + if (isset($_SERVER['HTTP_REFERER'])) { // Tell Javascript that we posted successfully if (isset($_COOKIE[$config['cookies']['js']])) @@ -1211,13 +1209,13 @@ if (isset($_POST['delete'])) { // Encode and set cookie setcookie($config['cookies']['js'], json_encode($js), 0, $config['cookies']['jail'] ? $config['cookies']['path'] : '/', null, false, false); } - + $root = $post['mod'] ? $config['root'] . $config['file_mod'] . '?/' : $config['root']; - + if ($noko) { $redirect = $root . $board['dir'] . $config['dir']['res'] . link_for($post, false, false, $thread) . (!$post['op'] ? '#' . $id : ''); - + if (!$post['op'] && isset($_SERVER['HTTP_REFERER'])) { $regex = array( 'board' => str_replace('%s', '(\w{1,8})', preg_quote($config['board_path'], '/')), @@ -1234,15 +1232,15 @@ if (isset($_POST['delete'])) { } } else { $redirect = $root . $board['dir'] . $config['file_index']; - + } buildThread($post['op'] ? $id : $post['thread']); - + if ($config['syslog']) _syslog(LOG_INFO, 'New post: /' . $board['dir'] . $config['dir']['res'] . link_for($post) . (!$post['op'] ? '#' . $id : '')); - + if (!$post['mod']) header('X-Associated-Content: "' . $redirect . '"'); @@ -1259,12 +1257,12 @@ if (isset($_POST['delete'])) { if ($config['try_smarter'] && $post['op']) $build_pages = range(1, $config['max_pages']); - + if ($post['op']) clean($id); - + event('post-after', $post); - + buildIndex(); // We are already done, let's continue our heavy-lifting work in the background (if we run off FastCGI) @@ -1275,13 +1273,13 @@ if (isset($_POST['delete'])) { rebuildThemes('post-thread', $board['uri']); else rebuildThemes('post', $board['uri']); - + } elseif (isset($_POST['appeal'])) { if (!isset($_POST['ban_id'])) error($config['error']['bot']); - + $ban_id = (int)$_POST['ban_id']; - + $bans = Bans::find($_SERVER['REMOTE_ADDR']); foreach ($bans as $_ban) { if ($_ban['id'] == $ban_id) { @@ -1289,33 +1287,33 @@ if (isset($_POST['delete'])) { break; } } - + if (!isset($ban)) { error(_("That ban doesn't exist or is not for you.")); } - + if ($ban['expires'] && $ban['expires'] - $ban['created'] <= $config['ban_appeals_min_length']) { error(_("You cannot appeal a ban of this length.")); } - + $query = query("SELECT `denied` FROM ``ban_appeals`` WHERE `ban_id` = $ban_id") or error(db_error()); $ban_appeals = $query->fetchAll(PDO::FETCH_COLUMN); - + if (count($ban_appeals) >= $config['ban_appeals_max']) { error(_("You cannot appeal this ban again.")); } - + foreach ($ban_appeals as $is_denied) { if (!$is_denied) error(_("There is already a pending appeal for this ban.")); } - + $query = prepare("INSERT INTO ``ban_appeals`` VALUES (NULL, :ban_id, :time, :message, 0)"); $query->bindValue(':ban_id', $ban_id, PDO::PARAM_INT); $query->bindValue(':time', time(), PDO::PARAM_INT); $query->bindValue(':message', $_POST['appeal']); $query->execute() or error(db_error($query)); - + displayBan($ban); } else { if (!file_exists($config['has_installed'])) { From d02c4eb31d41b470ec193104d86aec55e08b1883 Mon Sep 17 00:00:00 2001 From: Zankaria Date: Tue, 16 Jan 2024 18:01:13 +0000 Subject: [PATCH 2/4] Refactor post.php: split upload file hashing to a function --- post.php | 47 ++++++++++++++++++++++++++++++++++------------- 1 file changed, 34 insertions(+), 13 deletions(-) diff --git a/post.php b/post.php index ebb048fe..611e9d15 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 array $config instance configuration. + * @param string $file file to the the md5 of. + * @return string|false + */ +function md5_hash_of_file($config, $path) { + $cmd = false; + if ($config['bsd_md5']) { + $cmd = '/sbin/md5 -r'; + } + if ($config['gnu_md5']) { + $cmd = 'md5sum'; + } + + if ($cmd) { + $output = shell_exec_error($cmd . " " . escapeshellarg($path)); + $output = explode(' ', $output); + return $output[0]; + } else { + return md5_file($path); + } +} + +/** + * Method handling functions + */ + $dropped_post = false; // Is it a post coming from NNTP? Let's extract it and pretend it's a normal post. @@ -837,12 +870,7 @@ if (isset($_POST['delete'])) { $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) { @@ -863,14 +891,7 @@ if (isset($_POST['delete'])) { if (!is_readable($upload)) 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; From c43229af8ed637e64f68ade86de90da23e98ccd9 Mon Sep 17 00:00:00 2001 From: Zankaria Date: Tue, 30 Jan 2024 14:26:19 +0100 Subject: [PATCH 3/4] Refactor post.php: extract symbol post body stripping --- post.php | 44 ++++++++++++++++++++++++++++---------------- 1 file changed, 28 insertions(+), 16 deletions(-) diff --git a/post.php b/post.php index 611e9d15..0265fb29 100644 --- a/post.php +++ b/post.php @@ -34,6 +34,33 @@ function md5_hash_of_file($config, $path) { } } +/** + * Strip the symbols incompatible with the current database version. + * + * @param string @input The input string. + * @return string The value stripped of incompatible symbols. + */ +function strip_symbols($input) { + if (mysql_version() >= 50503) { + return $input; // Assume we're using the utf8mb4 charset + } else { + // MySQL's `utf8` charset only supports up to 3-byte symbols + // Remove anything >= 0x010000 + + $chars = preg_split('//u', $input, -1, PREG_SPLIT_NO_EMPTY); + $ret = ''; + foreach ($chars as $char) { + $o = 0; + $ord = ordutf8($char, $o); + if ($ord >= 0x010000) { + continue; + } + $ret .= $char; + } + return $ret; + } +} + /** * Method handling functions */ @@ -850,22 +877,7 @@ if (isset($_POST['delete'])) { $post['body'] .= "\n".$proxy.""; } - if (mysql_version() >= 50503) { - $post['body_nomarkup'] = $post['body']; // Assume we're using the utf8mb4 charset - } else { - // MySQL's `utf8` charset only supports up to 3-byte symbols - // Remove anything >= 0x010000 - - $chars = preg_split('//u', $post['body'], -1, PREG_SPLIT_NO_EMPTY); - $post['body_nomarkup'] = ''; - foreach ($chars as $char) { - $o = 0; - $ord = ordutf8($char, $o); - if ($ord >= 0x010000) - continue; - $post['body_nomarkup'] .= $char; - } - } + $post['body_nomarkup'] = strip_symbols($post['body']); $post['tracked_cites'] = markup($post['body'], true); From d3cd289511c0d4e9cd6933659d2e879ff7e461c4 Mon Sep 17 00:00:00 2001 From: Zankaria Date: Mon, 29 Jan 2024 14:53:18 +0100 Subject: [PATCH 4/4] Refactor post.php: break up file upload --- post.php | 136 ++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 89 insertions(+), 47 deletions(-) diff --git a/post.php b/post.php index 0265fb29..bd1591d9 100644 --- a/post.php +++ b/post.php @@ -61,6 +61,88 @@ function strip_symbols($input) { } } +/** + * Download the url's target with curl. + * + * @param string $url Url to the file to download. + * @param int $timeout Request timeout in seconds. + * @param File $fd File descriptor to save the content to. + * @return null|string Returns a string on error. + */ +function download_file_into($url, $timeout, $fd) { + $err = null; + $curl = curl_init(); + curl_setopt($curl, CURLOPT_URL, $url); + curl_setopt($curl, CURLOPT_FAILONERROR, true); + curl_setopt($curl, CURLOPT_FOLLOWLOCATION, false); + curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 5); + curl_setopt($curl, CURLOPT_TIMEOUT, $timeout); + curl_setopt($curl, CURLOPT_USERAGENT, 'Tinyboard'); + curl_setopt($curl, CURLOPT_FILE, $fd); + curl_setopt($curl, CURLOPT_PROTOCOLS, CURLPROTO_HTTP | CURLPROTO_HTTPS); + curl_setopt($curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4); + + if (curl_exec($curl) === false) { + $err = curl_error($curl); + } + + curl_close($curl); + return $err; +} + +/** + * Download a remote file from the given url. + * The file is deleted at shutdown. + * + * @param string $file_url The url to download the file from. + * @param int $request_timeout Timeout to retrieve the file. + * @param array $extra_extensions Allowed file extensions. + * @param string $tmp_dir Temporary directory to save the file into. + * @param array $error_array An array with error codes, used to create exceptions on failure. + * @return array Returns an array describing the file on success. + * @throws Exception on error. + */ +function download_file_from_url($file_url, $request_timeout, $allowed_extensions, $tmp_dir, &$error_array) { + if (!preg_match('@^https?://@', $file_url)) { + throw new InvalidArgumentException($error_array['invalidimg']); + } + + if (mb_strpos($file_url, '?') !== false) { + $url_without_params = mb_substr($file_url, 0, mb_strpos($file_url, '?')); + } else { + $url_without_params = $file_url; + } + + $extension = strtolower(mb_substr($url_without_params, mb_strrpos($url_without_params, '.') + 1)); + + if (!in_array($extension, $allowed_extensions)) { + throw new InvalidArgumentException($error_array['unknownext']); + } + + $tmp_file = tempnam($tmp_dir, 'url'); + function unlink_tmp_file($file) { + @unlink($file); + fatal_error_handler(); + } + register_shutdown_function('unlink_tmp_file', $tmp_file); + + $fd = fopen($tmp_file, 'w'); + + $dl_err = download_file_into($tmp_file, $request_timeout, $fd); + fclose($fd); + if ($dl_err !== null) { + throw new Exception($error_array['nomove'] . '
Curl says: ' . $dl_err); + } + + return array( + 'name' => basename($url_without_params), + 'tmp_name' => $tmp_file, + 'file_tmp' => true, + 'error' => 0, + 'size' => filesize($tmp_file) + ); +} + /** * Method handling functions */ @@ -609,58 +691,18 @@ if (isset($_POST['delete'])) { } if ($config['allow_upload_by_url'] && isset($_POST['file_url']) && !empty($_POST['file_url'])) { - $post['file_url'] = $_POST['file_url']; - if (!preg_match('@^https?://@', $post['file_url'])) - error($config['error']['invalidimg']); - - if (mb_strpos($post['file_url'], '?') !== false) - $url_without_params = mb_substr($post['file_url'], 0, mb_strpos($post['file_url'], '?')); - else - $url_without_params = $post['file_url']; - - $post['extension'] = strtolower(mb_substr($url_without_params, mb_strrpos($url_without_params, '.') + 1)); + $allowed_extensions = $config['allowed_ext_files']; + // Add allowed extensions for OP, if enabled. if ($post['op'] && $config['allowed_ext_op']) { - if (!in_array($post['extension'], $config['allowed_ext_op'])) - error($config['error']['unknownext']); + array_merge($allowed_extensions, $config['allowed_ext_op']); } - else if (!in_array($post['extension'], $config['allowed_ext']) && !in_array($post['extension'], $config['allowed_ext_files'])) - error($config['error']['unknownext']); - $post['file_tmp'] = tempnam($config['tmp'], 'url'); - function unlink_tmp_file($file) { - @unlink($file); - fatal_error_handler(); + try { + $_FILES['file'] = download_file_from_url($_POST['file_url'], $config['upload_by_url_timeout'], $allowed_extensions, $config['tmp'], $config['error']); + } catch (Exception $e) { + error($e->getMessage()); } - register_shutdown_function('unlink_tmp_file', $post['file_tmp']); - - $fp = fopen($post['file_tmp'], 'w'); - - $curl = curl_init(); - curl_setopt($curl, CURLOPT_URL, $post['file_url']); - curl_setopt($curl, CURLOPT_FAILONERROR, true); - curl_setopt($curl, CURLOPT_FOLLOWLOCATION, false); - curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 5); - curl_setopt($curl, CURLOPT_TIMEOUT, $config['upload_by_url_timeout']); - curl_setopt($curl, CURLOPT_USERAGENT, 'Tinyboard'); - curl_setopt($curl, CURLOPT_BINARYTRANSFER, true); - curl_setopt($curl, CURLOPT_FILE, $fp); - curl_setopt($curl, CURLOPT_PROTOCOLS, CURLPROTO_HTTP | CURLPROTO_HTTPS); - - if (curl_exec($curl) === false) - error($config['error']['nomove'] . '
Curl says: ' . curl_error($curl)); - - curl_close($curl); - - fclose($fp); - - $_FILES['file'] = array( - 'name' => basename($url_without_params), - 'tmp_name' => $post['file_tmp'], - 'file_tmp' => true, - 'error' => 0, - 'size' => filesize($post['file_tmp']) - ); } $post['name'] = $_POST['name'] != '' ? $_POST['name'] : $config['anonymous'];