src/Controller/DefaultController.php line 66

  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\MailSecondarieUtenti;
  4. use App\Entity\PagineEComponenti;
  5. use App\Entity\User;
  6. use App\Model\Mailer;
  7. use App\Model\Security\ReCaptchaGoogle;
  8. use App\Model\Translating\Translating;
  9. use Doctrine\ORM\EntityManagerInterface;
  10. use Psr\Log\LoggerInterface;
  11. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  12. use Symfony\Component\HttpFoundation\Cookie;
  13. use Symfony\Component\HttpFoundation\File\UploadedFile;
  14. use Symfony\Component\HttpFoundation\Request;
  15. use Symfony\Component\HttpFoundation\Response;
  16. use Symfony\Component\HttpKernel\KernelInterface;
  17. use Symfony\Component\Routing\Annotation\Route;
  18. use Twig\Environment;
  19. /**
  20.  *
  21.  */
  22. class DefaultController extends AbstractController
  23. {
  24.     /** @var LoggerInterface */
  25.     private $logger;
  26.     /** @var ReturnImpostazioniServiceController */
  27.     private $imp;
  28.     /** @var KernelInterface */
  29.     private $kernel;
  30.     /** @var ServiziController */
  31.     private $servizi;
  32.     /** @var Environment */
  33.     private $twig;
  34.     /** @var Translating */
  35.     private $translating;
  36.     /** @var Mailer */
  37.     private $mailer;
  38.     /**
  39.      * DefaultController constructor.
  40.      * @param LoggerInterface $logger
  41.      * @param ReturnImpostazioniServiceController $imp
  42.      * @param KernelInterface $kernel
  43.      * @param ServiziController $servizi
  44.      * @param Environment $twig
  45.      * @param Translating $translating
  46.      * @param Mailer $mailer
  47.      */
  48.     public function __construct(LoggerInterface $logger, ReturnImpostazioniServiceController $imp, KernelInterface $kernel, ServiziController $servizi, Environment $twig, Translating $translating, Mailer $mailer)
  49.     {
  50.         $this->logger = $logger;
  51.         $this->imp = $imp;
  52.         $this->kernel = $kernel;
  53.         $this->servizi = $servizi;
  54.         $this->twig = $twig;
  55.         $this->translating = $translating;
  56.         $this->mailer = $mailer;
  57.     }
  58.     /**
  59.      * @Route("/", name="homepage")
  60.      */
  61.     public function indexAction(Request $request)
  62.     {
  63.         $customHomePage = $this->imp->ritornaImpostazioneDaNome("CustomHomepage");
  64.         if ($customHomePage != ''){
  65.             /** @var PagineEComponenti $pagina */
  66.             $pagina = $this->servizi->doctrine->getRepository(PagineEComponenti::class)->find($customHomePage);
  67.             if ($pagina){
  68.                 $response =  $this->render('ZZ_front_end/pagine_secondarie/pagine_secondarie_render.html.twig', array(
  69.                     'pagina' => $pagina
  70.                 ));
  71.                 return $response;
  72.             }else{
  73.                 $response = new Response("", '404');
  74.                 return $response;
  75.             }
  76.         }else {
  77.             /** @var User $user */
  78.             $user = $this->servizi->OttieniUtenteCorrente();
  79.             if ($user) {
  80.                 if ($user->getUtenteComune()) {
  81.                     $response = $this->redirectToRoute('home_shop_comune');
  82.                 } else
  83.                     $response = $this->redirectToRoute('home_shop');
  84.             } else {
  85.                 $response = $this->redirectToRoute('home_shop_comune');
  86.             }
  87.             return $response;
  88.         }
  89.     }
  90.     /**
  91.      * @Route("/products/home", name="home_prodotti")
  92.      */
  93.     public function productsAction(Request $request)
  94.     {
  95.         /** @var User $user */
  96.         $user = $this->servizi->OttieniUtenteCorrente();
  97.         if ($user) {
  98.             if ($user->getUtenteComune()) {
  99.                 return $this->redirectToRoute('home_shop_comune');
  100.             } else
  101.                 return $this->redirectToRoute('home_shop');
  102.         } else {
  103.             if ($this->imp->ritornaImpostazioneDaNome('AbilitaB2C') != '1'){
  104.                 return $this->redirectToRoute('home_login');
  105.             }
  106.             return $this->redirectToRoute('home_shop_comune');
  107.         }
  108.     }
  109.     /**
  110.      * @Route("/admin/uploadFileAsset", name="admin_upload_file_asset", options={"expose"=true})
  111.      */
  112.     public function uploadFileInAsset(Request $request)
  113.     {
  114.         $pathStore = $this->kernel->getProjectDir()  . DIRECTORY_SEPARATOR . "public" . DIRECTORY_SEPARATOR . "uploads" . DIRECTORY_SEPARATOR;
  115.         $pathRitorno = DIRECTORY_SEPARATOR ."uploads" . DIRECTORY_SEPARATOR;
  116.         $listaFile = $request->files;
  117.         /**
  118.          * @var  $key string
  119.          */
  120.         foreach ($listaFile as $value){
  121.             /** @var  $file UploadedFile */
  122.             $file = $value[0];
  123.             $nome = time() . "-" . $file->getClientOriginalName();
  124.             $file->move($pathStore, $nome);
  125.         }
  126.         return new Response("");
  127.     }
  128.     /**
  129.      * @Route("/setLocale/{locale}", name="set_locale")
  130.      */
  131.     public function setlocale(Request $request, $locale)
  132.     {
  133.         $request->getSession()->set('_locale', $locale);
  134.         $referral = $request->server->get('HTTP_REFERER');
  135.         $response = $this->redirect($referral);
  136.         $response->headers->setCookie(new Cookie('_locale', $locale, strtotime( '+1 year' ), '/'));
  137.         return $response;
  138.     }
  139.     /**
  140.      * @param EntityManagerInterface $em
  141.      * @param string $ip
  142.      * @param string $debug
  143.      * @param boolean $esito
  144.      * @param string $messaggio
  145.      * @param User $utente
  146.      */
  147.     function ScriviRegistroAttivita($em, $ip, $debug, $esito, $messaggio, $utente)
  148.     {
  149.         $this->servizi->ScriviRegistroAttivita($ip, $debug, $esito, $messaggio, $utente);
  150.     }
  151.     /**
  152.      * @Route("/contatto/mail", methods={"POST"}, name="contatto_per_mail")
  153.      */
  154.     public function contattoPerMail(Request $request){
  155.         $sendMail = new Mailer($this->imp, $this->twig, $this->servizi);
  156.         $mailFrom = $this->imp->ritornaImpostazioneDaNome('IndirizzoMailFromInvioComunicazioni');
  157.         $oggetto = $request->get('ddflt-oggetto');
  158.         $mailInvio = $request->get('ddflt-mail-invio');
  159.         $mailInvioCc = $request->get('ddflt-mail-invio-cc');
  160.         $traduzione = new Translating($this->servizi->doctrine, $this->logger);
  161.         $traduzione->CaricaTraduzioniDaFamiglia('form_contatto', $request->getLocale());
  162.         $parametri = $request->request->keys();
  163.         $procedi = true;
  164.         if($oggetto != '' && $mailInvio != ''){
  165.             if (array_key_exists('recaptcha', $parametri)){
  166.                 /*
  167.                  * Il captcha deve essere un input con nome = recaptcha e class = re-captcha-v3
  168.                  */
  169.                 $token = $parametri['recaptcha'];
  170.                 if (!ReCaptchaGoogle::captchaverify($token, $this->imp)){
  171.                     $procedi = false;
  172.                     $this->addFlash(
  173.                         'error',
  174.                         $traduzione->OttieniElementoDizionario('form_contatto_captcha_error', 'form_contatto')->valoreUscita
  175.                     );
  176.                 }
  177.             }
  178.             if($procedi){
  179.                 $testoMail = $traduzione->OttieniElementoDizionario('form_contatto_titolo', 'form_contatto')->valoreUscita . '<br /><br />
  180.                     <table class="table table-responsive table-striped">
  181.                     <thead>
  182.                     <tr>
  183.                         <th>' . $traduzione->OttieniElementoDizionario('form_contatto_parametro', 'form_contatto')->valoreUscita . '</th>
  184.                         <th>' . $traduzione->OttieniElementoDizionario('form_contatto_valore', 'form_contatto')->valoreUscita . '</th>
  185.                     </tr>
  186.                     </thead>
  187.                     <tbody>';
  188.                 foreach ($parametri as $key) {
  189.                     if ($key != ('recaptcha'||'ddflt-mail-invio'||'ddflt-mail-invio-cc'||'ddflt-oggetto')) {
  190.                         $testoMail .= '<tr><td>' . $key . '</td><td>' . $request->request->get($key) . '</td></tr>';
  191.                     }
  192.                 }
  193.                 $testoMail .= '</tbody></table>';
  194.                 $sendMail->SendMail($oggetto, $testoMail, explode(';', $mailFrom), explode(';', $mailInvioCc), '', '', true);
  195.                 $this->addFlash(
  196.                     'notice',
  197.                     $traduzione->OttieniElementoDizionario('form_contatto_messaggio_inviato', 'form_contatto')->valoreUscita
  198.                 );
  199.             }
  200.         }else{
  201.             $procedi = false;
  202.             $this->addFlash(
  203.                 'error',
  204.                 $traduzione->OttieniElementoDizionario('form_contatto_captcha_error', 'form_contatto')->valoreUscita
  205.             );
  206.         }
  207.         return $this->redirect($this->servizi->getRefererRoute($request));
  208.     }
  209.     /**
  210.      * @Route("/contact/form", name="contatto_form", methods={"POST"})
  211.      */
  212.     public function contattoDaForm(Request $request){
  213.         $traduzione = $this->translating;
  214.         $traduzione->CaricaTraduzioniDaFamiglia('form_contatto', $request->getLocale());
  215.         $mailInvioContatto = $this->imp->ritornaImpostazioneDaNome('MailInvioContattiForm');
  216.         $mailFrom = $this->imp->ritornaImpostazioneDaNome('IndirizzoMailFromInvioComunicazioni');
  217.         $arrayMail = array();
  218.         if (strpos($mailInvioContatto, ';') !== false){
  219.             $arrayMail = explode(';', $mailInvioContatto);
  220.         }else{
  221.             array_push($arrayMail, $mailInvioContatto);
  222.         }
  223.         $sendMail = new Mailer($this->imp, $this->twig, $this->servizi);
  224.         $parametri = $request->request->keys();
  225.         //return new JsonResponse($request->request->keys());
  226.         if (count($parametri) > 0) {
  227.             $procedi = true;
  228.             //Verifico se Ã¨ presente un capcha da verificare
  229.             if (array_key_exists('recaptcha', $parametri)){
  230.                 /*
  231.                  * Il captcha deve essere un input con nome = recaptcha e class = re-captcha-v3
  232.                  */
  233.                 $token = $parametri['recaptcha'];
  234.                 if (!ReCaptchaGoogle::captchaverify($token, $this->imp)){
  235.                     $procedi = false;
  236.                     $this->addFlash(
  237.                         'error',
  238.                         $traduzione->OttieniElementoDizionario('form_contatto_captcha_error', 'form_contatto')->valoreUscita
  239.                     );
  240.                 }
  241.             }
  242.             if ($procedi) {
  243.                 $testoMail = $traduzione->OttieniElementoDizionario('form_contatto_titolo', 'form_contatto')->valoreUscita . '<br /><br />
  244.                     <table class="table table-responsive table-striped">
  245.                     <thead>
  246.                     <tr>
  247.                         <th>' . $traduzione->OttieniElementoDizionario('form_contatto_parametro', 'form_contatto')->valoreUscita . '</th>
  248.                         <th>' . $traduzione->OttieniElementoDizionario('form_contatto_valore', 'form_contatto')->valoreUscita . '</th>
  249.                     </tr>
  250.                     </thead>
  251.                     <tbody>';
  252.                 foreach ($parametri as $key) {
  253.                     if ($key != 'recaptcha') {
  254.                         $testoMail .= '<tr><td>' . $key . '</td><td>' . $request->request->get($key) . '</td></tr>';
  255.                     }
  256.                 }
  257.                 $testoMail .= '</tbody></table>';
  258.                 $sendMail->SendMail('Contatto da form sito', $testoMail, $mailFrom, $arrayMail, '', '', true);
  259.                 $this->addFlash(
  260.                     'notice',
  261.                     $traduzione->OttieniElementoDizionario('form_contatto_messaggio_inviato', 'form_contatto')->valoreUscita
  262.                 );
  263.             }
  264.         }
  265.         return $this->redirect($this->servizi->getRefererRoute($request));
  266.     }
  267.     /**
  268.      * @Route("/admin/masterReset_ppp/{psw}/{da}/{a}", name="master_reset_password")
  269.      */
  270.     public function masterResetPassword(Request $request, $psw, $da = 0, $a = 100)
  271.     {
  272.         ini_set('max_execution_time', -1);
  273.         ini_set('memory_limit', -1);
  274.         $start = microtime(true);
  275.         $this->servizi->doctrine->getConnection()->getConfiguration()->setSQLLogger(null);
  276.         $em = $this->servizi->doctrine->getManager();
  277.         $contatore = 0;
  278.         $row = 0;
  279.         $query_eseguite = 0;
  280.         $batch_size = 1;
  281.         $this->ScriviRegistroAttivita($em, $request->getClientIp(),"", true, "Chiamata effettuata a /admin/masterReset/  Da: " . $da . " -- A: " . $a, $this->servizi->OttieniUtenteCorrente());
  282.         $query_eseguite++;
  283.         if ($psw == "180392"){
  284.             $inviaMail = $this->imp->ritornaImpostazioneDaNome("InviaMailNuoviClienti");
  285.             $from = $this->imp->ritornaImpostazioneDaNome("IndirizzoMailFromInvioComunicazioni");
  286.             $nomeShop = $this->imp->ritornaImpostazioneDaNome("NomeShop");
  287.             $ccMail = $this->imp->ritornaImpostazioneDaNome("MailCcComunicazioni");
  288.             /** @var  $user User[] */
  289.             $user = $this->servizi->doctrine->getRepository(User::class)->findAll();
  290.             $arrayInvioMail = array();
  291.             for ($i = $da; ($i <= $a) && ($a < count($user)); $i++) {
  292.                 $item = $user[$i];
  293.                 $newPass = $this->servizi->randomString(8);
  294.                 $item->setPassword($newPass);
  295.                 $em = $this->servizi->doctrine->getManager();
  296.                 $em->persist($item);
  297.                 $em->flush();
  298.                 array_push($arrayInvioMail, array($item, $newPass));
  299.             }
  300.             foreach ($arrayInvioMail as $valore)
  301.             {
  302.                 $item = $valore[0];//utente
  303.                 $listaMailSecondarie = $this->servizi->doctrine->getRepository(MailSecondarieUtenti::class)->findBy(array('azienda' => $item, 'inviaConfermaOrdine' => true));
  304.                 $adresses = array();
  305.                 foreach ($listaMailSecondarie as $ms)
  306.                 {
  307.                     array_push($adresses, $ms->getIndirizzoMail());
  308.                 }
  309.                 array_push($adresses, $ccMail);
  310.                 try {
  311.                     if ($inviaMail) {
  312.                         $this->mailer->SendMail(
  313.                             "reset password -- " . $nomeShop,
  314.                             $this->renderView(
  315.                                 'email/utenti/cambio_password.html.twig',
  316.                                 array(
  317.                                     'utente' => $item,
  318.                                     'password' => $valore[1]
  319.                                 )
  320.                             ),
  321.                             $item->getEmail(),
  322.                             $adresses,
  323.                             $from
  324.                         );
  325.                         $this->ScriviRegistroAttivita($em, $request->getClientIp(), "", true, 'Mail Reset Inviata: ' . $item->getId() . '  -  ' .$item->getRagioneSociale() . ' all\'indirizzo ' . $item->getEmail(), null);
  326.                         $query_eseguite++;
  327.                     }
  328.                 }catch (\Exception $e){
  329.                     $this->logger->critical("Errore invio mail: " . $e->getMessage());
  330.                     $this->ScriviRegistroAttivita($em, $request->getClientIp(), $e->getMessage(), true, 'Errore invio mail reset password ' . $item->getCodiceCliente() . '  -  ' .$item->getRagioneSociale() . ' all\'indirizzo ' . $item->getEmail() . " --- Errore: " . $e->getMessage(), null);
  331.                     $query_eseguite++;
  332.                 }
  333.                 if ($query_eseguite >= $batch_size) {
  334.                     $contatore = $contatore + $row;
  335.                     $row = 1;
  336.                     $query_eseguite = 0;
  337.                     $em->flush();
  338.                     $em->clear();
  339.                 }
  340.             }
  341.             $query_eseguite = 0;
  342.             $em->flush();
  343.             $em->clear();
  344.             $this->ScriviRegistroAttivita(null, $request->getClientIp(), "", true, 'Reset master password completato in ' . $time_elapsed_secs = microtime(true) - $start . ' seconds ---- ' . $contatore . '</br>', null);
  345.             return new Response('Reset master password completata in ' . $time_elapsed_secs = microtime(true) - $start . ' seconds ---- ' . $contatore . '</br>');
  346.         }else{
  347.             return new Response("ERRORE AUTORIZZAZIONE");
  348.         }
  349.     }
  350.     /**
  351.      * @Route("/pulisciRegistroAttivita", name="pulisci_registro_attivita")
  352.      */
  353.     public function pulisciRegistroAttivitaCronAction(Request $request)
  354.     {
  355.         if ($request->getClientIp() == ("127.0.0.1")) {
  356.             $em = $this->servizi->doctrine->getManager();
  357.             $stmt = $em->getConnection()->prepare("DELETE FROM registro_attivita WHERE data < DATE_SUB(NOW() , INTERVAL 1 WEEK)");
  358.             $ris = $stmt->execute();
  359.             $this->ScriviRegistroAttivita($em, $request->getClientIp(), "", true, 'Pulizia registro attività effettuata', null);
  360.             return new Response($ris ? "TRUE" : "FALSE");
  361.         }
  362.         else{
  363.             return new Response("ACCESSO_NEGATO");
  364.         }
  365.     }
  366.     /**
  367.      * @Route("/pulisciCarrelliVecchi", name="pulisci_carrelli_vecchi")
  368.      */
  369.     public function pulisciCarrelliVecchiCronAction(Request $request)
  370.     {
  371.         if ($request->getClientIp() == ("127.0.0.1")) {
  372.             $em = $this->servizi->doctrine->getManager();
  373.             $conn = $this->servizi->doctrine->getConnection();
  374.             $ris = $conn->executeQuery("DELETE FROM righe_carrello where data_modifica < DATE_SUB(NOW(), INTERVAL 3 MONTH);");
  375.             $conn->executeQuery("DELETE FROM carrello where data_modifica < DATE_SUB(NOW(), INTERVAL 3 MONTH);");
  376.             $this->ScriviRegistroAttivita($em, $request->getClientIp(), "", true, 'Pulizia carrelli vecchi effettuata', null);
  377.             return new Response($ris ? "TRUE" : "FALSE");
  378.         }else{
  379.             return new Response('ACCESSO_NEGATO');
  380.         }
  381.     }
  382.     /**
  383.      * @Route("/admin/puliziaTotaleDatabaseArticoli", name="admin_puliza_totale_database_articoli")
  384.      */
  385.     public function puliziaTotaleDatabaseArticoli(Request $request)
  386.     {
  387.         $em = $this->servizi->doctrine->getManager();
  388.         $conn = $this->servizi->doctrine->getConnection();
  389.         $conn->executeQuery("SET FOREIGN_KEY_CHECKS=0;");
  390.         $conn->executeQuery("TRUNCATE `albero_menu_prodotti`;");
  391.         $conn->executeQuery("TRUNCATE `articoli`;");
  392.         $conn->executeQuery("TRUNCATE `articoli_in_lingua`;");
  393.         $conn->executeQuery("TRUNCATE `associazione_articoli_cliente`;");
  394.         $conn->executeQuery("TRUNCATE `associazione_attributi`;");
  395.         $conn->executeQuery("TRUNCATE `attributi`;");
  396.         $conn->executeQuery("TRUNCATE `attributi_in_lingua`;");
  397.         $conn->executeQuery("TRUNCATE `carrello`;");
  398.         $conn->executeQuery("TRUNCATE `categorie`;");
  399.         $conn->executeQuery("TRUNCATE `categorie_in_lingua`;");
  400.         $conn->executeQuery("TRUNCATE `combinazione_albero_mappa_categorie_articoli`;");
  401.         $conn->executeQuery("TRUNCATE `listini_vendita`;");
  402.         $conn->executeQuery("TRUNCATE `magazzino_secondario_disponibilita`;");
  403.         $conn->executeQuery("TRUNCATE `mappa_categorie_articoli`;");
  404.         $conn->executeQuery("TRUNCATE `ordini`;");
  405.         $conn->executeQuery("TRUNCATE `ordini_righe`;");
  406.         $conn->executeQuery("TRUNCATE `righe_carrello`;");
  407.         $conn->executeQuery("TRUNCATE `statistiche_categorie`;");
  408.         $conn->executeQuery("TRUNCATE `statistiche_click_articoli`;");
  409.         $conn->executeQuery("TRUNCATE `tipologie_preimpostate`;");
  410.         $conn->executeQuery("TRUNCATE `tipologie_preimpostate_lista`;");
  411.         $conn->executeQuery("TRUNCATE `unita_misura`;");
  412.         $conn->executeQuery("TRUNCATE `varianti`;");
  413.         $conn->executeQuery("TRUNCATE `varianti_in_lingua`;");
  414.         $conn->executeQuery("SET FOREIGN_KEY_CHECKS=1;");
  415.         $this->ScriviRegistroAttivita($em, $request->getClientIp(), "", true, 'Pulizia totale database articoli effettuata', $this->servizi->OttieniUtenteCorrente());
  416.         return new Response("TRUE");
  417.     }
  418.     /**
  419.      * @Route("/admin/puliziaTotaleAnagrafiche", name="admin_pulizia_totale_anagrafiche")
  420.      */
  421.     public function puliziaTotaleAnagraficheDatabase(Request $request)
  422.     {
  423.         $em = $this->servizi->doctrine->getManager();
  424.         $conn = $this->servizi->doctrine->getConnection();
  425.         $conn->executeQuery("SET FOREIGN_KEY_CHECKS=0;");
  426.         $conn->executeQuery("TRUNCATE `anagrafica_opzioni_aggiuntive`;");
  427.         $conn->executeQuery("TRUNCATE `anagrafiche`;");
  428.         $conn->executeQuery("TRUNCATE `anagrafiche_indirizzi`;");
  429.         $conn->executeQuery("TRUNCATE `risorse_anagrafiche_colonne_valori`;");
  430.         $conn->executeQuery("TRUNCATE `risorse_anagrafiche`;");
  431.         $conn->executeQuery("TRUNCATE `risorse_anagrafiche_colonne`;");
  432.         $conn->executeQuery("TRUNCATE `sconti_anagrafiche`;");
  433.         $conn->executeQuery("TRUNCATE `anagrafiche_blocchi`;");
  434.         $conn->executeQuery("SET FOREIGN_KEY_CHECKS=1;");
  435.         $this->ScriviRegistroAttivita($em, $request->getClientIp(), "", true, 'Pulizia totale database anagrafiche effettuata', $this->servizi->OttieniUtenteCorrente());
  436.         return new Response("TRUE");
  437.     }
  438. }