2013-08-11 13:30:41 +02:00
|
|
|
<?php
|
|
|
|
/**
|
2019-01-05 13:20:11 +01:00
|
|
|
* JsonFormat - JSON Feed Version 1
|
|
|
|
* https://jsonfeed.org/version/1
|
|
|
|
*
|
|
|
|
* Validators:
|
|
|
|
* https://validator.jsonfeed.org
|
|
|
|
* https://github.com/vigetlabs/json-feed-validator
|
|
|
|
*/
|
2016-09-10 20:41:11 +02:00
|
|
|
class JsonFormat extends FormatAbstract {
|
2019-10-31 19:00:12 +01:00
|
|
|
const MIME_TYPE = 'application/json';
|
|
|
|
|
2019-01-05 13:20:11 +01:00
|
|
|
const VENDOR_EXCLUDES = array(
|
|
|
|
'author',
|
|
|
|
'title',
|
|
|
|
'uri',
|
|
|
|
'timestamp',
|
|
|
|
'content',
|
|
|
|
'enclosures',
|
|
|
|
'categories',
|
2019-02-03 20:56:41 +01:00
|
|
|
'uid',
|
2019-01-05 13:20:11 +01:00
|
|
|
);
|
|
|
|
|
2016-09-10 20:41:11 +02:00
|
|
|
public function stringify(){
|
2019-02-03 20:56:41 +01:00
|
|
|
$urlPrefix = (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'] : '';
|
2019-01-05 13:20:11 +01:00
|
|
|
|
|
|
|
$extraInfos = $this->getExtraInfos();
|
2018-12-26 22:41:32 +01:00
|
|
|
|
2019-01-05 13:20:11 +01:00
|
|
|
$data = array(
|
2019-02-03 20:56:41 +01:00
|
|
|
'version' => 'https://jsonfeed.org/version/1',
|
|
|
|
'title' => (!empty($extraInfos['name'])) ? $extraInfos['name'] : $urlHost,
|
|
|
|
'home_page_url' => (!empty($extraInfos['uri'])) ? $extraInfos['uri'] : REPOSITORY,
|
|
|
|
'feed_url' => $urlPrefix . $urlHost . $urlRequest
|
2019-01-05 13:20:11 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
if (!empty($extraInfos['icon'])) {
|
|
|
|
$data['icon'] = $extraInfos['icon'];
|
|
|
|
$data['favicon'] = $extraInfos['icon'];
|
2018-12-26 22:41:32 +01:00
|
|
|
}
|
|
|
|
|
2019-01-05 13:20:11 +01:00
|
|
|
$items = array();
|
|
|
|
foreach ($this->getItems() as $item) {
|
|
|
|
$entry = array();
|
|
|
|
|
2019-02-03 20:56:41 +01:00
|
|
|
$entryAuthor = $item->getAuthor();
|
|
|
|
$entryTitle = $item->getTitle();
|
|
|
|
$entryUri = $item->getURI();
|
|
|
|
$entryTimestamp = $item->getTimestamp();
|
|
|
|
$entryContent = $this->sanitizeHtml($item->getContent());
|
|
|
|
$entryEnclosures = $item->getEnclosures();
|
|
|
|
$entryCategories = $item->getCategories();
|
2019-01-05 13:20:11 +01:00
|
|
|
|
|
|
|
$vendorFields = $item->toArray();
|
|
|
|
foreach (self::VENDOR_EXCLUDES as $key) {
|
|
|
|
unset($vendorFields[$key]);
|
|
|
|
}
|
|
|
|
|
2019-02-03 20:56:41 +01:00
|
|
|
$entry['id'] = $item->getUid();
|
|
|
|
|
|
|
|
if (empty($entry['id'])) {
|
|
|
|
$entry['id'] = $entryUri;
|
|
|
|
}
|
2019-01-05 13:20:11 +01:00
|
|
|
|
|
|
|
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(
|
2019-02-03 20:56:41 +01:00
|
|
|
'url' => $enclosure,
|
|
|
|
'mime_type' => getMimeType($enclosure)
|
2019-01-05 13:20:11 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
|
2018-12-26 22:41:32 +01:00
|
|
|
$toReturn = json_encode($data, JSON_PRETTY_PRINT);
|
2016-11-07 20:49:44 +01:00
|
|
|
|
|
|
|
// Remove invalid non-UTF8 characters
|
|
|
|
ini_set('mbstring.substitute_character', 'none');
|
|
|
|
$toReturn = mb_convert_encoding($toReturn, $this->getCharset(), 'UTF-8');
|
|
|
|
return $toReturn;
|
2016-09-10 20:41:11 +02:00
|
|
|
}
|
2013-08-11 13:30:41 +02:00
|
|
|
|
2016-09-10 20:41:11 +02:00
|
|
|
public function display(){
|
|
|
|
$this
|
2019-10-31 19:00:12 +01:00
|
|
|
->setContentType(self::MIME_TYPE . '; charset=' . $this->getCharset())
|
2016-09-10 20:41:11 +02:00
|
|
|
->callContentType();
|
2013-08-11 13:30:41 +02:00
|
|
|
|
2016-09-10 20:41:11 +02:00
|
|
|
return parent::display();
|
|
|
|
}
|
2019-01-05 13:20:11 +01:00
|
|
|
|
|
|
|
private function isHTML($text) {
|
|
|
|
return (strlen(strip_tags($text)) != strlen($text));
|
|
|
|
}
|
2016-02-19 17:15:06 +01:00
|
|
|
}
|