2016-09-05 18:05:19 +02:00
|
|
|
<?php
|
2018-11-16 21:48:59 +01:00
|
|
|
/**
|
|
|
|
* This file is part of RSS-Bridge, a PHP project capable of generating RSS and
|
|
|
|
* Atom feeds for websites that don't have one.
|
|
|
|
*
|
|
|
|
* For the full license information, please view the UNLICENSE file distributed
|
|
|
|
* with this source code.
|
|
|
|
*
|
|
|
|
* @package Core
|
|
|
|
* @license https://unlicense.org/ UNLICENSE
|
|
|
|
* @link https://github.com/rss-bridge/rss-bridge
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* An abstract class for format implementations
|
|
|
|
*
|
|
|
|
* This class implements {@see FormatInterface}
|
|
|
|
*/
|
2016-09-10 20:41:11 +02:00
|
|
|
abstract class FormatAbstract implements FormatInterface {
|
2018-11-16 21:48:59 +01:00
|
|
|
|
|
|
|
/** The default charset (UTF-8) */
|
2016-09-10 20:41:11 +02:00
|
|
|
const DEFAULT_CHARSET = 'UTF-8';
|
|
|
|
|
2018-11-18 16:30:34 +01:00
|
|
|
/** @var string|null $contentType The content type */
|
|
|
|
protected $contentType = null;
|
2018-11-16 21:48:59 +01:00
|
|
|
|
|
|
|
/** @var string $charset The charset */
|
|
|
|
protected $charset;
|
2016-09-10 20:41:11 +02:00
|
|
|
|
2018-11-16 21:48:59 +01:00
|
|
|
/** @var array $items The items */
|
|
|
|
protected $items;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var int $lastModified A timestamp to indicate the last modified time of
|
|
|
|
* the output data.
|
|
|
|
*/
|
|
|
|
protected $lastModified;
|
|
|
|
|
|
|
|
/** @var array $extraInfos The extra infos */
|
|
|
|
protected $extraInfos;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*
|
|
|
|
* @param string $charset {@inheritdoc}
|
|
|
|
*/
|
2016-09-10 20:41:11 +02:00
|
|
|
public function setCharset($charset){
|
|
|
|
$this->charset = $charset;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2018-11-16 21:48:59 +01:00
|
|
|
/** {@inheritdoc} */
|
2016-09-10 20:41:11 +02:00
|
|
|
public function getCharset(){
|
|
|
|
$charset = $this->charset;
|
|
|
|
|
2016-11-07 20:20:52 +01:00
|
|
|
return is_null($charset) ? static::DEFAULT_CHARSET : $charset;
|
2016-09-10 20:41:11 +02:00
|
|
|
}
|
|
|
|
|
2018-11-16 21:48:59 +01:00
|
|
|
/**
|
|
|
|
* Set the content type
|
|
|
|
*
|
|
|
|
* @param string $contentType The content type
|
|
|
|
* @return self The format object
|
|
|
|
*/
|
2016-09-10 20:41:11 +02:00
|
|
|
protected function setContentType($contentType){
|
|
|
|
$this->contentType = $contentType;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2018-11-16 21:48:59 +01:00
|
|
|
/**
|
|
|
|
* Set the last modified time
|
|
|
|
*
|
|
|
|
* @param int $lastModified The last modified time
|
|
|
|
* @return void
|
|
|
|
*/
|
2018-08-25 21:00:38 +02:00
|
|
|
public function setLastModified($lastModified){
|
|
|
|
$this->lastModified = $lastModified;
|
|
|
|
}
|
|
|
|
|
2018-11-16 21:48:59 +01:00
|
|
|
/**
|
|
|
|
* Send header with the currently specified content type
|
|
|
|
*
|
2018-11-18 16:30:34 +01:00
|
|
|
* @throws \LogicException if the content type is not set
|
|
|
|
* @throws \LogicException if the content type is not a string
|
|
|
|
*
|
2018-11-16 21:48:59 +01:00
|
|
|
* @return void
|
|
|
|
*/
|
2016-09-10 20:41:11 +02:00
|
|
|
protected function callContentType(){
|
2018-11-18 16:30:34 +01:00
|
|
|
if(empty($this->contentType))
|
|
|
|
throw new \LogicException('Content-Type is not set!');
|
|
|
|
|
|
|
|
if(!is_string($this->contentType))
|
|
|
|
throw new \LogicException('Content-Type must be a string!');
|
|
|
|
|
2016-09-10 20:41:11 +02:00
|
|
|
header('Content-Type: ' . $this->contentType);
|
|
|
|
}
|
|
|
|
|
2018-11-16 21:48:59 +01:00
|
|
|
/** {@inheritdoc} */
|
2016-09-10 20:41:11 +02:00
|
|
|
public function display(){
|
2018-08-25 21:00:38 +02:00
|
|
|
if ($this->lastModified) {
|
|
|
|
header('Last-Modified: ' . gmdate('D, d M Y H:i:s ', $this->lastModified) . 'GMT');
|
|
|
|
}
|
2016-09-10 20:41:11 +02:00
|
|
|
echo $this->stringify();
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2018-11-16 21:48:59 +01:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*
|
|
|
|
* @param array $items {@inheritdoc}
|
|
|
|
*/
|
2016-09-10 20:41:11 +02:00
|
|
|
public function setItems(array $items){
|
|
|
|
$this->items = array_map(array($this, 'array_trim'), $items);
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2018-11-16 21:48:59 +01:00
|
|
|
/** {@inheritdoc} */
|
2016-09-10 20:41:11 +02:00
|
|
|
public function getItems(){
|
|
|
|
if(!is_array($this->items))
|
|
|
|
throw new \LogicException('Feed the ' . get_class($this) . ' with "setItems" method before !');
|
|
|
|
|
|
|
|
return $this->items;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-11-16 21:48:59 +01:00
|
|
|
* {@inheritdoc}
|
|
|
|
*
|
|
|
|
* @param array $extraInfos {@inheritdoc}
|
|
|
|
*/
|
2016-09-10 20:41:11 +02:00
|
|
|
public function setExtraInfos(array $extraInfos = array()){
|
2018-08-21 17:46:47 +02:00
|
|
|
foreach(array('name', 'uri', 'icon') as $infoName) {
|
2017-07-29 19:28:00 +02:00
|
|
|
if(!isset($extraInfos[$infoName])) {
|
2016-09-10 20:41:11 +02:00
|
|
|
$extraInfos[$infoName] = '';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->extraInfos = $extraInfos;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2018-11-16 21:48:59 +01:00
|
|
|
/** {@inheritdoc} */
|
2016-09-10 20:41:11 +02:00
|
|
|
public function getExtraInfos(){
|
2017-07-29 19:28:00 +02:00
|
|
|
if(is_null($this->extraInfos)) { // No extra info ?
|
2016-09-10 20:41:11 +02:00
|
|
|
$this->setExtraInfos(); // Define with default value
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->extraInfos;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-11-16 21:48:59 +01:00
|
|
|
* Sanitize HTML while leaving it functional.
|
|
|
|
*
|
|
|
|
* Keeps HTML as-is (with clickable hyperlinks) while reducing annoying and
|
|
|
|
* potentially dangerous things.
|
|
|
|
*
|
|
|
|
* @param string $html The HTML content
|
|
|
|
* @return string The sanitized HTML content
|
|
|
|
*
|
|
|
|
* @todo This belongs into `html.php`
|
|
|
|
* @todo Maybe switch to http://htmlpurifier.org/
|
|
|
|
* @todo Maybe switch to http://www.bioinformatics.org/phplabware/internal_utilities/htmLawed/index.php
|
2016-09-10 20:41:11 +02:00
|
|
|
*/
|
|
|
|
protected function sanitizeHtml($html)
|
|
|
|
{
|
2017-02-14 17:28:07 +01:00
|
|
|
$html = str_replace('<script', '<‌script', $html); // Disable scripts, but leave them visible.
|
|
|
|
$html = str_replace('<iframe', '<‌iframe', $html);
|
|
|
|
$html = str_replace('<link', '<‌link', $html);
|
2016-09-10 20:41:11 +02:00
|
|
|
// We leave alone object and embed so that videos can play in RSS readers.
|
|
|
|
return $html;
|
|
|
|
}
|
|
|
|
|
2018-11-16 21:48:59 +01:00
|
|
|
/**
|
|
|
|
* Trim each element of an array
|
|
|
|
*
|
|
|
|
* This function applies `trim()` to all elements in the array, if the element
|
|
|
|
* is a valid string.
|
|
|
|
*
|
|
|
|
* @param array $elements The array to trim
|
|
|
|
* @return array The trimmed array
|
|
|
|
*
|
|
|
|
* @todo This is a utility function that doesn't belong here, find a new home.
|
|
|
|
*/
|
2016-09-10 20:41:11 +02:00
|
|
|
protected function array_trim($elements){
|
2017-07-29 19:28:00 +02:00
|
|
|
foreach($elements as $key => $value) {
|
2016-09-10 20:41:11 +02:00
|
|
|
if(is_string($value))
|
|
|
|
$elements[$key] = trim($value);
|
|
|
|
}
|
|
|
|
return $elements;
|
|
|
|
}
|
2016-09-05 18:05:19 +02:00
|
|
|
}
|