src/Controller/WebsiteController.php line 29

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Website;
  4. use Doctrine\ORM\EntityManagerInterface;
  5. use Symfony\Contracts\HttpClient\HttpClientInterface;
  6. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  7. use Symfony\Component\HttpFoundation\Response;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Symfony\Component\Routing\Annotation\Route;
  10. use Symfony\Component\HttpFoundation\JsonResponse;
  11. use Symfony\Component\Validator\Constraints\DateTime;
  12. class WebsiteController extends AbstractController
  13. {
  14.     private $em;
  15.     public function __construct(EntityManagerInterface $emHttpClientInterface $client)
  16.     {
  17.         $this->em $em;
  18.         $this->client $client;
  19.     }
  20.     /**
  21.      * @Route("/website", name="app_website")
  22.      */
  23.     public function index(Request $request): Response
  24.     {
  25.         $websites $this->em->getRepository(Website::class)->findAll();
  26.         $websitesNotArchive = array();
  27.         foreach ($websites as $key => $website_infos) {
  28.             if($website_infos->getStatut() != 'archive') {
  29.                 $websitesNotArchive[] = $website_infos;
  30.             }
  31.             if( !empty( $website_infos->getStats() ) && $website_infos->getStats() != "-"){
  32.                 $stats unserialize($website_infos->getStats());
  33.                 $website_infos->setStatsArray($stats);
  34.             }
  35.         }
  36.         // $last_line = system('curl -i -k -X GET -u publigo:Em~uh569 "https://ns3164592.ip-51-91-73.eu:8443/api/v2/domains"', $retval);
  37.         
  38.         // dd($last_line);
  39.         return $this->render('website/index.html.twig', [
  40.             'websites' => $websitesNotArchive,
  41.             'pageClass' => 'websites',
  42.             'controller_name' => 'WebsiteController',
  43.         ]);
  44.     }
  45.     /**
  46.      * @Route("/archive-website", name="archive_website")
  47.      */
  48.     public function archiveWebsite(Request $request): Response
  49.     {
  50.         $websites $this->em->getRepository(Website::class)->findBy(array('statut' => 'archive'));
  51.         return $this->render('website/archive.html.twig', [
  52.             'websites' => $websites,
  53.             'pageClass' => 'websites',
  54.             'controller_name' => 'WebsiteController',
  55.         ]);
  56.     }
  57.     /**
  58.      * @Route("/add-website/{domain}/{subfolder}", methods={"GET"}, name="add_website")
  59.      */
  60.     public function addWebsite($domain$subfolder "")
  61.     {
  62.         $date_creation = new \DateTime('now');
  63.         $url_principal = ($subfolder != "") ? $domain.'/'.$subfolder $domain ;
  64.         $website = new Website();
  65.         $website->setUrlPrincipal($url_principal);
  66.         $website->setDateCreation($date_creation);
  67.         $website->setCmsType('-');
  68.         $website->setCmsVersion('-');
  69.         $website->setServeur('-');
  70.         $website->setVersionPhp('-');
  71.         $website->setIsHttps(false);
  72.         $website->setIsRgpd(false);
  73.         $website->setStatut('en-cours');
  74.         $this->em->persist($website);
  75.         $this->em->flush();
  76.         $datas $this->updateWebsite($domain);
  77.         $error '';
  78.         if ($error != '') {
  79.             return new JsonResponse(['error' => $error], JsonResponse::HTTP_CREATED);
  80.         } else {
  81.             return new JsonResponse($datasJsonResponse::HTTP_CREATED);      
  82.         }
  83.     }
  84.     /**
  85.      * @Route("/remove-website/{domain}/{subfolder}", methods={"GET"}, name="remove_website")
  86.      */
  87.     public function removeWebsite($domain$subfolder "")
  88.     {
  89.         $error '';
  90.         $is_remove false;
  91.         $url_principal = ($subfolder != "") ? $domain.'/'.$subfolder $domain ;
  92.         $website $this->em->getRepository(Website::class)->findOneBy( array("url_principal" => $url_principal));
  93.         if($website) {
  94.             $this->em->remove($website);
  95.             $this->em->flush();
  96.             $is_remove true;
  97.         } else {
  98.             $error 404;
  99.         }
  100.         if ($error != '') {
  101.             return new JsonResponse(['error' => $error], JsonResponse::HTTP_CREATED);
  102.         } else {
  103.             return new JsonResponse(['remove' => $is_remove], JsonResponse::HTTP_CREATED);
  104.         }
  105.     }
  106.     /**
  107.      * @Route("/change-website/{domain}/{subfolder}", methods={"GET"}, name="change_website")
  108.      */
  109.     public function changeWebsite($domain$subfolder ""Request $request)
  110.     {
  111.         $error '';
  112.         $is_updated false;
  113.         $url_principal = ($subfolder != "") ? $domain.'/'.$subfolder $domain ;
  114.         $website $this->em->getRepository(Website::class)->findOneBy( array("url_principal" => $url_principal));
  115.         if($website) {
  116.             $website->setStatut($request->query->get('status'));
  117.             if($request->query->get('status') == 'archive') {
  118.                 $today = new \DateTime('now');
  119.                 $website->setDateArchive($today);
  120.             }
  121.             $website->setStatut($request->query->get('status'));
  122.             $this->em->flush();
  123.             $is_updated true;
  124.         } else {
  125.             $error 404;
  126.         }
  127.         if ($error != '') {
  128.             return new JsonResponse(['error' => $error], JsonResponse::HTTP_CREATED);
  129.         } else {
  130.             return new JsonResponse(['updatedStatus' => $is_updated], JsonResponse::HTTP_CREATED);
  131.         }
  132.     }
  133.     /**
  134.      * @Route("/update-website/{domain}/{subfolder}", methods={"GET"}, name="update_website")
  135.      */
  136.     public function updateWebsite($domain$subfolder ""$is_wix false)
  137.     {
  138.         $error '';
  139.         $url_principal = ($subfolder != "") ? $domain.'/'.$subfolder $domain ;
  140.         $suffix = ($is_wix === true) ? '' 'publigo-website-infos.php' ;
  141.         
  142.         if(substr_count($url_principal'.') == 1$prefix "www.";
  143.         elseif(substr_count($url_principal'.') == 2$prefix "";
  144.         $website $this->em->getRepository(Website::class)->findOneBy( array("url_principal" => $url_principal));
  145.         if(!$is_wix && $website && $website->getCmsType() == "Wix") {
  146.             $is_wix true;
  147.             $suffix '';
  148.         }
  149.         $statusCode false;
  150.         $is_https true;
  151.         try {
  152.             $response $this->client->request(
  153.                 'GET',
  154.                 'https://'.$prefix.$url_principal.'/'.$suffix
  155.             );
  156.             $statusCode $response->getStatusCode();
  157.         } catch (\Exception $e){
  158.             $is_https false;
  159.             try {
  160.                 $response $this->client->request(
  161.                     'GET',
  162.                     'http://'.$prefix.$url_principal.'/'.$suffix
  163.                 );
  164.                 $statusCode $response->getStatusCode();
  165.             } catch (\Exception $e){
  166.                 $statusCode == $response->getStatusCode();
  167.             }
  168.         }
  169.         if($response->getStatusCode() == 200 && strpos(strtolower($response->getContent()),"wix") !== false$is_wix true;
  170.         if($is_wix) {
  171.             $datas = array(
  172.                 "is_active" => $website->getStatut(),
  173.                 "cms" => $website->getCmsType(),
  174.                 "version" => $website->getCmsVersion(),
  175.                 "created_date" => $website->getDateCreation()->format('Y-m-d'),
  176.                 "server_domain" => "Wix",
  177.                 "is_https" => $is_https,
  178.                 "php_version" => $website->getVersionPhp(),
  179.                 "is_rgpd" => (strpos(strtolower($response->getContent()),"rgpd") !== false) ? true $website->isIsRgpd(),
  180.             );
  181.             $website->setServeur($datas['server_domain']);
  182.             $website->setIsHttps($datas['is_https']);
  183.             $website->setIsRgpd($datas['is_rgpd']);
  184.             if(strpos(strtolower($response->getContent()),"publigo") !== false
  185.                 || strpos(strtolower($response->getContent()),"39 laclergerie") !== false
  186.             {
  187.                 $datas['is_active'] = "actif";
  188.                 $website->setStatut('actif');
  189.             } else {
  190.                 $datas['is_active'] = "inactif";
  191.                 $website->setStatut('inactif');
  192.             }
  193.             $this->em->persist($website);
  194.             $this->em->flush();
  195.         } elseif(($statusCode == 500 || $statusCode == 503 || $statusCode == 403 || $statusCode == 520) && $website) {
  196.             $datas = array(
  197.                 "is_active" => "inactif",
  198.                 "cms" => $website->getCmsType(),
  199.                 "version" => $website->getCmsVersion(),
  200.                 "created_date" => $website->getDateCreation(),
  201.                 "server_domain" => $website->getServeur(),
  202.                 "is_https" => $website->isIsHttps(),
  203.                 "php_version" => $website->getVersionPhp(),
  204.                 "is_rgpd" => $website->isIsRgpd(),
  205.                 "size" => $website->getSize(),
  206.             );
  207.             $website->setStatut('inactif');
  208.             $this->em->persist($website);
  209.             $this->em->flush();
  210.         } elseif($statusCode != 404 && strpos($response->getContent(),"Gandi.net") === false && !$is_wix ) {
  211.             $datas json_decode($response->getContent());
  212.             $date_creation = \DateTime::createFromFormat('Y-m-d'$datas->created_date);
  213.             $website->setDateCreation($date_creation);
  214.             $website->setCmsType($datas->cms);
  215.             $website->setCmsVersion($datas->version);
  216.             $website->setServeur($datas->server_domain);
  217.             $website->setVersionPhp($datas->php_version);
  218.             $website->setIsHttps($datas->is_https);
  219.             $website->setIsRgpd($datas->is_rgpd);
  220.             $website->setStatut($datas->is_active);
  221.             if($datas->size$website->setSize($datas->size);
  222.             $this->em->persist($website);
  223.             $this->em->flush();
  224.         } else {
  225.             // $error = $statusCode;
  226.             $datas json_decode('{"is_active":"inactif","cms":"-","version":"-","created_date":"-","server_domain":"-","is_https":false,"php_version":"-","is_rgpd":false,"size":null}');
  227.             $datas = array(
  228.                 "is_active" => "inactif",
  229.                 "cms" => $website->getCmsType(),
  230.                 "version" => $website->getCmsVersion(),
  231.                 "created_date" => $website->getDateCreation(),
  232.                 "server_domain" => $website->getServeur(),
  233.                 "is_https" => $website->isIsHttps(),
  234.                 "php_version" => $website->getVersionPhp(),
  235.                 "is_rgpd" => $website->isIsRgpd(),
  236.                 "size" => $website->getSize(),
  237.             );
  238.             $shema $is_https "https" "http" ;
  239.             try {
  240.                 $test $this->client->request(
  241.                     'GET',
  242.                     $shema.'://'.$prefix.$url_principal.'/'
  243.                 );
  244.                 $statusCode $test->getStatusCode();
  245.                 $ip_domain gethostbyname($prefix.$url_principal);
  246.                 $server_domain file_get_contents('https://www.publigo.fr/serveur-publigo.php?ip='.$ip_domain);
  247.                 if(strpos($test->getContent(),"<title>Site en cours de maintenance ou désactivé. Merci de votre compréhension.</title>") !== false 
  248.                     || $server_domain == ""
  249.                     ) {
  250.                         $website->setStatut('inactif');
  251.                         $this->em->persist($website);
  252.                         $this->em->flush();
  253.                 }
  254.             } catch (\Exception $e){
  255.                 dd($e);
  256.             }
  257.         }
  258.         // dd($website, $datas, $date_creation);
  259.         if ($error != '') {
  260.             return new JsonResponse(['error' => $error], JsonResponse::HTTP_CREATED);
  261.         } else {
  262.             return new JsonResponse($datasJsonResponse::HTTP_CREATED);      
  263.         }
  264.     }
  265.     /**
  266.      * @Route("/check-ga3/", methods={"GET"}, name="check_ga3")
  267.      */
  268.     public function checkGa3()
  269.     {
  270.         $websites $this->em->getRepository(Website::class)->findBy( array("statut" => 'actif'));
  271.         $websites_changed = array();
  272.         $error '';
  273.         $subfolder "";
  274.         $suffix '';
  275.         foreach ($websites as $key => $website) {
  276.             if($website->getCmsType() != "Wix") {
  277.                 $domain $website->getUrlPrincipal();
  278.                 $url_principal = ($subfolder != "") ? $domain.'/'.$subfolder $domain ;
  279.                 
  280.                 if(substr_count($url_principal'.') == 1$prefix "www.";
  281.                 elseif(substr_count($url_principal'.') == 2$prefix "";
  282.                 $statusCode false;
  283.                 $is_https true;
  284.                 try {
  285.                     $response $this->client->request(
  286.                         'GET',
  287.                         'https://'.$prefix.$url_principal.'/'.$suffix
  288.                     );
  289.                     $statusCode $response->getStatusCode();
  290.                 } catch (\Exception $e){
  291.                     $is_https false;
  292.                     try {
  293.                         $response $this->client->request(
  294.                             'GET',
  295.                             'http://'.$prefix.$url_principal.'/'.$suffix
  296.                         );
  297.                         $statusCode $response->getStatusCode();
  298.                     } catch (\Exception $e){
  299.                         $statusCode == $response->getStatusCode();
  300.                     }
  301.                 }
  302.                 if($statusCode == 200$clean_content str_replace('X-UA-'''$response->getContent());
  303.                 if($statusCode == 200 && strpos(strtolower($clean_content),'ua-') ) {
  304.                     echo '<div>';
  305.                         echo '<a href="http://'.$url_principal.'" target="_blank">'.$url_principal.'</a> : '.$website->getCmsType()."<br>";
  306.                     echo '</div>';
  307.                 }
  308.             }
  309.         }
  310.         die();
  311.         return true;
  312.     }
  313.     /**
  314.      * @Route("/cron-website/", methods={"GET"}, name="cron_website")
  315.      */
  316.     public function cronWebsite()
  317.     {
  318.         $websites $this->em->getRepository(Website::class)->findBy( array("statut" => 'actif'));
  319.         $websites_changed = array();
  320.         $error '';
  321.         foreach ($websites as $key => $website) {
  322.             $datas_origine json_encode( array(
  323.                 'is_active' => $website->getStatut(),
  324.                 'cms' => $website->getCmsType(),
  325.                 'version' => $website->getCmsVersion(),
  326.                 'created_date' => $website->getDateCreation()->format('Y-m-d'),
  327.                 'server_domain' => $website->getServeur(),
  328.                 'is_https' => $website->isIsHttps(),
  329.                 'php_version' => $website->getVersionPhp(),
  330.                 'is_rgpd' => $website->isIsRgpd(),
  331.                 'size' => $website->getSize(),
  332.             ));
  333.             $is_wix = ($website->getCmsType() == "Wix") ? true false;
  334.             $datas_check $this->updateWebsite($website->getUrlPrincipal(),'',$is_wix)->getContent();
  335.             if($datas_origine != $datas_check$websites_changed[$website->getUrlPrincipal()] = $datas_check;
  336.             // sleep(1.5);
  337.         }
  338.         foreach ($websites_changed as $key => $website_changed) {
  339.             echo $key." : ".$website_changed."\n";
  340.         }
  341.         if ($error != '') {
  342.             return new JsonResponse(['error' => $error], JsonResponse::HTTP_CREATED);
  343.         } else {
  344.             // return new JsonResponse($websites_changed, JsonResponse::HTTP_CREATED);
  345.             return new JsonResponse(array(), JsonResponse::HTTP_CREATED);
  346.         }
  347.     }
  348. }