From 5a0fbc3890d3cdd30ff56674d50578ff2ec40878 Mon Sep 17 00:00:00 2001 From: flashwave Date: Thu, 2 Feb 2023 19:24:36 +0000 Subject: [PATCH] Updated Satori scripts. --- public/_flashii.php | 3 - public/booru.php | 180 ++++++++++++++++++++++++++++++++ public/get-profile-field.php | 2 - public/get-user-for-tetrio.php | 47 --------- public/recent-forum-posts.php | 5 +- public/recent-registrations.php | 3 +- public/tetrio-user-reverse.php | 28 ----- public/translate.php | 17 +-- public/word-define.php | 39 +++++++ 9 files changed, 227 insertions(+), 97 deletions(-) create mode 100644 public/booru.php delete mode 100644 public/get-user-for-tetrio.php delete mode 100644 public/tetrio-user-reverse.php create mode 100644 public/word-define.php diff --git a/public/_flashii.php b/public/_flashii.php index e433f25..35a3936 100644 --- a/public/_flashii.php +++ b/public/_flashii.php @@ -6,7 +6,6 @@ define('STR_CONFIG', $config['msz-config-path']); header('Content-Type: application/json; charset=utf-8'); if(!is_file(STR_CONFIG)) { - http_response_code(500); echo '{"error":101}'; exit; } @@ -15,7 +14,6 @@ $config = parse_ini_file(STR_CONFIG); if(empty($config['driver']) || empty($config['unix_socket']) || empty($config['username']) || empty($config['database'])) { - http_response_code(500); echo '{"error":102}'; exit; } @@ -36,7 +34,6 @@ try { . ', sql_mode = \'STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION\'', ]); } catch(PDOException $ex) { - http_response_code(500); echo '{"error":103}'; exit; } diff --git a/public/booru.php b/public/booru.php new file mode 100644 index 0000000..6d4f88d --- /dev/null +++ b/public/booru.php @@ -0,0 +1,180 @@ + [ + 'title' => 'Danbooru', + 'type' => 'danbooru', + 'randomUrlFormat' => 'https://danbooru.donmai.us/posts.json?limit=20&tags=order:random+limit:20+%s', + 'postUrlFormat' => 'https://danbooru.donmai.us/posts/%s', + 'implicitTags' => [ + 'misaka_mikoto' => ['-shokuhou_misaki'], + 'shokuhou_misaki' => ['-misaka_mikoto'], + 'kagari_(rewrite)' => ['-screencap'], + 'kasuga_ayumu' => ['-rating:explicit'], + 'osaka_(azumanga_daioh)' => ['-rating:explicit'], + ], + ], + 'gelbooru' => [ + 'title' => 'Gelbooru', + 'type' => 'gelbooru', + 'randomUrlFormat' => 'https://gelbooru.com/index.php?page=dapi&s=post&q=index&json=1&limit=20&tags=sort:random+%s', + 'postUrlFormat' => 'https://gelbooru.com/index.php?page=post&s=view&id=%s', + 'implicitTags' => [ + 'misaka_mikoto' => ['-shokuhou_misaki'], + 'shokuhou_misaki' => ['-misaka_mikoto'], + 'kagari_(rewrite)' => ['-screencap'], + 'kasuga_ayumu' => ['-rating:explicit'], + ], + ], + 'yandere' => [ + 'title' => 'Yande.re', + 'type' => 'yandere', + 'randomUrlFormat' => 'https://yande.re/post.json?api_version=2&limit=20&tags=order:random+%s', + 'postUrlFormat' => 'https://yande.re/post/show/%s', + 'implicitTags' => [ + 'misaka_mikoto' => ['-shokuhou_misaki'], + 'shokuhou_misaki' => ['-misaka_mikoto'], + 'kagari_(rewrite)' => ['-cap'], + 'kasuga_ayumu' => ['-rating:explicit'], + ], + ], +]); + +$config = parse_ini_file(__DIR__ . '/../config/flashii.ini'); + +header('Content-Type: application/json; charset=utf-8'); + +$booru = (string)filter_input(INPUT_GET, 'b'); +$tags = (string)filter_input(INPUT_GET, 't'); +$tags = array_filter(explode(' ', trim($tags))); + +$randomSource = empty($booru); + +if($randomSource) + $booru = BOORUS[array_rand(BOORUS)]; +elseif(!in_array($booru, BOORUS)) + die('{"error":"booru"}'); + +if($booru === 'danbooru' && $randomSource) + $booru = BOORUS[1 + (array_rand(BOORUS) - 1)]; + +if(!array_key_exists($booru, BOORU_INFOS)) + die('{"error":"booru-info"}'); + +$booruInfo = BOORU_INFOS[$booru]; + +if(empty($booruInfo['randomUrlFormat'])) + die('{"error":"random"}'); +if(empty($booruInfo['postUrlFormat'])) + die('{"error":"post"}'); +if(empty($booruInfo['type'])) + die('{"error":"type"}'); + +$isDanbooru = $booruInfo['type'] === 'danbooru'; +$isGelbooru = $booruInfo['type'] === 'gelbooru'; +$isYandere = $booruInfo['type'] === 'yandere'; + +if(!empty($booruInfo['implicitTags'])) { + $originalTags = $tags; + foreach($booruInfo['implicitTags'] as $targetTag => $addTags) { + if(!in_array($targetTag, $originalTags)) + continue; + + foreach($addTags as $tag) + if(!in_array($tag, $tags)) + $tags[] = $tag; + } +} + +$tagString = implode('+', array_map(fn($x) => urlencode($x), $tags)); + +$curl = curl_init(sprintf($booruInfo['randomUrlFormat'], $tagString)); + +$headers = ['Accept: application/json']; +if($isDanbooru) + $headers[] = 'Authorization: Basic ' . base64_encode($config['danbooru-token']); + +curl_setopt_array($curl, [ + CURLOPT_AUTOREFERER => true, + CURLOPT_CERTINFO => false, + CURLOPT_FAILONERROR => false, + CURLOPT_FOLLOWLOCATION => true, + CURLOPT_MAXREDIRS => 5, + CURLOPT_PATH_AS_IS => true, + CURLOPT_RETURNTRANSFER => true, + CURLOPT_TCP_FASTOPEN => true, + CURLOPT_PROTOCOLS => CURLPROTO_HTTPS, + CURLOPT_REDIR_PROTOCOLS => CURLPROTO_HTTPS, + CURLOPT_CONNECTTIMEOUT => 2, + CURLOPT_TIMEOUT => 5, + CURLOPT_USERAGENT => 'Satori/20230202 (+https://fii.moe/satori)', + CURLOPT_HTTPHEADER => $headers, +]); +$response = json_decode(curl_exec($curl)); +curl_close($curl); + +$posts = []; + +// gelbooru and yandere block remote embedding, so not even going to bother with a file_url for those + +if($isDanbooru) { + if(!empty($response) && is_array($response)) { + foreach($response as $raw) { + $posts[] = $post = new stdClass; + $post->id = $raw->id; + $post->rating = $raw->rating; + if(!empty($raw->file_url)) + $post->file_url = $raw->file_url; + $post->post_url = sprintf($booruInfo['postUrlFormat'], $raw->id); + $post->tags = explode(' ', $raw->tag_string); + $post->tags_general = explode(' ', $raw->tag_string_general); + $post->tags_character = explode(' ', $raw->tag_string_character); + $post->tags_copyright = explode(' ', $raw->tag_string_copyright); + $post->tags_artist = explode(' ', $raw->tag_string_artist); + $post->tags_meta = explode(' ', $raw->tag_string_meta); + } + } +} elseif($isGelbooru) { + if(!empty($response) && !empty($response->post) && is_array($response->post)) { + foreach($response->post as $raw) { + $posts[] = $post = new stdClass; + $post->id = $raw->id; + $post->rating = $raw->rating[0]; + //if(!empty($raw->file_url)) + // $post->file_url = $raw->file_url; + $post->post_url = sprintf($booruInfo['postUrlFormat'], $raw->id); + $post->tags = explode(' ', $raw->tags); + } + } +} elseif($isYandere) { + if(!empty($response) && !empty($response->posts) && is_array($response->posts)) { + foreach($response->posts as $raw) { + $posts[] = $post = new stdClass; + $post->id = $raw->id; + $post->rating = $raw->rating; + //if(!empty($raw->file_url)) + // $post->file_url = $raw->file_url; + $post->post_url = sprintf($booruInfo['postUrlFormat'], $raw->id); + $post->tags = explode(' ', $raw->tags); + } + } +} else + die('{"error":"support"}'); + +echo json_encode([ + 'source' => [ + 'name' => $booru, + 'type' => $booruInfo['type'], + 'title' => $booruInfo['title'] ?? $booru, + ], + 'tags' => $tags, + 'posts' => $posts, +]); diff --git a/public/get-profile-field.php b/public/get-profile-field.php index 1ddb6d8..edca303 100644 --- a/public/get-profile-field.php +++ b/public/get-profile-field.php @@ -13,10 +13,8 @@ try { if($data) echo json_encode($data); else { - http_response_code(404); echo '{"error":105}'; } } catch(PDOException $ex) { - http_response_code(500); echo '{"error":104}'; } diff --git a/public/get-user-for-tetrio.php b/public/get-user-for-tetrio.php deleted file mode 100644 index 24f0f0f..0000000 --- a/public/get-user-for-tetrio.php +++ /dev/null @@ -1,47 +0,0 @@ -profileFields(); -$discord = ''; -foreach($fields as $field) - if($field->field_key === 'discord') { - $discord = $field->field_value; - break; - } - -$regAddr = $userInfo->getRegisterRemoteAddress(); -$lastAddr = $userInfo->getLastRemoteAddress(); - -if(strpos($regAddr, ':') === false) - $regAddr = '::ffff:' . $regAddr; -if(strpos($lastAddr, ':') === false) - $lastAddr = '::ffff:' . $lastAddr; - -echo json_encode([ - 'id' => $userInfo->getId(), - 'name' => $userInfo->getUsername(), - 'email' => $userInfo->getEmailAddress(), - 'reg_addr' => $regAddr, - 'last_addr' => $lastAddr, - 'asscord' => $discord, -]); diff --git a/public/recent-forum-posts.php b/public/recent-forum-posts.php index 81def32..c75ed4d 100644 --- a/public/recent-forum-posts.php +++ b/public/recent-forum-posts.php @@ -27,10 +27,10 @@ try { ON f.`forum_id` = p.`forum_id` WHERE `post_id` > :post_id AND `post_deleted` IS NULL - AND `post_created` > NOW() - INTERVAL 2 DAY + AND `post_created` > NOW() - INTERVAL 7 DAY AND p.`forum_id` IN (2, 7, 24, 6, 5, 4, 16, 20, 8, 19, 10, 11, 13, 21, 15, 14, 27, 29, 28, 18, 23) ORDER BY `post_id` - LIMIT 10 + LIMIT 6 '); $fetch->bindValue('post_id', $startId); if($fetch->execute()) @@ -38,6 +38,5 @@ try { else echo '[]'; } catch(PDOException $ex) { - http_response_code(500); echo '{"error":104}'; } diff --git a/public/recent-registrations.php b/public/recent-registrations.php index 22678b7..a3216bb 100644 --- a/public/recent-registrations.php +++ b/public/recent-registrations.php @@ -8,7 +8,7 @@ try { SELECT `user_id`, `username` FROM `msz_users` WHERE `user_id` > :user_id - AND `user_created` > NOW() - INTERVAL 2 DAY + AND `user_created` > NOW() - INTERVAL 7 DAY ORDER BY `user_id` LIMIT 10 '); @@ -18,6 +18,5 @@ try { else echo '[]'; } catch(PDOException $ex) { - http_response_code(500); echo '{"error":104}'; } diff --git a/public/tetrio-user-reverse.php b/public/tetrio-user-reverse.php deleted file mode 100644 index 465d2ec..0000000 --- a/public/tetrio-user-reverse.php +++ /dev/null @@ -1,28 +0,0 @@ - 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:86.0) Gecko/20100101 Firefox/86.0', - CURLOPT_RETURNTRANSFER => true, - CURLOPT_POST => true, - CURLOPT_POSTFIELDS => json_encode(['query' => (string)filter_input(INPUT_GET, 'q', FILTER_SANITIZE_STRING)]), - CURLOPT_HTTPHEADER => [ - 'Authorization: Bearer ' . TETRIO_TOKEN, - 'Accept: application/json', - 'Content-Type: application/json', - ], -]); -$out = curl_exec($curl); -curl_close($curl); - -echo $out; diff --git a/public/translate.php b/public/translate.php index f2608ea..a2f6d90 100644 --- a/public/translate.php +++ b/public/translate.php @@ -1,7 +1,4 @@ code = $code; $this->name = $name; } - public static function fromCode($code) - { + public static function fromCode($code) { return new Language($code, locale_get_display_name($code)); } } @@ -111,8 +105,7 @@ $languages = [ $mode = $_GET['m'] ?? null; -switch ($mode) -{ +switch ($mode) { case 'list': $out = []; diff --git a/public/word-define.php b/public/word-define.php new file mode 100644 index 0000000..a336938 --- /dev/null +++ b/public/word-define.php @@ -0,0 +1,39 @@ +text)) + continue; + + $defs[] = [ + 'word' => $def->word ?? '', + 'pos' => $def->partOfSpeech ?? '', + 'attr' => $def->attributionText ?? '', + 'attr_url' => $def->attributionUrl ?? '', + 'dict' => $def->sourceDictionary ?? '', + 'text' => strtr($def->text, [ + '' => '[i]', + '' => '[/i]', + ]), + 'url' => $def->wordnikUrl ?? '', + ]; +} + +echo json_encode($defs);