View.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. $file = substr($template, 0, 10) === 'bootstrap-' ? 'bootstrap' : $template;
  48. $path = PATH . 'tpl' . DIRECTORY_SEPARATOR . $file . '.php';
  49. if (!file_exists($path)) {
  50. throw new Exception('Template ' . $template . ' not found!', 80);
  51. }
  52. extract($this->_variables);
  53. include $path;
  54. }
  55. /**
  56. * echo script tag incl. SRI hash for given script file
  57. *
  58. * @access private
  59. * @param string $file
  60. * @param string $attributes additional attributes to add into the script tag
  61. */
  62. private function _scriptTag($file, $attributes = '')
  63. {
  64. $sri = array_key_exists($file, $this->_variables['SRI']) ?
  65. ' integrity="' . $this->_variables['SRI'][$file] . '"' : '';
  66. // if the file isn't versioned (ends in a digit), add our own version
  67. $cacheBuster = ctype_digit(substr($file, -4, 1)) ?
  68. '' : '?' . rawurlencode($this->_variables['VERSION']);
  69. echo '<script ', $attributes,
  70. ' type="text/javascript" data-cfasync="false" src="', $file,
  71. $cacheBuster, '"', $sri, ' crossorigin="anonymous"></script>', PHP_EOL;
  72. }
  73. }