From 587d4fe6756187ecb567cbcd0d5f905dd64f8e93 Mon Sep 17 00:00:00 2001 From: Abe Tomoaki Date: Wed, 18 Sep 2024 07:35:18 +0900 Subject: [PATCH] Support requests in arrays --- no.php | 62 ++++++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 45 insertions(+), 17 deletions(-) diff --git a/no.php b/no.php index 4417084..bb8e71d 100644 --- a/no.php +++ b/no.php @@ -85,31 +85,59 @@ function build_domain_regex($hostname) $regex .= $main_domain; return $regex; } - -function build_multipart_data_files($delimiter, $fields, $files) { - # Inspiration from: https://gist.github.com/maxivak/18fcac476a2f4ea02e5f80b303811d5f :) - $data = ''; + +function build_multipart_data_fields($delimiter, $key, $value) { $eol = "\r\n"; - - foreach ($fields as $name => $content) { - $data .= "--" . $delimiter . $eol - . 'Content-Disposition: form-data; name="' . $name . "\"".$eol.$eol - . $content . $eol; + if (!is_array($value)) { + return "--" . $delimiter . $eol + . 'Content-Disposition: form-data; name="' . $key . '"' . $eol . $eol + . $value . $eol; } - - foreach ($files as $name => $content) { - $data .= "--" . $delimiter . $eol + + $data = ''; + foreach ($value as $k => $v) { + $data .= build_multipart_data_fields( + $delimiter, + sprintf('%s[%s]', $key, is_numeric($k) ? "" : $k), + $v + ); + + } + return $data; +} + +function build_multipart_data_files($delimiter, $fields, $files) { + $eol = "\r\n"; + if (!is_array($value)) { + return "--" . $delimiter . $eol . 'Content-Disposition: form-data; name="' . $name . '"; filename="' . $name . '"' . $eol . 'Content-Transfer-Encoding: binary'.$eol - ; - $data .= $eol; - $data .= $content . $eol; + . $eol . $value . $eol; } - $data .= "--" . $delimiter . "--".$eol; + $data = ''; + foreach ($value as $k => $v) { + $data .= build_multipart_data_files( + $delimiter, + sprintf('%s[%s]', $key, is_numeric($k) ? "" : $k), + $v + ); + } return $data; } +function build_multipart_data($delimiter, $fields, $files) { + # Inspiration from: https://gist.github.com/maxivak/18fcac476a2f4ea02e5f80b303811d5f :) + $data = ''; + foreach ($fields as $name => $content) { + $data .= build_multipart_data_fields($delimiter, $name, $content); + } + foreach ($files as $name => $content) { + $data .= build_multipart_data_files($delimiter, $name, $content); + } + return $data . "--" . $delimiter . "--" . $eol; +} + $curl = curl_init( $url ); curl_setopt( $curl, CURLOPT_HTTPHEADER, getRequestHeaders() ); curl_setopt( $curl, CURLOPT_FOLLOWLOCATION, true ); # follow redirects @@ -122,7 +150,7 @@ function build_multipart_data_files($delimiter, $fields, $files) { if (preg_match("/^multipart/", strtolower($_SERVER['CONTENT_TYPE']))) { $delimiter = '-------------' . uniqid(); - $post_data = build_multipart_data_files($delimiter, $_POST, $_FILES); + $post_data = build_multipart_data($delimiter, $_POST, $_FILES); curl_setopt( $curl, CURLOPT_HTTPHEADER, getRequestHeaders($delimiter) ); }