summaryrefslogtreecommitdiff
path: root/formats/JsonFormat.php
blob: fafe7a5acfcaf9b8a4272ceef315153b76103204 (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
119
120
121
122
123
124
125
126
<?php
/**
 * JsonFormat - JSON Feed Version 1
 * https://jsonfeed.org/version/1
 *
 * Validators:
 * https://validator.jsonfeed.org
 * https://github.com/vigetlabs/json-feed-validator
 */
class JsonFormat extends FormatAbstract {
	const VENDOR_EXCLUDES = array(
		'author',
		'title',
		'uri',
		'timestamp',
		'content',
		'enclosures',
		'categories',
	);

	public function stringify(){
		$urlScheme	= (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') ? 'https://' : 'http://';
		$urlHost	= (isset($_SERVER['HTTP_HOST'])) ? $_SERVER['HTTP_HOST'] : '';
		$urlPath	= (isset($_SERVER['PATH_INFO'])) ? $_SERVER['PATH_INFO'] : '';
		$urlRequest	= (isset($_SERVER['REQUEST_URI'])) ? $_SERVER['REQUEST_URI'] : '';

		$extraInfos = $this->getExtraInfos();

		$data = array(
			'version'		=> 'https://jsonfeed.org/version/1',
			'title'			=> (!empty($extraInfos['name'])) ? $extraInfos['name'] : $urlHost,
			'home_page_url'	=> (!empty($extraInfos['uri'])) ? $extraInfos['uri'] : REPOSITORY,
			'feed_url'		=> $urlScheme . $urlHost . $urlRequest
		);

		if (!empty($extraInfos['icon'])) {
			$data['icon'] = $extraInfos['icon'];
			$data['favicon'] = $extraInfos['icon'];
		}

		$items = array();
		foreach ($this->getItems() as $item) {
			$entry = array();

			$entryAuthor		= $item->getAuthor();
			$entryTitle			= $item->getTitle();
			$entryUri			= $item->getURI();
			$entryTimestamp		= $item->getTimestamp();
			$entryContent		= $this->sanitizeHtml($item->getContent());
			$entryEnclosures	= $item->getEnclosures();
			$entryCategories	= $item->getCategories();

			$vendorFields = $item->toArray();
			foreach (self::VENDOR_EXCLUDES as $key) {
				unset($vendorFields[$key]);
			}

			$entry['id'] = $entryUri;

			if (!empty($entryTitle)) {
				$entry['title'] = $entryTitle;
			}
			if (!empty($entryAuthor)) {
				$entry['author'] = array(
					'name' => $entryAuthor
				);
			}
			if (!empty($entryTimestamp)) {
				$entry['date_modified'] = gmdate(DATE_ATOM, $entryTimestamp);
			}
			if (!empty($entryUri)) {
				$entry['url'] = $entryUri;
			}
			if (!empty($entryContent)) {
				if ($this->isHTML($entryContent)) {
					$entry['content_html'] = $entryContent;
				} else {
					$entry['content_text'] = $entryContent;
				}
			}
			if (!empty($entryEnclosures)) {
				$entry['attachments'] = array();
				foreach ($entryEnclosures as $enclosure) {
					$entry['attachments'][] = array(
						'url'		=> $enclosure,
						'mime_type'	=> getMimeType($enclosure)
					);
				}
			}
			if (!empty($entryCategories)) {
				$entry['tags'] = array();
				foreach ($entryCategories as $category) {
					$entry['tags'][] = $category;
				}
			}
			if (!empty($vendorFields)) {
				$entry['_rssbridge'] = $vendorFields;
			}

			if (empty($entry['id']))
				$entry['id'] = hash('sha1', $entryTitle . $entryContent);

			$items[] = $entry;
		}
		$data['items'] = $items;

		$toReturn = json_encode($data, JSON_PRETTY_PRINT);

		// Remove invalid non-UTF8 characters
		ini_set('mbstring.substitute_character', 'none');
		$toReturn = mb_convert_encoding($toReturn, $this->getCharset(), 'UTF-8');
		return $toReturn;
	}

	public function display(){
		$this
			->setContentType('application/json; charset=' . $this->getCharset())
			->callContentType();

		return parent::display();
	}

	private function isHTML($text) {
		return (strlen(strip_tags($text)) != strlen($text));
	}
}