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:
Flavio Copes
2016-01-16 16:24:38 +01:00
parent 6df159277a
commit 1602f2c870
2 changed files with 25 additions and 0 deletions

View File

@@ -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) {