sakura/inc/api.php

261 lines
8.0 KiB
PHP
Raw Normal View History

2019-11-14 02:39:22 +08:00
<?php
/**
* Classes
*/
include_once('classes/Aplayer.php');
include_once('classes/Bilibili.php');
include_once('classes/Cache.php');
include_once('classes/Images.php');
include_once('classes/QQ.php');
use Sakura\API\Images;
use Sakura\API\QQ;
use Sakura\API\Cache;
2019-11-14 02:39:22 +08:00
/**
* Router
*/
add_action('rest_api_init', function () {
2019-11-25 23:47:56 +08:00
register_rest_route('sakura/v1', '/image/upload', array(
'methods' => 'POST',
'callback' => 'upload_image',
));
register_rest_route('sakura/v1', '/cache_search/json', array(
'methods' => 'GET',
'callback' => 'cache_search_json',
));
2019-11-27 17:17:54 +08:00
register_rest_route('sakura/v1', '/image/cover', array(
'methods' => 'GET',
'callback' => 'cover_gallery',
));
register_rest_route('sakura/v1', '/image/feature', array(
'methods' => 'GET',
'callback' => 'feature_gallery',
));
2019-12-04 20:35:35 +08:00
register_rest_route('sakura/v1', '/database/update', array(
2019-12-15 16:57:53 +08:00
'methods' => 'GET',
2019-12-04 20:35:35 +08:00
'callback' => 'update_database',
2019-11-28 23:21:24 +08:00
));
2019-12-03 21:20:42 +08:00
register_rest_route('sakura/v1', '/qqinfo/json', array(
'methods' => 'GET',
'callback' => 'get_qq_info',
));
2019-12-04 20:35:35 +08:00
register_rest_route('sakura/v1', '/qqinfo/avatar', array(
'methods' => 'GET',
'callback' => 'get_qq_avatar',
));
register_rest_route('sakura/v1', '/bangumi/bilibili', array(
'methods' => 'POST',
'callback' => 'bgm_bilibili',
));
register_rest_route('sakura/v1', '/meting/aplayer', array(
'methods' => 'GET',
'callback' => 'meting_aplayer',
));
2019-11-14 02:39:22 +08:00
});
/**
* Image uploader response
*/
function upload_image(WP_REST_Request $request) {
2019-11-25 23:47:56 +08:00
// see: https://developer.wordpress.org/rest-api/requests/
// handle file params $file === $_FILES
/**
* curl \
* -F "filecomment=This is an img file" \
* -F "cmt_img_file=@screenshot.jpg" \
* https://dev.2heng.xin/wp-json/sakura/v1/image/upload
*/
// $file = $request->get_file_params();
if (!check_ajax_referer('wp_rest', '_wpnonce', false)) {
2019-12-03 21:20:42 +08:00
$output = array('status' => 403,
2019-11-25 23:47:56 +08:00
'success' => false,
'message' => 'Unauthorized client.',
'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",
);
$result = new WP_REST_Response($output, 403);
$result->set_headers(array('Content-Type' => 'application/json'));
return $result;
}
$images = new \Sakura\API\Images();
2019-11-25 23:47:56 +08:00
switch (akina_option("img_upload_api")) {
case 'imgur':
$image = file_get_contents($_FILES["cmt_img_file"]["tmp_name"]);
$API_Request = $images->Imgur_API($image);
2019-11-25 23:47:56 +08:00
break;
case 'smms':
$image = $_FILES;
$API_Request = $images->SMMS_API($image);
2019-11-25 23:47:56 +08:00
break;
case 'chevereto':
$image = file_get_contents($_FILES["cmt_img_file"]["tmp_name"]);
$API_Request = $images->Chevereto_API($image);
2019-11-25 23:47:56 +08:00
break;
}
2019-12-03 21:20:42 +08:00
$result = new WP_REST_Response($API_Request, $API_Request['status']);
2019-11-15 00:11:02 +08:00
$result->set_headers(array('Content-Type' => 'application/json'));
return $result;
2019-11-14 02:39:22 +08:00
}
2019-11-27 17:17:54 +08:00
/*
* 随机封面图 rest api
* @rest api接口路径https://sakura.2heng.xin/wp-json/sakura/v1/image/cover
*/
function cover_gallery() {
$imgurl = Images::cover_gallery();
2019-11-28 23:21:24 +08:00
$data = array('cover image');
2019-11-27 23:32:41 +08:00
$response = new WP_REST_Response($data);
2019-11-28 23:21:24 +08:00
$response->set_status(302);
$response->header('Location', $imgurl);
2019-11-27 23:32:41 +08:00
return $response;
2019-11-27 17:17:54 +08:00
}
/*
* 随机文章特色图 rest api
* @rest api接口路径https://sakura.2heng.xin/wp-json/sakura/v1/image/feature
*/
function feature_gallery() {
2020-01-09 16:50:07 +08:00
return cover_gallery();
2019-11-28 23:21:24 +08:00
}
/*
2019-12-04 20:35:35 +08:00
* update database rest api
* @rest api接口路径https://sakura.2heng.xin/wp-json/sakura/v1/database/update
2019-11-28 23:21:24 +08:00
*/
2019-12-04 20:35:35 +08:00
function update_database() {
if (akina_option('cover_cdn_options') == "type_1") {
$output = Cache::update_database();
2020-01-09 16:50:07 +08:00
$result = new WP_REST_Response($output, 200);
return $result;
} else {
2020-01-09 16:50:07 +08:00
return new WP_REST_Response("Invalid access", 200);
2019-11-28 23:21:24 +08:00
}
2019-12-03 21:42:52 +08:00
}
2019-12-04 20:35:35 +08:00
/*
* 定制实时搜索 rest api
* @rest api接口路径https://sakura.2heng.xin/wp-json/sakura/v1/cache_search/json
* @可在cache_search_json()函数末尾通过设置 HTTP header 控制 json 缓存时间
2019-12-04 20:35:35 +08:00
*/
function cache_search_json() {
if (!check_ajax_referer('wp_rest', '_wpnonce', false)) {
$output = array(
'status' => 403,
'success' => false,
'message' => 'Unauthorized client.'
);
$result = new WP_REST_Response($output, 403);
} else {
$output = Cache::search_json();
$result = new WP_REST_Response($output, 200);
}
$result->set_headers(
array(
'Content-Type' => 'application/json',
'Cache-Control' => 'max-age=3600', // json 缓存控制
)
);
return $result;
2019-12-04 20:35:35 +08:00
}
/**
* QQ info
* https://sakura.2heng.xin/wp-json/sakura/v1/qqinfo/json
*/
function get_qq_info(WP_REST_Request $request) {
if (!check_ajax_referer('wp_rest', '_wpnonce', false)) {
$output = array(
'status' => 403,
'success' => false,
'message' => 'Unauthorized client.'
);
} elseif ($_GET['qq']) {
$qq = $_GET['qq'];
$output = QQ::get_qq_info($qq);
} else {
$output = array(
'status' => 400,
'success' => false,
'message' => 'Bad Request'
);
}
$result = new WP_REST_Response($output, $output['status']);
$result->set_headers(array('Content-Type' => 'application/json'));
return $result;
}
/**
* QQ头像链接解密
* https://sakura.2heng.xin/wp-json/sakura/v1/qqinfo/avatar
*/
function get_qq_avatar() {
$encrypted = $_GET["qq"];
$imgurl = QQ::get_qq_avatar($encrypted);
if (akina_option('qq_avatar_link') == 'type_2') {
$imgdata = file_get_contents($imgurl);
$response = new WP_REST_Response();
$response->set_headers(array(
'Content-Type' => 'image/jpeg',
'Cache-Control' => 'max-age=86400'
));
echo $imgdata;
} else {
$response = new WP_REST_Response();
$response->set_status(301);
$response->header('Location', $imgurl);
}
return $response;
}
function bgm_bilibili() {
if (!check_ajax_referer('wp_rest', 'r', false)) {
$output = array(
'status' => 403,
'success' => false,
'message' => 'Unauthorized client.'
);
$response = new WP_REST_Response($output, 403);
} else {
$page = $_GET["page"] ?: 2;
$bgm = new \Sakura\API\Bilibili();
$html = preg_replace("/\s+|\n+|\r/", ' ', $bgm->get_bgm_items($page));
$response = new WP_REST_Response($html, 200);
}
return $response;
}
function meting_aplayer() {
$type = $_GET['type'];
$id = $_GET['id'];
if (check_ajax_referer('wp_rest', '_wpnonce', false) || !wp_verify_nonce($_GET['meting_pnonce'], $type . '#:' . $id)) {
$Meting_API = new \Sakura\API\Aplayer();
$data = $Meting_API->get_data($type, $id);
if ($type === 'playlist') {
$response = new WP_REST_Response($data, 200);
$response->set_headers(array('cache-control' => 'max-age=3600'));
} elseif ($type === 'lyric') {
$response = new WP_REST_Response();
$response->set_headers(array('cache-control' => 'max-age=3600'));
echo $data;
} else {
$data = str_replace('http://', 'https://', $data);
$response = new WP_REST_Response();
$response->set_status(301);
$response->header('Location', $data);
}
} else {
$output = array(
'status' => 403,
'success' => false,
'message' => 'Unauthorized client.'
);
$response = new WP_REST_Response($output, 403);
}
return $response;
}