2016-09-05 19:05:19 +03:00
|
|
|
<?php
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2018-11-16 23:48:59 +03: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 21:41:11 +03:00
|
|
|
abstract class FormatAbstract implements FormatInterface
|
|
|
|
{
|
2018-11-16 23:48:59 +03:00
|
|
|
/** The default charset (UTF-8) */
|
2016-09-10 21:41:11 +03:00
|
|
|
const DEFAULT_CHARSET = 'UTF-8';
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2019-10-31 21:00:12 +03:00
|
|
|
/** MIME type of format output */
|
|
|
|
const MIME_TYPE = 'text/plain';
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2018-11-16 23:48:59 +03:00
|
|
|
/** @var string $charset The charset */
|
|
|
|
protected $charset;
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2018-11-16 23:48:59 +03:00
|
|
|
/** @var array $items The items */
|
|
|
|
protected $items;
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2018-11-16 23:48:59 +03:00
|
|
|
/**
|
|
|
|
* @var int $lastModified A timestamp to indicate the last modified time of
|
|
|
|
* the output data.
|
|
|
|
*/
|
|
|
|
protected $lastModified;
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2018-11-16 23:48:59 +03:00
|
|
|
/** @var array $extraInfos The extra infos */
|
|
|
|
protected $extraInfos;
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2019-10-31 21:00:12 +03:00
|
|
|
/** {@inheritdoc} */
|
|
|
|
public function getMimeType()
|
|
|
|
{
|
|
|
|
return static::MIME_TYPE;
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2018-11-16 23:48:59 +03:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*
|
|
|
|
* @param string $charset {@inheritdoc}
|
|
|
|
*/
|
2016-09-10 21:41:11 +03:00
|
|
|
public function setCharset($charset)
|
|
|
|
{
|
|
|
|
$this->charset = $charset;
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2016-09-10 21:41:11 +03:00
|
|
|
return $this;
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2018-11-16 23:48:59 +03:00
|
|
|
/** {@inheritdoc} */
|
2016-09-10 21:41:11 +03:00
|
|
|
public function getCharset()
|
|
|
|
{
|
|
|
|
$charset = $this->charset;
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2016-11-07 22:20:52 +03:00
|
|
|
return is_null($charset) ? static::DEFAULT_CHARSET : $charset;
|
2016-09-10 21:41:11 +03:00
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2018-11-16 23:48:59 +03:00
|
|
|
/**
|
|
|
|
* Set the last modified time
|
|
|
|
*
|
|
|
|
* @param int $lastModified The last modified time
|
|
|
|
* @return void
|
|
|
|
*/
|
2018-08-25 22:00:38 +03:00
|
|
|
public function setLastModified($lastModified)
|
|
|
|
{
|
|
|
|
$this->lastModified = $lastModified;
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2018-11-16 23:48:59 +03:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*
|
|
|
|
* @param array $items {@inheritdoc}
|
|
|
|
*/
|
2016-09-10 21:41:11 +03:00
|
|
|
public function setItems(array $items)
|
|
|
|
{
|
2018-12-27 00:41:32 +03:00
|
|
|
$this->items = $items;
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2016-09-10 21:41:11 +03:00
|
|
|
return $this;
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2018-11-16 23:48:59 +03:00
|
|
|
/** {@inheritdoc} */
|
2016-09-10 21:41:11 +03:00
|
|
|
public function getItems()
|
|
|
|
{
|
|
|
|
if (!is_array($this->items)) {
|
|
|
|
throw new \LogicException('Feed the ' . get_class($this) . ' with "setItems" method before !');
|
2022-07-01 16:10:30 +03:00
|
|
|
}
|
|
|
|
|
2016-09-10 21:41:11 +03:00
|
|
|
return $this->items;
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2016-09-10 21:41:11 +03:00
|
|
|
/**
|
2018-11-16 23:48:59 +03:00
|
|
|
* {@inheritdoc}
|
|
|
|
*
|
|
|
|
* @param array $extraInfos {@inheritdoc}
|
|
|
|
*/
|
2016-09-10 21:41:11 +03:00
|
|
|
public function setExtraInfos(array $extraInfos = [])
|
|
|
|
{
|
2021-06-25 22:45:25 +03:00
|
|
|
foreach (['name', 'uri', 'icon', 'donationUri'] as $infoName) {
|
2017-07-29 20:28:00 +03:00
|
|
|
if (!isset($extraInfos[$infoName])) {
|
2016-09-10 21:41:11 +03:00
|
|
|
$extraInfos[$infoName] = '';
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
}
|
|
|
|
|
2016-09-10 21:41:11 +03:00
|
|
|
$this->extraInfos = $extraInfos;
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2016-09-10 21:41:11 +03:00
|
|
|
return $this;
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2018-11-16 23:48:59 +03:00
|
|
|
/** {@inheritdoc} */
|
2016-09-10 21:41:11 +03:00
|
|
|
public function getExtraInfos()
|
|
|
|
{
|
2017-07-29 20:28:00 +03:00
|
|
|
if (is_null($this->extraInfos)) { // No extra info ?
|
2016-09-10 21:41:11 +03:00
|
|
|
$this->setExtraInfos(); // Define with default value
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2016-09-10 21:41:11 +03:00
|
|
|
return $this->extraInfos;
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2016-09-10 21:41:11 +03:00
|
|
|
/**
|
2018-11-16 23:48:59 +03: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 21:41:11 +03:00
|
|
|
*/
|
2022-06-17 21:46:15 +03:00
|
|
|
protected function sanitizeHtml(string $html): string
|
2016-09-10 21:41:11 +03:00
|
|
|
{
|
2017-02-14 19:28:07 +03: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 21:41:11 +03:00
|
|
|
// We leave alone object and embed so that videos can play in RSS readers.
|
|
|
|
return $html;
|
|
|
|
}
|
2016-09-05 19:05:19 +03:00
|
|
|
}
|