configHelper = $configHelper; $this->filesystem = $filesystem; } public function getImageDir() { $directory = $this->filesystem->getDirectoryWrite(DirectoryList::ROOT); $strPath = $this->configHelper->getImportFileDir(); if (!$strPath) { $strPath = self::MEDIA_IMAGE_IMPORT_FOLDER; } $strReturn = $directory->getAbsolutePath($strPath); return $strReturn; } public function processImport($arrData, $options = array()) { if (!count($arrData)) { return; } $objResponse = $this->objResponse; $intStartTime = $objResponse->microtimeFloat(); foreach ($arrData as $arrTypeImages) { $objResponse->addLogEntry('Start download of ' . count($arrTypeImages), 'global', 'images', 0); $this->downloadImages($arrTypeImages); } $objResponse->addLogEntry('Finish images import of ' . count($arrTypeImages), 'global', 'images', $objResponse->getDuration($intStartTime)); // $objResponse->addLogEntry('Images not supported.', 'global', 'categories', 0); } protected static function generateSplitPathForID($iID, $iCharNumber = 4) { $iID = (int) $iID; $sIDPath = ''; $sSuffix = ''; $sTmpID = (string) $iID; while (strlen($sTmpID) >= $iCharNumber) { $sSuffix .= substr($sTmpID, 0, $iCharNumber); $sIDPath .= '/' . $sSuffix; $sTmpID = substr($sTmpID, $iCharNumber); } return substr(($sIDPath . "/" . $iID), 1); } private function sanitizeFileName($strName) { $strName = basename($strName); $strName = str_replace([chr(0), '..'], ['', ''], $strName); $arrExtensions = explode(',', $this->configHelper->getDocumentExtensions()); $strPattern = '/(' . implode('|', array_map(function ($s) { return preg_quote(trim($s), '/'); }, $arrExtensions)) . ')$/'; return (preg_match($strPattern, $strName)) ? $strName : false; } public function downloadImages($arrImages) { if (!count($arrImages)) { return; } $objResponse = $this->objResponse; $strDir = $this->getImageDir(); if (!is_dir($strDir)) { mkdir($strDir, 0755, true); } if (!is_dir($strDir)) { throw new \Exception('Import Folder not found'); } $objResponse->increaseRecordCount(count($arrImages)); foreach ($arrImages as $arrProdImages) { $objResponse->addEntities($arrProdImages, 'images', array('csid', 'csclass', 'sku'), 'csid'); foreach ($arrProdImages as $arrImage) { $bForce = $arrImage['force'] ?? false; $strImportPath = $arrImage['path'] ?? ''; if (!$strImportPath) { $strImportPath = self::generateSplitPathForID($arrImage['fileid']); } if (substr($strImportPath, 0, 1) !== '/') { $strImportPath = $strDir . '/' . $strImportPath; } $strFileName = $this->sanitizeFileName($arrImage['filename']); if ($strFileName === false) { $objResponse->addLogEntry('Filename or extension not allowed ' . $arrImage['filename'], $arrImage['csid'], 'images', -1, true); continue; } if (!$bForce && !$this->isFileModified($strImportPath . '/' . $strFileName, $arrImage['mdate'])) { //$objResponse->addLogEntry('Skip unchanged image '.$strImportPath.$arrImage['filename'],$arrImage['csid'], 'images'); continue; } if (!$this->downloadFileByURL($arrImage['url'], $strImportPath . '/', $strFileName)) { $objResponse->addLogEntry('Could not download ' . $strFileName . ' with URL:' . $arrImage['url'] . ' to folder ' . $strImportPath . '/', $arrImage['csid'], 'images', -1, true); continue; } $objResponse->addLogEntry('Download ' . $arrImage['filename'] . ' with URL:' . $arrImage['url'], $arrImage['csid'], 'images'); } } } protected function downloadFileByURL($strUrl, $strTargetPath, $strFileName) { if (!is_dir($strTargetPath)) { @mkdir($strTargetPath, 0755, true); } if (!is_dir($strTargetPath) || !preg_match('/^https?/', $strUrl)) { return false; } $ch = curl_init($strUrl); $fp = fopen($strTargetPath . $strFileName, 'wb'); if (!$fp) { return false; } curl_setopt($ch, CURLOPT_FILE, $fp); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); curl_setopt($ch, CURLOPT_TIMEOUT, 240); //if ($this->strUsername) curl_setopt ($ch, CURLOPT_USERPWD, $this->strUsername.":".$this->strPassword); // Speichern Login-Daten in Optionen $result = curl_exec($ch); curl_close($ch); fclose($fp); clearstatcache(); if (is_file($strTargetPath . $strFileName)) { $intFileSize = (int) filesize($strTargetPath . $strFileName); if ($intFileSize) { $strContent = file_get_contents($strTargetPath . $strFileName, false, null, 0, 100); if (preg_match('/access.*?denied/i', $strContent)) { $intFileSize = 0; } } if (!$intFileSize) { @unlink($strTargetPath . $strFileName); return false; } return true; } return false; } protected function isFileModified($strPath, $strLastModificationDate) { if (!is_file($strPath) || !filesize($strPath)) { return true; } return (filemtime($strPath) < strtotime($strLastModificationDate)); } }