QRMarkup.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. /**
  3. * Class QRMarkup
  4. *
  5. * @created 17.12.2016
  6. * @author Smiley <smiley@chillerlan.net>
  7. * @copyright 2016 Smiley
  8. * @license MIT
  9. */
  10. namespace chillerlan\QRCode\Output;
  11. use function is_string, preg_match, strip_tags, trim;
  12. /**
  13. * Abstract for markup types: HTML, SVG, ... XML anyone?
  14. */
  15. abstract class QRMarkup extends QROutputAbstract{
  16. /**
  17. * note: we're not necessarily validating the several values, just checking the general syntax
  18. * note: css4 colors are not included
  19. *
  20. * @todo: XSS proof
  21. *
  22. * @see https://developer.mozilla.org/en-US/docs/Web/CSS/color_value
  23. * @inheritDoc
  24. */
  25. public static function moduleValueIsValid($value):bool{
  26. if(!is_string($value)){
  27. return false;
  28. }
  29. $value = trim(strip_tags($value), " '\"\r\n\t");
  30. // hex notation
  31. // #rgb(a)
  32. // #rrggbb(aa)
  33. if(preg_match('/^#([\da-f]{3}){1,2}$|^#([\da-f]{4}){1,2}$/i', $value)){
  34. return true;
  35. }
  36. // css: hsla/rgba(...values)
  37. if(preg_match('#^(hsla?|rgba?)\([\d .,%/]+\)$#i', $value)){
  38. return true;
  39. }
  40. // predefined css color
  41. if(preg_match('/^[a-z]+$/i', $value)){
  42. return true;
  43. }
  44. return false;
  45. }
  46. /**
  47. * @inheritDoc
  48. */
  49. protected function getModuleValue($value):string{
  50. return trim(strip_tags($value), " '\"\r\n\t");
  51. }
  52. /**
  53. * @inheritDoc
  54. */
  55. protected function getDefaultModuleValue(bool $isDark):string{
  56. return ($isDark) ? $this->options->markupDark : $this->options->markupLight;
  57. }
  58. /**
  59. * @inheritDoc
  60. */
  61. public function dump(string $file = null):string{
  62. $data = $this->createMarkup($file !== null);
  63. $this->saveToFile($data, $file);
  64. return $data;
  65. }
  66. /**
  67. * returns a string with all css classes for the current element
  68. */
  69. abstract protected function getCssClass(int $M_TYPE):string;
  70. /**
  71. *
  72. */
  73. abstract protected function createMarkup(bool $saveToFile):string;
  74. }