1
0

api.php 13 KB

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