mirror of
https://github.com/mashirozx/sakura.git
synced 2024-11-26 08:48:13 +08:00
change curl to wp_remote_post
This commit is contained in:
parent
db7bb49397
commit
19779aa407
273
inc/api.php
273
inc/api.php
@ -4,84 +4,77 @@
|
|||||||
* Router
|
* Router
|
||||||
*/
|
*/
|
||||||
add_action('rest_api_init', function () {
|
add_action('rest_api_init', function () {
|
||||||
register_rest_route('sakura/v1', '/image/upload', array(
|
register_rest_route('sakura/v1', '/image/upload', array(
|
||||||
'methods' => 'POST',
|
'methods' => 'POST',
|
||||||
'callback' => 'upload_image',
|
'callback' => 'upload_image',
|
||||||
));
|
));
|
||||||
register_rest_route('sakura/v1', '/cache_search/json', array(
|
register_rest_route('sakura/v1', '/cache_search/json', array(
|
||||||
'methods' => 'GET',
|
'methods' => 'GET',
|
||||||
'callback' => 'cache_search_json',
|
'callback' => 'cache_search_json',
|
||||||
));
|
));
|
||||||
});
|
});
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Image uploader response
|
* Image uploader response
|
||||||
*/
|
*/
|
||||||
function upload_image(WP_REST_Request $request)
|
function upload_image(WP_REST_Request $request) {
|
||||||
{
|
// see: https://developer.wordpress.org/rest-api/requests/
|
||||||
// see: https://developer.wordpress.org/rest-api/requests/
|
|
||||||
|
|
||||||
// handle file params $file === $_FILES
|
// handle file params $file === $_FILES
|
||||||
/**
|
/**
|
||||||
* curl \
|
* curl \
|
||||||
* -F "filecomment=This is an img file" \
|
* -F "filecomment=This is an img file" \
|
||||||
* -F "cmt_img_file=@screenshot.jpg" \
|
* -F "cmt_img_file=@screenshot.jpg" \
|
||||||
* https://dev.2heng.xin/wp-json/sakura/v1/image/upload
|
* https://dev.2heng.xin/wp-json/sakura/v1/image/upload
|
||||||
*/
|
*/
|
||||||
// $file = $request->get_file_params();
|
// $file = $request->get_file_params();
|
||||||
if ( !check_ajax_referer('wp_rest', '_wpnonce', false) ) {
|
if (!check_ajax_referer('wp_rest', '_wpnonce', false)) {
|
||||||
$output = array(
|
$output = array(
|
||||||
'status' => 403,
|
'status' => 403,
|
||||||
'success' => false,
|
'success' => false,
|
||||||
'message' => 'Unauthorized client.',
|
'message' => 'Unauthorized client.',
|
||||||
'link' => "https://view.moezx.cc/images/2019/11/14/step04.md.png",
|
'link' => "https://view.moezx.cc/images/2019/11/14/step04.md.png",
|
||||||
'proxy' => akina_option('cmt_image_proxy') . "https://view.moezx.cc/images/2019/11/14/step04.md.png",
|
'proxy' => akina_option('cmt_image_proxy') . "https://view.moezx.cc/images/2019/11/14/step04.md.png",
|
||||||
);
|
);
|
||||||
$result = new WP_REST_Response($output, 403);
|
$result = new WP_REST_Response($output, 403);
|
||||||
|
$result->set_headers(array('Content-Type' => 'application/json'));
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
|
|
||||||
|
switch (akina_option("img_upload_api")) {
|
||||||
|
case 'imgur':
|
||||||
|
$image = file_get_contents($_FILES["cmt_img_file"]["tmp_name"]);
|
||||||
|
$API_Request = Imgur_API($image);
|
||||||
|
break;
|
||||||
|
case 'smms':
|
||||||
|
$image = $_FILES;
|
||||||
|
$API_Request = SMMS_API($image);
|
||||||
|
break;
|
||||||
|
case 'chevereto':
|
||||||
|
$image = file_get_contents($_FILES["cmt_img_file"]["tmp_name"]);
|
||||||
|
$API_Request = Chevereto_API($image);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
$result = new WP_REST_Response($API_Request, $API_Request->status);
|
||||||
$result->set_headers(array('Content-Type' => 'application/json'));
|
$result->set_headers(array('Content-Type' => 'application/json'));
|
||||||
return $result;
|
return $result;
|
||||||
}
|
|
||||||
|
|
||||||
switch (akina_option("img_upload_api")) {
|
|
||||||
case 'imgur':
|
|
||||||
$image = file_get_contents($_FILES["cmt_img_file"]["tmp_name"]);
|
|
||||||
$API_Request = Imgur_API($image);
|
|
||||||
break;
|
|
||||||
case 'smms':
|
|
||||||
$image = $_FILES;
|
|
||||||
$API_Request = SMMS_API($image);
|
|
||||||
break;
|
|
||||||
case 'chevereto':
|
|
||||||
$image = file_get_contents($_FILES["cmt_img_file"]["tmp_name"]);
|
|
||||||
$API_Request = Chevereto_API($image);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
$result = new WP_REST_Response($API_Request, $API_Request->status);
|
|
||||||
$result->set_headers(array('Content-Type' => 'application/json'));
|
|
||||||
return $result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Chevereto upload interface
|
* Chevereto upload interface
|
||||||
*/
|
*/
|
||||||
function Chevereto_API($image)
|
function Chevereto_API($image) {
|
||||||
{
|
$upload_url = akina_option('cheverto_url').'/api/1/upload';
|
||||||
$fields = array(
|
$args = array(
|
||||||
'source' => base64_encode($image),
|
'body' => array(
|
||||||
'key' => akina_option('chevereto_api_key')
|
'source' => base64_encode($image),
|
||||||
|
'key' => akina_option('chevereto_api_key')
|
||||||
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
$ch = curl_init();
|
$response = wp_remote_post($upload_url, $args);
|
||||||
curl_setopt($ch, CURLOPT_URL, akina_option('cheverto_url').'/api/1/upload');
|
$reply = json_decode($response["body"]);
|
||||||
curl_setopt($ch, CURLOPT_POST, true);
|
|
||||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
||||||
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
|
|
||||||
|
|
||||||
$reply = curl_exec($ch);
|
|
||||||
curl_close($ch);
|
|
||||||
|
|
||||||
$reply = json_decode($reply);
|
|
||||||
|
|
||||||
if ($reply->status_txt == 'OK' && $reply->status_code == 200) {
|
if ($reply->status_txt == 'OK' && $reply->status_code == 200) {
|
||||||
$status = 200;
|
$status = 200;
|
||||||
@ -109,21 +102,20 @@ function Chevereto_API($image)
|
|||||||
/**
|
/**
|
||||||
* Imgur upload interface
|
* Imgur upload interface
|
||||||
*/
|
*/
|
||||||
function Imgur_API($image)
|
function Imgur_API($image) {
|
||||||
{
|
|
||||||
$client_id = akina_option('imgur_client_id');
|
$client_id = akina_option('imgur_client_id');
|
||||||
|
$upload_url = akina_option('imgur_upload_image_proxy');
|
||||||
|
$args = array(
|
||||||
|
'headers' => array(
|
||||||
|
'Authorization' => 'Client-ID ' . $client_id
|
||||||
|
),
|
||||||
|
'body' => array(
|
||||||
|
'image' => base64_encode($image)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
$ch = curl_init();
|
$response = wp_remote_post($upload_url, $args);
|
||||||
curl_setopt($ch, CURLOPT_URL, akina_option('imgur_upload_image_proxy'));
|
$reply = json_decode($response["body"]);
|
||||||
curl_setopt($ch, CURLOPT_POST, true);
|
|
||||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
||||||
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Client-ID ' . $client_id));
|
|
||||||
curl_setopt($ch, CURLOPT_POSTFIELDS, array('image' => base64_encode($image)));
|
|
||||||
|
|
||||||
$reply = curl_exec($ch);
|
|
||||||
curl_close($ch);
|
|
||||||
|
|
||||||
$reply = json_decode($reply);
|
|
||||||
|
|
||||||
if ($reply->success && $reply->status == 200) {
|
if ($reply->success && $reply->status == 200) {
|
||||||
$status = 200;
|
$status = 200;
|
||||||
@ -151,66 +143,81 @@ function Imgur_API($image)
|
|||||||
/**
|
/**
|
||||||
* smms upload interface
|
* smms upload interface
|
||||||
*/
|
*/
|
||||||
function SMMS_API($image)
|
function SMMS_API($image) {
|
||||||
{
|
$client_id = akina_option('smms_client_id');
|
||||||
$client_id = akina_option('smms_client_id');
|
$upload_url = "https://sm.ms/api/v2/upload";
|
||||||
|
$filename = $image['cmt_img_file']['name'];
|
||||||
|
$filedata = $image['cmt_img_file']['tmp_name'];
|
||||||
|
$Boundary = wp_generate_password();
|
||||||
|
$bits = file_get_contents($filedata);
|
||||||
|
|
||||||
$filename = $image['cmt_img_file']['name'];
|
$headers = array();
|
||||||
$filedata = $image['cmt_img_file']['tmp_name'];
|
array_push($headers, "Content-Type: multipart/form-data; boundary=$Boundary");
|
||||||
$filesize = $image['cmt_img_file']['size'];
|
array_push($headers, "Authorization: Basic " . $client_id);
|
||||||
|
array_push($headers, "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.97");
|
||||||
|
|
||||||
$url = "https://sm.ms/api/v2/upload";
|
$fields = array();
|
||||||
$headers = array();
|
array_push($fields, "--" . $Boundary);
|
||||||
array_push($headers, "Content-Type: multipart/form-data");
|
array_push($fields, "Content-Disposition: form-data; name=\"smfile\"; filename=\"$filename\"");
|
||||||
array_push($headers, "Authorization: Basic " . $client_id);
|
array_push($fields, '');
|
||||||
array_push($headers, "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.97");
|
array_push($fields, $bits);
|
||||||
|
array_push($fields, '');
|
||||||
|
array_push($fields, "--" . $Boundary . "--");
|
||||||
|
$fields = implode("\r\n", $fields);
|
||||||
|
|
||||||
$finfo = new \finfo(FILEINFO_MIME_TYPE);
|
$args = array(
|
||||||
$mimetype = $finfo->file($filedata);
|
'headers' => $headers,
|
||||||
|
'body' => $fields
|
||||||
|
);
|
||||||
|
|
||||||
$fields = array('smfile' => curl_file_create($filedata, $mimetype, $filename));
|
$response = wp_remote_post($upload_url, $args);
|
||||||
|
$reply = json_decode($response["body"]);
|
||||||
|
/**
|
||||||
|
* php curl
|
||||||
|
*
|
||||||
|
$ch = curl_init();
|
||||||
|
curl_setopt($ch, CURLOPT_URL, $url);
|
||||||
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||||
|
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
|
||||||
|
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
|
||||||
|
curl_setopt($ch, CURLOPT_POST, true);
|
||||||
|
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
|
||||||
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
|
||||||
|
|
||||||
$ch = curl_init();
|
$reply = curl_exec($ch);
|
||||||
curl_setopt($ch, CURLOPT_URL, $url);
|
curl_close($ch);
|
||||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
||||||
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
|
|
||||||
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
|
|
||||||
curl_setopt($ch, CURLOPT_POST, true);
|
|
||||||
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
|
|
||||||
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
|
|
||||||
|
|
||||||
$reply = curl_exec($ch);
|
$reply = json_decode($reply);
|
||||||
curl_close($ch);
|
*/
|
||||||
|
|
||||||
$reply = json_decode($reply);
|
if ($reply->success && $reply->code == 'success') {
|
||||||
|
$status = 200;
|
||||||
if ($reply->success && $reply->code == 'success') {
|
$success = true;
|
||||||
$status = 200;
|
$message = $reply->message;
|
||||||
$success = true;
|
$link = $reply->data->url;
|
||||||
$message = $reply->message;
|
$proxy = akina_option('cmt_image_proxy') . $link;
|
||||||
$link = $reply->data->url;
|
} else if (preg_match("/Image upload repeated limit/i", $reply->message, $matches)) {
|
||||||
$proxy = akina_option('cmt_image_proxy') . $link;
|
$status = 200;
|
||||||
} else if (preg_match("/Image upload repeated limit/i", $reply->message, $matches)) {
|
// sm.ms 接口不规范,建议检测到重复的情况下返回标准化的 code,并单独把 url 放进一个字段
|
||||||
$status = 200; // sm.ms 接口不规范,建议检测到重复的情况下返回标准化的 code,并单独把 url 放进一个字段
|
$success = true;
|
||||||
$success = true;
|
$message = $reply->message;
|
||||||
$message = $reply->message;
|
$link = str_replace('Image upload repeated limit, this image exists at: ', '', $reply->message);
|
||||||
$link = str_replace('Image upload repeated limit, this image exists at: ', '', $reply->message);
|
$proxy = akina_option('cmt_image_proxy') . $link;
|
||||||
$proxy = akina_option('cmt_image_proxy') . $link;
|
} else {
|
||||||
} else {
|
$status = 400;
|
||||||
$status = 400;
|
$success = false;
|
||||||
$success = false;
|
$message = $reply->message;
|
||||||
$message = $reply->message;
|
$link = 'https://view.moezx.cc/images/2019/10/28/default_d_h_large.gif';
|
||||||
$link = 'https://view.moezx.cc/images/2019/10/28/default_d_h_large.gif';
|
$proxy = akina_option('cmt_image_proxy') . $link;
|
||||||
$proxy = akina_option('cmt_image_proxy') . $link;
|
}
|
||||||
}
|
$output = array(
|
||||||
$output = array(
|
'status' => $status,
|
||||||
'status' => $status,
|
'success' => $success,
|
||||||
'success' => $success,
|
'message' => $message,
|
||||||
'message' => $message,
|
'link' => $link,
|
||||||
'link' => $link,
|
'proxy' => $proxy,
|
||||||
'proxy' => $proxy,
|
);
|
||||||
);
|
return $output;
|
||||||
return $output;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -218,8 +225,7 @@ function SMMS_API($image)
|
|||||||
* @rest api接口路径:https://sakura.2heng.xin/wp-json/sakura/v1/cache_search/json
|
* @rest api接口路径:https://sakura.2heng.xin/wp-json/sakura/v1/cache_search/json
|
||||||
* @可在cache_search_json()函数末尾通过设置 HTTP header 控制 json 缓存时间
|
* @可在cache_search_json()函数末尾通过设置 HTTP header 控制 json 缓存时间
|
||||||
*/
|
*/
|
||||||
function cache_search_json()
|
function cache_search_json() {
|
||||||
{
|
|
||||||
$vowels = array("[", "{", "]", "}", "<", ">", "\r\n", "\r", "\n", "-", "'", '"', '`', " ", ":", ";", '\\', " ", "toc");
|
$vowels = array("[", "{", "]", "}", "<", ">", "\r\n", "\r", "\n", "-", "'", '"', '`', " ", ":", ";", '\\', " ", "toc");
|
||||||
$regex = <<<EOS
|
$regex = <<<EOS
|
||||||
/<\/?[a-zA-Z]+("[^"]*"|'[^']*'|[^'">])*>|begin[\S\s]*\/begin|hermit[\S\s]*\/hermit|img[\S\s]*\/img|{{.*?}}|:.*?:/m
|
/<\/?[a-zA-Z]+("[^"]*"|'[^']*'|[^'">])*>|begin[\S\s]*\/begin|hermit[\S\s]*\/hermit|img[\S\s]*\/img|{{.*?}}|:.*?:/m
|
||||||
@ -227,7 +233,7 @@ EOS;
|
|||||||
|
|
||||||
$posts = new WP_Query('posts_per_page=-1&post_status=publish&post_type=post');
|
$posts = new WP_Query('posts_per_page=-1&post_status=publish&post_type=post');
|
||||||
while ($posts->have_posts()): $posts->the_post();
|
while ($posts->have_posts()): $posts->the_post();
|
||||||
$output .= '{"type":"post","link":"' . get_post_permalink() . '","title":' . json_encode(get_the_title()) . ',"comments":"' . get_comments_number('0', '1', '%') . '","text":' . json_encode(str_replace($vowels, " ", preg_replace($regex, ' ', get_the_content()))) . '},';
|
$output .= '{"type":"post","link":"' . get_post_permalink() . '","title":' . json_encode(get_the_title()) . ',"comments":"' . get_comments_number('0', '1', '%') . '","text":' . json_encode(str_replace($vowels, " ", preg_replace($regex, ' ', get_the_content()))) . '},';
|
||||||
endwhile;
|
endwhile;
|
||||||
wp_reset_postdata();
|
wp_reset_postdata();
|
||||||
|
|
||||||
@ -263,7 +269,8 @@ EOS;
|
|||||||
$data = '[' . $output . ']';
|
$data = '[' . $output . ']';
|
||||||
$result = new WP_REST_Response(json_decode($data), 200);
|
$result = new WP_REST_Response(json_decode($data), 200);
|
||||||
$result->set_headers(array('Content-Type' => 'application/json',
|
$result->set_headers(array('Content-Type' => 'application/json',
|
||||||
'Cache-Control' => 'max-age=3600')); // json 缓存控制
|
'Cache-Control' => 'max-age=3600'));
|
||||||
|
// json 缓存控制
|
||||||
|
|
||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user