From 19fbca2693eabb5ee75a65e76c06d639f838b089 Mon Sep 17 00:00:00 2001 From: "o.pinke" Date: Fri, 29 Apr 2022 21:33:22 +0200 Subject: [PATCH] use phpmailer #4 --- .gitignore | 2 + php/cl_contactform_output.php | 589 +++++++++++++++++----------------- 2 files changed, 305 insertions(+), 286 deletions(-) diff --git a/.gitignore b/.gitignore index 699142a..0bd8605 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,5 @@ dist/ nbdist/ .nb-gradle/ +/php/cl_contactform_output_Dodger.php +/php/cl_contactform_input_Dodger.php diff --git a/php/cl_contactform_output.php b/php/cl_contactform_output.php index 04dd49f..6d32e5e 100644 --- a/php/cl_contactform_output.php +++ b/php/cl_contactform_output.php @@ -1,344 +1,361 @@ email = array( - 'adresses' => '', - 'from' => array( - 'name' => '', - 'email' => '' - ), - 'message' => '', - 'subject' => '' - ); + public function __construct() { + $this->email = array( + 'adresses' => '', + 'from' => array( + 'name' => '', + 'email' => '' + ), + 'message' => '', + 'subject' => '' + ); - $this->form = array( - 'form' => '', - 'answer' => '', - 'colorError' => '' - ); + $this->form = array( + 'form' => '', + 'answer' => '', + 'colorError' => '' + ); + } + + private function sendEmail() { + $this->unravel($this->suppress('sent')); + $this->generateEmailMessage(); + + $oMailer = new PHPMailer(); + $oMailer->AddAddress($this->email['adresses']); + $oMailer->From = $this->email['from']['email']; + $oMailer->FromName = $this->email['from']['name']; + $oMailer->Subject = $this->email['subject']; + $oMailer->Body = $this->email['message']; + + if($oMailer->send()) { + return true; } + + return false; + } - private function sendEmail() { - $this->unravel($this->suppress('sent')); - $this->generateEmailMessage(); - mail( - $this->email['adresses'], - $this->email['subject'], - $this->email['message'], - sprintf( - "From: %s <%s>\nReply-To: %s\nX-Mailer: PHP/%s", - $this->email['from']['name'], - $this->email['from']['email'], - $this->email['from']['email'], - phpversion() - ) - ); - } - - private function unravel($toUnravel, $prefix = '') { - foreach ($toUnravel as $key => $value) { - if (is_array($value)) { - $this->unravel($value, $key . ' '); - } else { - $this->unraveled["{$prefix}{$key}"] = $value; - } - } - } - - private function add2Message($key, $value) { - if (strlen($key) > 25 OR strlen($value) > 54) { - $this->email['message'] .= "$key\n$value\n"; + private function unravel($toUnravel, $prefix = '') { + foreach ($toUnravel as $key => $value) { + if (is_array($value)) { + $this->unravel($value, $key . ' '); } else { - $this->email['message'] .= $key; - $this->email['message'] .= str_repeat(' ', 25 - strlen($key)); - $this->email['message'] .= "$value\n"; + $this->unraveled["{$prefix}{$key}"] = $value; } } + } - private function generateEmailMessage() { - if ($this->unraveled) - foreach ($this->unraveled as $key => $value) { - $this->add2Message($key, $value); - } + private function add2Message($key, $value) { + if (strlen($key) > 25 OR strlen($value) > 54) { + $this->email['message'] .= "$key\n$value\n"; + } else { + $this->email['message'] .= $key; + $this->email['message'] .= str_repeat(' ', 25 - strlen($key)); + $this->email['message'] .= "$value\n"; } + } - private function suppress() { - $suppress = func_get_args(); - foreach ($_POST as $key => $value) { - if (array_search($key, $suppress) === false) - $fields[$key] = $value; + private function generateEmailMessage() { + if ($this->unraveled) + foreach ($this->unraveled as $key => $value) { + $this->add2Message($key, $value); } - return $fields; + } + + private function suppress() { + $suppress = func_get_args(); + foreach ($_POST as $key => $value) { + if (array_search($key, $suppress) === false) + $fields[$key] = $value; } + return $fields; + } - public function addEmailAdress($email) { - if (empty($this->emailAdresses)) { - $this->email['adresses'] .= "$email"; - } else { - $this->email['adresses'] .= ", $email"; - } + public function addEmailAdress($email) { + if (empty($this->emailAdresses)) { + $this->email['adresses'] .= "$email"; + } else { + $this->email['adresses'] .= ", $email"; } + } - public function setEmailSubject($subject) { - $this->email['subject'] = $subject; - } + public function setEmailSubject($subject) { + $this->email['subject'] = $subject; + } - public function setEmailFrom($email, $name) { - $this->email['from']['email'] = $email; - $this->email['from']['name'] = $name; - } + public function setEmailFrom($email, $name) { + $this->email['from']['email'] = $email; + $this->email['from']['name'] = $name; + } - public function setForm($form) { - $this->form['form'] = $form; - } + public function setForm($form) { + $this->form['form'] = $form; + } - public function setAnswer($answer) { - $this->form['answer'] = $answer; - } + public function setAnswer($answer) { + $this->form['answer'] = $answer; + } - public function setBackgroundError($color) { - $this->form['colorError'] = $color; - } + public function setBackgroundError($color) { + $this->form['colorError'] = $color; + } - public function formInterpretation(&$form) { - $fields = explode('###', $form); - $field = array(); + public function formInterpretation(&$form) { + $fields = explode('###', $form); + $field = array(); - for ($i = 1; $i < count($fields); $i = $i + 2) { - $attributte = explode(';', trim($fields[$i])); - foreach ($attributte as $attribute) { - $nameValue = explode(':', trim($attribute)); - if ($nameValue[0] != 'option' && $nameValue[0] != 'optionvalue') { - $field["{$fields[$i]}"]["{$nameValue[0]}"] = $nameValue[1]; - } else { - $field["{$fields[$i]}"]["{$nameValue[0]}"][] = $nameValue[1]; - } - } - } - - return $field; - } - - public function formOutput($sent = false) { - echo '
'; - echo ''; - $form = $this->form['form']; - $formData = $this->formInterpretation($form); - $form = explode('###', $form); - - foreach ($form as $item) { - if (!empty($formData["{$item}"])) { - $this->formField($formData["{$item}"], $sent); + for ($i = 1; $i < count($fields); $i = $i + 2) { + $attributte = explode(';', trim($fields[$i])); + foreach ($attributte as $attribute) { + $nameValue = explode(':', trim($attribute)); + if ($nameValue[0] != 'option' && $nameValue[0] != 'optionvalue') { + $field["{$fields[$i]}"]["{$nameValue[0]}"] = $nameValue[1]; } else { - echo $item; + $field["{$fields[$i]}"]["{$nameValue[0]}"][] = $nameValue[1]; } } - - echo '
'; } - public function formField($attribute, $sent) { - $parameter = "name=\"{$attribute['name']}\""; - if ($sent && !$this->formFieldCorrect($attribute)) - $style = "style=\"background-color:{$this->form['colorError']};\""; + return $field; + } - switch ($attribute['type']) { - case 'select': - case 'password': - case 'text': - if (!empty($attribute['size'])) - $parameter .= " size=\"{$attribute['size']}\""; - break; + public function formOutput($sent = false) { + echo '
'; + echo ''; + $form = $this->form['form']; + $formData = $this->formInterpretation($form); + $form = explode('###', $form); + + foreach ($form as $item) { + if (!empty($formData["{$item}"])) { + $this->formField($formData["{$item}"], $sent); + } else { + echo $item; } + } - switch ($attribute['type']) { - case 'textarea': - case 'text': - if (!empty($attribute['size'])) - $parameter .= " size=\"{$attribute['size']}\""; - if (!empty($attribute['value'])) - $value = $attribute['value']; - if (!empty($_POST["{$attribute['name']}"])) - $value = $_POST["{$attribute['name']}"]; - break; - } + echo '
'; + } - switch ($attribute['type']) { - case 'text': - echo ""; - break; - case 'password': - echo ""; - break; - case 'textarea': - echo ""; - break; - case 'select': - echo ""; + break; + case 'password': + echo ""; + break; + case 'textarea': + echo ""; + break; + case 'select': + echo ""; - break; - case 'checkbox': - $formId = preg_split('[\[|\]]', $attribute['name']); - if ($sent) { - if ($_POST["{$formId[0]}"]["{$formId[1]}"] == $attribute['value']) { - echo ""; - } else { - echo ""; - } - } else { - if (!empty($attribute['selected']) && $attribute['selected'] == 'true') { - echo ""; - } else { - echo ""; - } - } - break; - case 'radio': - if (!empty($_POST["{$attribute['name']}"])) { - if ($_POST["{$attribute['name']}"] == $attribute['value']) { - echo ""; - } else { - echo ""; - } - } else { - if (!empty($attribute['selected']) && $attribute['selected'] == 'true') { - echo ""; - } else { - echo ""; - } - } - break; - } - } - - private function formComplete() { - $form = $this->form['form']; - $fields = $this->formInterpretation($form); - - foreach ($fields as $field) { - if (!$this->formFieldCorrect($field)) { - return false; } - } + echo ""; + break; + case 'checkbox': + $formId = preg_split('[\[|\]]', $attribute['name']); + if ($sent) { + if ($_POST["{$formId[0]}"]["{$formId[1]}"] == $attribute['value']) { + echo ""; + } else { + echo ""; + } + } else { + if (!empty($attribute['selected']) && $attribute['selected'] == 'true') { + echo ""; + } else { + echo ""; + } + } + break; + case 'radio': + if (!empty($_POST["{$attribute['name']}"])) { + if ($_POST["{$attribute['name']}"] == $attribute['value']) { + echo ""; + } else { + echo ""; + } + } else { + if (!empty($attribute['selected']) && $attribute['selected'] == 'true') { + echo ""; + } else { + echo ""; + } + } + break; + } + } - return true; + private function formComplete() { + $form = $this->form['form']; + $fields = $this->formInterpretation($form); + + foreach ($fields as $field) { + if (!$this->formFieldCorrect($field)) { + return false; + } } - private function success() { - $this->sendEmail(); + return true; + } + + private function success() { + if($this->sendEmail()) { echo $this->form['answer']; + } else { + echo ''.mi18n("Es ist ein Fehler aufgetreten!
Bitte versuchen Sie es später noch einmal.").'
'; + } + } + + private function formFieldCorrect(&$field) { + + $tmp_name = rtrim($field['name'], '[0]'); + preg_match('/\[(\d*)\]/', $field['name'], $matches); + $bEmptyPost = false; + if (is_array($_POST[$tmp_name])) { + if (empty($_POST[$tmp_name][$matches[1]])) { + $bEmptyPost = true; + } + } elseif (empty($_POST[$tmp_name])) { + $bEmptyPost = true; } - private function formFieldCorrect(&$field) { - // prüfung, ob pflichtfeld vorhanden - if (!empty($field['mandatory']) && $field['mandatory'] == 'true' && empty($_POST["{$field['name']}"])) - return false; - - // wenn das formularfeld kein pflichtfeld und nicht vorhanden ist, true zurück geben - if (empty($_POST["{$field['name']}"])) - return true; - - // regular expression prüfungen - if (!empty($field['valid']) && $field['valid'] == 'simpletext' && !preg_match("/^[öäüéàèâêîça-z-]*$/i", $_POST["{$field['name']}"])) - return false; - if (!empty($field['valid']) && $field['valid'] == 'text' && !preg_match("/^[ .,;!?()öäüéàèâêîça-z-]*$/i", $_POST["{$field['name']}"])) - return false; - if (!empty($field['valid']) && $field['valid'] == 'integer' && !preg_match("/^[0-9]*$/", $_POST["{$field['name']}"])) - return false; - if (!empty($field['valid']) && $field['valid'] == 'float' && !preg_match("/^[0-9]*[.]{0,1}[0-9]*$/", $_POST["{$field['name']}"])) - return false; - if (!empty($field['valid']) && $field['valid'] == 'date' && !preg_match("/^[0-9]{1,2}.[0-9]{1,2}.[0-9]{2}$/", $_POST["{$field['name']}"])) - return false; - if (!empty($field['valid']) && $field['valid'] == 'email' && !preg_match("/^[öäüéàèâêîç_a-z0-9-]+(\.[öäüéàèâêîç_a-z0-9-]+)*@[öäüéàèâêîça-z0-9-]+(\.[öäüéàèâêîça-z0-9-]+)*$/i", $_POST["{$field['name']}"])) - return false; - - // grössenbereich bei integer und float prüfen - if (!empty($field['minvalue']) && $_POST["{$field['name']}"] < $field['minvalue']) - return false; - if (!empty($field['maxvalue']) && $_POST["{$field['name']}"] > $field['maxvalue']) - return false; - - // längenbereich bei allen typen prüfen - if (!empty($field['minlength']) && strlen($_POST["{$field['name']}"]) < $field['minlength']) - return false; - if (!empty($field['maxlength']) && strlen($_POST["{$field['name']}"]) > $field['maxlength']) - return false; + if (!empty($field['mandatory']) && $field['mandatory'] == 'true' && $bEmptyPost) { + return false; + } + // wenn das formularfeld kein pflichtfeld und nicht vorhanden ist, true zurück geben + if ($bEmptyPost) { return true; } - public function process() { - if (!isset($_POST['sent'])) { - $this->formOutput(); - } elseif ($this->formComplete()) { - $this->success(); - } else { - $this->formOutput(true); - } + // regular expression prüfungen + if (!empty($feld['valid']) && $feld['valid'] == 'textmitumbruch' && !preg_match("/^[ \n\r.,;!?()öäüéàèâêîça-z0-9-]*$", $_POST["{$field['name']}"])) + return false; + if (!empty($field['valid']) && $field['valid'] == 'simpletext' && !preg_match("/^[öäüéàèâêîça-z-]*$/i", $_POST["{$field['name']}"])) + return false; + if (!empty($field['valid']) && $field['valid'] == 'text' && !preg_match("/^[ .,;!?()öäüéàèâêîça-z-]*$/i", $_POST["{$field['name']}"])) + return false; + if (!empty($field['valid']) && $field['valid'] == 'integer' && !preg_match("/^[0-9]*$/", $_POST["{$field['name']}"])) + return false; + if (!empty($field['valid']) && $field['valid'] == 'float' && !preg_match("/^[0-9]*[.]{0,1}[0-9]*$/", $_POST["{$field['name']}"])) + return false; + if (!empty($field['valid']) && $field['valid'] == 'date' && !preg_match("/^[0-9]{1,2}.[0-9]{1,2}.[0-9]{2}$/", $_POST["{$field['name']}"])) + return false; + if (!empty($field['valid']) && $field['valid'] == 'email' && !preg_match("/^[öäüéàèâêîç_a-z0-9-]+(\.[öäüéàèâêîç_a-z0-9-]+)*@[öäüéàèâêîça-z0-9-]+(\.[öäüéàèâêîça-z0-9-]+)*$/i", $_POST["{$field['name']}"])) + return false; + // grössenbereich bei integer und float prüfen + if (!empty($field['minvalue']) && $_POST["{$field['name']}"] < $field['minvalue']) + return false; + if (!empty($field['maxvalue']) && $_POST["{$field['name']}"] > $field['maxvalue']) + return false; + + // längenbereich bei allen typen prüfen + if (!empty($field['minlength']) && strlen($_POST["{$field['name']}"]) < $field['minlength']) + return false; + if (!empty($field['maxlength']) && strlen($_POST["{$field['name']}"]) > $field['maxlength']) + return false; + + return true; + } + + public function process() { + if (!isset($_POST['sent'])) { + $this->formOutput(); + } elseif ($this->formComplete()) { + $this->success(); + } else { + $this->formOutput(true); } - } - if (isset($edit) && $edit) { - echo "

