FormatAbstract.php 2.6 KB

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