1
0

View.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. use GlobIterator;
  14. /**
  15. * View
  16. *
  17. * Displays the templates
  18. */
  19. class View
  20. {
  21. /**
  22. * variables available in the template
  23. *
  24. * @access private
  25. * @var array
  26. */
  27. private $_variables = array();
  28. /**
  29. * assign variables to be used inside of the template
  30. *
  31. * @access public
  32. * @param string $name
  33. * @param mixed $value
  34. */
  35. public function assign($name, $value)
  36. {
  37. $this->_variables[$name] = $value;
  38. }
  39. /**
  40. * render a template
  41. *
  42. * @access public
  43. * @param string $template
  44. * @throws Exception
  45. */
  46. public function draw($template)
  47. {
  48. $dir = PATH . 'tpl' . DIRECTORY_SEPARATOR;
  49. $file = substr($template, 0, 10) === 'bootstrap-' ? 'bootstrap' : $template;
  50. $path = realpath($dir . $file . '.php');
  51. if ($path === false) {
  52. throw new Exception('Template ' . $template . ' not found!', 80);
  53. }
  54. foreach (new GlobIterator($dir . '*.php') as $tplFile) {
  55. if ($tplFile->getRealPath() === $path) {
  56. $templatesInPath = new GlobIterator(PATH . 'tpl' . DIRECTORY_SEPARATOR . '*.php');
  57. extract($this->_variables);
  58. include $path;
  59. return;
  60. }
  61. }
  62. throw new Exception('Template ' . $file . '.php not found in ' . $dir . '!', 81);
  63. }
  64. /**
  65. * echo script tag incl. SRI hash for given script file
  66. *
  67. * @access private
  68. * @param string $file
  69. * @param string $attributes additional attributes to add into the script tag
  70. */
  71. private function _scriptTag($file, $attributes = '')
  72. {
  73. $sri = array_key_exists($file, $this->_variables['SRI']) ?
  74. ' integrity="' . $this->_variables['SRI'][$file] . '"' : '';
  75. // if the file isn't versioned (ends in a digit), add our own version
  76. $cacheBuster = (bool) preg_match('#[0-9]\.js$#', (string) $file) ?
  77. '' : '?' . rawurlencode($this->_variables['VERSION']);
  78. echo '<script ', $attributes,
  79. ' type="text/javascript" data-cfasync="false" src="', $file,
  80. $cacheBuster, '"', $sri, ' crossorigin="anonymous"></script>', PHP_EOL;
  81. }
  82. }