TemplateSwitcher.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 = 'bootstrap5';
  27. /**
  28. * available templates
  29. *
  30. * @access protected
  31. * @static
  32. * @var array
  33. */
  34. protected static $_availableTemplates = [];
  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. } else {
  58. error_log('failed to set "' . $template . '" as a fallback, it needs to be added to the list of `availabletemplates` in the configuration file');
  59. }
  60. }
  61. /**
  62. * get user selected template or fallback
  63. *
  64. * @access public
  65. * @static
  66. * @return string
  67. */
  68. public static function getTemplate(): string
  69. {
  70. if (array_key_exists('template', $_COOKIE)) {
  71. $template = basename($_COOKIE['template']);
  72. if (self::isTemplateAvailable($template)) {
  73. return $template;
  74. }
  75. }
  76. return self::$_templateFallback;
  77. }
  78. /**
  79. * get list of available templates
  80. *
  81. * @access public
  82. * @static
  83. * @return array
  84. */
  85. public static function getAvailableTemplates(): array
  86. {
  87. return self::$_availableTemplates;
  88. }
  89. /**
  90. * check if the provided template is available
  91. *
  92. * @access public
  93. * @static
  94. * @return bool
  95. */
  96. public static function isTemplateAvailable(string $template): bool
  97. {
  98. if (in_array($template, self::getAvailableTemplates(), true)) {
  99. return true;
  100. }
  101. error_log('template "' . $template . '" is not in the list of `availabletemplates` in the configuration file');
  102. return false;
  103. }
  104. }