2013-07-02 19:52:29 +02:00
|
|
|
<?php
|
|
|
|
/*
|
|
|
|
* Copyright (c) 2010-2013 Tinyboard Development Group
|
|
|
|
*/
|
|
|
|
|
2013-09-06 12:12:04 +02:00
|
|
|
defined('TINYBOARD') or exit;
|
2013-08-20 20:52:12 +02:00
|
|
|
|
2013-07-02 19:52:29 +02:00
|
|
|
/**
|
|
|
|
* Class for generating json API compatible with 4chan API
|
|
|
|
*/
|
|
|
|
class Api {
|
2013-08-20 23:35:16 +02:00
|
|
|
function __construct(){
|
|
|
|
global $config;
|
2013-08-20 20:17:05 +02:00
|
|
|
/**
|
|
|
|
* Translation from local fields to fields in 4chan-style API
|
|
|
|
*/
|
2013-08-20 20:52:12 +02:00
|
|
|
$this->config = $config;
|
|
|
|
|
2013-08-20 20:17:05 +02:00
|
|
|
$this->postFields = array(
|
|
|
|
'id' => 'no',
|
|
|
|
'thread' => 'resto',
|
|
|
|
'subject' => 'sub',
|
|
|
|
'body' => 'com',
|
|
|
|
'email' => 'email',
|
|
|
|
'name' => 'name',
|
|
|
|
'trip' => 'trip',
|
|
|
|
'capcode' => 'capcode',
|
|
|
|
'time' => 'time',
|
|
|
|
'omitted' => 'omitted_posts',
|
|
|
|
'omitted_images' => 'omitted_images',
|
2013-12-28 19:46:50 +01:00
|
|
|
'replies' => 'replies',
|
|
|
|
'images' => 'images',
|
2013-08-20 20:17:05 +02:00
|
|
|
'sticky' => 'sticky',
|
|
|
|
'locked' => 'locked',
|
2013-12-28 19:46:50 +01:00
|
|
|
'bump' => 'last_modified',
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->threadsPageFields = array(
|
|
|
|
'id' => 'no',
|
2014-03-09 01:46:14 +01:00
|
|
|
'bump' => 'last_modified'
|
2013-08-20 20:17:05 +02:00
|
|
|
);
|
2013-07-02 19:52:29 +02:00
|
|
|
|
2014-04-27 15:48:47 +02:00
|
|
|
$this->fileFields = array(
|
2014-09-25 15:42:41 +02:00
|
|
|
'thumbheight' => 'tn_h',
|
|
|
|
'thumbwidth' => 'tn_w',
|
2014-10-16 17:08:39 +02:00
|
|
|
'height' => 'h',
|
|
|
|
'width' => 'w',
|
2014-04-27 15:48:47 +02:00
|
|
|
'size' => 'fsize',
|
|
|
|
);
|
|
|
|
|
2013-08-20 20:17:05 +02:00
|
|
|
if (isset($config['api']['extra_fields']) && gettype($config['api']['extra_fields']) == 'array'){
|
|
|
|
$this->postFields = array_merge($this->postFields, $config['api']['extra_fields']);
|
|
|
|
}
|
|
|
|
}
|
2013-07-02 19:52:29 +02:00
|
|
|
|
2013-08-20 20:17:05 +02:00
|
|
|
private static $ints = array(
|
2013-07-02 19:52:29 +02:00
|
|
|
'no' => 1,
|
|
|
|
'resto' => 1,
|
|
|
|
'time' => 1,
|
|
|
|
'tn_w' => 1,
|
|
|
|
'tn_h' => 1,
|
|
|
|
'w' => 1,
|
|
|
|
'h' => 1,
|
|
|
|
'fsize' => 1,
|
|
|
|
'omitted_posts' => 1,
|
|
|
|
'omitted_images' => 1,
|
2014-10-16 17:08:39 +02:00
|
|
|
'replies' => 1,
|
|
|
|
'images' => 1,
|
2013-07-02 19:52:29 +02:00
|
|
|
'sticky' => 1,
|
|
|
|
'locked' => 1,
|
2013-12-28 19:46:50 +01:00
|
|
|
'last_modified' => 1
|
2013-07-02 19:52:29 +02:00
|
|
|
);
|
|
|
|
|
2014-04-27 15:48:47 +02:00
|
|
|
private function translateFields($fields, $object, &$apiPost) {
|
2013-12-28 19:46:50 +01:00
|
|
|
foreach ($fields as $local => $translated) {
|
2014-04-27 15:48:47 +02:00
|
|
|
if (!isset($object->$local))
|
2013-07-02 19:52:29 +02:00
|
|
|
continue;
|
|
|
|
|
|
|
|
$toInt = isset(self::$ints[$translated]);
|
2014-04-27 15:48:47 +02:00
|
|
|
$val = $object->$local;
|
2013-07-02 19:52:29 +02:00
|
|
|
if ($val !== null && $val !== '') {
|
|
|
|
$apiPost[$translated] = $toInt ? (int) $val : $val;
|
|
|
|
}
|
2013-08-20 20:52:12 +02:00
|
|
|
|
2013-07-02 19:52:29 +02:00
|
|
|
}
|
2014-04-27 15:48:47 +02:00
|
|
|
}
|
2013-07-02 19:52:29 +02:00
|
|
|
|
2014-04-27 15:48:47 +02:00
|
|
|
private function translatePost($post, $threadsPage = false) {
|
|
|
|
$apiPost = array();
|
|
|
|
$fields = $threadsPage ? $this->threadsPageFields : $this->postFields;
|
|
|
|
$this->translateFields($fields, $post, $apiPost);
|
2013-12-28 19:46:50 +01:00
|
|
|
|
2014-04-27 15:48:47 +02:00
|
|
|
if ($threadsPage) return $apiPost;
|
2013-07-02 19:52:29 +02:00
|
|
|
|
2013-08-20 20:52:12 +02:00
|
|
|
// Handle country field
|
|
|
|
if (isset($post->body_nomarkup) && $this->config['country_flags']) {
|
|
|
|
$modifiers = extract_modifiers($post->body_nomarkup);
|
2013-08-21 00:11:32 +02:00
|
|
|
if (isset($modifiers['flag']) && isset($modifiers['flag alt']) && preg_match('/^[a-z]{2}$/', $modifiers['flag'])) {
|
|
|
|
$country = strtoupper($modifiers['flag']);
|
2013-08-20 20:52:12 +02:00
|
|
|
if ($country) {
|
|
|
|
$apiPost['country'] = $country;
|
|
|
|
$apiPost['country_name'] = $modifiers['flag alt'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-27 15:48:47 +02:00
|
|
|
// Handle files
|
|
|
|
// Note: 4chan only supports one file, so only the first file is taken into account for 4chan-compatible API.
|
|
|
|
if (isset($post->files) && $post->files && !$threadsPage) {
|
|
|
|
$file = $post->files[0];
|
|
|
|
$this->translateFields($this->fileFields, $file, $apiPost);
|
2014-10-16 17:08:39 +02:00
|
|
|
$apiPost['filename'] = substr($file->name, 0, strrpos($file->name, '.'));
|
2014-04-27 15:48:47 +02:00
|
|
|
$dotPos = strrpos($file->file, '.');
|
|
|
|
$apiPost['ext'] = substr($file->file, $dotPos);
|
|
|
|
$apiPost['tim'] = substr($file->file, 0, $dotPos);
|
2014-09-27 11:31:30 +02:00
|
|
|
$apiPost['md5'] = base64_encode(hex2bin($post->filehash));
|
2014-04-27 15:48:47 +02:00
|
|
|
}
|
|
|
|
|
2013-07-02 19:52:29 +02:00
|
|
|
return $apiPost;
|
|
|
|
}
|
|
|
|
|
2013-12-28 19:46:50 +01:00
|
|
|
function translateThread(Thread $thread, $threadsPage = false) {
|
2013-07-02 19:52:29 +02:00
|
|
|
$apiPosts = array();
|
2013-12-28 19:46:50 +01:00
|
|
|
$op = $this->translatePost($thread, $threadsPage);
|
|
|
|
if (!$threadsPage) $op['resto'] = 0;
|
2013-07-02 19:52:29 +02:00
|
|
|
$apiPosts['posts'][] = $op;
|
|
|
|
|
|
|
|
foreach ($thread->posts as $p) {
|
2013-12-28 19:46:50 +01:00
|
|
|
$apiPosts['posts'][] = $this->translatePost($p, $threadsPage);
|
2013-07-02 19:52:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $apiPosts;
|
|
|
|
}
|
|
|
|
|
|
|
|
function translatePage(array $threads) {
|
|
|
|
$apiPage = array();
|
|
|
|
foreach ($threads as $thread) {
|
|
|
|
$apiPage['threads'][] = $this->translateThread($thread);
|
|
|
|
}
|
|
|
|
return $apiPage;
|
|
|
|
}
|
|
|
|
|
2013-12-28 19:46:50 +01:00
|
|
|
function translateCatalogPage(array $threads, $threadsPage = false) {
|
2013-07-02 19:52:29 +02:00
|
|
|
$apiPage = array();
|
|
|
|
foreach ($threads as $thread) {
|
2013-12-28 19:46:50 +01:00
|
|
|
$ts = $this->translateThread($thread, $threadsPage);
|
2013-07-02 19:52:29 +02:00
|
|
|
$apiPage['threads'][] = current($ts['posts']);
|
|
|
|
}
|
|
|
|
return $apiPage;
|
|
|
|
}
|
|
|
|
|
2013-12-28 19:46:50 +01:00
|
|
|
function translateCatalog($catalog, $threadsPage = false) {
|
2013-07-02 19:52:29 +02:00
|
|
|
$apiCatalog = array();
|
|
|
|
foreach ($catalog as $page => $threads) {
|
2013-12-28 19:46:50 +01:00
|
|
|
$apiPage = $this->translateCatalogPage($threads, $threadsPage);
|
2013-07-02 19:52:29 +02:00
|
|
|
$apiPage['page'] = $page;
|
|
|
|
$apiCatalog[] = $apiPage;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $apiCatalog;
|
|
|
|
}
|
|
|
|
}
|