View.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 bool $async should it execute ASAP or only after HTML got parsed
  61. */
  62. private function _scriptTag($file, $async = true)
  63. {
  64. $sri = array_key_exists($file, $this->_variables['SRI']) ?
  65. ' integrity="' . $this->_variables['SRI'][$file] . '"' : '';
  66. $suffix = preg_match('#\d.js$#', $file) == 0 ?
  67. '?' . rawurlencode($this->_variables['VERSION']) : '';
  68. echo '<script ', $async ? 'async' : 'defer',
  69. ' type="text/javascript" data-cfasync="false" src="', $file,
  70. $suffix, '"', $sri, ' crossorigin="anonymous"></script>', PHP_EOL;
  71. }
  72. }