diff options
| author | czaks <marcin@6irc.net> | 2013-08-20 18:19:06 -0400 |
|---|---|---|
| committer | czaks <marcin@6irc.net> | 2013-08-20 18:19:06 -0400 |
| commit | f909fd190af4427dae1a8eddd4fa476c1243c433 (patch) | |
| tree | 2b95b9f86510286bf291a28d9c63d5398bbfe54c /inc/api.php | |
| parent | bb3f2d50756409a8c3aef704081b7cd7af0dd9ab (diff) | |
| parent | 07bf8914e688486e0c9e879ed25cfa866f3efb59 (diff) | |
Merge branch 'master' of https://github.com/ctrlcctrlv/Tinyboard
Diffstat (limited to 'inc/api.php')
| -rw-r--r-- | inc/api.php | 84 |
1 files changed, 48 insertions, 36 deletions
diff --git a/inc/api.php b/inc/api.php index 5592d8af..a8331590 100644 --- a/inc/api.php +++ b/inc/api.php @@ -1,49 +1,48 @@ <?php - /* * Copyright (c) 2010-2013 Tinyboard Development Group */ + /** * Class for generating json API compatible with 4chan API */ class Api { + function __construct(){ + global $config; + /** + * Translation from local fields to fields in 4chan-style API + */ + $this->config = $config; - /** - * Translation from local fields to fields in 4chan-style API - */ - public static $postFields = array( - 'id' => 'no', - 'thread' => 'resto', - 'subject' => 'sub', - 'email' => 'email', - 'name' => 'name', - 'trip' => 'trip', - 'capcode' => 'capcode', - 'body' => 'com', - 'time' => 'time', - 'thumb' => 'thumb', // non-compatible field - 'thumbx' => 'tn_w', - 'thumby' => 'tn_h', - 'file' => 'file', // non-compatible field - 'filex' => 'w', - 'filey' => 'h', - 'filesize' => 'fsize', - //'filename' => 'filename', - 'omitted' => 'omitted_posts', - 'omitted_images' => 'omitted_images', - //'posts' => 'replies', - //'ip' => '', - 'sticky' => 'sticky', - 'locked' => 'locked', - //'bumplocked' => '', - //'embed' => '', - //'root' => '', - //'mod' => '', - //'hr' => '', - ); + $this->postFields = array( + 'id' => 'no', + 'thread' => 'resto', + 'subject' => 'sub', + 'body' => 'com', + 'email' => 'email', + 'name' => 'name', + 'trip' => 'trip', + 'capcode' => 'capcode', + 'time' => 'time', + 'thumbheight' => 'tn_w', + 'thumbwidth' => 'tn_h', + 'fileheight' => 'w', + 'filewidth' => 'h', + 'filesize' => 'fsize', + 'filename' => 'filename', + 'omitted' => 'omitted_posts', + 'omitted_images' => 'omitted_images', + 'sticky' => 'sticky', + 'locked' => 'locked', + ); + + if (isset($config['api']['extra_fields']) && gettype($config['api']['extra_fields']) == 'array'){ + $this->postFields = array_merge($this->postFields, $config['api']['extra_fields']); + } + } - static $ints = array( + private static $ints = array( 'no' => 1, 'resto' => 1, 'time' => 1, @@ -60,7 +59,7 @@ class Api { private function translatePost($post) { $apiPost = array(); - foreach (self::$postFields as $local => $translated) { + foreach ($this->postFields as $local => $translated) { if (!isset($post->$local)) continue; @@ -69,6 +68,7 @@ class Api { if ($val !== null && $val !== '') { $apiPost[$translated] = $toInt ? (int) $val : $val; } + } if (isset($post->filename)) { @@ -77,6 +77,18 @@ class Api { $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']) && preg_match('/^[a-z]{2}$/', $modifiers['flag'])) { + $country = strtoupper($modifiers['flag']); + if ($country) { + $apiPost['country'] = $country; + $apiPost['country_name'] = $modifiers['flag alt']; + } + } + } + return $apiPost; } |
