Skip to content

Commit

Permalink
fix: custom styles from 1.x can crash the app
Browse files Browse the repository at this point in the history
  • Loading branch information
SychO9 committed Jan 10, 2025
1 parent 958dec5 commit 2f6a612
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ trim_trailing_whitespace = false
[*.{php,xml,json}]
indent_size = 4

[{tsconfig.json,prettierrc.json}]
[{tsconfig.json,prettierrc.json,package.json}]
indent_size = 2

[*.neon]
Expand Down
16 changes: 15 additions & 1 deletion framework/core/js/src/admin/components/EditCustomCssModal.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@
import app from '../../admin/app';
import SettingsModal from './SettingsModal';
import SettingsModal, { type ISettingsModalAttrs } from './SettingsModal';
import Mithril from 'mithril';

export default class EditCustomCssModal extends SettingsModal {
oninit(vnode: Mithril.Vnode<ISettingsModalAttrs, this>) {
super.oninit(vnode);

console.log(this.settings);

if (this.setting('custom_less_error')()) {
this.alertAttrs = {
type: 'error',
content: this.setting('custom_less_error')(),
};
}
}

className() {
return 'EditCustomCssModal TextareaCodeModal Modal--large';
}
Expand Down
8 changes: 5 additions & 3 deletions framework/core/src/Forum/ForumServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public function register(): void
$sources->addFile(__DIR__.'/../../less/forum.less');
$sources->addString(function () use ($container) {
return $container->make(SettingsRepositoryInterface::class)->get('custom_less', '');
});
}, 'custom_less');
});

$container->make(AddTranslations::class)->forFrontend('forum')->to($assets);
Expand Down Expand Up @@ -195,7 +195,8 @@ function (Saved $event) use ($container) {
$container->make('flarum.assets.forum'),
$container->make('flarum.locales'),
$container,
$container->make('flarum.less.config')
$container->make('flarum.less.config'),
$container->make(SettingsRepositoryInterface::class),
);
$validator->whenSettingsSaved($event);
}
Expand All @@ -208,7 +209,8 @@ function (Saving $event) use ($container) {
$container->make('flarum.assets.forum'),
$container->make('flarum.locales'),
$container,
$container->make('flarum.less.config')
$container->make('flarum.less.config'),
$container->make(SettingsRepositoryInterface::class),
);
$validator->whenSettingsSaving($event);
}
Expand Down
9 changes: 8 additions & 1 deletion framework/core/src/Forum/ValidateCustomLess.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ public function __construct(
protected Assets $assets,
protected LocaleManager $locales,
protected Container $container,
protected array $customLessSettings = []
protected array $customLessSettings = [],
protected SettingsRepositoryInterface $settings,

Check failure on line 36 in framework/core/src/Forum/ValidateCustomLess.php

View workflow job for this annotation

GitHub Actions / run / PHPStan PHP 8.2

Deprecated in PHP 8.0: Required parameter $settings follows optional parameter $customLessSettings.

Check failure on line 36 in framework/core/src/Forum/ValidateCustomLess.php

View workflow job for this annotation

GitHub Actions / run / PHPStan PHP 8.3

Deprecated in PHP 8.0: Required parameter $settings follows optional parameter $customLessSettings.

Check failure on line 36 in framework/core/src/Forum/ValidateCustomLess.php

View workflow job for this annotation

GitHub Actions / run / PHPStan PHP 8.4

Deprecated in PHP 8.0: Required parameter $settings follows optional parameter $customLessSettings.
) {
}

Expand Down Expand Up @@ -72,6 +73,8 @@ function ($settings) use ($event) {
$adapter = new InMemoryFilesystemAdapter();
$this->assets->setAssetsDir(new FilesystemAdapter(new Filesystem($adapter), $adapter));

$this->settings->delete('custom_less_error');

try {
$this->assets->makeCss()->commit();

Expand All @@ -82,6 +85,10 @@ function ($settings) use ($event) {
throw new ValidationException(['custom_less' => $e->getMessage()]);
}

if (! empty($this->settings->get('custom_less_error'))) {
throw new ValidationException(['custom_less' => $this->settings->get('custom_less_error')]);
}

$this->assets->setAssetsDir($assetsDir);
$this->container->instance(SettingsRepositoryInterface::class, $settings);
}
Expand Down
27 changes: 26 additions & 1 deletion framework/core/src/Frontend/Compiler/LessCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Flarum\Frontend\Compiler\Source\FileSource;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
use Less_Exception_Compiler;
use Less_FileManager;
use Less_Parser;
use Less_Tree_Import;
Expand Down Expand Up @@ -71,6 +72,10 @@ protected function compile(array $sources): string
return '';
}

if (! empty($this->settings->get('custom_less_error'))) {
unset($sources['custom_less']);
}

ini_set('xdebug.max_nesting_level', '200');

$parser = new Less_Parser([
Expand All @@ -96,7 +101,27 @@ protected function compile(array $sources): string
$parser->registerFunction($name, $callback);
}

return $this->finalize($parser->getCss());
try {
$compiled = $this->finalize($parser->getCss());

if (isset($sources['custom_less'])) {
$this->settings->delete('custom_less_error');
}

return $compiled;
} catch (Less_Exception_Compiler $e) {
if (isset($sources['custom_less'])) {
unset($sources['custom_less']);

$compiled = $this->compile($sources);

$this->settings->set('custom_less_error', $e->getMessage());

return $compiled;
}

throw $e;
}
}

protected function finalize(string $parsedCss): string
Expand Down
2 changes: 2 additions & 0 deletions framework/core/src/Frontend/Compiler/RevisionCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Flarum\Frontend\Compiler\Source\FileSource;
use Flarum\Frontend\Compiler\Source\SourceInterface;
use Flarum\Frontend\Compiler\Source\StringSource;
use Flarum\Settings\SettingsRepositoryInterface;
use Illuminate\Contracts\Filesystem\Cloud;

/**
Expand All @@ -29,6 +30,7 @@ class RevisionCompiler implements CompilerInterface
public function __construct(
protected Cloud $assetsDir,
protected string $filename,
protected SettingsRepositoryInterface $settings
) {
$this->versioner = new FileVersioner($assetsDir);
}
Expand Down
10 changes: 8 additions & 2 deletions framework/core/src/Frontend/Compiler/Source/SourceCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,18 @@ public function addFile(string $file, ?string $extensionId = null): static
return $this;
}

public function addString(Closure $callback): static
public function addString(Closure $callback, ?string $key = null): static
{
$this->sources[] = $this->validateSourceType(
$source = $this->validateSourceType(
new StringSource($callback)
);

if (! empty($key)) {
$this->sources[$key] = $source;
} else {
$this->sources[] = $source;
}

return $this;
}

Expand Down

0 comments on commit 2f6a612

Please sign in to comment.