<?php
namespace App\Application\Controller;
use App\Admin\Document\AbstractSEODocument;
use App\Admin\Document\Client;
use App\Admin\Document\Config;
use App\Admin\Document\FeedItemType;
use App\Admin\Document\Order;
use App\Admin\Document\Page;
use App\Admin\Document\UserAccount;
use App\Application\Cart\Cart;
use App\Application\Twig\GlobalLoader;
use Doctrine\ODM\MongoDB\DocumentManager;
use Doctrine\ODM\MongoDB\Query\Builder;
use Doctrine\ODM\MongoDB\Repository\DocumentRepository;
use Psr\Container\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
use AvenueAdminBundle\Controller\AbstractApplicationController;
abstract class AbstractController extends AbstractApplicationController
{
/**
* @var Cart
*/
protected $cart;
public function getCart(): Cart
{
return $this->cart;
}
/**
* @param Cart $cart
* @required
*/
public function setCart(Cart $cart) {
$this->cart = $cart;
}
/**
* @return Config
*/
protected function getConfig() {
return $this->getDocumentRepository(Config::class)->findOneBy([]);
}
protected function setSeoByUrl($url, $locale)
{
$page = $this->getDocumentRepository(Page::class)->findOneBy(
['url' => $url, 'active' => true]
);
if ($page) {
$this->setSeo($page, $locale);
} else {
throw $this->createNotFoundException('page not found or not active: ' . $url);
}
return $page;
}
protected function setSeo(AbstractSEODocument $document, $locale, $page = null) {
$this->setSeoVariables(
$document->getValidSeoTitle($locale) . ($page > 1 ? ". Страница " . $page : ''),
$document->getSeoDescription($locale) . ($page > 1 ? ". Страница " . $page : ''),
$document->getSeoKeywords($locale)
);
}
protected function setSeoVariables($seoTitle, $seoDescription = '', $seoKeywords = '')
{
parent::setSeoVariables($seoTitle . ' ' . $this->getConfig()->getSeoTail(), $seoDescription, $seoKeywords);
}
protected function trimPhone($phone) {
$s = mb_ereg_replace('[^0-9]', '', $phone);
if (strlen($s) == 1) {
$s = '';
}
return $s;
}
/**
* @return UserAccount
*/
protected function getLoggedInUser() {
return $this->getIdentity();
}
private $directorTag;
protected function getDirectorTag() {
if (!$this->directorTag) {
$this->directorTag = $this->getDocumentRepository(FeedItemType::class)->findOneBy(['url' => FeedItemType::URL_DIRECTOR]);
}
return $this->directorTag;
}
protected function printPdf($fileName, $html) {
$filePath = sys_get_temp_dir() . '/' . uniqid();
@unlink($filePath);
$temp = tempnam(sys_get_temp_dir(), 'PDF') . '.html';
if (!file_put_contents($temp, $html)) {
throw new \Exception('cannot write to ' . $temp);
}
$output = [];
$result = -1;
$documentFields = '-T 10mm -B 10mm -L 15mm -R 15mm';
$command = sprintf('/usr/local/bin/wkhtmltopdf' . ' ' . $documentFields . ' '
. '--footer-center "[page]" --footer-font-size 9 --footer-font-name "Times New Roman" --footer-spacing 5'
. ' %1$s "%2$s" 2>&1', $temp, $filePath);
exec($command, $output, $result);
@unlink($temp);
if ($result !== 0) {
throw new \Exception("error code $result: " . $command . ': ' . var_export($output, true));
}
if (!file_exists($filePath)) {
throw new \Exception('cannot create pdf: ' . $command . ': ' . var_export($output, true));
}
$content = file_get_contents($filePath);
header('Content-Type: application/pdf');
header('Content-disposition: inline; filename="' . $fileName . '.pdf"');
header('Cache-Control: no-cache, no-store, must-revalidate');
echo $content;
die;
}
protected function sendEmailOrderPaid(Order $order) {
if ($order->getUserAccount() && $order->getUserAccount()->getEmail()
) {
$text = $this->renderView(
'application/mail/paid.html.twig', [
'order' => $order,
]
);
try {
$this->getMail()->send($order->getUserAccount()->getEmail(), 'Спасибо за оплату заказа!', $text);
} catch (\Exception $e) {
}
}
}
/**
* @return GlobalLoader
*/
protected function getGlobal()
{
return $this->getTwig()->getGlobals()['global'];
}
/**
* @return int
*/
protected function getDefaultItemCountPerPage(){
return 6;
}
protected function detectCaptchaBot(Request $request)
{
$captcha = $request->get('token');
if (!$captcha) {
throw new \Exception('no token');
}
$secret = $this->getParameter('recaptcha_secret_key');
$url = "https://www.google.com/recaptcha/api/siteverify";
$data = [
'secret' => $secret,
'response' => $captcha,
];
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($data)
)
);
$context = @stream_context_create($options);
$json = @file_get_contents($url, false, $context);
$response = @json_decode($json, true);
if ($response !== null) {
if ($response['success'] == true) {
if ($response['score'] < 0.5) {
throw new \Exception('bot detected by recaptcha ' . $response->score);
}
} else {
if (in_array('browser-error', $response['error-codes'])) {
throw new \Exception('recaptcha error: ' . $json);
}
}
}
}
}