From d9dea033a9e19c68f4bbc25cc5e8a6ea5e88d225 Mon Sep 17 00:00:00 2001 From: Ludovic Poujol Date: Tue, 19 Mar 2019 18:13:48 +0100 Subject: [PATCH 1/5] New class DomainInputFormField Create a input that validates a domain name. --- evolibs/Form.php | 48 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) diff --git a/evolibs/Form.php b/evolibs/Form.php index 6748031..e398ccb 100644 --- a/evolibs/Form.php +++ b/evolibs/Form.php @@ -403,7 +403,53 @@ class TextInputFormField extends FormField { $out = ''; $out .= "

\n"; $out .= $this->getLabelHTML(); - $out .= $this->getInputHTML(); + $out .= $this->getInputHTML(); + $out .= $this->getErrorHTML(); + $out .= "

\n\n"; + return $out; + } +} + +class DomainInputFormField extends FormField { + protected $mandatory = NULL; + protected $textsize = NULL; + + public function __construct($label, $mandatory=TRUE) { + parent::__construct($label); + $this->mandatory = $mandatory; + $this->textsize = $textsize; + } + + public function verify($set_error) { + if($this->mandatory && (!strlen($this->value))) { + if($set_error) $this->error = 'Champ obligatoire'; + return FALSE; + } + + if (!preg_match("/^[a-z0-9-.]+\.[a-z]{2,}$/i", $this->value)) { + if($set_error) $this->error = 'Ceci n\'est pas un nom de domaine'; + return FALSE; + } + + return TRUE; + } + + public function getInputHTML() { + $input = ''; + $input .= 'name.'" value="'.htmlspecialchars($this->value,ENT_QUOTES).'"'; + $input .= ' maxlength="'.$this->textsize[1].'" size="'.$this->textsize[0].'" '; + if($this->read_only) { $input .= 'readonly="readonly="'; } + if($this->disabled) { $input .= 'disabled="disabled="'; } + $input .= '/>'; + return $input; + } + + public function __toString() { + $out = ''; + $out .= "

\n"; + $out .= $this->getLabelHTML(); + $out .= $this->getInputHTML(); $out .= $this->getErrorHTML(); $out .= "

\n\n"; return $out; From 1641aee38c013cb1b7dc7410e65b95bc3817124f Mon Sep 17 00:00:00 2001 From: Ludovic Poujol Date: Tue, 19 Mar 2019 18:14:21 +0100 Subject: [PATCH 2/5] Use our new DomainInputFormField for account creation --- inc/accounts.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/inc/accounts.php b/inc/accounts.php index 458cd91..45ecc83 100644 --- a/inc/accounts.php +++ b/inc/accounts.php @@ -251,7 +251,7 @@ function web_add_cluster($form, $admin_mail) { /* Construction du formulaire d'ajout */ $form = new FormPage("Ajout d'un compte web", FALSE); $form->addField('username', new TextInputFormField("Nom d'utilisateur", TRUE, array(20,16))); -$form->addField('domain', new TextInputFormField("Nom de domaine", TRUE)); +$form->addField('domain', new DomainInputFormField("Nom de domaine", TRUE)); $form->addField('domain_alias', new TextInputFormField("Alias (séparés par une virgule)", FALSE)); $form->addField('password_random', new CheckboxInputFormField("Mot de passe aléatoire ?", FALSE)); From 93aeb193eef71ad12a905f47482096f61ab12619 Mon Sep 17 00:00:00 2001 From: Ludovic Poujol Date: Tue, 19 Mar 2019 18:27:24 +0100 Subject: [PATCH 3/5] Use DomainInputFormField for alias creation --- inc/webadmin-edit.php | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/inc/webadmin-edit.php b/inc/webadmin-edit.php index 4ed6741..910a771 100644 --- a/inc/webadmin-edit.php +++ b/inc/webadmin-edit.php @@ -104,7 +104,7 @@ if (isset($_GET['del']) ) { include_once EVOADMIN_BASE . '../tpl/menu.tpl.php'; $form = new FormPage("Ajout d'un alias", FALSE); - $form->addField('domain_alias', new TextInputFormField("Alias", FALSE)); + $form->addField('domain_alias', new DomainInputFormField("Alias", TRUE)); if (!empty($_POST)) { $form->isCurrentPage(TRUE); @@ -205,6 +205,17 @@ if (isset($_GET['del']) ) { } } } + else { + print "

Ajout d'un serveralias


"; + print "
"; + print "
"; + print " Ajout d'un serveralias"; + print $form; + print "

"; + print "
"; + print "
"; + + } } else { print "

Ajout d'un serveralias


"; print "
"; From 3469dcfcd55cbdce68e61bf360f7c661fd9aac63 Mon Sep 17 00:00:00 2001 From: Ludovic Poujol Date: Tue, 19 Mar 2019 18:38:17 +0100 Subject: [PATCH 4/5] New class DomainListInputFormField for alias list validation upon account creation --- evolibs/Form.php | 52 ++++++++++++++++++++++++++++++++++++++++++++++++ inc/accounts.php | 4 ++-- 2 files changed, 54 insertions(+), 2 deletions(-) diff --git a/evolibs/Form.php b/evolibs/Form.php index e398ccb..1e7b2b6 100644 --- a/evolibs/Form.php +++ b/evolibs/Form.php @@ -456,6 +456,58 @@ class DomainInputFormField extends FormField { } } + +class DomainListInputFormField extends FormField { + protected $mandatory = NULL; + protected $textsize = NULL; + + public function __construct($label, $mandatory=TRUE) { + parent::__construct($label); + $this->mandatory = $mandatory; + $this->textsize = $textsize; + } + + public function verify($set_error) { + if($this->mandatory && (!strlen($this->value))) { + if($set_error) $this->error = 'Champ obligatoire'; + return FALSE; + } + + if(strlen($this->value)){ + $list = explode(',', $this->value); + foreach ($list as $value) { + if (!preg_match("/^[a-z0-9-.]+\.[a-z]{2,}$/i", $value)) { + if($set_error) $this->error = 'Ceci n\'est pas un nom de domaine'; + return FALSE; + } + } + } + + return TRUE; + } + + public function getInputHTML() { + $input = ''; + $input .= 'name.'" value="'.htmlspecialchars($this->value,ENT_QUOTES).'"'; + $input .= ' maxlength="'.$this->textsize[1].'" size="'.$this->textsize[0].'" '; + if($this->read_only) { $input .= 'readonly="readonly="'; } + if($this->disabled) { $input .= 'disabled="disabled="'; } + $input .= '/>'; + return $input; + } + + public function __toString() { + $out = ''; + $out .= "

\n"; + $out .= $this->getLabelHTML(); + $out .= $this->getInputHTML(); + $out .= $this->getErrorHTML(); + $out .= "

\n\n"; + return $out; + } +} + class DateInputFormField extends TextInputFormField { public function __construct($label, $mandatory=TRUE) { parent::__construct($label, $mandatory, array(7, 10)); diff --git a/inc/accounts.php b/inc/accounts.php index 45ecc83..8a4c91f 100644 --- a/inc/accounts.php +++ b/inc/accounts.php @@ -252,8 +252,8 @@ function web_add_cluster($form, $admin_mail) { $form = new FormPage("Ajout d'un compte web", FALSE); $form->addField('username', new TextInputFormField("Nom d'utilisateur", TRUE, array(20,16))); $form->addField('domain', new DomainInputFormField("Nom de domaine", TRUE)); -$form->addField('domain_alias', new TextInputFormField("Alias (séparés par une virgule)", FALSE)); -$form->addField('password_random', +$form->addField('domain_alias', new DomainListInputFormField("Alias (séparés par une virgule, sans espaces)", FALSE)); +$form->addField('password_random', new CheckboxInputFormField("Mot de passe aléatoire ?", FALSE)); $form->getField('password_random')->setValue(TRUE); $form->addField('password', new PasswordInputFormField('Mot de passe', FALSE)); From 501f6ebbab42b669ae4be493d92bf8d6245b1fbe Mon Sep 17 00:00:00 2001 From: Ludovic Poujol Date: Tue, 19 Mar 2019 19:11:58 +0100 Subject: [PATCH 5/5] Better regex for domain matching --- evolibs/Form.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/evolibs/Form.php b/evolibs/Form.php index 1e7b2b6..3ee2671 100644 --- a/evolibs/Form.php +++ b/evolibs/Form.php @@ -426,7 +426,7 @@ class DomainInputFormField extends FormField { return FALSE; } - if (!preg_match("/^[a-z0-9-.]+\.[a-z]{2,}$/i", $this->value)) { + if (!preg_match("/^[a-z0-9-.]+\.[a-z0-9-]{2,}$/i", $this->value)) { if($set_error) $this->error = 'Ceci n\'est pas un nom de domaine'; return FALSE; } @@ -476,7 +476,7 @@ class DomainListInputFormField extends FormField { if(strlen($this->value)){ $list = explode(',', $this->value); foreach ($list as $value) { - if (!preg_match("/^[a-z0-9-.]+\.[a-z]{2,}$/i", $value)) { + if (!preg_match("/^[a-z0-9-.]+\.[a-z0-9-]{2,}$/i", $value)) { if($set_error) $this->error = 'Ceci n\'est pas un nom de domaine'; return FALSE; }