<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Request;
use Google\Cloud\Storage\StorageClient;
use App\Entity\UrlUpdate;
use App\Repository\UrlUpdateRepository;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\Bundle\DoctrineBundle\Registry;
class UrlsearchController extends AbstractController
{
#[Route('/urlsearch', name: 'app_urlsearch')]
public function index(): Response
{
return $this->render('urlsearch/index.html.twig', [
'controller_name' => 'UrlsearchController',
]);
}
#[Route('/update', name: 'app_appupdate')]
public function updateurl(Request $request): Response
{
$jsonsend = $request->getContent();
if(!$this->isJson($jsonsend)){
return $this->returnError();
}
$json = json_decode($jsonsend, true);
$response = new Response();
$varFile = 'test.txt';
if(!isset($json['test'])){
return $this->returnError();
}
$varDir = 'test';
if($json['test'] == '0'){
$varFile = 'prod.txt';
$varDir = 'prod';
}
$timeconteggio = file_get_contents($varFile);
$storage = new StorageClient([
'keyFilePath' => 'bulgari-303613-d1e8adbe4a39.json',
]);
$bucket = $storage->bucket('change2app');
$array_3D = [];
$crc = uniqid() . uniqid();
$storage->registerStreamWrapper();
$sapMemo = '';
$datetimemax = 0;
$index_data = file_get_contents("index_data.json");
$jsonSap = json_decode($index_data, true);
for($i=0; $i<count($jsonSap); $i++){
$sapCheck = $jsonSap[$i]["sap"];
$datetimeupd = $jsonSap[$i]["time"];
if(!file_exists($varDir . "/" . $sapCheck . ".txt") || $datetimeupd != file_get_contents($varDir . "/" . $sapCheck . ".txt")){
if(!file_exists("acl_check/" . $sapCheck . ".txt") || file_exists("exported/" . $sapCheck . ".txt")){
$array_3D[] = array(
"sap" => strtoupper($sapCheck),
"url" => "https://www.change2.app/include.js?SAP=" . $sapCheck . "&CRC=" . $crc
);
file_put_contents($varDir . "/" . $sapCheck . ".txt", $datetimeupd);
}
}
}
$response->setContent(
json_encode([
'status' => 'success',
'code' => '200',
'result' => $array_3D
])
);
$response->headers->set('Content-Type', 'application/json');
file_put_contents($varFile, $datetimemax);
return $response;
die();
/*
$options = ['suffix' => 'configurator_settings.json'];
foreach ($bucket->objects($options) as $object) {
$stringUrl = $object->name();
$sapCheck = explode('/', $stringUrl);
$datetimeupd = strtotime($object->info()['updated']);
if($sapMemo != $sapCheck[0]){
$sapMemo = $sapCheck[0];
$object_exist = $bucket->object($sapCheck[0] . '/configurator_settings.json');
$object_exist2 = $bucket->object($sapCheck[0] . '/__settings__.js');
if($object_exist->exists() && $object_exist2->exists()){
$datetimeupdExist = strtotime($object_exist->info()['updated']);
if($datetimemax < $datetimeupdExist){
$datetimemax = $datetimeupdExist;
}
$contents = file_get_contents('gs://change2app/' . $sapCheck[0] . '/configurator_settings.json');
$json_check = json_decode($contents,true);
if(isset($json_check['player3D']) && intval($json_check['player3D']) == 1){
if($timeconteggio < $datetimeupdExist){
$array_3D[] = array(
"sap" => $sapCheck[0],
"url" => "https://www.change2.app/include.js?SAP=" . $sapCheck[0] . "&CRC=" . $crc
);
}
}
}
}
}
$response->setContent(
json_encode([
'status' => 'success',
'code' => '200',
'result' => $array_3D
])
);
$response->headers->set('Content-Type', 'application/json');
file_put_contents($varFile, $datetimemax);
return $response;
*/
}
#[Route('/fromto', name: 'app_fromto')]
public function fromto(Request $request): Response
{
$jsonsend = $request->getContent();
if(!$this->isJson($jsonsend)){
return $this->returnError();
}
$json = json_decode($jsonsend, true);
$response = new Response();
if(!isset($json['from']) || !isset($json['to'])){
return $this->returnError();
}
$storage = new StorageClient([
'keyFilePath' => 'bulgari-303613-d1e8adbe4a39.json',
]);
$bucket = $storage->bucket('change2app');
$array_3D = [];
$crc = uniqid() . uniqid();
$storage->registerStreamWrapper();
$sapMemo = '';
$datetimemax = 0;
$index_data = file_get_contents("index_data.json");
$jsonSap = json_decode($index_data, true);
for($i=0; $i<count($jsonSap); $i++){
$sapCheck = $jsonSap[$i]["sap"];
$datetimeupd = $jsonSap[$i]["time"];
if($json['from'] <= $datetimeupd && $json['to'] >= $datetimeupd){
if(!file_exists("acl_check/" . $sapCheck . ".txt") || file_exists("exported/" . $sapCheck . ".txt")){
$array_3D[] = array(
"sap" => strtoupper($sapCheck),
"url" => "https://www.change2.app/include.js?SAP=" . $sapCheck . "&CRC=" . $crc
);
}
}
}
$response->setContent(
json_encode([
'status' => 'success',
'code' => '200',
'result' => $array_3D
])
);
$response->headers->set('Content-Type', 'application/json');
return $response;
}
#[Route('/create_index', name: 'create_index')]
public function create_index(UrlUpdateRepository $urlUpdateRepository): Response
{
$storage = new StorageClient([
'keyFilePath' => 'bulgari-303613-d1e8adbe4a39.json',
]);
$bucket = $storage->bucket('change2app');
$array_3D = [];
$storage->registerStreamWrapper();
$sapMemo = '';
$datetimemax = 0;
$indexingCheck = file_get_contents("indexing.txt");
if($indexingCheck == "1"){
die("sta gia girando");
}
file_put_contents("indexing.txt", "1");
$index_data = file_get_contents("index_data.json");
foreach ($bucket->objects() as $object) {
$stringUrl = $object->name();
$sapCheck = explode('/', $stringUrl);
$datetimeupd = strtotime($object->info()['updated']);
if($sapMemo != $sapCheck[0]){
$sapMemo = $sapCheck[0];
$object_exist = $bucket->object($sapCheck[0] . '/configurator_settings.json');
if($object_exist->exists()){
$datetimeupdExist = strtotime($object_exist->info()['updated']);
$contents = file_get_contents('gs://change2app/' . $sapCheck[0] . '/configurator_settings.json');
$json_check = json_decode($contents,true);
if(isset($json_check['player3D']) && intval($json_check['player3D']) == 1){
$array_3D[] = array(
"sap" => $sapCheck[0],
"time" => $datetimeupdExist
);
}
//controllo se devo ricreare l'acl e controllare l'errore del file index.html
//if(strpos($index_data, '{"sap":"' . $sapCheck[0] . '","time":' . $datetimeupdExist . '}') !== false){
//nessuna novita
//}else{
file_put_contents("time_check/" . $sapCheck[0] . ".txt", $datetimeupdExist);
file_put_contents("acl_check/" . $sapCheck[0] . ".txt", $datetimeupdExist);
//}
}
}
}
file_put_contents("index_data.json", json_encode($array_3D));
file_put_contents("indexing.txt", "0");
die('fine');
}
#[Route('/create_acl', name: 'create_acl')]
public function create_acl(UrlUpdateRepository $urlUpdateRepository): Response
{
$storage = new StorageClient([
'keyFilePath' => 'bulgari-303613-d1e8adbe4a39.json',
]);
$bucket = $storage->bucket('change2app');
$array_3D = [];
$storage->registerStreamWrapper();
$sapMemo = '';
$datetimemax = 0;
$files_time = scandir("acl_check");
if(count($files_time) > 2){
$sap = str_replace('.txt', '', $files_time[count($files_time)-1]);
//$sap = "br859769";
$options = ["prefix" => $sap . "/"];
echo $sap;
$object_exist = $bucket->object($sap . '/__settings__.js');
$object_exist2 = $bucket->object($sap . '/index.html');
//if($object_exist2->exists()){
if(!$object_exist->exists() && $object_exist2->exists()){
$contents = file_get_contents('gs://change2app/' . $sap . '/index.html');
$content_elaborate = str_replace('</script>', '', str_replace('<script>', '', $contents));
$content_elaborate = str_replace('CONFIG_FILENAME = ASSET_PREFIX ', 'CONFIG_FILENAME = ASSET_PREFIX + ', $content_elaborate);
$ret = explode(PHP_EOL, $content_elaborate);
for($z=0; $z<count($ret); $z++){
if(strpos($ret[$z], 'ASSET_PREFIX = "') !== false){
$ret[$z] = 'ASSET_PREFIX = "";';
}
}
$content_elaborate = implode(PHP_EOL, $ret);
//die($content_elaborate);
//$stream_data = fopen('data://text/plain,' . $content_elaborate, 'r');
//die($stream_data);
file_put_contents("tmp_upload.txt", $content_elaborate);
$bucket->upload(fopen("tmp_upload.txt", 'r'), [
'name' => $sap . '/__settings__.js',
]);
//$object_exist2->delete();
}
foreach ($bucket->objects($options) as $object) {
$acl = $object->acl();
$checkAclOk = 0;
foreach ($acl->get() as $item) {
if($item['entity'] == 'allUsers' && $item['role'] == 'READER'){
$checkAclOk = 1;
}
}
if($checkAclOk == 0){
$object->update(['acl' => []], ['predefinedAcl' => 'PUBLICREAD']);
}
}
//die();
unlink("acl_check/" . $sap . ".txt");
file_put_contents("exported/" . $sap . ".txt", "1");
}
die("fine");
}
#[Route('/check_update', name: 'app_check_update')]
public function checkupdate(UrlUpdateRepository $urlUpdateRepository): Response
{
$storage = new StorageClient([
'keyFilePath' => 'bulgari-303613-d1e8adbe4a39.json',
]);
$bucket = $storage->bucket('change2app');
$array_3D = [];
$array_3D_date = [];
$crc = uniqid() . uniqid();
$storage->registerStreamWrapper();
$sapMemo = '';
$datetimemax = 0;
foreach ($bucket->objects() as $object) {
$stringUrl = $object->name();
$sapCheck = explode('/', $stringUrl);
$datetimeupd = strtotime($object->info()['updated']);
if($sapMemo != $sapCheck[0]){
$sapMemo = $sapCheck[0];
$object_exist = $bucket->object($sapCheck[0] . '/configurator_settings.json');
if($object_exist->exists()){
$datetimeupdExist = strtotime($object_exist->info()['updated']);
if($datetimemax < $datetimeupdExist){
$datetimemax = $datetimeupdExist;
}
$contents = file_get_contents('gs://change2app/' . $sapCheck[0] . '/configurator_settings.json');
$json_check = json_decode($contents,true);
if(isset($json_check['player3D']) && intval($json_check['player3D']) == 1){
$array_3D[] = array(
"sap" => $sapCheck[0],
"url" => "https://www.change2.app/include.js?SAP=" . $sapCheck[0] . "&CRC=" . $crc
);
//$urlUpdate = $urlUpdateRepository->findOneBy(array('sap' => $sapCheck[0], 'url' => $stringUrl));
//if (!$urlUpdate){
// $urlUpdate = new UrlUpdate();
// $urlUpdate->setSap($sapCheck[0]);
// $urlUpdate->setUrl($stringUrl);
//}
//if($sapCheck[0] == '41273' || $sapCheck[0] == 'core' || $sapCheck[0] == 'core' || $sapCheck[0] == 'AN858365' || $sapCheck[0] == '41252'){
//$object->update(['acl' => []], ['predefinedAcl' => 'PUBLICREAD']);
//}
//echo $sapCheck[0] . ' - ' . $datetimeupd . ' - ' . $object->info()['updated'] . ' - ' . $stringUrl . PHP_EOL;
}
}
}
if($object->name() != 'include.js'){
$object->update(['acl' => []], ['predefinedAcl' => 'PUBLICREAD']);
}
}
print_r($array_3D);
echo $datetimemax;
die('ok');
}
public function returnError(){
$response = new Response();
$response->setContent(
json_encode([
'status' => 'error',
'code' => '403',
'message' => 'Invalid JSON'
])
);
$response->headers->set('Content-Type', 'application/json');
return $response;
}
function isJson($string)
{
json_decode($string);
return (json_last_error() == JSON_ERROR_NONE);
}
}