Commit e46995e0 authored by Côme Chilliet's avatar Côme Chilliet

🚑 fix(templates) Show an error when a template mask use a non-existing attribute

Instead of PHP error + crash, use an Exception and catch it in
 management to show it to the user and cancel the operation.

issue #6032
parent 3fcc1df6
......@@ -118,7 +118,7 @@ class templateHandling
}
/*! \brief Parse a mask (without surrounding %) using $attrs attributes, apply modifiers and returns an array containing possible results */
public static function parseMask ($mask, array $attrs)
public static function parseMask (string $mask, array $attrs): array
{
if ($mask == '|') {
return ['%'];
......@@ -137,7 +137,7 @@ class templateHandling
$result = [$result];
}
} elseif (($mask != '') && !preg_match('/c/', $modifiers)) {
trigger_error("'$mask' was not found in attributes");
throw new FusionDirectoryException(sprintf(_('"%s" was not found in attributes'), $mask));
}
$len = strlen($modifiers);
for ($i = 0; $i < $len; ++$i) {
......@@ -211,7 +211,7 @@ class templateHandling
*
* \return string the string with patterns replaced by their values
*/
public static function parseString ($string, array $attrs, $escapeMethod = NULL, $unique = NULL, $target = NULL)
public static function parseString (string $string, array $attrs, $escapeMethod = NULL, string $unique = NULL, string $target = NULL): string
{
global $config;
......@@ -261,7 +261,7 @@ class templateHandling
* \param array $variables The possible values for each mask with its position and length
* \param callable $escapeMethod Method to call to escape mask result
*/
protected static function iteratePossibleValues ($rule, array $variables, $escapeMethod = NULL)
protected static function iteratePossibleValues (string $rule, array $variables, $escapeMethod = NULL)
{
if (!count($variables)) {
yield $rule;
......@@ -466,7 +466,7 @@ class templateHandling
*
* \return array an array of possible values
* */
protected static function applyModifier ($m, array $args, $str)
protected static function applyModifier (string $m, array $args, $str): array
{
mb_internal_encoding('UTF-8');
mb_regex_encoding('UTF-8');
......
......@@ -458,7 +458,12 @@ class management
if ($this->tabObject instanceOf simpleTabs) {
$this->tabObject->save_object();
} elseif (is_object($this->dialogObject) && method_exists($this->dialogObject, 'save_object')) {
$this->dialogObject->save_object();
try {
$this->dialogObject->save_object();
} catch (FusionDirectoryException $e) {
msg_dialog::display(_('Error'), $e->getMessage(), ERROR_DIALOG);
$this->closeDialogs();
}
}
/* Display tab object */
......
<?php
/*
This code is part of FusionDirectory (http://www.fusiondirectory.org/)
Copyright (C) 2013-2019 FusionDirectory
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
*/
/*!
* \brief Template dialog handling
*/
class templateDialog
{
protected $simpleManagement;
protected $type;
protected $template = NULL;
protected $templates;
protected $target = NULL;
protected $tabObject;
protected $post_finish = 'template_continue';
protected $post_cancel = 'template_cancel';
function __construct ($simpleManagement, $type, $dn = NULL, $target = NULL)
{
$this->simpleManagement = $simpleManagement;
$this->type = $type;
$this->templates = objects::getTemplates($this->type);
if ($dn !== NULL) {
if (isset($this->templates[$dn])) {
$this->template = new template($this->type, $dn);
} else {
trigger_error('Unknown template "'.$dn.'"');
}
}
$this->target = $target;
}
function save_object ()
{
if (isset($_POST[$this->post_cancel])) {
return $this->handle_cancel();
}
if (($this->target === NULL) &&
(isset($_POST[$this->post_finish]) || isset($_GET[$this->post_finish])) &&
is_object($this->template)) {
$this->template->save_object();
return $this->handle_finish();
}
if (
isset($_POST['template']) &&
isset($this->templates[$_POST['template']])
) {
if (is_object($this->template)) {
trigger_error('redefining template object');
}
$this->template = new template($this->type, $_POST['template']);
/* This method can loop if there are several targets */
unset($_POST['template']);
}
if (is_object($this->template)) {
if ($this->target !== NULL) {
$this->simpleManagement->openTabObject($this->template->apply($this->target));
$this->simpleManagement->handleTemplateApply();
return FALSE;
} else {
$this->template->save_object();
}
}
return TRUE;
}
function setNextTarget ($target)
{
$this->target = $target;
$this->template->reset();
}
function execute ()
{
$smarty = get_smarty();
if (is_object($this->template)) {
$templateOutput = $this->template->execute();
if ($this->template->dialogOpened()) {
return $templateOutput;
} else {
$smarty->assign('template_dialog', $templateOutput);
}
} else {
$smarty->assign('templates', $this->templates);
}
$display = $smarty->fetch(get_template_path('template.tpl'));
return $display;
}
function handle_finish ()
{
$this->simpleManagement->closeDialogs();
$this->simpleManagement->openTabObject($this->template->apply());
return FALSE;
}
function handle_cancel ()
{
$this->simpleManagement->remove_lock();
$this->simpleManagement->closeDialogs();
return FALSE;
}
}
......@@ -18,112 +18,6 @@
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
*/
/*!
* \brief Template dialog handling
*/
class templateDialog
{
protected $simpleManagement;
protected $type;
protected $template = NULL;
protected $templates;
protected $target = NULL;
protected $tabObject;
protected $post_finish = 'template_continue';
protected $post_cancel = 'template_cancel';
function __construct ($simpleManagement, $type, $dn = NULL, $target = NULL)
{
$this->simpleManagement = $simpleManagement;
$this->type = $type;
$this->templates = objects::getTemplates($this->type);
if ($dn !== NULL) {
if (isset($this->templates[$dn])) {
$this->template = new template($this->type, $dn);
} else {
trigger_error('Unknown template "'.$dn.'"');
}
}
$this->target = $target;
}
function save_object ()
{
if (isset($_POST[$this->post_cancel])) {
return $this->handle_cancel();
}
if (($this->target === NULL) &&
(isset($_POST[$this->post_finish]) || isset($_GET[$this->post_finish])) &&
is_object($this->template)) {
$this->template->save_object();
return $this->handle_finish();
}
if (
isset($_POST['template']) &&
isset($this->templates[$_POST['template']])
) {
if (is_object($this->template)) {
trigger_error('redefining template object');
}
$this->template = new template($this->type, $_POST['template']);
/* This method can loop if there are several targets */
unset($_POST['template']);
}
if (is_object($this->template)) {
if ($this->target !== NULL) {
$this->simpleManagement->openTabObject($this->template->apply($this->target));
$this->simpleManagement->handleTemplateApply();
return FALSE;
} else {
$this->template->save_object();
}
}
return TRUE;
}
function setNextTarget ($target)
{
$this->target = $target;
$this->template->reset();
}
function execute ()
{
$smarty = get_smarty();
if (is_object($this->template)) {
$templateOutput = $this->template->execute();
if ($this->template->dialogOpened()) {
return $templateOutput;
} else {
$smarty->assign('template_dialog', $templateOutput);
}
} else {
$smarty->assign('templates', $this->templates);
}
$display = $smarty->fetch(get_template_path('template.tpl'));
return $display;
}
function handle_finish ()
{
$this->simpleManagement->closeDialogs();
$this->simpleManagement->openTabObject($this->template->apply());
return FALSE;
}
function handle_cancel ()
{
$this->simpleManagement->remove_lock();
$this->simpleManagement->closeDialogs();
return FALSE;
}
}
/*!
* \brief Management base class
*/
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment