public function testListFiles() { // testing empty file $this->file->shouldReceive('isFile')->with('foo.file')->andReturn(true); $this->file->shouldReceive('isFile')->with('bar.file')->andReturn(true); $this->assertEquals(array(), $this->archive->listFiles()); // testing not empty file $this->archive->add('foo.file'); $this->archive->add('bar.file'); $this->assertEquals(array('foo.file', 'bar.file'), $this->archive->listFiles()); // testing with a empty sub dir $this->file->shouldReceive('isFile')->with('/path/to/subDirEmpty')->andReturn(false); $this->file->shouldReceive('files')->with('/path/to/subDirEmpty')->andReturn(array()); $this->file->shouldReceive('directories')->with('/path/to/subDirEmpty')->andReturn(array()); $this->archive->folder('subDirEmpty')->add('/path/to/subDirEmpty'); $this->assertEquals(array('foo.file', 'bar.file'), $this->archive->listFiles()); // testing with a not empty sub dir $this->file->shouldReceive('isFile')->with('/path/to/subDir')->andReturn(false); $this->file->shouldReceive('isFile')->with('sub.file')->andReturn(true); $this->file->shouldReceive('files')->with('/path/to/subDir')->andReturn(array('sub.file')); $this->file->shouldReceive('directories')->with('/path/to/subDir')->andReturn(array()); $this->archive->folder('subDir')->add('/path/to/subDir'); $this->assertEquals(array('foo.file', 'bar.file', 'subDir/sub.file'), $this->archive->listFiles()); }
/** * Parse attachments * @return array Returns array with failed or success data * (See parser-common/src/Parser.php) for more info. */ public function parse() { // Validate user based regex try { preg_match(config("{$this->configBase}.parser.file_regex"), '', $matches); } catch (\Exception $e) { $this->warningCount++; return $this->failed('Configuration error in the regular expression'); } foreach ($this->parsedMail->getAttachments() as $attachment) { if (strpos($attachment->filename, '.zip') !== false && $attachment->contentType == 'application/octet-stream') { $zip = new Zipper(); if (!$this->createWorkingDir()) { return $this->failed("Unable to create working directory"); } file_put_contents($this->tempPath . $attachment->filename, $attachment->getContent()); $zip->zip($this->tempPath . $attachment->filename); $zip->extractTo($this->tempPath); foreach ($zip->listFiles() as $index => $compressedFile) { if (strpos($compressedFile, '.csv') !== false) { // For each CSV file we find, we are going to do magic (however they usually only send 1 zip) if (preg_match(config("{$this->configBase}.parser.file_regex"), $compressedFile, $matches)) { $this->feedName = $matches[1]; // If feed is known and enabled, validate data and save report if ($this->isKnownFeed() && $this->isEnabledFeed()) { $csvReports = new Reader\CsvReader(new SplFileObject($this->tempPath . $compressedFile)); $csvReports->setHeaderRowNumber(0); foreach ($csvReports as $report) { // Handle field mappings first $aliasses = config("{$this->configBase}.feeds.{$this->feedName}.aliasses"); if (is_array($aliasses)) { foreach ($aliasses as $alias => $real) { if (array_key_exists($alias, $report)) { $report[$real] = $report[$alias]; unset($report[$alias]); } } } /* * Legacy 3.x fix for migrations. * * This resolves shadowserver errors where the CSV was send in duplicate resulting * in the header fields being used as data. If the header is detected the row can * be skipped safely */ if ($report['ip'] === 'ip') { continue; } // Sanity check if ($this->hasRequiredFields($report) === true) { // incident has all requirements met, filter and add! $report = $this->applyFilters($report); $incident = new Incident(); $incident->source = config("{$this->configBase}.parser.name"); $incident->source_id = false; $incident->ip = $report['ip']; $incident->domain = false; $incident->class = config("{$this->configBase}.feeds.{$this->feedName}.class"); $incident->type = config("{$this->configBase}.feeds.{$this->feedName}.type"); $incident->timestamp = strtotime($report['timestamp']); $incident->information = json_encode($report); // some rows have a domain, which is an optional column we want to register switch ($this->feedName) { case "spam_url": if (isset($report['url'])) { $incident->domain = getDomain($report['url']); } break; case "ssl_scan": if (isset($report['subject_common_name'])) { /* * Common name does not add http://, but that is required for * the domain helper check so lets add it manually */ $testurl = "http://{$report['subject_common_name']}"; $incident->domain = getDomain($testurl); } break; case "compromised_website": if (isset($report['http_host'])) { $incident->domain = getDomain($report['http_host']); } break; } $this->incidents[] = $incident; } //End hasRequired fields } // End foreach report loop } // End isKnown & isEnabled } else { // Pregmatch failed to get feedName from attachment $this->warningCount++; } } else { // Attached file is not a CSV within a ZIP file $this->warningCount++; } } // End each file in ZIP attachment loop } // End if not a ZIP attachment } // End foreach attachment loop return $this->success(); }
public function postEmpresa(Request $request) { ini_set('max_execution_time', $this->max_time); if (!$request->hasFile('empresa')) { return response()->json(['error' => "Ya se subio el archivo"], 200); } $file = $request->empresa; if ($file->getClientOriginalExtension() != "xml" && $file->getClientOriginalExtension() != "zip") { return response()->json(['error' => "El formato debe de ser xml o zip"], 200); } try { if ($file->getClientOriginalExtension() == "zip") { $errores = array(); $zipper = new Zipper(); $zipper->make($file->getRealPath()); $count = 0; foreach ($zipper->listFiles() as $file) { $fileExpled = explode(".", $file); if ($fileExpled[count($fileExpled) - 1] == "xml") { $nombre = $file; try { $resp = $this->empresaFiles($zipper->getFileContent($file)); } catch (\Exception $e) { $resp = "no"; $errMess = $e->getMessage(); $errores[] = "Error: {$errMess} en archivo: {$nombre} "; } if ($resp == "yes") { $count++; } } } if (count($errores) > 0) { return response()->json(['error' => $errores], 200); } } else { $nombre = $file->getClientOriginalName(); $text = file_get_contents($file->getRealPath()); $this->empresaFiles($text, false); } } catch (\Exception $e) { return response()->json(['error' => "Archivo: {$nombre} => " . $e->getMessage()], 200); } return response()->json("yes", 200); }
/** * List files that are within the archive * * @return array * @static */ public static function listFiles() { return \Chumper\Zipper\Zipper::listFiles(); }