Format.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <?php
  2. /**
  3. * All format logic
  4. * Note : adapter are store in other place
  5. */
  6. interface FormatInterface{
  7. public function stringify();
  8. public function display();
  9. public function setDatas(array $bridge);
  10. }
  11. abstract class FormatAbstract implements FormatInterface{
  12. const DEFAULT_CHARSET = 'UTF-8';
  13. protected
  14. $contentType,
  15. $charset,
  16. $datas,
  17. $extraInfos
  18. ;
  19. public function setCharset($charset){
  20. $this->charset = $charset;
  21. return $this;
  22. }
  23. public function getCharset(){
  24. $charset = $this->charset;
  25. return is_null($charset) ? self::DEFAULT_CHARSET : $charset;
  26. }
  27. protected function setContentType($contentType){
  28. $this->contentType = $contentType;
  29. return $this;
  30. }
  31. protected function callContentType(){
  32. header('Content-Type: ' . $this->contentType);
  33. }
  34. public function display(){
  35. echo $this->stringify();
  36. return $this;
  37. }
  38. public function setDatas(array $datas){
  39. $this->datas = $datas;
  40. return $this;
  41. }
  42. public function getDatas(){
  43. if( !is_array($this->datas) ){
  44. throw new \LogicException('Feed the ' . get_class($this) . ' with "setDatas" method before !');
  45. }
  46. return $this->datas;
  47. }
  48. /**
  49. * Define common informations can be required by formats and set default value for unknow values
  50. * @param array $extraInfos array with know informations (there isn't merge !!!)
  51. * @return this
  52. */
  53. public function setExtraInfos(array $extraInfos = array()){
  54. foreach(array('name', 'uri') as $infoName){
  55. if( !isset($extraInfos[$infoName]) ){
  56. $extraInfos[$infoName] = '';
  57. }
  58. }
  59. $this->extraInfos = $extraInfos;
  60. return $this;
  61. }
  62. /**
  63. * Return extra infos
  64. * @return array See "setExtraInfos" detail method to know what extra are disponibles
  65. */
  66. public function getExtraInfos(){
  67. if( is_null($this->extraInfos) ){ // No extra info ?
  68. $this->setExtraInfos(); // Define with default value
  69. }
  70. return $this->extraInfos;
  71. }
  72. /**
  73. * Sanitized html while leaving it functionnal.
  74. * The aim is to keep html as-is (with clickable hyperlinks)
  75. * while reducing annoying and potentially dangerous things.
  76. * Yes, I know sanitizing HTML 100% is an impossible task.
  77. * Maybe we'll switch to http://htmlpurifier.org/
  78. * or http://www.bioinformatics.org/phplabware/internal_utilities/htmLawed/index.php
  79. */
  80. public function sanitizeHtml($html)
  81. {
  82. $html = str_replace('<script','<&zwnj;script',$html); // Disable scripts, but leave them visible.
  83. $html = str_replace('<iframe','<&zwnj;iframe',$html);
  84. $html = str_replace('<link','<&zwnj;link',$html);
  85. // We leave alone object and embed so that videos can play in RSS readers.
  86. return $html;
  87. }
  88. }
  89. class Format{
  90. static protected $dirFormat;
  91. public function __construct(){
  92. throw new \LogicException('Please use ' . __CLASS__ . '::create for new object.');
  93. }
  94. static public function create($nameFormat){
  95. if( !static::isValidNameFormat($nameFormat) ){
  96. throw new \InvalidArgumentException('Name format must be at least one uppercase follow or not by alphabetic characters.');
  97. }
  98. $pathFormat = self::getDir() . $nameFormat . '.php';
  99. if( !file_exists($pathFormat) ){
  100. throw new \Exception('The format you looking for does not exist.');
  101. }
  102. require_once $pathFormat;
  103. return new $nameFormat();
  104. }
  105. static public function setDir($dirFormat){
  106. if( !is_string($dirFormat) ){
  107. throw new \InvalidArgumentException('Dir format must be a string.');
  108. }
  109. if( !file_exists($dirFormat) ){
  110. throw new \Exception('Dir format does not exist.');
  111. }
  112. self::$dirFormat = $dirFormat;
  113. }
  114. static public function getDir(){
  115. $dirFormat = self::$dirFormat;
  116. if( is_null($dirFormat) ){
  117. throw new \LogicException(__CLASS__ . ' class need to know format path !');
  118. }
  119. return $dirFormat;
  120. }
  121. static public function isValidNameFormat($nameFormat){
  122. return preg_match('@^[A-Z][a-zA-Z]*$@', $nameFormat);
  123. }
  124. /**
  125. * Read format dir and catch informations about each format depending annotation
  126. * @return array Informations about each format
  127. */
  128. static public function searchInformation(){
  129. $pathDirFormat = self::getDir();
  130. $listFormat = array();
  131. $searchCommonPattern = array('name');
  132. $dirFiles = scandir($pathDirFormat);
  133. if( $dirFiles !== false ){
  134. foreach( $dirFiles as $fileName ){
  135. if( preg_match('@([^.]+)\.php@U', $fileName, $out) ){ // Is PHP file ?
  136. $infos = array(); // Information about the bridge
  137. $resParse = token_get_all(file_get_contents($pathDirFormat . $fileName)); // Parse PHP file
  138. foreach($resParse as $v){
  139. if( is_array($v) && $v[0] == T_DOC_COMMENT ){ // Lexer node is COMMENT ?
  140. $commentary = $v[1];
  141. foreach( $searchCommonPattern as $name){ // Catch information with common pattern
  142. preg_match('#@' . preg_quote($name, '#') . '\s+(.+)#', $commentary, $outComment);
  143. if( isset($outComment[1]) ){
  144. $infos[$name] = $outComment[1];
  145. }
  146. }
  147. }
  148. }
  149. if( isset($infos['name']) ){ // If informations containt at least a name
  150. $listFormat[$out[1]] = $infos;
  151. }
  152. }
  153. }
  154. }
  155. return $listFormat;
  156. }
  157. }