FormatAbstract.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. require_once(__DIR__ . '/FormatInterface.php');
  3. abstract class FormatAbstract implements FormatInterface {
  4. const DEFAULT_CHARSET = 'UTF-8';
  5. protected
  6. $contentType,
  7. $charset,
  8. $items,
  9. $extraInfos
  10. ;
  11. public function setCharset($charset){
  12. $this->charset = $charset;
  13. return $this;
  14. }
  15. public function getCharset(){
  16. $charset = $this->charset;
  17. return is_null($charset) ? self::DEFAULT_CHARSET : $charset;
  18. }
  19. protected function setContentType($contentType){
  20. $this->contentType = $contentType;
  21. return $this;
  22. }
  23. protected function callContentType(){
  24. header('Content-Type: ' . $this->contentType);
  25. }
  26. public function display(){
  27. echo $this->stringify();
  28. return $this;
  29. }
  30. public function setItems(array $items){
  31. $this->items = array_map(array($this, 'array_trim'), $items);
  32. return $this;
  33. }
  34. public function getItems(){
  35. if(!is_array($this->items))
  36. throw new \LogicException('Feed the ' . get_class($this) . ' with "setItems" method before !');
  37. return $this->items;
  38. }
  39. /**
  40. * Define common informations can be required by formats and set default value for unknow values
  41. * @param array $extraInfos array with know informations (there isn't merge !!!)
  42. * @return this
  43. */
  44. public function setExtraInfos(array $extraInfos = array()){
  45. foreach(array('name', 'uri') as $infoName){
  46. if( !isset($extraInfos[$infoName]) ){
  47. $extraInfos[$infoName] = '';
  48. }
  49. }
  50. $this->extraInfos = $extraInfos;
  51. return $this;
  52. }
  53. /**
  54. * Return extra infos
  55. * @return array See "setExtraInfos" detail method to know what extra are disponibles
  56. */
  57. public function getExtraInfos(){
  58. if( is_null($this->extraInfos) ){ // No extra info ?
  59. $this->setExtraInfos(); // Define with default value
  60. }
  61. return $this->extraInfos;
  62. }
  63. /**
  64. * Sanitized html while leaving it functionnal.
  65. * The aim is to keep html as-is (with clickable hyperlinks)
  66. * while reducing annoying and potentially dangerous things.
  67. * Yes, I know sanitizing HTML 100% is an impossible task.
  68. * Maybe we'll switch to http://htmlpurifier.org/
  69. * or http://www.bioinformatics.org/phplabware/internal_utilities/htmLawed/index.php
  70. */
  71. protected function sanitizeHtml($html)
  72. {
  73. $html = str_replace('<script','<&zwnj;script',$html); // Disable scripts, but leave them visible.
  74. $html = str_replace('<iframe','<&zwnj;iframe',$html);
  75. $html = str_replace('<link','<&zwnj;link',$html);
  76. // We leave alone object and embed so that videos can play in RSS readers.
  77. return $html;
  78. }
  79. protected function array_trim($elements){
  80. foreach($elements as $key => $value){
  81. if(is_string($value))
  82. $elements[$key] = trim($value);
  83. }
  84. return $elements;
  85. }
  86. }