Merge pull request #142 from spirit1431007/pr-branch

change curl to wp_remote_post
pull/144/head
Mashiro 2019-11-26 18:57:50 +08:00 committed by GitHub
commit 8ffa252dcd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 131 additions and 137 deletions

View File

@ -79,7 +79,6 @@ window.dataLayer=window.dataLayer||[];function gtag(){dataLayer.push(arguments)}
</head>
<body <?php body_class(); ?>>
<?php if(get_template_directory_uri() != get_site_url().'/wp-content/themes/Sakura') echo '<div style="position:fixed;height:100%;width:100%;top:0;left:0;font-size:20px;z-index:999999;background-color: #fff;">Plz rename the theme folder name as <span style="color:red">Sakura</span>!<br>请将主题文件夹名改为 <span style="color:red">Sakura</span></div>'; ?>
<?php if(!function_exists('curl_exec')) echo '<div style="position:fixed;height:100%;width:100%;top:0;left:0;font-size:20px;z-index:999999;background-color: #fff;">主题需要 PHP 的 curl 支持!请在 `php.ini` 里开启或者联系你的主机商。<br>The theme requires PHP\'s curl support! Please turn on in `php.ini` or contact your hosting provider.</div>'; ?>
<div class="scrollbar" id="bar"></div>
<section id="main-container">
<?php

View File

@ -17,8 +17,7 @@ add_action('rest_api_init', function () {
/**
* 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/
// handle file params $file === $_FILES
@ -65,23 +64,17 @@ function upload_image(WP_REST_Request $request)
/**
* Chevereto upload interface
*/
function Chevereto_API($image)
{
$fields = array(
function Chevereto_API($image) {
$upload_url = akina_option('cheverto_url').'/api/1/upload';
$args = array(
'body' => array(
'source' => base64_encode($image),
'key' => akina_option('chevereto_api_key')
)
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, akina_option('cheverto_url').'/api/1/upload');
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);
$response = wp_remote_post($upload_url, $args);
$reply = json_decode($response["body"]);
if ($reply->status_txt == 'OK' && $reply->status_code == 200) {
$status = 200;
@ -109,21 +102,20 @@ function Chevereto_API($image)
/**
* Imgur upload interface
*/
function Imgur_API($image)
{
function Imgur_API($image) {
$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();
curl_setopt($ch, CURLOPT_URL, akina_option('imgur_upload_image_proxy'));
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);
$response = wp_remote_post($upload_url, $args);
$reply = json_decode($response["body"]);
if ($reply->success && $reply->status == 200) {
$status = 200;
@ -151,38 +143,38 @@ function Imgur_API($image)
/**
* smms upload interface
*/
function SMMS_API($image)
{
function SMMS_API($image) {
$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'];
$filesize = $image['cmt_img_file']['size'];
$Boundary = wp_generate_password();
$bits = file_get_contents($filedata);
$url = "https://sm.ms/api/v2/upload";
$headers = array();
array_push($headers, "Content-Type: multipart/form-data");
array_push($headers, "Content-Type: multipart/form-data; boundary=$Boundary");
array_push($headers, '');
array_push($headers, "Authorization: Basic " . $client_id);
array_push($headers, '');
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");
$headers = implode("\r\n", $headers);
$finfo = new \finfo(FILEINFO_MIME_TYPE);
$mimetype = $finfo->file($filedata);
$fields = array();
array_push($fields, "--" . $Boundary);
array_push($fields, "Content-Disposition: form-data; name=\"smfile\"; filename=\"$filename\"");
array_push($fields, '');
array_push($fields, $bits);
array_push($fields, '');
array_push($fields, "--" . $Boundary . "--");
$fields = implode("\r\n", $fields);
$fields = array('smfile' => curl_file_create($filedata, $mimetype, $filename));
$args = array(
'headers' => $headers,
'body' => $fields
);
$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);
$reply = curl_exec($ch);
curl_close($ch);
$reply = json_decode($reply);
$response = wp_remote_post($upload_url, $args);
$reply = json_decode($response["body"]);
if ($reply->success && $reply->code == 'success') {
$status = 200;
@ -218,8 +210,7 @@ function SMMS_API($image)
* @rest api接口路径https://sakura.2heng.xin/wp-json/sakura/v1/cache_search/json
* @可在cache_search_json()函数末尾通过设置 HTTP header 控制 json 缓存时间
*/
function cache_search_json()
{
function cache_search_json() {
$vowels = array("[", "{", "]", "}", "<", ">", "\r\n", "\r", "\n", "-", "'", '"', '`', " ", ":", ";", '\\', " ", "toc");
$regex = <<<EOS
/<\/?[a-zA-Z]+("[^"]*"|'[^']*'|[^'">])*>|begin[\S\s]*\/begin|hermit[\S\s]*\/hermit|img[\S\s]*\/img|{{.*?}}|:.*?:/m
@ -262,8 +253,12 @@ EOS;
$data = '[' . $output . ']';
$result = new WP_REST_Response(json_decode($data), 200);
$result->set_headers(array('Content-Type' => 'application/json',
'Cache-Control' => 'max-age=3600')); // json 缓存控制
$result->set_headers(
array(
'Content-Type' => 'application/json',
'Cache-Control' => 'max-age=3600'// json 缓存控制
)
);
return $result;
}