Merge branch 'master' of https://github.com/ctrlcctrlv/Tinyboard
This commit is contained in:
commit
861ed07032
83
inc/api.php
83
inc/api.php
@ -1,49 +1,47 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2010-2013 Tinyboard Development Group
|
* Copyright (c) 2010-2013 Tinyboard Development Group
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class for generating json API compatible with 4chan API
|
* Class for generating json API compatible with 4chan API
|
||||||
*/
|
*/
|
||||||
class Api {
|
class Api {
|
||||||
|
function __construct($config){
|
||||||
|
/**
|
||||||
|
* Translation from local fields to fields in 4chan-style API
|
||||||
|
*/
|
||||||
|
$this->config = $config;
|
||||||
|
|
||||||
/**
|
$this->postFields = array(
|
||||||
* Translation from local fields to fields in 4chan-style API
|
'id' => 'no',
|
||||||
*/
|
'thread' => 'resto',
|
||||||
public static $postFields = array(
|
'subject' => 'sub',
|
||||||
'id' => 'no',
|
'body' => 'com',
|
||||||
'thread' => 'resto',
|
'email' => 'email',
|
||||||
'subject' => 'sub',
|
'name' => 'name',
|
||||||
'email' => 'email',
|
'trip' => 'trip',
|
||||||
'name' => 'name',
|
'capcode' => 'capcode',
|
||||||
'trip' => 'trip',
|
'time' => 'time',
|
||||||
'capcode' => 'capcode',
|
'thumbx' => 'tn_w',
|
||||||
'body' => 'com',
|
'thumby' => 'tn_h',
|
||||||
'time' => 'time',
|
'filex' => 'w',
|
||||||
'thumb' => 'thumb', // non-compatible field
|
'filey' => 'h',
|
||||||
'thumbx' => 'tn_w',
|
'filesize' => 'fsize',
|
||||||
'thumby' => 'tn_h',
|
'filename' => 'filename',
|
||||||
'file' => 'file', // non-compatible field
|
'omitted' => 'omitted_posts',
|
||||||
'filex' => 'w',
|
'omitted_images' => 'omitted_images',
|
||||||
'filey' => 'h',
|
'sticky' => 'sticky',
|
||||||
'filesize' => 'fsize',
|
'locked' => 'locked',
|
||||||
//'filename' => 'filename',
|
);
|
||||||
'omitted' => 'omitted_posts',
|
|
||||||
'omitted_images' => 'omitted_images',
|
|
||||||
//'posts' => 'replies',
|
|
||||||
//'ip' => '',
|
|
||||||
'sticky' => 'sticky',
|
|
||||||
'locked' => 'locked',
|
|
||||||
//'bumplocked' => '',
|
|
||||||
//'embed' => '',
|
|
||||||
//'root' => '',
|
|
||||||
//'mod' => '',
|
|
||||||
//'hr' => '',
|
|
||||||
);
|
|
||||||
|
|
||||||
static $ints = array(
|
if (isset($config['api']['extra_fields']) && gettype($config['api']['extra_fields']) == 'array'){
|
||||||
|
$this->postFields = array_merge($this->postFields, $config['api']['extra_fields']);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static $ints = array(
|
||||||
'no' => 1,
|
'no' => 1,
|
||||||
'resto' => 1,
|
'resto' => 1,
|
||||||
'time' => 1,
|
'time' => 1,
|
||||||
@ -60,7 +58,7 @@ class Api {
|
|||||||
|
|
||||||
private function translatePost($post) {
|
private function translatePost($post) {
|
||||||
$apiPost = array();
|
$apiPost = array();
|
||||||
foreach (self::$postFields as $local => $translated) {
|
foreach ($this->postFields as $local => $translated) {
|
||||||
if (!isset($post->$local))
|
if (!isset($post->$local))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
@ -69,6 +67,7 @@ class Api {
|
|||||||
if ($val !== null && $val !== '') {
|
if ($val !== null && $val !== '') {
|
||||||
$apiPost[$translated] = $toInt ? (int) $val : $val;
|
$apiPost[$translated] = $toInt ? (int) $val : $val;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isset($post->filename)) {
|
if (isset($post->filename)) {
|
||||||
@ -77,6 +76,18 @@ class Api {
|
|||||||
$apiPost['ext'] = substr($post->filename, $dotPos);
|
$apiPost['ext'] = substr($post->filename, $dotPos);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Handle country field
|
||||||
|
if (isset($post->body_nomarkup) && $this->config['country_flags']) {
|
||||||
|
$modifiers = extract_modifiers($post->body_nomarkup);
|
||||||
|
if (isset($modifiers['flag']) && isset($modifiers['flag alt'])) {
|
||||||
|
$country = mb_convert_case($modifiers['flag'], MB_CASE_UPPER);
|
||||||
|
if ($country) {
|
||||||
|
$apiPost['country'] = $country;
|
||||||
|
$apiPost['country_name'] = $modifiers['flag alt'];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return $apiPost;
|
return $apiPost;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1343,6 +1343,21 @@
|
|||||||
// });
|
// });
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
* =============
|
||||||
|
* API settings
|
||||||
|
* =============
|
||||||
|
*/
|
||||||
|
|
||||||
|
// Whether or not to use the API, disabled by default.
|
||||||
|
$config['api']['enabled'] = false;
|
||||||
|
|
||||||
|
// Extra fields in to be shown in the array that are not 4chan API compatible.
|
||||||
|
// You canget these by taking a look at the schema for posts_ tables. The array should be formatted as $db_name => $translated_name.
|
||||||
|
// For example:
|
||||||
|
|
||||||
|
// $config['api']['extra_fields'] = array('body_nomarkup'=>'com_nomarkup');
|
||||||
|
|
||||||
|
/*
|
||||||
* ====================
|
* ====================
|
||||||
* Other/uncategorized
|
* Other/uncategorized
|
||||||
* ====================
|
* ====================
|
||||||
|
@ -959,6 +959,7 @@ function deletePost($id, $error_if_doesnt_exist=true, $rebuild_after=true) {
|
|||||||
// Delete thread HTML page
|
// Delete thread HTML page
|
||||||
file_unlink($board['dir'] . $config['dir']['res'] . sprintf($config['file_page'], $post['id']));
|
file_unlink($board['dir'] . $config['dir']['res'] . sprintf($config['file_page'], $post['id']));
|
||||||
file_unlink($board['dir'] . $config['dir']['res'] . sprintf($config['file_page50'], $post['id']));
|
file_unlink($board['dir'] . $config['dir']['res'] . sprintf($config['file_page50'], $post['id']));
|
||||||
|
file_unlink($board['dir'] . $config['dir']['res'] . sprintf('%d.json', $post['id']));
|
||||||
|
|
||||||
$antispam_query = prepare('DELETE FROM ``antispam`` WHERE `board` = :board AND `thread` = :thread');
|
$antispam_query = prepare('DELETE FROM ``antispam`` WHERE `board` = :board AND `thread` = :thread');
|
||||||
$antispam_query->bindValue(':board', $board['uri']);
|
$antispam_query->bindValue(':board', $board['uri']);
|
||||||
@ -1299,8 +1300,10 @@ function buildIndex() {
|
|||||||
if (!$config['try_smarter'])
|
if (!$config['try_smarter'])
|
||||||
$antibot = create_antibot($board['uri']);
|
$antibot = create_antibot($board['uri']);
|
||||||
|
|
||||||
$api = new Api();
|
if ($config['api']['enabled']) {
|
||||||
$catalog = array();
|
$api = new Api($config);
|
||||||
|
$catalog = array();
|
||||||
|
}
|
||||||
|
|
||||||
for ($page = 1; $page <= $config['max_pages']; $page++) {
|
for ($page = 1; $page <= $config['max_pages']; $page++) {
|
||||||
$filename = $board['dir'] . ($page == 1 ? $config['file_index'] : sprintf($config['file_page'], $page));
|
$filename = $board['dir'] . ($page == 1 ? $config['file_index'] : sprintf($config['file_page'], $page));
|
||||||
@ -1324,27 +1327,33 @@ function buildIndex() {
|
|||||||
file_write($filename, Element('index.html', $content));
|
file_write($filename, Element('index.html', $content));
|
||||||
|
|
||||||
// json api
|
// json api
|
||||||
$threads = $content['threads'];
|
if ($config['api']['enabled']) {
|
||||||
$json = json_encode($api->translatePage($threads));
|
$threads = $content['threads'];
|
||||||
$jsonFilename = $board['dir'] . ($page-1) . ".json"; // pages should start from 0
|
$json = json_encode($api->translatePage($threads));
|
||||||
file_write($jsonFilename, $json);
|
$jsonFilename = $board['dir'] . ($page-1) . ".json"; // pages should start from 0
|
||||||
|
file_write($jsonFilename, $json);
|
||||||
|
|
||||||
$catalog[$page-1] = $threads;
|
$catalog[$page-1] = $threads;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if ($page < $config['max_pages']) {
|
if ($page < $config['max_pages']) {
|
||||||
for (;$page<=$config['max_pages'];$page++) {
|
for (;$page<=$config['max_pages'];$page++) {
|
||||||
$filename = $board['dir'] . ($page==1 ? $config['file_index'] : sprintf($config['file_page'], $page));
|
$filename = $board['dir'] . ($page==1 ? $config['file_index'] : sprintf($config['file_page'], $page));
|
||||||
file_unlink($filename);
|
file_unlink($filename);
|
||||||
|
|
||||||
$jsonFilename = $board['dir'] . ($page-1) . ".json";
|
if ($config['api']['enabled']) {
|
||||||
file_unlink($jsonFilename);
|
$jsonFilename = $board['dir'] . ($page-1) . ".json";
|
||||||
|
file_unlink($jsonFilename);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// json api catalog
|
// json api catalog
|
||||||
$json = json_encode($api->translateCatalog($catalog));
|
if ($config['api']['enabled']) {
|
||||||
$jsonFilename = $board['dir'] . "catalog.json";
|
$json = json_encode($api->translateCatalog($catalog));
|
||||||
file_write($jsonFilename, $json);
|
$jsonFilename = $board['dir'] . "catalog.json";
|
||||||
|
file_write($jsonFilename, $json);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function buildJavascript() {
|
function buildJavascript() {
|
||||||
@ -1765,10 +1774,12 @@ function buildThread($id, $return = false, $mod = false) {
|
|||||||
$build_pages[] = thread_find_page($id);
|
$build_pages[] = thread_find_page($id);
|
||||||
|
|
||||||
// json api
|
// json api
|
||||||
$api = new Api();
|
if ($config['api']['enabled']) {
|
||||||
$json = json_encode($api->translateThread($thread));
|
$api = new Api($config);
|
||||||
$jsonFilename = $board['dir'] . $config['dir']['res'] . $id . ".json";
|
$json = json_encode($api->translateThread($thread));
|
||||||
file_write($jsonFilename, $json);
|
$jsonFilename = $board['dir'] . $config['dir']['res'] . $id . ".json";
|
||||||
|
file_write($jsonFilename, $json);
|
||||||
|
}
|
||||||
|
|
||||||
if ($return) {
|
if ($return) {
|
||||||
return $body;
|
return $body;
|
||||||
|
Loading…
Reference in New Issue
Block a user