Merge pull request #795 from Zankaria/api-modern

Modernize (a bit) the Api class
This commit is contained in:
Lorenzo Yario 2024-08-16 21:43:17 -07:00 committed by GitHub
commit 39876f3cc7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 88 additions and 69 deletions

View File

@ -9,14 +9,49 @@ defined('TINYBOARD') or exit;
* Class for generating json API compatible with 4chan API * Class for generating json API compatible with 4chan API
*/ */
class Api { class Api {
function __construct(){ private bool $show_filename;
global $config; private bool $hide_email;
/** private bool $country_flags;
* Translation from local fields to fields in 4chan-style API private array $postFields;
*/
$this->config = $config;
$this->postFields = array( private const INTS = [
'no' => 1,
'resto' => 1,
'time' => 1,
'tn_w' => 1,
'tn_h' => 1,
'w' => 1,
'h' => 1,
'fsize' => 1,
'omitted_posts' => 1,
'omitted_images' => 1,
'replies' => 1,
'images' => 1,
'sticky' => 1,
'locked' => 1,
'last_modified' => 1
];
private const THREADS_PAGE_FIELDS = [
'id' => 'no',
'bump' => 'last_modified'
];
private const FILE_FIELDS = [
'thumbheight' => 'tn_h',
'thumbwidth' => 'tn_w',
'height' => 'h',
'width' => 'w',
'size' => 'fsize'
];
public function __construct(bool $show_filename, bool $hide_email, bool $country_flags) {
// Translation from local fields to fields in 4chan-style API
$this->show_filename = $show_filename;
$this->hide_email = $hide_email;
$this->country_flags = $country_flags;
$this->postFields = [
'id' => 'no', 'id' => 'no',
'thread' => 'resto', 'thread' => 'resto',
'subject' => 'sub', 'subject' => 'sub',
@ -35,91 +70,64 @@ class Api {
'cycle' => 'cyclical', 'cycle' => 'cyclical',
'bump' => 'last_modified', 'bump' => 'last_modified',
'embed' => 'embed', 'embed' => 'embed',
); ];
$this->threadsPageFields = array(
'id' => 'no',
'bump' => 'last_modified'
);
$this->fileFields = array(
'thumbheight' => 'tn_h',
'thumbwidth' => 'tn_w',
'height' => 'h',
'width' => 'w',
'size' => 'fsize',
);
if (isset($config['api']['extra_fields']) && gettype($config['api']['extra_fields']) == 'array'){ if (isset($config['api']['extra_fields']) && gettype($config['api']['extra_fields']) == 'array'){
$this->postFields = array_merge($this->postFields, $config['api']['extra_fields']); $this->postFields = array_merge($this->postFields, $config['api']['extra_fields']);
} }
} }
private static $ints = array(
'no' => 1,
'resto' => 1,
'time' => 1,
'tn_w' => 1,
'tn_h' => 1,
'w' => 1,
'h' => 1,
'fsize' => 1,
'omitted_posts' => 1,
'omitted_images' => 1,
'replies' => 1,
'images' => 1,
'sticky' => 1,
'locked' => 1,
'last_modified' => 1
);
private function translateFields($fields, $object, &$apiPost) { private function translateFields($fields, $object, &$apiPost) {
foreach ($fields as $local => $translated) { foreach ($fields as $local => $translated) {
if (!isset($object->$local)) if (!isset($object->$local)) {
continue; continue;
}
$toInt = isset(self::$ints[$translated]); $toInt = isset(self::INTS[$translated]);
$val = $object->$local; $val = $object->$local;
if (isset($this->config['hide_email']) && $this->config['hide_email'] && $local === 'email') { if ($this->hide_email && $local === 'email') {
$val = ''; $val = '';
} }
if ($val !== null && $val !== '') { if ($val !== null && $val !== '') {
$apiPost[$translated] = $toInt ? (int) $val : $val; $apiPost[$translated] = $toInt ? (int) $val : $val;
} }
} }
} }
private function translateFile($file, $post, &$apiPost) { private function translateFile($file, $post, &$apiPost) {
$this->translateFields($this->fileFields, $file, $apiPost); $this->translateFields(self::FILE_FIELDS, $file, $apiPost);
$dotPos = strrpos($file->file, '.'); $dotPos = strrpos($file->file, '.');
$apiPost['ext'] = substr($file->file, $dotPos); $apiPost['ext'] = substr($file->file, $dotPos);
$apiPost['tim'] = substr($file->file, 0, $dotPos); $apiPost['tim'] = substr($file->file, 0, $dotPos);
if (isset($this->config['show_filename']) && $this->config['show_filename']) {
if ($this->show_filename) {
$apiPost['filename'] = @substr($file->name, 0, strrpos($file->name, '.')); $apiPost['filename'] = @substr($file->name, 0, strrpos($file->name, '.'));
} } else {
else {
$apiPost['filename'] = substr($file->file, 0, $dotPos); $apiPost['filename'] = substr($file->file, 0, $dotPos);
} }
if (isset ($file->hash) && $file->hash) { if (isset ($file->hash) && $file->hash) {
$apiPost['md5'] = base64_encode(hex2bin($file->hash)); $apiPost['md5'] = base64_encode(hex2bin($file->hash));
} } elseif (isset ($post->filehash) && $post->filehash) {
else if (isset ($post->filehash) && $post->filehash) {
$apiPost['md5'] = base64_encode(hex2bin($post->filehash)); $apiPost['md5'] = base64_encode(hex2bin($post->filehash));
} }
} }
private function translatePost($post, $threadsPage = false) { private function translatePost($post, bool $threadsPage = false) {
global $config, $board; global $config, $board;
$apiPost = array();
$fields = $threadsPage ? $this->threadsPageFields : $this->postFields; $apiPost = [];
$fields = $threadsPage ? self::THREADS_PAGE_FIELDS : $this->postFields;
$this->translateFields($fields, $post, $apiPost); $this->translateFields($fields, $post, $apiPost);
if (isset($config['poster_ids']) && $config['poster_ids']) $apiPost['id'] = poster_id($post->ip, $post->thread, $board['uri']); if (isset($config['poster_ids']) && $config['poster_ids']) {
if ($threadsPage) return $apiPost; $apiPost['id'] = poster_id($post->ip, $post->thread, $board['uri']);
}
if ($threadsPage) {
return $apiPost;
}
// Handle country field // Handle country field
if (isset($post->body_nomarkup) && $this->config['country_flags']) { if (isset($post->body_nomarkup) && $this->country_flags) {
$modifiers = extract_modifiers($post->body_nomarkup); $modifiers = extract_modifiers($post->body_nomarkup);
if (isset($modifiers['flag']) && isset($modifiers['flag alt']) && preg_match('/^[a-z]{2}$/', $modifiers['flag'])) { if (isset($modifiers['flag']) && isset($modifiers['flag alt']) && preg_match('/^[a-z]{2}$/', $modifiers['flag'])) {
$country = strtoupper($modifiers['flag']); $country = strtoupper($modifiers['flag']);
@ -139,12 +147,15 @@ class Api {
if (isset($post->files) && $post->files && !$threadsPage) { if (isset($post->files) && $post->files && !$threadsPage) {
$file = $post->files[0]; $file = $post->files[0];
$this->translateFile($file, $post, $apiPost); $this->translateFile($file, $post, $apiPost);
if (sizeof($post->files) > 1) {
$extra_files = array();
foreach ($post->files as $i => $f) {
if ($i == 0) continue;
$extra_file = array(); if (sizeof($post->files) > 1) {
$extra_files = [];
foreach ($post->files as $i => $f) {
if ($i == 0) {
continue;
}
$extra_file = [];
$this->translateFile($f, $post, $extra_file); $this->translateFile($f, $post, $extra_file);
$extra_files[] = $extra_file; $extra_files[] = $extra_file;
@ -156,8 +167,8 @@ class Api {
return $apiPost; return $apiPost;
} }
function translateThread(Thread $thread, $threadsPage = false) { public function translateThread(Thread $thread, bool $threadsPage = false) {
$apiPosts = array(); $apiPosts = [];
$op = $this->translatePost($thread, $threadsPage); $op = $this->translatePost($thread, $threadsPage);
if (!$threadsPage) $op['resto'] = 0; if (!$threadsPage) $op['resto'] = 0;
$apiPosts['posts'][] = $op; $apiPosts['posts'][] = $op;
@ -169,16 +180,16 @@ class Api {
return $apiPosts; return $apiPosts;
} }
function translatePage(array $threads) { public function translatePage(array $threads) {
$apiPage = array(); $apiPage = [];
foreach ($threads as $thread) { foreach ($threads as $thread) {
$apiPage['threads'][] = $this->translateThread($thread); $apiPage['threads'][] = $this->translateThread($thread);
} }
return $apiPage; return $apiPage;
} }
function translateCatalogPage(array $threads, $threadsPage = false) { public function translateCatalogPage(array $threads, bool $threadsPage = false) {
$apiPage = array(); $apiPage = [];
foreach ($threads as $thread) { foreach ($threads as $thread) {
$ts = $this->translateThread($thread, $threadsPage); $ts = $this->translateThread($thread, $threadsPage);
$apiPage['threads'][] = current($ts['posts']); $apiPage['threads'][] = current($ts['posts']);
@ -186,8 +197,8 @@ class Api {
return $apiPage; return $apiPage;
} }
function translateCatalog($catalog, $threadsPage = false) { public function translateCatalog($catalog, bool $threadsPage = false) {
$apiCatalog = array(); $apiCatalog = [];
foreach ($catalog as $page => $threads) { foreach ($catalog as $page => $threads) {
$apiPage = $this->translateCatalogPage($threads, $threadsPage); $apiPage = $this->translateCatalogPage($threads, $threadsPage);
$apiPage['page'] = $page; $apiPage['page'] = $page;

View File

@ -1628,7 +1628,11 @@ function buildIndex($global_api = "yes") {
$antibot = null; $antibot = null;
if ($config['api']['enabled']) { if ($config['api']['enabled']) {
$api = new Api(); $api = new Api(
$config['show_filename'],
$config['hide_email'],
$config['country_flags']
);
$catalog = array(); $catalog = array();
} }
@ -2286,7 +2290,11 @@ function buildThread($id, $return = false, $mod = false) {
// json api // json api
if ($config['api']['enabled'] && !$mod) { if ($config['api']['enabled'] && !$mod) {
$api = new Api(); $api = new Api(
$config['show_filename'],
$config['hide_email'],
$config['country_flags']
);
$json = json_encode($api->translateThread($thread)); $json = json_encode($api->translateThread($thread));
$jsonFilename = $board['dir'] . $config['dir']['res'] . $id . '.json'; $jsonFilename = $board['dir'] . $config['dir']['res'] . $id . '.json';
file_write($jsonFilename, $json); file_write($jsonFilename, $json);