vendor/virus-camp/scan-file-security/src/API/FileHandler.php line 111

Open in your IDE?
  1. <?php
  2. namespace VSC\API;
  3. use Exception;
  4. use RuntimeException;
  5. use CURLFile;
  6. use GuzzleHttp\Client;
  7. use GuzzleHttp\Exception\RequestException;
  8. use VSC\Config\VirusCampConfiguration;
  9. use GuzzleHttp\Psr7\Utils;
  10. /**
  11.  * Classe pour gĂ©rer les fichiers et leurs scans
  12.  */
  13. class FileHandler extends VirusCamp
  14. {
  15.     private array $fileInformation = [];
  16.     /**
  17.      * Scanner un fichier (chemin du fichier)
  18.      *
  19.      * @param string $filePath
  20.      * @return mixed
  21.      * @throws Exception
  22.      */
  23.     public function scanFile(string $filePath)
  24.     {
  25.         unset($this->headers['headers']['Content-Type']);
  26.         $additional_data = [
  27.             "multipart"=>[
  28.                 [
  29.                     "name"=>"file",
  30.                     "contents"=>Utils::tryFopen($filePath,'r'),
  31.                     'filename'=>$this->getNameFile($filePath)
  32.                 ],
  33.             ]
  34.         ]; 
  35.         return $this->post('scan'$additional_data);
  36.     }
  37.     /**
  38.      * Scanner un fichier
  39.      *
  40.      * @param string $filePath
  41.      * @return mixed
  42.      * @throws Exception
  43.      */
  44.     public function scanFileInInput(Array $file)
  45.     {
  46.         $this->loadFile($file["tmp_name"]);
  47.         unset($this->headers['headers']['Content-Type']);
  48.         $additional_data = [
  49.             "multipart"=>[
  50.                 [
  51.                     "name"=>"file",
  52.                     "contents"=>Utils::tryFopen($file["tmp_name"],'r'),
  53.                     'filename'=>$file["name"]
  54.                     
  55.                 ],
  56.             ]
  57.         ]; 
  58.         return ($this->post('scan'$additional_data));
  59.     }
  60.     public function resultsScan(Array|string $file,string $typeFile "urlOrDir") : Array
  61.     {
  62.         if ($typeFile === "input") {
  63.             extract($this->scanFileInInput($file));
  64.         }elseif ($typeFile === "urlOrDir") {
  65.             extract($this->scanFile($file));
  66.         }
  67.         $errorInScan = [];
  68.         $infosScan $this->get("file/".$uuid."/scans");
  69.         foreach ($infosScan as $infos) {
  70.             if ($infos['Detected']) {
  71.                 $errorInScan[]= [
  72.                     'virusName' => $infos['VirusName'],
  73.                     'antivirusName' => $infos['AntivirusEngineVersion']['AntivirusEngine']['Name'],
  74.                 ];
  75.             }
  76.         }
  77.         return $errorInScan;
  78.     }
  79.     private function getNameFile($fileUrl){
  80.         $path parse_url($fileUrlPHP_URL_PATH);
  81.         $filename basename($path);
  82.         return $filename;
  83.     }
  84.     public function getScanResults(string $uuid): array
  85.     {
  86.         return $this->get("file/{$uuid}/scans");
  87.     }
  88.     public function loadFile(string $filePath){
  89.         if(!file_exists($filePath)){
  90.             throw new RuntimeException("Le fichier n'existe pas {$filePath}");
  91.         }
  92.         $this->fileInformation pathinfo($filePath);
  93.         if(isset($this->fileInformation['filename'])){
  94.             $this->fileName $this->fileInformation['filename'];
  95.         }
  96.         if(isset($this->fileInformation['extension'])){
  97.             $this->fileExtension $this->fileInformation['extension'];
  98.         }
  99.         if(isset($this->fileInformation['basename'])){
  100.             $this->fileBaseName $this->fileInformation['basename'];
  101.         }
  102.         if(isset($this->fileInformation['dirname'])){
  103.             $this->fileDirName $this->fileInformation['dirname'];
  104.         }
  105.     }
  106.     private function getFileInformation(){
  107.         return $this->fileInformation;
  108.     }
  109.     private function getFileFileName(){
  110.        if(empty($this->fileInformation) && !isset($this->fileInformation['filename'])){
  111.             return null;
  112.        }
  113.        return $this->fileInformation['filename'];
  114.     }
  115.     private function getFileExtension(){
  116.         return $this->fileExtension;
  117.     }
  118.     private function getFileBaseName(){
  119.         return $this->fileBaseName;
  120.     }
  121.     private function getFilePath(){
  122.         return $this->fileDirName.'/'.$this->fileBaseName;
  123.     }
  124. }