View.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php declare(strict_types=1);
  2. /**
  3. * PrivateBin
  4. *
  5. * a zero-knowledge paste bin
  6. *
  7. * @link https://github.com/PrivateBin/PrivateBin
  8. * @copyright 2012 Sébastien SAUVAGE (sebsauvage.net)
  9. * @license https://www.opensource.org/licenses/zlib-license.php The zlib/libpng License
  10. */
  11. namespace PrivateBin;
  12. use Exception;
  13. /**
  14. * View
  15. *
  16. * Displays the templates
  17. */
  18. class View
  19. {
  20. /**
  21. * variables available in the template
  22. *
  23. * @access private
  24. * @var array
  25. */
  26. private $_variables = [];
  27. /**
  28. * assign variables to be used inside of the template
  29. *
  30. * @access public
  31. * @param string $name
  32. * @param mixed $value
  33. */
  34. public function assign($name, $value)
  35. {
  36. $this->_variables[$name] = $value;
  37. }
  38. /**
  39. * render a template
  40. *
  41. * @access public
  42. * @param string $template
  43. * @throws Exception
  44. */
  45. public function draw($template)
  46. {
  47. $dir = PATH . 'tpl' . DIRECTORY_SEPARATOR;
  48. $path = $dir . $template . '.php';
  49. if (!is_file($path)) {
  50. throw new Exception('Template ' . $template . ' not found in file ' . $path . '!', 80);
  51. }
  52. if (!in_array($path, glob($dir . '*.php', GLOB_NOSORT | GLOB_ERR), true)) {
  53. throw new Exception('Template ' . $template . '.php not found in ' . $dir . '!', 81);
  54. }
  55. extract($this->_variables);
  56. include $path;
  57. }
  58. /**
  59. * get cache buster query string
  60. *
  61. * if the file isn't versioned (ends in a digit), adds our own version as a query string
  62. *
  63. * @access private
  64. * @param string $file
  65. */
  66. private function _getCacheBuster($file)
  67. {
  68. if ((bool) preg_match('#[0-9]\.m?js$#', (string) $file)) {
  69. return '';
  70. }
  71. return '?' . rawurlencode($this->_variables['VERSION']);
  72. }
  73. /**
  74. * get SRI hash for given file
  75. *
  76. * @access private
  77. * @param string $file
  78. */
  79. private function _getSri($file)
  80. {
  81. if (array_key_exists($file, $this->_variables['SRI'])) {
  82. return ' integrity="' . $this->_variables['SRI'][$file] . '"';
  83. }
  84. return '';
  85. }
  86. /**
  87. * echo module preload link tag incl. SRI hash for given script file
  88. *
  89. * @access private
  90. * @param string $file
  91. */
  92. private function _linkTag($file)
  93. {
  94. echo '<link rel="modulepreload" href="', $file,
  95. $this->_getCacheBuster($file), '"', $this->_getSri($file), ' />', PHP_EOL;
  96. }
  97. /**
  98. * echo script tag incl. SRI hash for given script file
  99. *
  100. * @access private
  101. * @param string $file
  102. * @param string $attributes additional attributes to add into the script tag
  103. */
  104. private function _scriptTag($file, $attributes = '')
  105. {
  106. echo '<script ', $attributes,
  107. ' type="text/javascript" data-cfasync="false" src="', $file,
  108. $this->_getCacheBuster($file), '"', $this->_getSri($file),
  109. ' crossorigin="anonymous"></script>', PHP_EOL;
  110. }
  111. }