PrivateBin/lib/TemplateSwitcher.php
Matt Van Horn c5c910a537
Switch to short array syntax (#1640)
Mass-converts all array() literals to [] across lib/, tpl/, tst/ as
discussed in #1640. PHP supports short array syntax since 5.4 and the
project's required PHP version is 7.4+, so this is a pure stylistic
change with no behavior impact.

Also flips the StyleCI configuration so future PRs are checked against
short_array_syntax instead of long_array_syntax. Per @elrido in #1640,
the StyleCI swap and the mass change need to land together so no
branch breaks under the wrong rule.

Generated with `php-cs-fixer fix --rules=array_syntax` against the
.styleci.yml finder paths. 39 files touched, 487 insertions / 487
deletions (every change is a single-token replacement). Verified:

- `php -l` clean on all touched files
- `phpunit` from `tst/`: 230 / 230 tests pass (with 6329 assertions),
  excluding `ControllerWithGcsTest` which fails on PHP 8.5 because of
  an unrelated upstream `google/cloud-core` deprecation. The same 14
  GCS-suite errors reproduce on `master` before this change.

Closes #1640
2026-05-03 05:23:11 -07:00

112 lines
2.6 KiB
PHP

<?php declare(strict_types=1);
/**
* PrivateBin
*
* a zero-knowledge paste bin
*
* @link https://github.com/PrivateBin/PrivateBin
* @copyright 2012 Sébastien SAUVAGE (sebsauvage.net)
* @license https://www.opensource.org/licenses/zlib-license.php The zlib/libpng License
*/
namespace PrivateBin;
/**
* TemplateSwitcher
*
* Provides tool to change application template
*/
class TemplateSwitcher
{
/**
* template fallback
*
* @access protected
* @static
* @var string
*/
protected static $_templateFallback = 'bootstrap5';
/**
* available templates
*
* @access protected
* @static
* @var array
*/
protected static $_availableTemplates = [];
/**
* set available templates
*
* @access public
* @static
* @param array $templates
*/
public static function setAvailableTemplates(array $templates)
{
self::$_availableTemplates = $templates;
}
/**
* set the default template
*
* @access public
* @static
* @param string $template
*/
public static function setTemplateFallback(string $template)
{
if (self::isTemplateAvailable($template)) {
self::$_templateFallback = $template;
} else {
error_log('failed to set "' . $template . '" as a fallback, it needs to be added to the list of `availabletemplates` in the configuration file');
}
}
/**
* get user selected template or fallback
*
* @access public
* @static
* @return string
*/
public static function getTemplate(): string
{
if (array_key_exists('template', $_COOKIE)) {
$template = basename($_COOKIE['template']);
if (self::isTemplateAvailable($template)) {
return $template;
}
}
return self::$_templateFallback;
}
/**
* get list of available templates
*
* @access public
* @static
* @return array
*/
public static function getAvailableTemplates(): array
{
return self::$_availableTemplates;
}
/**
* check if the provided template is available
*
* @access public
* @static
* @return bool
*/
public static function isTemplateAvailable(string $template): bool
{
if (in_array($template, self::getAvailableTemplates(), true)) {
return true;
}
error_log('template "' . $template . '" is not in the list of `availabletemplates` in the configuration file');
return false;
}
}