TemplateSwitcher.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. /**
  13. * TemplateSwitcher
  14. *
  15. * Provides tool to change application template
  16. */
  17. class TemplateSwitcher
  18. {
  19. /**
  20. * template fallback
  21. *
  22. * @access protected
  23. * @static
  24. * @var string
  25. */
  26. protected static $_templateFallback = 'bootstrap';
  27. /**
  28. * available templates
  29. *
  30. * @access protected
  31. * @static
  32. * @var array
  33. */
  34. protected static $_availableTemplates = array();
  35. /**
  36. * set available templates
  37. *
  38. * @access public
  39. * @static
  40. * @param array $templates
  41. */
  42. public static function setAvailableTemplates(array $templates)
  43. {
  44. self::$_availableTemplates = $templates;
  45. }
  46. /**
  47. * set the default template
  48. *
  49. * @access public
  50. * @static
  51. * @param string $template
  52. */
  53. public static function setTemplateFallback(string $template)
  54. {
  55. if (self::isTemplateAvailable($template)) {
  56. self::$_templateFallback = $template;
  57. if (!in_array($template, self::getAvailableTemplates())) {
  58. // Add custom template to the available templates list
  59. self::$_availableTemplates[] = $template;
  60. }
  61. }
  62. }
  63. /**
  64. * get currently loaded template
  65. *
  66. * @access public
  67. * @static
  68. * @return string
  69. */
  70. public static function getTemplate(): string
  71. {
  72. $selectedTemplate = self::getSelectedByUserTemplate();
  73. return $selectedTemplate ?? self::$_templateFallback;
  74. }
  75. /**
  76. * get list of available templates
  77. *
  78. * @access public
  79. * @static
  80. * @return array
  81. */
  82. public static function getAvailableTemplates(): array
  83. {
  84. return self::$_availableTemplates;
  85. }
  86. /**
  87. * check if the provided template is available
  88. *
  89. * @access public
  90. * @static
  91. * @return bool
  92. */
  93. public static function isTemplateAvailable(string $template): bool
  94. {
  95. if (in_array($template, self::getAvailableTemplates())) {
  96. return true;
  97. }
  98. if (View::isBootstrapTemplate($template)) {
  99. return false;
  100. }
  101. return file_exists(View::getTemplateFilePath($template));
  102. }
  103. /**
  104. * get the template selected by user
  105. *
  106. * @access private
  107. * @static
  108. * @return string|null
  109. */
  110. private static function getSelectedByUserTemplate(): ?string
  111. {
  112. $templateCookieValue = $_COOKIE['template'] ?? '';
  113. if (self::isTemplateAvailable($templateCookieValue)) {
  114. return $templateCookieValue;
  115. }
  116. return null;
  117. }
  118. }