2013-08-11 15:30:41 +04:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* All format logic
|
|
|
|
* Note : adapter are store in other place
|
|
|
|
*/
|
|
|
|
|
|
|
|
interface FormatInterface{
|
|
|
|
public function stringify();
|
|
|
|
public function display();
|
|
|
|
public function setDatas(array $bridge);
|
|
|
|
}
|
|
|
|
|
|
|
|
abstract class FormatAbstract implements FormatInterface{
|
|
|
|
const DEFAULT_CHARSET = 'UTF-8';
|
|
|
|
|
|
|
|
protected
|
|
|
|
$contentType,
|
|
|
|
$charset,
|
|
|
|
$datas,
|
|
|
|
$extraInfos
|
|
|
|
;
|
|
|
|
|
|
|
|
public function setCharset($charset){
|
|
|
|
$this->charset = $charset;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getCharset(){
|
|
|
|
$charset = $this->charset;
|
|
|
|
|
|
|
|
return is_null($charset) ? self::DEFAULT_CHARSET : $charset;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function setContentType($contentType){
|
|
|
|
$this->contentType = $contentType;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function callContentType(){
|
|
|
|
header('Content-Type: ' . $this->contentType);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function display(){
|
|
|
|
echo $this->stringify();
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setDatas(array $datas){
|
|
|
|
$this->datas = $datas;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getDatas(){
|
|
|
|
if( !is_array($this->datas) ){
|
|
|
|
throw new \LogicException('Feed the ' . get_class($this) . ' with "setDatas" method before !');
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->datas;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Define common informations can be required by formats and set default value for unknow values
|
|
|
|
* @param array $extraInfos array with know informations (there isn't merge !!!)
|
|
|
|
* @return this
|
|
|
|
*/
|
|
|
|
public function setExtraInfos(array $extraInfos = array()){
|
|
|
|
foreach(array('name', 'uri') as $infoName){
|
|
|
|
if( !isset($extraInfos[$infoName]) ){
|
|
|
|
$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(){
|
|
|
|
if( is_null($this->extraInfos) ){ // No extra info ?
|
|
|
|
$this->setExtraInfos(); // Define with default value
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->extraInfos;
|
|
|
|
}
|
2013-08-13 00:37:19 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
public function sanitizeHtml($html)
|
|
|
|
{
|
|
|
|
$html = str_replace('<script','<‌script',$html); // Disable scripts, but leave them visible.
|
|
|
|
$html = str_replace('<iframe','<‌iframe',$html);
|
|
|
|
$html = str_replace('<link','<‌link',$html);
|
|
|
|
// We leave alone object and embed so that videos can play in RSS readers.
|
|
|
|
return $html;
|
|
|
|
}
|
2013-08-11 15:30:41 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
class Format{
|
|
|
|
|
|
|
|
static protected $dirFormat;
|
|
|
|
|
|
|
|
public function __construct(){
|
|
|
|
throw new \LogicException('Please use ' . __CLASS__ . '::create for new object.');
|
|
|
|
}
|
|
|
|
|
|
|
|
static public function create($nameFormat){
|
2016-08-23 14:44:23 +03:00
|
|
|
if( !preg_match('@^[A-Z][a-zA-Z]*$@', $nameFormat)){
|
2013-08-11 15:30:41 +04:00
|
|
|
throw new \InvalidArgumentException('Name format must be at least one uppercase follow or not by alphabetic characters.');
|
|
|
|
}
|
|
|
|
|
2016-08-23 15:34:07 +03:00
|
|
|
$nameFormat=$nameFormat.'Format';
|
2013-08-11 15:30:41 +04:00
|
|
|
$pathFormat = self::getDir() . $nameFormat . '.php';
|
|
|
|
|
|
|
|
if( !file_exists($pathFormat) ){
|
|
|
|
throw new \Exception('The format you looking for does not exist.');
|
|
|
|
}
|
|
|
|
|
|
|
|
require_once $pathFormat;
|
|
|
|
|
|
|
|
return new $nameFormat();
|
|
|
|
}
|
|
|
|
|
|
|
|
static public function setDir($dirFormat){
|
|
|
|
if( !is_string($dirFormat) ){
|
|
|
|
throw new \InvalidArgumentException('Dir format must be a string.');
|
|
|
|
}
|
|
|
|
|
|
|
|
if( !file_exists($dirFormat) ){
|
|
|
|
throw new \Exception('Dir format does not exist.');
|
|
|
|
}
|
|
|
|
|
|
|
|
self::$dirFormat = $dirFormat;
|
|
|
|
}
|
|
|
|
|
|
|
|
static public function getDir(){
|
|
|
|
$dirFormat = self::$dirFormat;
|
|
|
|
|
|
|
|
if( is_null($dirFormat) ){
|
|
|
|
throw new \LogicException(__CLASS__ . ' class need to know format path !');
|
|
|
|
}
|
|
|
|
|
|
|
|
return $dirFormat;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Read format dir and catch informations about each format depending annotation
|
|
|
|
* @return array Informations about each format
|
|
|
|
*/
|
|
|
|
static public function searchInformation(){
|
|
|
|
$pathDirFormat = self::getDir();
|
|
|
|
|
|
|
|
$listFormat = array();
|
|
|
|
|
|
|
|
$searchCommonPattern = array('name');
|
|
|
|
|
|
|
|
$dirFiles = scandir($pathDirFormat);
|
|
|
|
if( $dirFiles !== false ){
|
2016-08-23 15:29:53 +03:00
|
|
|
foreach( $dirFiles as $fileName ){
|
2016-08-23 15:34:07 +03:00
|
|
|
if( preg_match('@^([^.]+)Format\.php$@U', $fileName, $out) ){ // Is PHP file ?
|
2016-08-23 15:29:53 +03:00
|
|
|
$listFormat[] = $out[1];
|
2013-08-11 15:30:41 +04:00
|
|
|
}
|
2016-08-23 15:29:53 +03:00
|
|
|
}
|
2013-08-11 15:30:41 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return $listFormat;
|
|
|
|
}
|
2016-08-23 15:29:53 +03:00
|
|
|
}
|