src/Controller/WebsiteController.php line 393

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