2016-09-05 19:05:19 +03:00
|
|
|
<?php
|
2018-11-06 21:23:32 +03:00
|
|
|
|
2016-09-10 21:41:11 +03:00
|
|
|
abstract class FormatAbstract implements FormatInterface {
|
|
|
|
const DEFAULT_CHARSET = 'UTF-8';
|
|
|
|
|
|
|
|
protected
|
|
|
|
$contentType,
|
|
|
|
$charset,
|
|
|
|
$items,
|
2018-08-25 22:00:38 +03:00
|
|
|
$lastModified,
|
2016-09-10 22:01:02 +03:00
|
|
|
$extraInfos;
|
2016-09-10 21:41:11 +03:00
|
|
|
|
|
|
|
public function setCharset($charset){
|
|
|
|
$this->charset = $charset;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getCharset(){
|
|
|
|
$charset = $this->charset;
|
|
|
|
|
2016-11-07 22:20:52 +03:00
|
|
|
return is_null($charset) ? static::DEFAULT_CHARSET : $charset;
|
2016-09-10 21:41:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function setContentType($contentType){
|
|
|
|
$this->contentType = $contentType;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2018-08-25 22:00:38 +03:00
|
|
|
public function setLastModified($lastModified){
|
|
|
|
$this->lastModified = $lastModified;
|
|
|
|
}
|
|
|
|
|
2016-09-10 21:41:11 +03:00
|
|
|
protected function callContentType(){
|
|
|
|
header('Content-Type: ' . $this->contentType);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function display(){
|
2018-08-25 22:00:38 +03:00
|
|
|
if ($this->lastModified) {
|
|
|
|
header('Last-Modified: ' . gmdate('D, d M Y H:i:s ', $this->lastModified) . 'GMT');
|
|
|
|
}
|
2016-09-10 21:41:11 +03:00
|
|
|
echo $this->stringify();
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setItems(array $items){
|
|
|
|
$this->items = array_map(array($this, 'array_trim'), $items);
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getItems(){
|
|
|
|
if(!is_array($this->items))
|
|
|
|
throw new \LogicException('Feed the ' . get_class($this) . ' with "setItems" method before !');
|
|
|
|
|
|
|
|
return $this->items;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-08-21 18:46:47 +03:00
|
|
|
* Define common informations can be required by formats and set default value for unknown values
|
2016-09-10 21:41:11 +03:00
|
|
|
* @param array $extraInfos array with know informations (there isn't merge !!!)
|
|
|
|
* @return this
|
|
|
|
*/
|
|
|
|
public function setExtraInfos(array $extraInfos = array()){
|
2018-08-21 18:46:47 +03:00
|
|
|
foreach(array('name', 'uri', 'icon') as $infoName) {
|
2017-07-29 20:28:00 +03:00
|
|
|
if(!isset($extraInfos[$infoName])) {
|
2016-09-10 21:41:11 +03:00
|
|
|
$extraInfos[$infoName] = '';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->extraInfos = $extraInfos;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return extra infos
|
|
|
|
* @return array See "setExtraInfos" detail method to know what extra are disponibles
|
|
|
|
*/
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->extraInfos;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sanitized html while leaving it functionnal.
|
|
|
|
* The aim is to keep html as-is (with clickable hyperlinks)
|
|
|
|
* while reducing annoying and potentially dangerous things.
|
|
|
|
* Yes, I know sanitizing HTML 100% is an impossible task.
|
|
|
|
* Maybe we'll switch to http://htmlpurifier.org/
|
|
|
|
* or http://www.bioinformatics.org/phplabware/internal_utilities/htmLawed/index.php
|
|
|
|
*/
|
|
|
|
protected function sanitizeHtml($html)
|
|
|
|
{
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function array_trim($elements){
|
2017-07-29 20:28:00 +03:00
|
|
|
foreach($elements as $key => $value) {
|
2016-09-10 21:41:11 +03:00
|
|
|
if(is_string($value))
|
|
|
|
$elements[$key] = trim($value);
|
|
|
|
}
|
|
|
|
return $elements;
|
|
|
|
}
|
2016-09-05 19:05:19 +03:00
|
|
|
}
|