" . mi18n("Formularkonfiguration") . "

"; - echo "

" . mi18n("Hier ist das Formular sowie der Text einzugeben, der zusammen mit dem Formular ausgegeben werden soll:") . "

"; - echo "CMS_HTML[0]"; - echo "

" . mi18n("Hier ist die Ausgabe einzugeben, die erscheint, wenn das Formular erfolgreich prozessiert worden ist:") . "

"; - echo "CMS_HTML[1]"; - } else { - $form = new w3form(); - $form->addEmailAdress("CMS_VALUE[0]"); - $form->setEmailSubject("CMS_VALUE[1]"); - $form->setEmailFrom("CMS_VALUE[2]", "CMS_VALUE[3]"); - $form->setBackgroundError("CMS_VALUE[4]"); - $form->setForm("CMS_HTML[0]"); - $form->setAnswer("CMS_HTML[1]"); - $form->process(); - } +} - ?> \ No newline at end of file +if (isset($edit) && $edit) { + echo "

" . mi18n("Formularkonfiguration") . "

"; + echo "

" . mi18n("Hier ist das Formular sowie der Text einzugeben, der zusammen mit dem Formular ausgegeben werden soll:") . "

"; + echo "CMS_HTML[0]"; + echo "

" . mi18n("Hier ist die Ausgabe einzugeben, die erscheint, wenn das Formular erfolgreich prozessiert worden ist:") . "

"; + echo "CMS_HTML[1]"; +} else { + $form = new w3form(); + $form->addEmailAdress("CMS_VALUE[0]"); + $form->setEmailSubject("CMS_VALUE[1]"); + $form->setEmailFrom("CMS_VALUE[2]", "CMS_VALUE[3]"); + $form->setBackgroundError("CMS_VALUE[4]"); + $form->setForm("CMS_HTML[0]"); + $form->setAnswer("CMS_HTML[1]"); + $form->process(); +} +?> \ No newline at end of file