mirror of
https://github.com/getgrav/grav-plugin-admin.git
synced 2026-05-06 22:37:42 +02:00
Fix #396 Check frontmatter for validity prior to saving
If saving in Expert mode and the frontmatter field contained invalid yaml, the page content was changed to the error description. Instead, avoid saving the page and return an error in the Admin interface.
This commit is contained in:
@@ -21,6 +21,7 @@ use Grav\Common\Markdown\ParsedownExtra;
|
||||
use RocketTheme\Toolbox\Event\Event;
|
||||
use RocketTheme\Toolbox\File\File;
|
||||
use RocketTheme\Toolbox\File\JsonFile;
|
||||
use Symfony\Component\Yaml\Exception\ParseException;
|
||||
use Symfony\Component\Yaml\Yaml;
|
||||
|
||||
class AdminController
|
||||
@@ -1055,6 +1056,24 @@ class AdminController
|
||||
return $obj;
|
||||
}
|
||||
|
||||
public function checkValidFrontmatter($frontmatter)
|
||||
{
|
||||
try {
|
||||
// Try native PECL YAML PHP extension first if available.
|
||||
if (function_exists('yaml_parse')) {
|
||||
$saved = @ini_get('yaml.decode_php');
|
||||
@ini_set('yaml.decode_php', 0);
|
||||
@yaml_parse("---\n" . $frontmatter . "\n...");
|
||||
@ini_set('yaml.decode_php', $saved);
|
||||
} else {
|
||||
Yaml::parse($frontmatter);
|
||||
}
|
||||
} catch (ParseException $e) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles form and saves the input data if its valid.
|
||||
*
|
||||
@@ -1079,6 +1098,11 @@ class AdminController
|
||||
$route = !isset($data['route']) ? dirname($this->admin->route) : $data['route'];
|
||||
$obj = $this->admin->page(true);
|
||||
|
||||
if (isset($data['frontmatter']) && !$this->checkValidFrontmatter($data['frontmatter'])) {
|
||||
$this->admin->setMessage($this->admin->translate('PLUGIN_ADMIN.INVALID_FRONTMATTER_COULD_NOT_SAVE'), 'error');
|
||||
return false;
|
||||
}
|
||||
|
||||
//Handle system.home.hide_in_urls
|
||||
$hide_home_route = $config->get('system.home.hide_in_urls', false);
|
||||
if ($hide_home_route) {
|
||||
|
||||
Reference in New Issue
Block a user