summaryrefslogtreecommitdiff
path: root/inc/api.php
blob: 2bf6b7b84d01e7ce4689083bd5fff65d94a00449 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
<?php
/*
 *  Copyright (c) 2010-2013 Tinyboard Development Group
 */

/**
 * Class for generating json API compatible with 4chan API
 */
class Api {
	function __construct($config){
		/**
		 * Translation from local fields to fields in 4chan-style API
		 */
		$this->postFields = array(
			'id' => 'no',
			'thread' => 'resto',
			'subject' => 'sub',
			'body' => 'com',
			'email' => 'email',
			'name' => 'name',
			'trip' => 'trip',
			'capcode' => 'capcode',
			'time' => 'time',
			'thumbx' => 'tn_w',
			'thumby' => 'tn_h',
			'filex' => 'w',
			'filey' => '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']);
		}
	}

	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,
		'sticky' => 1,
		'locked' => 1,
	);

	private function translatePost($post) {
		$apiPost = array();
		foreach ($this->postFields as $local => $translated) {
			if (!isset($post->$local))
				continue;

			$toInt = isset(self::$ints[$translated]);
			$val = $post->$local;
			if ($val !== null && $val !== '') {
				$apiPost[$translated] = $toInt ? (int) $val : $val;
			}
		}

		if (isset($post->filename)) {
			$dotPos = strrpos($post->filename, '.');
			$apiPost['filename'] = substr($post->filename, 0, $dotPos);
			$apiPost['ext'] = substr($post->filename, $dotPos);
		}

		return $apiPost;
	}

	function translateThread(Thread $thread) {
		$apiPosts = array();
		$op = $this->translatePost($thread);
		$op['resto'] = 0;
		$apiPosts['posts'][] = $op;

		foreach ($thread->posts as $p) {
			$apiPosts['posts'][] = $this->translatePost($p);
		}

		return $apiPosts;
	}

	function translatePage(array $threads) {
		$apiPage = array();
		foreach ($threads as $thread) {
			$apiPage['threads'][] = $this->translateThread($thread);
		}
		return $apiPage;
	}

	function translateCatalogPage(array $threads) {
		$apiPage = array();
		foreach ($threads as $thread) {
			$ts = $this->translateThread($thread);
			$apiPage['threads'][] = current($ts['posts']);
		}
		return $apiPage;
	}

	function translateCatalog($catalog) {
		$apiCatalog = array();
		foreach ($catalog as $page => $threads) {
			$apiPage = $this->translateCatalogPage($threads);
			$apiPage['page'] = $page;
			$apiCatalog[] = $apiPage;
		}

		return $apiCatalog;
	}
}