TemplateSwitcher.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. } 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) && self::isTemplateAvailable($_COOKIE['template'])) {
  71. return $_COOKIE['template'];
  72. }
  73. return 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(), true)) {
  96. return true;
  97. }
  98. error_log('template "' . $template . '" is not in the list of `availabletemplates` in the configuration file');
  99. return false;
  100. }
  101. }