Skip to content

Commit

Permalink
fix(theme): more robust fallback for favorite preset
Browse files Browse the repository at this point in the history
  • Loading branch information
mrflos committed Dec 10, 2024
1 parent 094bb85 commit 41b7d84
Showing 1 changed file with 25 additions and 29 deletions.
54 changes: 25 additions & 29 deletions includes/services/ThemeManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,25 +119,24 @@ public function loadTemplates($metadata = []): ?array
$this->setFavorite('preset', $this->getConfigAsStringOrDefault('favorite_preset', ''));
} else {
// Sinon, on récupère premièrement les valeurs passées en REQUEST, ou deuxièmement les métasdonnées présentes pour la page, ou troisièmement les valeurs du fichier de configuration
if (isset($_REQUEST['theme']) && (is_dir('custom/themes/' . $_REQUEST['theme']) || is_dir('themes/' . $_REQUEST['theme'])) &&
isset($_REQUEST['style']) && (is_file('custom/themes/' . $_REQUEST['theme'] . '/styles/' . $_REQUEST['style']) || is_file('themes/' . $_REQUEST['theme'] . '/styles/' . $_REQUEST['style'])) &&
isset($_REQUEST['squelette']) && (is_file('custom/themes/' . $_REQUEST['theme'] . '/squelettes/' . $_REQUEST['squelette']) || is_file('themes/' . $_REQUEST['theme'] . '/squelettes/' . $_REQUEST['squelette']))
if (isset($_REQUEST['theme']) && (is_dir('custom/themes/' . $_REQUEST['theme']) || is_dir('themes/' . $_REQUEST['theme']))
&& isset($_REQUEST['style']) && (is_file('custom/themes/' . $_REQUEST['theme'] . '/styles/' . $_REQUEST['style']) || is_file('themes/' . $_REQUEST['theme'] . '/styles/' . $_REQUEST['style']))
&& isset($_REQUEST['squelette']) && (is_file('custom/themes/' . $_REQUEST['theme'] . '/squelettes/' . $_REQUEST['squelette']) || is_file('themes/' . $_REQUEST['theme'] . '/squelettes/' . $_REQUEST['squelette']))
) {
$this->setFavorite('theme', $_REQUEST['theme']);
$this->setFavorite('style', $_REQUEST['style']);
$this->setFavorite('squelette', $_REQUEST['squelette']);

// presets
if (isset($_REQUEST['preset']) &&
(
if (isset($_REQUEST['preset'])
&& (
(
($isCustom = (substr($_REQUEST['preset'], 0, strlen(self::CUSTOM_CSS_PRESETS_PREFIX)) == self::CUSTOM_CSS_PRESETS_PREFIX))
&& is_file(self::CUSTOM_CSS_PRESETS_PATH . '/' . substr($_REQUEST['preset'], strlen(self::CUSTOM_CSS_PRESETS_PREFIX)))
)
||
(
!$isCustom &&
(
|| (
!$isCustom
&& (
is_file('custom/themes/' . $_REQUEST['theme'] . '/presets/' . $_REQUEST['preset'])
|| is_file('themes/' . $_REQUEST['theme'] . '/presets/' . $_REQUEST['preset'])
)
Expand All @@ -147,7 +146,7 @@ public function loadTemplates($metadata = []): ?array
$this->setFavorite('preset', $_REQUEST['preset']);
}

if (isset($_REQUEST['bgimg']) && (is_file('files/backgrounds/' . $_REQUEST['bgimg']))) {
if (isset($_REQUEST['bgimg']) && is_file('files/backgrounds/' . $_REQUEST['bgimg'])) {
$this->setFavorite('background_image', $_REQUEST['bgimg']);
} else {
$this->setFavorite('background_image', BACKGROUND_IMAGE_PAR_DEFAUT);
Expand Down Expand Up @@ -194,10 +193,10 @@ public function loadTemplates($metadata = []): ?array
&& !file_exists('themes/' . $this->favorites['theme'] . '/styles/' . $this->favorites['style']))
) {
if (
$this->favorites['theme'] != THEME_PAR_DEFAUT ||
(
$this->favorites['theme'] == THEME_PAR_DEFAUT && (!file_exists('themes/' . THEME_PAR_DEFAUT . '/squelettes/' . $this->favorites['squelette']) or
!file_exists('themes/' . THEME_PAR_DEFAUT . '/styles/' . $this->favorites['style']))
$this->favorites['theme'] != THEME_PAR_DEFAUT
|| (
$this->favorites['theme'] == THEME_PAR_DEFAUT && (!file_exists('themes/' . THEME_PAR_DEFAUT . '/squelettes/' . $this->favorites['squelette'])
or !file_exists('themes/' . THEME_PAR_DEFAUT . '/styles/' . $this->favorites['style']))
)
) {
if (
Expand All @@ -220,14 +219,11 @@ public function loadTemplates($metadata = []): ?array
}
// test l'existence du preset
if (!empty($this->favorites['preset'])
&&
(
(
&& (
($isCutom = substr($this->favorites['preset'], 0, strlen(self::CUSTOM_CSS_PRESETS_PREFIX)) == self::CUSTOM_CSS_PRESETS_PREFIX)
&& !file_exists(self::CUSTOM_CSS_PRESETS_PATH . DIRECTORY_SEPARATOR
. substr($this->favorites['preset'], strlen(self::CUSTOM_CSS_PRESETS_PREFIX)))
)
)
) {
unset($this->favorites['preset']);
}
Expand Down Expand Up @@ -258,9 +254,9 @@ public function loadTheme(): bool
$squelette = empty($squelette) ? SQUELETTE_PAR_DEFAUT : $squelette;

// do not load the file if already loaded
$fileAlreadyLoaded = $this->fileLoaded &&
($this->theme == $theme) &&
($this->squelette == $squelette);
$fileAlreadyLoaded = $this->fileLoaded
&& ($this->theme == $theme)
&& ($this->squelette == $squelette);
if ($fileAlreadyLoaded) {
return true;
}
Expand Down Expand Up @@ -357,7 +353,7 @@ public function getFavoriteStyle(): string

public function getFavoritePreset(): string
{
return $this->favorites['preset'];
return $this->favorites['preset'] ?? '';
}

public function getFavoriteBackgroundImage(): string
Expand Down Expand Up @@ -387,7 +383,7 @@ protected function getConfigAsStringOrDefault(string $key, string $default): str

private function renderActions(string $text): ?string
{
if ($act = preg_match_all('/' . '(\\{\\{)' . '(.*?)' . '(\\}\\})' . '/is', $text, $matches)) {
if ($act = preg_match_all('/(\\{\\{)(.*?)(\\}\\})/is', $text, $matches)) {
$i = 0;
$j = 0;
foreach ($matches as $valeur) {
Expand Down Expand Up @@ -549,7 +545,7 @@ private function checkPOSTToAddCustomCSSPreset(array $post): bool
*/
public function getPresetsData(): ?array
{
$themePresets = ($this->getTemplates())[$this->getFavoriteTheme()]['presets'] ?? [];
$themePresets = $this->getTemplates()[$this->getFavoriteTheme()]['presets'] ?? [];
$dataHtmlForPresets = array_map(function ($value) {
return $this->extractDataFromPreset($value);
}, $themePresets);
Expand Down Expand Up @@ -782,8 +778,8 @@ protected function formatCSS(array $data): string
'url' => [],
];
}
if (isset($raw['unicode-range']) &&
!isset($formattedData[$key]['charsets'][$charset]['unicode-range'])) {
if (isset($raw['unicode-range'])
&& !isset($formattedData[$key]['charsets'][$charset]['unicode-range'])) {
$formattedData[$key]['charsets'][$charset]['unicode-range'] = $raw['unicode-range'];
}
if (!isset($formattedData[$key]['charsets'][$charset]['url'][$format])) {
Expand Down Expand Up @@ -893,8 +889,8 @@ protected function importFontFile(string $family, string $style, string $weight,
$errorNb = curl_errno($ch);
curl_close($ch);
if (!$errorNb && !empty($result)) {
if (file_put_contents(self::CUSTOM_FONT_PATH . "/$folderSystemName/$fileName", $result) &&
file_exists(self::CUSTOM_FONT_PATH . "/$folderSystemName/$fileName")) {
if (file_put_contents(self::CUSTOM_FONT_PATH . "/$folderSystemName/$fileName", $result)
&& file_exists(self::CUSTOM_FONT_PATH . "/$folderSystemName/$fileName")) {
return '../../' . self::CUSTOM_FONT_PATH . "/$folderSystemName/$fileName";
}
}
Expand Down Expand Up @@ -937,4 +933,4 @@ public function saveMetadataIfNeeded(Event $event)
}
}
}
}
}

0 comments on commit 41b7d84

Please sign in to comment.