File: /var/www/vhosts/creativefellows.nl/oudlondon.creativefellows.nl/src/Action/Form/FormPostAction.php
<?php
namespace App\Action\Form;
use App\Action\Action;
use App\Domain\Mail\FormMailer;
use App\Domain\Form\Service\FormReader;
use App\Domain\Page\Service\PageReader;
use App\Domain\Upload\Service\UploadReader;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Container\ContainerInterface;
use Slim\Interfaces\RouteParserInterface;
use ReCaptcha\ReCaptcha;
final class FormPostAction extends Action
{
protected $container;
protected $routeParser;
protected $mailer;
protected $form;
protected $pageReader;
protected $uploadReader;
public function __construct(
ContainerInterface $container,
RouteParserInterface $routeParser,
FormMailer $mailer,
PageReader $pageReader,
FormReader $form,
UploadReader $uploadReader
) {
$this->container = $container;
$this->routeParser = $routeParser;
$this->mailer = $mailer;
$this->form = $form;
$this->pageReader = $pageReader;
$this->uploader = $uploadReader;
$this->template_path = $this->container->get('settings')['email_template_path'];
$this->domain_path = $this->container->get('settings')['domain_path'];
$this->email_settings = $this->container->get('settings')['email_settings'];
}
private function captchaSecretKey()
{
return $this->container->get('settings')['google_captcha']["server"];
}
public function action() : ResponseInterface
{
// arguments
$id = $this->args['id'] ?? null;
$name = $this->args['name'] ?? null;
// Post
$formData = $this->request->getParsedBody();
// iupload
$files = $this->uploader->uploadFiles( $this->request->getUploadedFiles() );
// merge array
$formData = array_merge($formData, $files);
// validate captcha
//$recaptcha = new ReCaptcha( $this->captchaSecretKey() );
//$captcha_verify = $recaptcha->verify($formData['g-recaptcha-response'], $_SERVER['REMOTE_ADDR']);
// form
$form = $this->form->getForm($id);
// set post data in fields
$form->setPostData($formData);
// check if passed cpatcha test
/*if (!$captcha_verify->isSuccess())
{
// form submit failed, return to original page
$page = $this->pageReader->getPageDataById( $formData["uid"] );
return $this->response
->withHeader('Location', $this->domain_path . $page->urlOriginal() .'?hash='. $form->hash() .'&s=0' )
->withStatus(200);
}*/
// send copy to user
$form->setUserEmail();
// check honeypot field
$honeypot = $form->honeyPot();
if( $honeypot->isSpam() )
{
// form submit failed, return to original page
$page = $this->pageReader->getPageDataById( $formData["uid"] );
// return to referer page
return $this->response
->withHeader('Location', $page->url() .'?hash='. $form->hash() .'&s=0' )
->withStatus(200);
}
// set up template
$email = $this->replaceText("template.html",[
"/{logo_path}/" => $this->domain_path . $this->email_settings["logo"],
"/{company_name}/" => $this->email_settings["name"],
"/{primary_color}/" => $this->email_settings["primary_color"],
"/{secondary_color}/" => $this->email_settings["secondary_color"],
"/{subject}/" => $form->name(),
"/{email_intro}/" => $form->emailIntro(),
"/{body_html}/" => $form->getEmailHtml(),
"/{contact_us}/" => $this->routeParser->urlFor('page.view',["section" => "contact"]),
]);
// send email to user
$this->mailer->body( $email );
$this->mailer->subject( $form->name() );
$this->mailer->from($this->email_settings );
$this->mailer->to( $form->receivers() );
// send email
$this->mailer->send();
// unset form storage
unset($_SESSION["form"]);
// return to complete page
return $this->response
->withHeader('Location', $this->routeParser->urlFor('form.sent',["page" => "formulier-verstuurd", "name" => $form->url() ]))
->withStatus(200);
}
private function replaceText($template_email_name, $replacements)
{
$text_email = file_get_contents($this->template_path . $template_email_name);
$keys = array_keys($replacements);
$values = array_values($replacements);
return preg_replace($keys,$values,$text_email);
}
}