<?php
namespace App\Controller\Core;
use App\Entity\BaseForm;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use App\Manager\InfoManager;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\HttpFoundation\RequestStack;
class BaseFrontController extends AbstractController
{
protected array $locals = [];
protected RequestStack $requestStack;
protected InfoManager $infoManager;
/**
* @param RequestStack $requestStack
*/
public function __construct(RequestStack $requestStack, InfoManager $infoManager)
{
$this->requestStack = $requestStack;
$this->infoManager = $infoManager;
$this->locals['menu_active'] = '';
}
protected function getForm(string $type, BaseForm $entity, string $route, array $parameters = []): FormInterface
{
return $this->createForm($type, $entity, [
'action' => $this->generateUrl($route, $parameters),
'method' => 'POST',
]);
}
protected function getFormView(string $type, BaseForm $entity, string $route, array $parameters = []): FormView
{
return $this->getForm($type, $entity, $route, $parameters)->createView();
}
protected function saveFormSession(string $key, BaseForm $value)
{
$session = $this->requestStack->getSession();
$session->set($key, $value);
}
protected function getFormSession(string $key, $default = null): ?BaseForm
{
$session = $this->requestStack->getSession();
$form = $session->get($key, $default);
$session->remove($key);
return $form;
}
protected function captchaValidate(){
$request = $this->requestStack->getCurrentRequest();
$captcha = $request->request->get('g-recaptcha-response','');
$info = $this->infoManager->find(1);
if( !($info->getCaptchaSite() &&
$info->getCaptchaSecret() &&
$info->getCaptchaShow()) ) {
return true;
}
$captcha_url = 'https://www.google.com/recaptcha/api/siteverify';
$captcha_data = array(
'secret' => $info->getCaptchaSecret(),
'response' => $captcha,
'remoteip' => $request->getClientIp()
);
$resp = $this->googleCurl($captcha_url, $captcha_data);
if ( array_key_exists('success',$resp) && $resp['success'] ) {
return true;
}
return false;
}
protected function googleCurl($url, $data): ? array
{
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT ,3);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_TIMEOUT, 20);
$response = curl_exec($ch);
curl_close($ch);
return json_decode($response, true);
}
}