From cafeec15f58f69572e71797860903f7fed930c4d Mon Sep 17 00:00:00 2001 From: sparkyx Date: Tue, 26 Dec 2023 11:25:35 +0100 Subject: [PATCH] =?UTF-8?q?Task=20#0002318:=20Envoi=20email=20:=20forcer?= =?UTF-8?q?=20le=20domaine=20de=20l'exp=C3=A9diteur=20Adapter=20le=20fichi?= =?UTF-8?q?er=20config?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/class/sendmail.class.php | 40 +++- include/config.inc.example | 5 +- include/lib/config_file.php | 276 +++++++++++++---------- include/lib/sendmail_core.class.php | 4 +- unit-test/include/class/sendmailTest.php | 187 +++++++++++++++ 5 files changed, 386 insertions(+), 126 deletions(-) create mode 100644 unit-test/include/class/sendmailTest.php diff --git a/include/class/sendmail.class.php b/include/class/sendmail.class.php index d8cc8567d..006298fb4 100644 --- a/include/class/sendmail.class.php +++ b/include/class/sendmail.class.php @@ -96,6 +96,7 @@ class Sendmail extends Sendmail_Core { $max_email = $p_repo->get_value("select dos_email from ac_dossier where dos_id=$1", array($p_dossier_id)); + if ($max_email == "") return 0; return $max_email; } /** @@ -114,6 +115,43 @@ class Sendmail extends Sendmail_Core } + + /** + * @brief check if there is a mandatory domain, if yes + * @return void + * @throws Exception + */ + function verify() + { + try { + parent::verify(); + } catch (\Exception $e) { + throw $e; + } + if ( defined('ALLOWED_EMAIL_DOMAIN') ) + { + if ( ALLOWED_EMAIL_DOMAIN != "") + { + $as_domain=explode(",", ALLOWED_EMAIL_DOMAIN); + $valid=0; + foreach ($as_domain as $domain) { + $domain="@$domain"; + if (strpos($this->from,$domain) != 0 ) { + $valid=1; + break; + } + } + if ( $valid == 0) { + throw new Exception("Domaine email {$this->from} interdit",EXC_INVALID); + + } + + } + + } + + } + /** * @brief Add $p_amount_email to email sent * @param $p_repo Database @@ -124,7 +162,7 @@ class Sendmail extends Sendmail_Core { if ( $p_dossier == -1) return ; $email_sent = $this->get_email_sent($p_repo,$p_dossier,$p_date); - if ( empty($email_sent) ){ + if ( $email_sent == 0 ){ $p_repo->exec_sql("insert into public.dossier_sent_email(de_sent_email,dos_id,de_date) values($1,$2,$3)", array(1,$p_dossier,$p_date)); return; diff --git a/include/config.inc.example b/include/config.inc.example index 114108b1b..f98304544 100644 --- a/include/config.inc.example +++ b/include/config.inc.example @@ -88,4 +88,7 @@ define ("DEBUGNOALYSS",0); // define ("AUDIT_ENABLE",true); // // display a captcha -// define ("NOALYSS_CAPTCHA",true); \ No newline at end of file +// define ("NOALYSS_CAPTCHA",true); +// When sending an email , the domain of this email must be in comma separated list , +// if the list is an empty string then all the domain are allowed +// define ('ALLOWED_EMAIL_DOMAIN',''); \ No newline at end of file diff --git a/include/lib/config_file.php b/include/lib/config_file.php index b3d8a9d42..19eee50fc 100644 --- a/include/lib/config_file.php +++ b/include/lib/config_file.php @@ -19,7 +19,8 @@ // Copyright Author Dany De Bontridder danydb@aevalys.eu -/*!\file +/*! + \file * \brief functions concerning the config file config.inc.php. The domain is not set into the form for security issues */ @@ -40,7 +41,8 @@ function is_unix() } -/*!\brief +/* + * !\brief *\param array with the index * - ctmp temporary folder * - cpath path to postgresql @@ -112,63 +114,60 @@ function config_file_form($p_array=null) require NOALYSS_TEMPLATE.'/template_config_form.php'; } /** - * Display the content of the config.inc.php with variables + * @brief Display the content of the config.inc.php with variables * @param type $p_array * @param type $from_setup * @param type $p_os */ -function display_file_config($p_array,$from_setup=1,$p_os=1) +function display_file_config($p_array, $from_setup = 1, $p_os = 1) { extract($p_array, EXTR_SKIP); print ('$name) == "") + if (trim($this->$name??"") == "") { - throw new Exception( sprintf(_("%s est vide"),$name)); + throw new Exception( sprintf(_("%s est vide"),$name),EXC_INVALID); } } } diff --git a/unit-test/include/class/sendmailTest.php b/unit-test/include/class/sendmailTest.php new file mode 100644 index 000000000..1b327c82f --- /dev/null +++ b/unit-test/include/class/sendmailTest.php @@ -0,0 +1,187 @@ + + * + * 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * + * Author : Dany De Bontridder danydb@noalyss.eu $(DATE) + */ + +/** + * @file + * @brief noalyss + */ + +use PHPUnit\Framework\TestCase; + +require DIRTEST . '/global.php'; + +define ("ALLOWED_EMAIL_DOMAIN","linux.org,localhost"); + +/** + * @testdox Class SendmailTest : used for ... + * @backupGlobals enabled + * @coversDefaultClass Sendmail + */ +class SendmailTest extends TestCase +{ + + /** + * @testdox verify that email has the mandatory filed + * @covers Sendmail::verify + * @backupGlobals enabled + */ + function testVerifyCorrect() + { + $sendmail=new \Sendmail(); + $sendmail->mailto("dany@gmail.com"); + $sendmail->set_from("web@localhost"); + $sendmail->set_subject("Test envoi"); + $sendmail->set_message("corps du message"); + try { + $sendmail->verify(); + $this->assertTrue(true); + } catch (\Exception $e) { + $this->assertTrue(false," check email fails"); + } + + } + /** + * @testdox verify that email fails it mailto is absent + * @covers Sendmail::verify + * @backupGlobals enabled + */ + function testVerifyFailsMailto() + { + $sendmail=new \Sendmail(); + $sendmail->mailto(""); + $sendmail->set_from("web@localhost"); + $sendmail->set_subject("Test envoi"); + $sendmail->set_message("corps du message"); + $this->expectExceptionCode(EXC_INVALID); + $sendmail->verify(); + + } + /** + * @testdox verify that email fails it recipient is absent + * @covers Sendmail::verify + * @backupGlobals enabled + */ + function testVerifyFailsFrom() + { + $sendmail=new \Sendmail(); + $sendmail->mailto("web@localhost"); + $sendmail->set_from(""); + $sendmail->set_subject("Test envoi"); + $sendmail->set_message("corps du message"); + $this->expectExceptionCode(EXC_INVALID); + $sendmail->verify(); + + } + /** + * @testdox verify that email fails it subject is absent + * @covers Sendmail::verify + * @backupGlobals enabled + */ + function testVerifyFailsSubject() + { + $sendmail=new \Sendmail(); + $sendmail->mailto("web@localhost"); + $sendmail->set_from("dany@gmail.com"); + $sendmail->set_message("corps du message"); + $this->expectExceptionCode(EXC_INVALID); + $sendmail->verify(); + + } + /** + * @testdox verify that email fails it content is absent + * @covers Sendmail::verify + * @backupGlobals enabled + */ + function testVerifyFailsContent() + { + $sendmail=new \Sendmail(); + $sendmail->mailto(""); + $sendmail->set_from("web@localhost"); + $sendmail->set_subject("Test envoi"); + $this->expectExceptionCode(EXC_INVALID); + $sendmail->verify(); + + } + /** + * @testdox Check that the from of email respect Domain : domain allowed + * @covers Sendmail::verify + * @backupGlobals enabled + */ + function testVerifyDomainFails() + { + $sendmail=new \Sendmail(); + $sendmail->mailto("web@localhost"); + $sendmail->set_from("dany@gmail.com"); + $sendmail->set_subject("Test envoi"); + $sendmail->set_message("corps du message"); + + $this->expectExceptionCode(EXC_INVALID); + $sendmail->verify(); + + } + /** + * @testdox Check that the from of email respect Domain : domain forbidden + * @covers Sendmail::verify + * @backupGlobals enabled + */ + function testVerifyDomainSuccess() + { + $sendmail=new \Sendmail(); + $sendmail->mailto("web@localhost"); + $sendmail->set_from("dany@linux.org"); + $sendmail->set_subject("Test envoi"); + $sendmail->set_message("corps du message"); + + $sendmail->verify(); + $this->assertTrue(true); + + } + /* + *@testdox test increment email + *@covers Sendmail::increment_mail Sendmail::get_email_sent + */ + function testIncrement_mail() + { + $cn=new \Database(); + $dos_id=DOSSIER; + $sendmail=new Sendmail(); + $cn->exec_sql("delete from dossier_sent_email where dos_id=$dos_id and de_date='20100101'"); + $email_sent=$sendmail->get_email_sent($cn, $dos_id, '20100101'); + $this->assertTrue($email_sent == 0,"error cannot count email get $email_sent"); + + $sendmail->increment_mail($cn, $dos_id, "20100101"); + $email_sent=$sendmail->get_email_sent($cn, $dos_id, '20100101'); + $this->assertTrue($email_sent == 1,"error cannot count email get $email_sent"); + + $sendmail->increment_mail($cn, $dos_id, "20100101"); + $email_sent=$sendmail->get_email_sent($cn, $dos_id, '20100101'); + $this->assertTrue($email_sent == 2,"error cannot count email"); + + $sendmail->increment_mail($cn, $dos_id, "20100101"); + $email_sent=$sendmail->get_email_sent($cn, $dos_id, '20100101'); + $this->assertTrue($email_sent == 3,"error cannot count email $email_sent"); + + $cn->exec_sql("delete from dossier_sent_email where dos_id=$dos_id and de_date='20100101'"); + + } +} \ No newline at end of file