Example #1
0
 /**
  * Scan a file via VirusTotal
  * @param  string  $filename  The filename to scan, provide an absolute filename
  * @param  boolean $rescan    Wether or not to force a rescan
  * @return array              Thee information returned by VirusTotal.
  */
 public static function scanFile($filename, $rescan = false)
 {
     // The api key must be set!
     $apikey = config("virus-total-api.api_key");
     if (!$apikey) {
         throw new \Exception("Please setup your VirusTotal api key to use the api.", 1);
     }
     // Prepare the file scanner
     $filescan = new \VirusTotal\File($apikey);
     // Try/Catch to get rate limit exceptions thrown by the api wrapper
     try {
         // Force rescan if needed.
         if ($rescan) {
             $scanResults = $filescan->rescan($filename);
         } else {
             $scanResults = $filescan->getReport($filename);
             if ($scanResults['response_code'] == 0) {
                 $scanResults = $filescan->scan($filename);
             }
         }
     } catch (\Exception $e) {
         if ($e instanceof \VirusTotal\Exceptions\RateLimitException) {
             return ['success' => false, 'error' => 'rate limit exceeded'];
         } else {
             throw new \Exception("Please setup a valid VirusTotal api key to use the api.", 1);
         }
     }
     return array_merge(['success' => true], $scanResults);
 }