TemplateSwitcher.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. * template fallback
  20. *
  21. * @access protected
  22. * @static
  23. * @var string
  24. */
  25. protected static $_templateFallback = "bootstrap";
  26. /**
  27. * available templates
  28. *
  29. * @access protected
  30. * @static
  31. * @var array
  32. */
  33. protected static $_availableTemplates = [
  34. 'page',
  35. 'bootstrap',
  36. 'bootstrap-compact',
  37. 'bootstrap-dark',
  38. 'bootstrap-page',
  39. 'bootstrap5',
  40. ];
  41. /**
  42. * set the default template
  43. *
  44. * @access public
  45. * @static
  46. * @param string $template
  47. */
  48. public static function setTemplateFallback(string $template)
  49. {
  50. if (self::isTemplateAvailable($template)) {
  51. self::$_templateFallback = $template;
  52. }
  53. }
  54. /**
  55. * get currently loaded template
  56. *
  57. * @access public
  58. * @static
  59. * @return string
  60. */
  61. public static function getTemplate(): string
  62. {
  63. $selectedTemplate = self::getSelectedByUserTemplate();
  64. return $selectedTemplate ?? self::$_templateFallback;
  65. }
  66. /**
  67. * get list of available templates
  68. *
  69. * @access public
  70. * @static
  71. * @return array
  72. */
  73. public static function getAvailableTemplates(): array
  74. {
  75. return self::$_availableTemplates;
  76. }
  77. /**
  78. * check if the provided template is available
  79. *
  80. * @access public
  81. * @static
  82. * @return bool
  83. */
  84. public static function isTemplateAvailable(string $template): bool
  85. {
  86. return in_array($template, self::getAvailableTemplates());
  87. }
  88. /**
  89. * get the template selected by user
  90. *
  91. * @access private
  92. * @static
  93. * @return string
  94. */
  95. private static function getSelectedByUserTemplate(): ?string
  96. {
  97. $selectedTemplate = null;
  98. $templateCookieValue = $_COOKIE['template'] ?? "";
  99. if (self::isTemplateAvailable($templateCookieValue)) {
  100. $selectedTemplate = $templateCookieValue;
  101. }
  102. return $selectedTemplate;
  103. }
  104. }