src/Controller/OnlineFileController.php line 16

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Kernel;
  4. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  5. use Symfony\Component\Cache\Adapter\FilesystemAdapter;
  6. use Symfony\Component\HttpFoundation\JsonResponse;
  7. use Symfony\Component\HttpFoundation\Response;
  8. use Symfony\Component\Routing\Annotation\Route;
  9. use Symfony\Contracts\Cache\ItemInterface;
  10. class OnlineFileController extends AbstractController
  11. {
  12.     #[Route('/onlineFile/{url}/{action?}'name'onlineFile')]
  13.     public function index(Kernel $kernel,$url,$action=null): Response
  14.     {
  15.         $url urldecode($url);
  16.         $cache = new FilesystemAdapter(directory:$kernel->getProjectDir().DIRECTORY_SEPARATOR.'var'.DIRECTORY_SEPARATOR.'onlineFileCheck');
  17.         if(strtolower($action) == 'help' || strtolower($url) == 'help')
  18.         {
  19.             return new Response('/onlineFile/[url_encode(url_encode(url))]/{help|store|compare|show}');
  20.         }
  21.         elseif(strtolower($action) == 'store')
  22.         {
  23.             $content file_get_contents($url);
  24.             $hash hash('sha256',$content);
  25.             $cached = new \stdClass();
  26.             $cached->url $url;
  27.             $cached->content $content;
  28.             $cached->hash $hash;
  29.             $cached->stored = new \DateTime();
  30.             $cacheItem $cache->getItem($url);
  31.             $cacheItem->set($cached);
  32.             $cache->save($cacheItem);
  33.             return new JsonResponse($cached);
  34.         }
  35.         elseif(strtolower($action) == 'compare')
  36.         {
  37.             $cached $cache->getItem($url)->get();
  38.             if($cached !== null)
  39.             {
  40.                 $old json_decode($cached->content,true);
  41.                 $new json_decode(file_get_contents($url),true);
  42.                 $diffs array_diff($old,$new);
  43.                 if(count($diffs) > 0)
  44.                     $response '<pre>'.var_export($diffs,true).'</pre>';
  45.                 else
  46.                     $response 'NO DIFF';
  47.             }
  48.             else
  49.                 $response 'NO CACHE FOUND';
  50.             return new Response($response);
  51.         }
  52.         elseif(strtolower($action) == 'show')
  53.         {
  54.             $cached $cache->getItem($url)->get();
  55.             if($cached !== null)
  56.             {
  57.                 $response 'HASH : '.$cached->hash.'<br /><br /><pre>'.json_encode(json_decode($cached->content),JSON_PRETTY_PRINT).'</pre>';
  58.             }
  59.             else
  60.                 $response 'NO CACHE FOUND';
  61.             return new Response($response);
  62.         }
  63.         else
  64.         {
  65.             $response 'OK';
  66.             $cached $cache->getItem($url)->get();
  67.             if($cached !== null)
  68.             {
  69.                 $content file_get_contents($url);
  70.                 $hash hash('sha256',$content);
  71.                 if($cached->hash !== $hash)
  72.                 {
  73.                     $response 'CHANGED';
  74.                 }
  75.             }
  76.             else
  77.                 $response 'EMPTY';
  78.             return new JsonResponse($response);
  79.         }
  80.     }
  81. }