api.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. <?php
  2. /**
  3. * @Author: fuukei
  4. * @Date: 2022-03-13 18:16:15
  5. * @Last Modified by: cocdeshijie
  6. * @Last Modified time: 2022-04-16 13:27:30
  7. */
  8. /**
  9. * Classes
  10. */
  11. include_once('classes/Aplayer.php');
  12. include_once('classes/Bilibili.php');
  13. include_once('classes/Cache.php');
  14. include_once('classes/Images.php');
  15. include_once('classes/QQ.php');
  16. include_once('classes/Captcha.php');
  17. include_once('classes/MyAnimeList.php');
  18. include_once('classes/BilibiliFavList.php');
  19. use Sakura\API\Images;
  20. use Sakura\API\QQ;
  21. use Sakura\API\Cache;
  22. use Sakura\API\Captcha;
  23. /**
  24. * Router
  25. */
  26. add_action('rest_api_init', function () {
  27. register_rest_route('sakura/v1', '/image/upload', array(
  28. 'methods' => 'POST',
  29. 'callback' => 'upload_image',
  30. 'permission_callback'=>'__return_true'
  31. ));
  32. register_rest_route('sakura/v1', '/cache_search/json', array(
  33. 'methods' => 'GET',
  34. 'callback' => 'cache_search_json',
  35. 'permission_callback'=>'__return_true'
  36. ));
  37. register_rest_route('sakura/v1', '/image/cover', array(
  38. 'methods' => 'GET',
  39. 'callback' => 'cover_gallery',
  40. 'permission_callback'=>'__return_true'
  41. ));
  42. register_rest_route('sakura/v1', '/image/feature', array(
  43. 'methods' => 'GET',
  44. 'callback' => 'feature_gallery',
  45. 'permission_callback'=>'__return_true'
  46. ));
  47. // register_rest_route('sakura/v1', '/database/update', array(
  48. // 'methods' => 'GET',
  49. // 'callback' => 'update_database',
  50. // 'permission_callback'=>'__return_true'
  51. // ));
  52. register_rest_route('sakura/v1', '/qqinfo/json', array(
  53. 'methods' => 'GET',
  54. 'callback' => 'get_qq_info',
  55. 'permission_callback'=>'__return_true'
  56. ));
  57. register_rest_route('sakura/v1', '/qqinfo/avatar', array(
  58. 'methods' => 'GET',
  59. 'callback' => 'get_qq_avatar',
  60. 'permission_callback'=>'__return_true'
  61. ));
  62. register_rest_route('sakura/v1', '/bangumi/bilibili', array(
  63. 'methods' => 'POST',
  64. 'callback' => 'bgm_bilibili',
  65. 'permission_callback'=>'__return_true'
  66. ));
  67. register_rest_route('sakura/v1', '/favlist/bilibili', array(
  68. 'methods' => 'POST',
  69. 'callback' => 'favlist_bilibili',
  70. 'permission_callback'=>'__return_true'
  71. ));
  72. register_rest_route('sakura/v1', '/meting/aplayer', array(
  73. 'methods' => 'GET',
  74. 'callback' => 'meting_aplayer',
  75. 'permission_callback'=>'__return_true'
  76. ));
  77. register_rest_route('sakura/v1', '/captcha/create', array(
  78. 'methods' => 'GET',
  79. 'callback' => 'create_CAPTCHA',
  80. 'permission_callback'=>'__return_true'
  81. ));
  82. });
  83. /**
  84. * Image uploader response
  85. */
  86. function upload_image(WP_REST_Request $request) {
  87. // see: https://developer.wordpress.org/rest-api/requests/
  88. // handle file params $file === $_FILES
  89. /**
  90. * curl \
  91. * -F "filecomment=This is an img file" \
  92. * -F "cmt_img_file=@screenshot.jpg" \
  93. * https://dev.2heng.xin/wp-json/sakura/v1/image/upload
  94. */
  95. // $file = $request->get_file_params();
  96. if (!check_ajax_referer('wp_rest', '_wpnonce', false)) {
  97. $output = array('status' => 403,
  98. 'success' => false,
  99. 'message' => 'Unauthorized client.',
  100. 'link' => "https://s.nmxc.ltd/sakurairo_vision/@2.6/basic/step04.md.png",
  101. 'proxy' => iro_opt('comment_image_proxy') . "https://s.nmxc.ltd/sakurairo_vision/@2.6/basic/step04.md.png",
  102. );
  103. $result = new WP_REST_Response($output, 403);
  104. $result->set_headers(array('Content-Type' => 'application/json'));
  105. return $result;
  106. }
  107. $images = new \Sakura\API\Images();
  108. switch (iro_opt("img_upload_api")) {
  109. case 'imgur':
  110. $image = file_get_contents($_FILES["cmt_img_file"]["tmp_name"]);
  111. $API_Request = $images->Imgur_API($image);
  112. break;
  113. case 'smms':
  114. $image = $_FILES;
  115. $API_Request = $images->SMMS_API($image);
  116. break;
  117. case 'chevereto':
  118. $image = file_get_contents($_FILES["cmt_img_file"]["tmp_name"]);
  119. $API_Request = $images->Chevereto_API($image);
  120. break;
  121. case 'lsky':
  122. $image = $_FILES;
  123. $API_Request = $images->LSKY_API($image);
  124. break;
  125. }
  126. $result = new WP_REST_Response($API_Request, $API_Request['status']);
  127. $result->set_headers(array('Content-Type' => 'application/json'));
  128. return $result;
  129. }
  130. /*
  131. * 随机封面图 rest api
  132. * @rest api接口路径:https://sakura.2heng.xin/wp-json/sakura/v1/image/cover
  133. */
  134. function cover_gallery() {
  135. $type = $_GET['type'] ?? '';
  136. // $type = in_array('type',$_GET) ? $_GET['type']:'';
  137. if ($type === 'mobile' && iro_opt('random_graphs_mts')){
  138. $imgurl = Images::mobile_cover_gallery();
  139. }else{
  140. $imgurl = Images::cover_gallery();
  141. }
  142. if (!$imgurl['status']){
  143. return new WP_REST_Response(
  144. array(
  145. 'status' => 500,
  146. 'success' => false,
  147. 'message' => $imgurl['msg']
  148. ),
  149. 500
  150. );
  151. }
  152. $data = array('cover image');
  153. $response = new WP_REST_Response($data);
  154. $response->set_status(302);
  155. $response->header('Location', $imgurl['url']);
  156. return $response;
  157. }
  158. /*
  159. * 随机文章特色图 rest api
  160. * @rest api接口路径:https://sakura.2heng.xin/wp-json/sakura/v1/image/feature
  161. */
  162. function feature_gallery() {
  163. $size = isset($_GET['size']) ? (in_array($_GET['size'], ['source','th']) ? $_GET['size'] : 'source') : 'source';
  164. $imgurl = Images::feature_gallery($size);
  165. if (!$imgurl['status']){
  166. return new WP_REST_Response(
  167. array(
  168. 'status' => 500,
  169. 'success' => false,
  170. 'message' => $imgurl['msg']
  171. ),
  172. 500
  173. );
  174. }
  175. $data = array('feature image');
  176. $response = new WP_REST_Response($data);
  177. $response->set_status(302);
  178. $response->header('Location', $imgurl['url']);
  179. return $response;
  180. }
  181. /*
  182. * update database rest api
  183. * @rest api接口路径:https://sakura.2heng.xin/wp-json/sakura/v1/database/update
  184. */
  185. // function update_database() {
  186. // if (iro_opt('random_graphs_options') == "webp_optimization") {
  187. // $output = Cache::update_database();
  188. // $result = new WP_REST_Response($output, 200);
  189. // return $result;
  190. // } else {
  191. // return new WP_REST_Response("Invalid access", 200);
  192. // }
  193. // }
  194. /*
  195. * 定制实时搜索 rest api
  196. * @rest api接口路径:https://sakura.2heng.xin/wp-json/sakura/v1/cache_search/json
  197. * @可在cache_search_json()函数末尾通过设置 HTTP header 控制 json 缓存时间
  198. */
  199. function cache_search_json() {
  200. if (!check_ajax_referer('wp_rest', '_wpnonce', false)) {
  201. $output = array(
  202. 'status' => 403,
  203. 'success' => false,
  204. 'message' => 'Unauthorized client.'
  205. );
  206. $result = new WP_REST_Response($output, 403);
  207. } else {
  208. $output = Cache::search_json();
  209. $result = new WP_REST_Response($output, 200);
  210. }
  211. $result->set_headers(
  212. array(
  213. 'Content-Type' => 'application/json',
  214. 'Cache-Control' => 'max-age=3600', // json 缓存控制
  215. )
  216. );
  217. return $result;
  218. }
  219. /**
  220. * QQ info
  221. * https://sakura.2heng.xin/wp-json/sakura/v1/qqinfo/json
  222. */
  223. function get_qq_info(WP_REST_Request $request) {
  224. if (!check_ajax_referer('wp_rest', '_wpnonce', false)) {
  225. $output = array(
  226. 'status' => 403,
  227. 'success' => false,
  228. 'message' => 'Unauthorized client.'
  229. );
  230. } elseif ($_GET['qq']) {
  231. $qq = $_GET['qq'];
  232. $output = QQ::get_qq_info($qq);
  233. } else {
  234. $output = array(
  235. 'status' => 400,
  236. 'success' => false,
  237. 'message' => 'Bad Request'
  238. );
  239. }
  240. $result = new WP_REST_Response($output, $output['status']);
  241. $result->set_headers(array('Content-Type' => 'application/json'));
  242. return $result;
  243. }
  244. /**
  245. * QQ头像链接解密
  246. * https://sakura.2heng.xin/wp-json/sakura/v1/qqinfo/avatar
  247. */
  248. function get_qq_avatar() {
  249. $encrypted = $_GET["qq"];
  250. $imgurl = QQ::get_qq_avatar($encrypted);
  251. if (iro_opt('qq_avatar_link') == 'type_2') {
  252. $imgdata = file_get_contents($imgurl);
  253. $response = new WP_REST_Response();
  254. $response->set_headers(array(
  255. 'Content-Type' => 'image/jpeg',
  256. 'Cache-Control' => 'max-age=86400'
  257. ));
  258. echo $imgdata;
  259. } else {
  260. $response = new WP_REST_Response();
  261. $response->set_status(301);
  262. $response->header('Location', $imgurl);
  263. }
  264. return $response;
  265. }
  266. function bgm_bilibili() {
  267. if (!check_ajax_referer('wp_rest', '_wpnonce', false)) {
  268. $output = array(
  269. 'status' => 403,
  270. 'success' => false,
  271. 'message' => 'Unauthorized client.'
  272. );
  273. $response = new WP_REST_Response($output, 403);
  274. } else {
  275. $page = $_GET["page"] ?: 2;
  276. $bgm = new \Sakura\API\Bilibili();
  277. $html = preg_replace("/\s+|\n+|\r/", ' ', $bgm->get_bgm_items($page));
  278. $response = new WP_REST_Response($html, 200);
  279. }
  280. $page = $_GET["page"] ?: 2;
  281. $bgm = new \Sakura\API\Bilibili();
  282. $html = preg_replace("/\s+|\n+|\r/", ' ', $bgm->get_bgm_items($page));
  283. $response = new WP_REST_Response($html, 200);
  284. return $response;
  285. }
  286. function favlist_bilibili() {
  287. if (!check_ajax_referer('wp_rest', '_wpnonce', false)) {
  288. $output = array(
  289. 'status' => 403,
  290. 'success' => false,
  291. 'message' => 'Unauthorized client.'
  292. );
  293. $response = new WP_REST_Response($output, 403);
  294. } else {
  295. $page = $_GET["page"] ?: 2;
  296. $folder_id = $_GET["folder_id"];
  297. $bgm = new \Sakura\API\BilibiliFavList();
  298. $html = preg_replace("/\s+|\n+|\r/", ' ', $bgm->load_folder_items($folder_id, $page));
  299. $response = new WP_REST_Response($html, 200);
  300. }
  301. return $response;
  302. }
  303. function meting_aplayer() {
  304. $type = $_GET['type'];
  305. $id = $_GET['id'];
  306. if(in_array('_wpnonce',$_GET)) $wpnonce = $_GET['_wpnonce'];
  307. if(in_array('meting_nonce',$_GET)) $meting_nonce = $_GET['meting_nonce'];
  308. if ((isset($wpnonce) && !check_ajax_referer('wp_rest', $wpnonce, false)) || (isset($meting_nonce) && !wp_verify_nonce($meting_nonce, $type . '#:' . $id))) {
  309. $output = array(
  310. 'status' => 403,
  311. 'success' => false,
  312. 'message' => 'Unauthorized client.'
  313. );
  314. $response = new WP_REST_Response($output, 403);
  315. } else {
  316. $Meting_API = new \Sakura\API\Aplayer();
  317. $data = $Meting_API->get_data($type, $id);
  318. if ($type === 'playlist') {
  319. $response = new WP_REST_Response($data, 200);
  320. $response->set_headers(array('cache-control' => 'max-age=3600'));
  321. } elseif ($type === 'lyric') {
  322. $response = new WP_REST_Response();
  323. $response->set_headers(array('cache-control' => 'max-age=3600'));
  324. $response->set_headers(array('Content-Type' => 'text/plain; charset=utf-8'));
  325. $response->set_data($data);
  326. } else {
  327. $response = new WP_REST_Response();
  328. $response->set_status(301);
  329. $response->header('Location', $data);
  330. }
  331. }
  332. return $response;
  333. }
  334. function create_CAPTCHA(){
  335. $CAPTCHA = new Captcha();
  336. $response = new WP_REST_Response($CAPTCHA->create_captcha_img());
  337. $response->set_status(200);
  338. $response->set_headers(array('Content-Type' => 'application/json'));
  339. return $response;
  340. }