View.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. $path = self::getTemplateFilePath($template);
  48. if (!file_exists($path)) {
  49. throw new Exception('Template ' . $template . ' not found!', 80);
  50. }
  51. extract($this->_variables);
  52. include $path;
  53. }
  54. /**
  55. * Get template file path
  56. *
  57. * @access public
  58. * @param string $template
  59. * @return string
  60. */
  61. public static function getTemplateFilePath(string $template): string
  62. {
  63. $file = self::isBootstrapTemplate($template) ? 'bootstrap' : basename($template);
  64. return PATH . 'tpl' . DIRECTORY_SEPARATOR . $file . '.php';
  65. }
  66. /**
  67. * Is the template a variation of the bootstrap template
  68. *
  69. * @access public
  70. * @param string $template
  71. * @return bool
  72. */
  73. public static function isBootstrapTemplate(string $template): bool
  74. {
  75. return substr($template, 0, 10) === 'bootstrap-';
  76. }
  77. /**
  78. * echo script tag incl. SRI hash for given script file
  79. *
  80. * @access private
  81. * @param string $file
  82. * @param string $attributes additional attributes to add into the script tag
  83. */
  84. private function _scriptTag($file, $attributes = '')
  85. {
  86. $sri = array_key_exists($file, $this->_variables['SRI']) ?
  87. ' integrity="' . $this->_variables['SRI'][$file] . '"' : '';
  88. // if the file isn't versioned (ends in a digit), add our own version
  89. $cacheBuster = (bool) preg_match('#[0-9]\.js$#', (string) $file) ?
  90. '' : '?' . rawurlencode($this->_variables['VERSION']);
  91. echo '<script ', $attributes,
  92. ' type="text/javascript" data-cfasync="false" src="', $file,
  93. $cacheBuster, '"', $sri, ' crossorigin="anonymous"></script>', PHP_EOL;
  94. }
  95. }