ioFile = $ioFile; $this->filesystem = $filesystem; $this->csv = $csv; $this->date = $date; $this->curl = $curl; $this->countryCollectionFactory = $countryCollectionFactory; $this->mmdbReader = $mmdbReader; } /** * @param $url * @return float|int */ public function retrieveRemoteFileSize($url) { $contentLength = 0; $this->curl->setOptions([ CURLOPT_NOBODY => true, CURLOPT_HEADER => true, CURLOPT_RETURNTRANSFER => true, CURLOPT_FOLLOWLOCATION => true ]); $this->curl->get($url); $data = $this->curl->getBody(); if ($data) { $status = 404; if (preg_match("/^HTTP\/1\.[01] (\d\d\d)/", $data, $matches)) { $status = (int)$matches[1]; } if (($status > 300 && $status <= 308) || (preg_match("/Content-Length: (\d+)/", $data, $matches) && $status === 200) ) { $contentLength = (int)$matches[1]; } } return $contentLength; } /** * @return array */ public function autoImportData(): array { set_time_limit(0); $copyFile = false; $importProgress = [ 'status' => 'process', 'total' => 100, 'exec' => 1, 'step' => $this->step, 'message' => __('Download in Progress...'), 'remote_file_size' => $this->retrieveRemoteFileSize($this->pathSourceFile), 'local_file_name' => $this->getDataFile() ]; $this->setProgress($importProgress); if ($this->ioFile->fileExists($this->getDataFile(), true)) { $copyFile = true; } else { $this->ioFile->setAllowCreateFolders(true); if ($this->ioFile->createDestinationDir($this->getDataPath())) { $copyFile = $this->ioFile->cp($this->getPathSourceFile(), $this->getDataFile()); } } $this->mmdbReader->refresh(); $result = [ 'status' => 'success', 'message' => $this->getInstalledMessage() ]; if (! $copyFile) { $lastError = error_get_last(); $result = ['status' => 'fail', 'message' => $lastError['message']]; } return $result; } /** * @return string */ private function getPathSourceFile(): string { if (!empty($this->pathSourceFile)) { $this->pathSourceFile .= '?t=' . $this->date->gmtTimestamp(); } return $this->pathSourceFile; } /** * @param $importProgress */ public function setProgress($importProgress) { $this->ioFile->setAllowCreateFolders(true); if ($this->ioFile->createDestinationDir($this->getDataPath())) { $this->csv->saveData( $this->getProgressFile(), [$importProgress] ); } } /** * @return array */ public function getProgress() { if ($this->ioFile->fileExists($this->getProgressFile(), true)) { $csvData = $this->csv->getData($this->getProgressFile()); if (isset($csvData[0][6]) && isset($csvData[0][5]) && $this->ioFile->fileExists($csvData[0][6]) ) { $total = $csvData[0][5]; $exec = filesize($csvData[0][6]); } else { $total = (int)$csvData[0][1]; $exec = (int)$csvData[0][2]; } $importProgress = [ 'status' => $csvData[0][0], 'total' => $total, 'exec' => $exec, 'step' => (int)$csvData[0][3], 'message' => $csvData[0][4] ]; } else { $importProgress = [ 'status' => 'process', 'total' => 100, 'exec' => 1, 'step' => $this->step, 'message' => __('Download in Progress...') ]; } return $importProgress; } /** * @return mixed */ public function getInstalledVersion() { return ($metadata = $this->mmdbReader->metadata()) ? $metadata['buildDate'] : null; } /** * @return string */ public function getInstalledMessage(): string { $metadata = $this->mmdbReader->metadata(); return $metadata ? (string)__( 'Installed Database - %1 (Build Date: %2)', $metadata['databaseType'], $metadata['buildDate'] ) : ''; } /** * @return bool|mixed */ public function isLatestInstalledVersion() { $result = false; $installedBuildDate = $this->getInstalledVersion(); if (!$installedBuildDate) { return false; } $existVersion = null; $copyFile = false; $this->ioFile->setAllowCreateFolders(true); if ($this->ioFile->createDestinationDir($this->getDataPath())) { $copyFile = $this->ioFile->cp($this->pathVersionsFile, $this->getVersionsFile()); } if ($copyFile) { $csvData = $this->csv->getData($this->getVersionsFile()); foreach ($csvData as $data) { if ($data[0] == $this->getDataName()) { $existVersion = [ 'data_name' => $data[0], 'file_date' => $data[1] ]; break; } } $this->ioFile->rm($this->getVersionsFile()); if ($existVersion) { $result = strtotime($existVersion['file_date']) === strtotime($installedBuildDate); } } return $result; } /** * @return string */ public function getDataName(): string { return $this->dataName; } /** * @return string */ public function getDataLabel() { return $this->dataLabel; } /** * @return string */ public function getDataPath() { return $this->filesystem->getDirectoryRead(DirectoryList::MEDIA) ->getAbsolutePath(Config::LOCAL_PATH); } /** * @return string */ public function getDataFile() { return $this->getDataPath() . $this->fileName; } /** * @return string */ public function getVersionsFile() { return $this->getDataPath() . $this->versionsFileName; } /** * @return string */ public function getProgressFile() { return $this->getDataPath() . $this->progressFileName; } }