View.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 = array();
  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. $file = substr($template, 0, 10) === 'bootstrap-' ? 'bootstrap' : $template;
  49. $path = $dir . $file . '.php';
  50. if (!is_file($path)) {
  51. throw new Exception('Template ' . $template . ' not found in file ' . $path . '!', 80);
  52. }
  53. if (!in_array($path, glob($dir . '*.php', GLOB_NOSORT | GLOB_ERR), true)) {
  54. throw new Exception('Template ' . $file . '.php not found in ' . $dir . '!', 81);
  55. }
  56. extract($this->_variables);
  57. include $path;
  58. }
  59. /**
  60. * get cache buster query string
  61. *
  62. * if the file isn't versioned (ends in a digit), adds our own version as a query string
  63. *
  64. * @access private
  65. * @param string $file
  66. */
  67. private function _getCacheBuster($file)
  68. {
  69. //
  70. if ((bool) preg_match('#[0-9]\.m?js$#', (string) $file)) {
  71. return '';
  72. }
  73. return '?' . rawurlencode($this->_variables['VERSION']);
  74. }
  75. /**
  76. * get SRI hash for given file
  77. *
  78. * @access private
  79. * @param string $file
  80. */
  81. private function _getSri($file)
  82. {
  83. if (array_key_exists($file, $this->_variables['SRI'])) {
  84. return ' integrity="' . $this->_variables['SRI'][$file] . '"';
  85. }
  86. return '';
  87. }
  88. /**
  89. * echo module preload link tag incl. SRI hash for given script file
  90. *
  91. * @access private
  92. * @param string $file
  93. */
  94. private function _linkTag($file)
  95. {
  96. echo '<link rel="modulepreload" href="', $file,
  97. $this->_getCacheBuster($file), '"', $this->_getSri($file), ' />', PHP_EOL;
  98. }
  99. /**
  100. * echo script tag incl. SRI hash for given script file
  101. *
  102. * @access private
  103. * @param string $file
  104. * @param string $attributes additional attributes to add into the script tag
  105. */
  106. private function _scriptTag($file, $attributes = '')
  107. {
  108. echo '<script ', $attributes,
  109. ' type="text/javascript" data-cfasync="false" src="', $file,
  110. $this->_getCacheBuster($file), '"', $this->_getSri($file),
  111. ' crossorigin="anonymous"></script>', PHP_EOL;
  112. }
  113. }