2013-08-11 15:30:41 +04:00
|
|
|
<?php
|
|
|
|
/**
|
2019-01-05 15:20:11 +03: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 21:41:11 +03:00
|
|
|
class JsonFormat extends FormatAbstract {
|
2019-10-31 21:00:12 +03:00
|
|
|
const MIME_TYPE = 'application/json';
|
|
|
|
|
2019-01-05 15:20:11 +03:00
|
|
|
const VENDOR_EXCLUDES = array(
|
|
|
|
'author',
|
|
|
|
'title',
|
|
|
|
'uri',
|
|
|
|
'timestamp',
|
|
|
|
'content',
|
|
|
|
'enclosures',
|
|
|
|
'categories',
|
2019-02-03 22:56:41 +03:00
|
|
|
'uid',
|
2019-01-05 15:20:11 +03:00
|
|
|
);
|
|
|
|
|
2016-09-10 21:41:11 +03:00
|
|
|
public function stringify(){
|
2019-02-03 22:56:41 +03: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 15:20:11 +03:00
|
|
|
|
|
|
|
$extraInfos = $this->getExtraInfos();
|
2018-12-27 00:41:32 +03:00
|
|
|
|
2019-01-05 15:20:11 +03:00
|
|
|
$data = array(
|
2019-02-03 22:56:41 +03: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 15:20:11 +03:00
|
|
|
);
|
|
|
|
|
|
|
|
if (!empty($extraInfos['icon'])) {
|
|
|
|
$data['icon'] = $extraInfos['icon'];
|
|
|
|
$data['favicon'] = $extraInfos['icon'];
|
2018-12-27 00:41:32 +03:00
|
|
|
}
|
|
|
|
|
2019-01-05 15:20:11 +03:00
|
|
|
$items = array();
|
|
|
|
foreach ($this->getItems() as $item) {
|
|
|
|
$entry = array();
|
|
|
|
|
2019-02-03 22:56:41 +03: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 15:20:11 +03:00
|
|
|
|
|
|
|
$vendorFields = $item->toArray();
|
|
|
|
foreach (self::VENDOR_EXCLUDES as $key) {
|
|
|
|
unset($vendorFields[$key]);
|
|
|
|
}
|
|
|
|
|
2019-02-03 22:56:41 +03:00
|
|
|
$entry['id'] = $item->getUid();
|
|
|
|
|
|
|
|
if (empty($entry['id'])) {
|
|
|
|
$entry['id'] = $entryUri;
|
|
|
|
}
|
2019-01-05 15:20:11 +03: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 22:56:41 +03:00
|
|
|
'url' => $enclosure,
|
|
|
|
'mime_type' => getMimeType($enclosure)
|
2019-01-05 15:20:11 +03: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-27 00:41:32 +03:00
|
|
|
$toReturn = json_encode($data, JSON_PRETTY_PRINT);
|
2016-11-07 22:49:44 +03: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 21:41:11 +03:00
|
|
|
}
|
2013-08-11 15:30:41 +04:00
|
|
|
|
2016-09-10 21:41:11 +03:00
|
|
|
public function display(){
|
|
|
|
$this
|
2019-10-31 21:00:12 +03:00
|
|
|
->setContentType(self::MIME_TYPE . '; charset=' . $this->getCharset())
|
2016-09-10 21:41:11 +03:00
|
|
|
->callContentType();
|
2013-08-11 15:30:41 +04:00
|
|
|
|
2016-09-10 21:41:11 +03:00
|
|
|
return parent::display();
|
|
|
|
}
|
2019-01-05 15:20:11 +03:00
|
|
|
|
|
|
|
private function isHTML($text) {
|
|
|
|
return (strlen(strip_tags($text)) != strlen($text));
|
|
|
|
}
|
2016-02-19 19:15:06 +03:00
|
|
|
}
|