src/Controller/UrlsearchController.php line 26

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  4. use Symfony\Component\HttpFoundation\Response;
  5. use Symfony\Component\Routing\Annotation\Route;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Google\Cloud\Storage\StorageClient;
  8. use App\Entity\UrlUpdate;
  9. use App\Repository\UrlUpdateRepository;
  10. use Doctrine\ORM\EntityManagerInterface;
  11. use Doctrine\Bundle\DoctrineBundle\Registry;
  12. class UrlsearchController extends AbstractController
  13. {
  14.     #[Route('/urlsearch'name'app_urlsearch')]
  15.     public function index(): Response
  16.     {
  17.         return $this->render('urlsearch/index.html.twig', [
  18.             'controller_name' => 'UrlsearchController',
  19.         ]);
  20.     }
  21.     #[Route('/update'name'app_appupdate')]
  22.     public function updateurl(Request $request): Response
  23.     {
  24.     $jsonsend $request->getContent();
  25.         if(!$this->isJson($jsonsend)){
  26.                 return $this->returnError();
  27.         }
  28.         $json json_decode($jsonsendtrue);
  29.     $response = new Response();
  30.     $varFile 'test.txt';
  31.     if(!isset($json['test'])){
  32.             return $this->returnError();
  33.     }
  34.     $varDir 'test';
  35.     if($json['test'] == '0'){
  36.         $varFile 'prod.txt';
  37.         $varDir 'prod';
  38.     }
  39.     $timeconteggio file_get_contents($varFile);
  40.     $storage = new StorageClient([
  41.                 'keyFilePath' => 'bulgari-303613-d1e8adbe4a39.json'
  42.         ]);
  43.         $bucket $storage->bucket('change2app');
  44.         $array_3D = [];
  45.         $crc uniqid() . uniqid();
  46.         $storage->registerStreamWrapper();
  47.         $sapMemo '';
  48.         $datetimemax 0;
  49.     $index_data file_get_contents("index_data.json");
  50.         $jsonSap json_decode($index_datatrue);
  51.         for($i=0$i<count($jsonSap); $i++){
  52.                 $sapCheck $jsonSap[$i]["sap"];
  53.                 $datetimeupd $jsonSap[$i]["time"];
  54.                 if(!file_exists($varDir  "/" $sapCheck ".txt") || $datetimeupd != file_get_contents($varDir  "/" $sapCheck ".txt")){
  55.             if(!file_exists("acl_check/" $sapCheck ".txt") || file_exists("exported/" $sapCheck ".txt")){
  56.                 $array_3D[] = array(
  57.                                 "sap" => strtoupper($sapCheck),
  58.                                     "url" => "https://www.change2.app/include.js?SAP=" $sapCheck "&CRC=" $crc
  59.                             );
  60.                 file_put_contents($varDir  "/" $sapCheck ".txt"$datetimeupd);
  61.             }
  62.         }
  63.     }
  64.     $response->setContent(
  65.         json_encode([
  66.                 'status' => 'success',
  67.                 'code' => '200',
  68.                 'result' => $array_3D
  69.         ])
  70.         );
  71.         $response->headers->set('Content-Type''application/json');
  72.         file_put_contents($varFile$datetimemax);
  73.         return $response;
  74.     die();
  75.     /*
  76.     $options = ['suffix' => 'configurator_settings.json'];
  77.         foreach ($bucket->objects($options) as $object) {
  78.                 $stringUrl = $object->name();
  79.                 $sapCheck = explode('/', $stringUrl);
  80.                 $datetimeupd = strtotime($object->info()['updated']);
  81.                 if($sapMemo != $sapCheck[0]){
  82.                         $sapMemo = $sapCheck[0];
  83.                         $object_exist = $bucket->object($sapCheck[0] . '/configurator_settings.json');
  84.             $object_exist2 = $bucket->object($sapCheck[0] . '/__settings__.js');
  85.                         if($object_exist->exists() && $object_exist2->exists()){
  86.                                 $datetimeupdExist = strtotime($object_exist->info()['updated']);
  87.                                 if($datetimemax < $datetimeupdExist){
  88.                                         $datetimemax = $datetimeupdExist;
  89.                                 }
  90.                                 $contents = file_get_contents('gs://change2app/' . $sapCheck[0] . '/configurator_settings.json');
  91.                                 $json_check = json_decode($contents,true);
  92.                                 if(isset($json_check['player3D']) && intval($json_check['player3D']) == 1){
  93.                     if($timeconteggio < $datetimeupdExist){
  94.                                             $array_3D[] = array(
  95.                                                     "sap" => $sapCheck[0],
  96.                                                     "url" => "https://www.change2.app/include.js?SAP=" . $sapCheck[0] . "&CRC=" . $crc
  97.                                             );
  98.                     }
  99.                 }
  100.                         }
  101.                 }
  102.         }
  103.         $response->setContent(
  104.         json_encode([
  105.                 'status' => 'success',
  106.                 'code' => '200',
  107.                 'result' => $array_3D
  108.         ])
  109.            );
  110.         $response->headers->set('Content-Type', 'application/json');
  111.     file_put_contents($varFile, $datetimemax);
  112.     return $response;
  113.     */
  114.     }
  115.     #[Route('/fromto'name'app_fromto')]
  116.     public function fromto(Request $request): Response
  117.     {
  118.         $jsonsend $request->getContent();
  119.         if(!$this->isJson($jsonsend)){
  120.                 return $this->returnError();
  121.         }
  122.         $json json_decode($jsonsendtrue);
  123.         $response = new Response();
  124.         if(!isset($json['from']) || !isset($json['to'])){
  125.                 return $this->returnError();
  126.         }
  127.     $storage = new StorageClient([
  128.                 'keyFilePath' => 'bulgari-303613-d1e8adbe4a39.json'
  129.         ]);
  130.         $bucket $storage->bucket('change2app');
  131.         $array_3D = [];
  132.         $crc uniqid() . uniqid();
  133.         $storage->registerStreamWrapper();
  134.         $sapMemo '';
  135.         $datetimemax 0;
  136.     $index_data file_get_contents("index_data.json");
  137.     $jsonSap json_decode($index_datatrue);
  138.     for($i=0$i<count($jsonSap); $i++){
  139.         $sapCheck $jsonSap[$i]["sap"];
  140.         $datetimeupd $jsonSap[$i]["time"];
  141.         if($json['from'] <= $datetimeupd && $json['to'] >= $datetimeupd){
  142.             if(!file_exists("acl_check/" $sapCheck ".txt") || file_exists("exported/" $sapCheck ".txt")){
  143.                 $array_3D[] = array(
  144.                                     "sap" => strtoupper($sapCheck),
  145.                                         "url" => "https://www.change2.app/include.js?SAP=" $sapCheck "&CRC=" $crc
  146.                                 );
  147.             }
  148.         }
  149.     }
  150.     $response->setContent(
  151.         json_encode([
  152.                 'status' => 'success',
  153.                 'code' => '200',
  154.                 'result' => $array_3D
  155.         ])
  156.         );
  157.         $response->headers->set('Content-Type''application/json');
  158.         return $response;
  159.     }
  160.     #[Route('/create_index'name'create_index')]
  161.     public function create_index(UrlUpdateRepository $urlUpdateRepository): Response
  162.     {
  163.         $storage = new StorageClient([
  164.                 'keyFilePath' => 'bulgari-303613-d1e8adbe4a39.json'
  165.         ]);
  166.         $bucket $storage->bucket('change2app');
  167.         $array_3D = [];
  168.         $storage->registerStreamWrapper();
  169.         $sapMemo '';
  170.         $datetimemax 0;
  171.     $indexingCheck file_get_contents("indexing.txt");
  172.     if($indexingCheck == "1"){
  173.         die("sta gia girando");
  174.     }
  175.     file_put_contents("indexing.txt""1");
  176.     $index_data file_get_contents("index_data.json");
  177.         foreach ($bucket->objects() as $object) {
  178.                 $stringUrl $object->name();
  179.                 $sapCheck explode('/'$stringUrl);
  180.                 $datetimeupd strtotime($object->info()['updated']);
  181.                 if($sapMemo != $sapCheck[0]){
  182.                         $sapMemo $sapCheck[0];
  183.                         $object_exist $bucket->object($sapCheck[0] . '/configurator_settings.json');
  184.                         if($object_exist->exists()){
  185.                                 $datetimeupdExist strtotime($object_exist->info()['updated']);
  186.                                 $contents file_get_contents('gs://change2app/' $sapCheck[0] . '/configurator_settings.json');
  187.                                 $json_check json_decode($contents,true);
  188.                                 if(isset($json_check['player3D']) && intval($json_check['player3D']) == 1){
  189.                                         $array_3D[] = array(
  190.                                                 "sap" => $sapCheck[0],
  191.                         "time" => $datetimeupdExist
  192.                                         );
  193.                                 }
  194.                 //controllo se devo ricreare l'acl e controllare l'errore del file index.html
  195.                 //if(strpos($index_data, '{"sap":"' . $sapCheck[0] . '","time":' . $datetimeupdExist . '}') !== false){
  196.                     //nessuna novita
  197.                 //}else{
  198.                     file_put_contents("time_check/" $sapCheck[0] . ".txt"$datetimeupdExist);
  199.                     file_put_contents("acl_check/" $sapCheck[0] . ".txt"$datetimeupdExist);
  200.                 //}
  201.                         }
  202.                 }
  203.         }
  204.     file_put_contents("index_data.json"json_encode($array_3D));
  205.     file_put_contents("indexing.txt""0");
  206.         die('fine');
  207.     }
  208.     #[Route('/create_acl'name'create_acl')]
  209.     public function create_acl(UrlUpdateRepository $urlUpdateRepository): Response
  210.     {
  211.         $storage = new StorageClient([
  212.                 'keyFilePath' => 'bulgari-303613-d1e8adbe4a39.json'
  213.         ]);
  214.         $bucket $storage->bucket('change2app');
  215.         $array_3D = [];
  216.         $storage->registerStreamWrapper();
  217.         $sapMemo '';
  218.         $datetimemax 0;
  219.     $files_time scandir("acl_check");
  220.     if(count($files_time) > 2){
  221.         $sap str_replace('.txt'''$files_time[count($files_time)-1]);
  222.         //$sap = "br859769";
  223.         
  224.         $options = ["prefix" => $sap "/"];
  225.         echo $sap;
  226.         $object_exist $bucket->object($sap '/__settings__.js');
  227.                    $object_exist2 $bucket->object($sap '/index.html');
  228.         
  229.         //if($object_exist2->exists()){
  230.         if(!$object_exist->exists() && $object_exist2->exists()){
  231.             $contents file_get_contents('gs://change2app/' $sap '/index.html');
  232.             $content_elaborate str_replace('</script>'''str_replace('<script>'''$contents));
  233.             $content_elaborate str_replace('CONFIG_FILENAME = ASSET_PREFIX   ''CONFIG_FILENAME = ASSET_PREFIX + '$content_elaborate);
  234.             
  235.             $ret explode(PHP_EOL$content_elaborate);
  236.             for($z=0$z<count($ret); $z++){
  237.                 if(strpos($ret[$z], 'ASSET_PREFIX = "') !== false){
  238.                     $ret[$z] = 'ASSET_PREFIX = "";';
  239.                 }
  240.             }
  241.             $content_elaborate implode(PHP_EOL$ret);
  242.             //die($content_elaborate);
  243.             //$stream_data = fopen('data://text/plain,' . $content_elaborate, 'r');
  244.             //die($stream_data);
  245.             file_put_contents("tmp_upload.txt"$content_elaborate);
  246.             $bucket->upload(fopen("tmp_upload.txt"'r'), [
  247.                     'name' => $sap '/__settings__.js',
  248.                 ]);
  249.             //$object_exist2->delete();
  250.         }
  251.         foreach ($bucket->objects($options) as $object) {
  252.             $acl $object->acl();
  253.             $checkAclOk 0;
  254.             foreach ($acl->get() as $item) {
  255.                 if($item['entity'] == 'allUsers' && $item['role'] == 'READER'){
  256.                     $checkAclOk 1;
  257.                 }
  258.                 }
  259.             if($checkAclOk == 0){
  260.                 $object->update(['acl' => []], ['predefinedAcl' => 'PUBLICREAD']);
  261.             }
  262.         }
  263.         //die();
  264.         unlink("acl_check/" $sap ".txt");
  265.         file_put_contents("exported/" $sap ".txt""1");
  266.     }
  267.     die("fine");
  268.     }
  269.     #[Route('/check_update'name'app_check_update')]
  270.     public function checkupdate(UrlUpdateRepository $urlUpdateRepository): Response
  271.     {
  272.     $storage = new StorageClient([
  273.         'keyFilePath' => 'bulgari-303613-d1e8adbe4a39.json'
  274.     ]);
  275.     $bucket $storage->bucket('change2app');
  276.     $array_3D = [];
  277.     $array_3D_date = [];
  278.     $crc uniqid() . uniqid();
  279.     $storage->registerStreamWrapper();
  280.     $sapMemo '';
  281.     $datetimemax 0;
  282.     foreach ($bucket->objects() as $object) {
  283.         $stringUrl $object->name();
  284.         $sapCheck explode('/'$stringUrl);
  285.         $datetimeupd strtotime($object->info()['updated']);
  286.         
  287.         if($sapMemo != $sapCheck[0]){
  288.             $sapMemo $sapCheck[0];
  289.             $object_exist $bucket->object($sapCheck[0] . '/configurator_settings.json');
  290.             if($object_exist->exists()){
  291.                 $datetimeupdExist strtotime($object_exist->info()['updated']);
  292.                 if($datetimemax $datetimeupdExist){
  293.                                 $datetimemax $datetimeupdExist;
  294.                         }
  295.                 $contents file_get_contents('gs://change2app/' $sapCheck[0] . '/configurator_settings.json');
  296.                 $json_check json_decode($contents,true);
  297.                 if(isset($json_check['player3D']) && intval($json_check['player3D']) == 1){
  298.                     $array_3D[] = array(
  299.                                             "sap" => $sapCheck[0],
  300.                                             "url" => "https://www.change2.app/include.js?SAP=" $sapCheck[0] . "&CRC=" $crc
  301.                                     );
  302.                     //$urlUpdate = $urlUpdateRepository->findOneBy(array('sap' => $sapCheck[0], 'url' => $stringUrl));
  303.                             //if (!$urlUpdate){
  304.                                 //    $urlUpdate = new UrlUpdate();
  305.                                 //    $urlUpdate->setSap($sapCheck[0]);
  306.                                 //    $urlUpdate->setUrl($stringUrl);
  307.                             //}
  308.                     //if($sapCheck[0] == '41273' || $sapCheck[0] == 'core' || $sapCheck[0] == 'core' || $sapCheck[0] == 'AN858365' || $sapCheck[0] == '41252'){
  309.                         //$object->update(['acl' => []], ['predefinedAcl' => 'PUBLICREAD']);
  310.                     //}
  311.                     //echo $sapCheck[0] . ' - ' . $datetimeupd . ' - ' . $object->info()['updated'] . ' - ' . $stringUrl . PHP_EOL;
  312.                 }
  313.             }
  314.         }
  315.         if($object->name() != 'include.js'){
  316.             $object->update(['acl' => []], ['predefinedAcl' => 'PUBLICREAD']);
  317.         }
  318.     }
  319.     print_r($array_3D);
  320.     echo $datetimemax;
  321.     die('ok');
  322.     }
  323.     public function returnError(){
  324.                 $response = new Response();
  325.             $response->setContent(
  326.                         json_encode([
  327.                                 'status' => 'error',
  328.                                 'code' => '403',
  329.                                 'message' => 'Invalid JSON'
  330.                         ])
  331.                 );
  332.         $response->headers->set('Content-Type''application/json');
  333.         return $response;
  334.     }
  335.     function isJson($string)
  336.     {
  337.                 json_decode($string);
  338.                 return (json_last_error() == JSON_ERROR_NONE);
  339.     }
  340. }