TemplateSwitcher.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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;
  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. }
  58. }
  59. /**
  60. * get currently loaded template
  61. *
  62. * @access public
  63. * @static
  64. * @return string
  65. */
  66. public static function getTemplate(): string
  67. {
  68. $selectedTemplate = self::getSelectedByUserTemplate();
  69. return $selectedTemplate ?? self::$_templateFallback;
  70. }
  71. /**
  72. * get list of available templates
  73. *
  74. * @access public
  75. * @static
  76. * @return array
  77. */
  78. public static function getAvailableTemplates(): array
  79. {
  80. return self::$_availableTemplates;
  81. }
  82. /**
  83. * check if the provided template is available
  84. *
  85. * @access public
  86. * @static
  87. * @return bool
  88. */
  89. public static function isTemplateAvailable(string $template): bool
  90. {
  91. return in_array($template, self::getAvailableTemplates());
  92. }
  93. /**
  94. * get the template selected by user
  95. *
  96. * @access private
  97. * @static
  98. * @return string|null
  99. */
  100. private static function getSelectedByUserTemplate(): ?string
  101. {
  102. $selectedTemplate = null;
  103. $templateCookieValue = $_COOKIE['template'] ?? '';
  104. if (self::isTemplateAvailable($templateCookieValue)) {
  105. $selectedTemplate = $templateCookieValue;
  106. }
  107. return $selectedTemplate;
  108. }
  109. }