diff --git a/post.php b/post.php index f0297c28..c47bf8cd 100644 --- a/post.php +++ b/post.php @@ -12,8 +12,8 @@ require_once 'inc/bootstrap.php'; /** * Get the md5 hash of the file. * - * @param [type] $config instance configuration. - * @param [type] $file file to the the md5 of. + * @param [type] $config Instance configuration. + * @param [type] $file File to do the md5 of. * @return string|false */ function md5_hash_of_file($config, $file) @@ -38,16 +38,17 @@ function md5_hash_of_file($config, $file) /** * Strip the markup from the given string * - * @param string $post_body the body of the post. + * @param string $post_body The body of the post. * @return string */ function strip_markup($post_body) { if (mysql_version() >= 50503) { - return $post_body; // Assume we're using the utf8mb4 charset + // Assume we're using the utf8mb4 charset. + return $post_body; } else { - // MySQL's `utf8` charset only supports up to 3-byte symbols - // Remove anything >= 0x010000 + // 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); $res = ''; @@ -63,6 +64,36 @@ function strip_markup($post_body) } } +/** + * Download the url's target with curl. + * + * @param [type] $url + * @param [type] $timeout + * @param [type] $fd + * @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; +} + /** * Method handling functions */ @@ -740,27 +771,13 @@ function handle_post() $post['file_tmp'] = tempnam($config['tmp'], 'url'); 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); - curl_setopt($curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4); - - if (curl_exec($curl) === false) { - error($config['error']['nomove'] . '
Curl says: ' . curl_error($curl)); - } - - curl_close($curl); + $fd = fopen($post['file_tmp'], 'w'); - fclose($fp); + $dl_err = download_file_into($post['file_url'], $config['upload_by_url_timeout'], $fd); + fclose($fd); + if ($dl_err !== null) { + error($config['error']['nomove'] . '
Curl says: ' . $dl_err); + } $_FILES[$post['file_tmp']] = array( 'name' => basename($url_without_params), @@ -893,7 +910,6 @@ function handle_post() in_array($cap, $config['mod']['capcode'][$mod['type']]) ) ) { - $post['capcode'] = utf8tohtml($cap); $post['name'] = $name; }