<?php
namespace App\Controller;
use App\Entity\Website;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Validator\Constraints\DateTime;
class WebsiteController extends AbstractController
{
private $em;
public function __construct(EntityManagerInterface $em, HttpClientInterface $client)
{
$this->em = $em;
$this->client = $client;
}
/**
* @Route("/website", name="app_website")
*/
public function index(Request $request): Response
{
$websites = $this->em->getRepository(Website::class)->findAll();
$websitesNotArchive = array();
foreach ($websites as $key => $website_infos) {
if($website_infos->getStatut() != 'archive') {
$websitesNotArchive[] = $website_infos;
}
if( !empty( $website_infos->getStats() ) && $website_infos->getStats() != "-"){
$stats = unserialize($website_infos->getStats());
$website_infos->setStatsArray($stats);
}
}
// $last_line = system('curl -i -k -X GET -u publigo:Em~uh569 "https://ns3164592.ip-51-91-73.eu:8443/api/v2/domains"', $retval);
// dd($last_line);
return $this->render('website/index.html.twig', [
'websites' => $websitesNotArchive,
'pageClass' => 'websites',
'controller_name' => 'WebsiteController',
]);
}
/**
* @Route("/archive-website", name="archive_website")
*/
public function archiveWebsite(Request $request): Response
{
$websites = $this->em->getRepository(Website::class)->findBy(array('statut' => 'archive'));
return $this->render('website/archive.html.twig', [
'websites' => $websites,
'pageClass' => 'websites',
'controller_name' => 'WebsiteController',
]);
}
/**
* @Route("/add-website/{domain}/{subfolder}", methods={"GET"}, name="add_website")
*/
public function addWebsite($domain, $subfolder = "")
{
$date_creation = new \DateTime('now');
$url_principal = ($subfolder != "") ? $domain.'/'.$subfolder : $domain ;
$website = new Website();
$website->setUrlPrincipal($url_principal);
$website->setDateCreation($date_creation);
$website->setCmsType('-');
$website->setCmsVersion('-');
$website->setServeur('-');
$website->setVersionPhp('-');
$website->setIsHttps(false);
$website->setIsRgpd(false);
$website->setStatut('en-cours');
$this->em->persist($website);
$this->em->flush();
$datas = $this->updateWebsite($domain);
$error = '';
if ($error != '') {
return new JsonResponse(['error' => $error], JsonResponse::HTTP_CREATED);
} else {
return new JsonResponse($datas, JsonResponse::HTTP_CREATED);
}
}
/**
* @Route("/remove-website/{domain}/{subfolder}", methods={"GET"}, name="remove_website")
*/
public function removeWebsite($domain, $subfolder = "")
{
$error = '';
$is_remove = false;
$url_principal = ($subfolder != "") ? $domain.'/'.$subfolder : $domain ;
$website = $this->em->getRepository(Website::class)->findOneBy( array("url_principal" => $url_principal));
if($website) {
$this->em->remove($website);
$this->em->flush();
$is_remove = true;
} else {
$error = 404;
}
if ($error != '') {
return new JsonResponse(['error' => $error], JsonResponse::HTTP_CREATED);
} else {
return new JsonResponse(['remove' => $is_remove], JsonResponse::HTTP_CREATED);
}
}
/**
* @Route("/change-website/{domain}/{subfolder}", methods={"GET"}, name="change_website")
*/
public function changeWebsite($domain, $subfolder = "", Request $request)
{
$error = '';
$is_updated = false;
$url_principal = ($subfolder != "") ? $domain.'/'.$subfolder : $domain ;
$website = $this->em->getRepository(Website::class)->findOneBy( array("url_principal" => $url_principal));
if($website) {
$website->setStatut($request->query->get('status'));
if($request->query->get('status') == 'archive') {
$today = new \DateTime('now');
$website->setDateArchive($today);
}
$website->setStatut($request->query->get('status'));
$this->em->flush();
$is_updated = true;
} else {
$error = 404;
}
if ($error != '') {
return new JsonResponse(['error' => $error], JsonResponse::HTTP_CREATED);
} else {
return new JsonResponse(['updatedStatus' => $is_updated], JsonResponse::HTTP_CREATED);
}
}
/**
* @Route("/update-website/{domain}/{subfolder}", methods={"GET"}, name="update_website")
*/
public function updateWebsite($domain, $subfolder = "", $is_wix = false)
{
$error = '';
$url_principal = ($subfolder != "") ? $domain.'/'.$subfolder : $domain ;
$suffix = ($is_wix === true) ? '' : 'publigo-website-infos.php' ;
if(substr_count($url_principal, '.') == 1) $prefix = "www.";
elseif(substr_count($url_principal, '.') == 2) $prefix = "";
$website = $this->em->getRepository(Website::class)->findOneBy( array("url_principal" => $url_principal));
if(!$is_wix && $website && $website->getCmsType() == "Wix") {
$is_wix = true;
$suffix = '';
}
$statusCode = false;
$is_https = true;
try {
$response = $this->client->request(
'GET',
'https://'.$prefix.$url_principal.'/'.$suffix
);
$statusCode = $response->getStatusCode();
} catch (\Exception $e){
$is_https = false;
try {
$response = $this->client->request(
'GET',
'http://'.$prefix.$url_principal.'/'.$suffix
);
$statusCode = $response->getStatusCode();
} catch (\Exception $e){
$statusCode == $response->getStatusCode();
}
}
if($response->getStatusCode() == 200 && strpos(strtolower($response->getContent()),"wix") !== false) $is_wix = true;
if($is_wix) {
$datas = array(
"is_active" => $website->getStatut(),
"cms" => $website->getCmsType(),
"version" => $website->getCmsVersion(),
"created_date" => $website->getDateCreation()->format('Y-m-d'),
"server_domain" => "Wix",
"is_https" => $is_https,
"php_version" => $website->getVersionPhp(),
"is_rgpd" => (strpos(strtolower($response->getContent()),"rgpd") !== false) ? true : $website->isIsRgpd(),
);
$website->setServeur($datas['server_domain']);
$website->setIsHttps($datas['is_https']);
$website->setIsRgpd($datas['is_rgpd']);
if(strpos(strtolower($response->getContent()),"publigo") !== false
|| strpos(strtolower($response->getContent()),"39 laclergerie") !== false)
{
$datas['is_active'] = "actif";
$website->setStatut('actif');
} else {
$datas['is_active'] = "inactif";
$website->setStatut('inactif');
}
$this->em->persist($website);
$this->em->flush();
} elseif(($statusCode == 500 || $statusCode == 503 || $statusCode == 403 || $statusCode == 520) && $website) {
$datas = array(
"is_active" => "inactif",
"cms" => $website->getCmsType(),
"version" => $website->getCmsVersion(),
"created_date" => $website->getDateCreation(),
"server_domain" => $website->getServeur(),
"is_https" => $website->isIsHttps(),
"php_version" => $website->getVersionPhp(),
"is_rgpd" => $website->isIsRgpd(),
"size" => $website->getSize(),
);
$website->setStatut('inactif');
$this->em->persist($website);
$this->em->flush();
} elseif($statusCode != 404 && strpos($response->getContent(),"Gandi.net") === false && !$is_wix ) {
$datas = json_decode($response->getContent());
$date_creation = \DateTime::createFromFormat('Y-m-d', $datas->created_date);
$website->setDateCreation($date_creation);
$website->setCmsType($datas->cms);
$website->setCmsVersion($datas->version);
$website->setServeur($datas->server_domain);
$website->setVersionPhp($datas->php_version);
$website->setIsHttps($datas->is_https);
$website->setIsRgpd($datas->is_rgpd);
$website->setStatut($datas->is_active);
if($datas->size) $website->setSize($datas->size);
$this->em->persist($website);
$this->em->flush();
} else {
// $error = $statusCode;
$datas = json_decode('{"is_active":"inactif","cms":"-","version":"-","created_date":"-","server_domain":"-","is_https":false,"php_version":"-","is_rgpd":false,"size":null}');
$datas = array(
"is_active" => "inactif",
"cms" => $website->getCmsType(),
"version" => $website->getCmsVersion(),
"created_date" => $website->getDateCreation(),
"server_domain" => $website->getServeur(),
"is_https" => $website->isIsHttps(),
"php_version" => $website->getVersionPhp(),
"is_rgpd" => $website->isIsRgpd(),
"size" => $website->getSize(),
);
$shema = $is_https ? "https" : "http" ;
try {
$test = $this->client->request(
'GET',
$shema.'://'.$prefix.$url_principal.'/'
);
$statusCode = $test->getStatusCode();
$ip_domain = gethostbyname($prefix.$url_principal);
$server_domain = file_get_contents('https://www.publigo.fr/serveur-publigo.php?ip='.$ip_domain);
if(strpos($test->getContent(),"<title>Site en cours de maintenance ou désactivé. Merci de votre compréhension.</title>") !== false
|| $server_domain == ""
) {
$website->setStatut('inactif');
$this->em->persist($website);
$this->em->flush();
}
} catch (\Exception $e){
dd($e);
}
}
// dd($website, $datas, $date_creation);
if ($error != '') {
return new JsonResponse(['error' => $error], JsonResponse::HTTP_CREATED);
} else {
return new JsonResponse($datas, JsonResponse::HTTP_CREATED);
}
}
/**
* @Route("/check-ga3/", methods={"GET"}, name="check_ga3")
*/
public function checkGa3()
{
$websites = $this->em->getRepository(Website::class)->findBy( array("statut" => 'actif'));
$websites_changed = array();
$error = '';
$subfolder = "";
$suffix = '';
foreach ($websites as $key => $website) {
if($website->getCmsType() != "Wix") {
$domain = $website->getUrlPrincipal();
$url_principal = ($subfolder != "") ? $domain.'/'.$subfolder : $domain ;
if(substr_count($url_principal, '.') == 1) $prefix = "www.";
elseif(substr_count($url_principal, '.') == 2) $prefix = "";
$statusCode = false;
$is_https = true;
try {
$response = $this->client->request(
'GET',
'https://'.$prefix.$url_principal.'/'.$suffix
);
$statusCode = $response->getStatusCode();
} catch (\Exception $e){
$is_https = false;
try {
$response = $this->client->request(
'GET',
'http://'.$prefix.$url_principal.'/'.$suffix
);
$statusCode = $response->getStatusCode();
} catch (\Exception $e){
$statusCode == $response->getStatusCode();
}
}
if($statusCode == 200) $clean_content = str_replace('X-UA-', '', $response->getContent());
if($statusCode == 200 && strpos(strtolower($clean_content),'ua-') ) {
echo '<div>';
echo '<a href="http://'.$url_principal.'" target="_blank">'.$url_principal.'</a> : '.$website->getCmsType()."<br>";
echo '</div>';
}
}
}
die();
return true;
}
/**
* @Route("/cron-website/", methods={"GET"}, name="cron_website")
*/
public function cronWebsite()
{
$websites = $this->em->getRepository(Website::class)->findBy( array("statut" => 'actif'));
$websites_changed = array();
$error = '';
foreach ($websites as $key => $website) {
$datas_origine = json_encode( array(
'is_active' => $website->getStatut(),
'cms' => $website->getCmsType(),
'version' => $website->getCmsVersion(),
'created_date' => $website->getDateCreation()->format('Y-m-d'),
'server_domain' => $website->getServeur(),
'is_https' => $website->isIsHttps(),
'php_version' => $website->getVersionPhp(),
'is_rgpd' => $website->isIsRgpd(),
'size' => $website->getSize(),
));
$is_wix = ($website->getCmsType() == "Wix") ? true : false;
$datas_check = $this->updateWebsite($website->getUrlPrincipal(),'',$is_wix)->getContent();
if($datas_origine != $datas_check) $websites_changed[$website->getUrlPrincipal()] = $datas_check;
// sleep(1.5);
}
foreach ($websites_changed as $key => $website_changed) {
echo $key." : ".$website_changed."\n";
}
if ($error != '') {
return new JsonResponse(['error' => $error], JsonResponse::HTTP_CREATED);
} else {
// return new JsonResponse($websites_changed, JsonResponse::HTTP_CREATED);
return new JsonResponse(array(), JsonResponse::HTTP_CREATED);
}
}
}