HEX
Server: Apache
System: Linux v38079.2is.nl 3.10.0-1160.119.1.el7.x86_64 #1 SMP Tue Jun 4 14:43:51 UTC 2024 x86_64
User: democfellows (10015)
PHP: 8.1.34
Disabled: opcache_get_status
Upload Files
File: /var/www/vhosts/creativefellows.nl/fvr.creativefellows.nl/src/Action/Form/FormPostAction.php
<?php

namespace App\Action\Form;

use App\Action\Action;
use App\Domain\Page\Service\PageReader;
use App\Domain\Mail\FormMailer;
use App\Domain\Form\Service\FormReader;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Container\ContainerInterface;
use Slim\Interfaces\RouteParserInterface;



final class FormPostAction extends Action
{	
    protected $container;	
	protected $routeParser;
	protected $mailer;
	protected $form;
	protected $pageReader;
	
    public function __construct(
		ContainerInterface $container, 
		RouteParserInterface $routeParser,
		PageReader $pageReader, 
		FormMailer $mailer,
		FormReader $form
	) {
        $this->container 	= $container;
		$this->routeParser	= $routeParser;
		$this->mailer 		= $mailer;
		$this->form			= $form;
		$this->pageReader 	= $pageReader;
		
		$this->template_path = $this->container->get('settings')['email']['template_path'];
		$this->email_from 	 = $this->container->get('settings')['email']['from'];		
		$this->domain_path 	 = $this->container->get('settings')['domain_path'];
		
	
    }
	
    public function action() : ResponseInterface
	{
		// Arguments
	    $id = ($this->args['id'] ?? null);
	    $name = ($this->args['name'] ?? null);
			
        // Post
        $formData = $this->request->getParsedBody();

		// Referer uid
		$page_uid = $formData["uid"] ?? null;
		
		$slider_validate	= isset($formData["slider-validate"]) ? $formData["slider-validate"] : null;
		$slide_is_human		= isset($formData["slided_by_human"]) ? $formData["slided_by_human"] : null;
		
		// form
		$form = $this->form->getForm($id);
		
		// Validate slider				
		if(is_numeric($id) && $slider_validate !== null && $slider_validate == $slide_is_human)
		{	
						
			// set post data in fields
			$form->setPostData($formData);
			
			// set up template	
			$email = $this->replaceText("template.html",[
				"/{domain_path}/" => $this->domain_path,
				"/{body_html}/" => $form->getEmailHtml()
			]);
		
				// send email to user
			$this->mailer->body( $email );
			$this->mailer->subject( $form->name() );
			$this->mailer->from( $this->email_from );
			$this->mailer->to( $form->receivers() );
			$this->mailer->send();
					
			// return to complete page
	        return $this->response
				->withHeader('Location', $this->routeParser->urlFor('page.view',["section" => "contact", "page" => "formulier-verstuurd"]).'?hash='. $form->hash() .'#'. $form->url() )
				->withStatus(200);	
		
		}
		
		// form submit failed, return to original page
		$page = $this->pageReader->getPageDataById($page_uid);
	
        return $this->response
			->withHeader('Location', $page->getUrl().'?hash='. $form->hash().'&s=0' )
			->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);
	
	}
		
}