TemplateSwitcher.php 2.5 KB

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