/** * Check validity of a base URL * * @param string $baseUrl * @return void * @throws \Magento\Framework\Model\Exception * @throws \Exception */ protected function _checkUrl($baseUrl) { try { $staticFile = $this->_findFirstFileRelativePath('', '/.+\\.(html?|js|css|gif|jpe?g|png)$/'); $staticUrl = $baseUrl . $this->_filesystem->getUri(\Magento\Framework\App\Filesystem::PUB_DIR) . '/' . $staticFile; $client = new \Magento\Framework\HTTP\ZendClient($staticUrl); $response = $client->request('GET'); } catch (\Exception $e) { $this->messageManager->addError(__('The URL "%1" is not accessible.', $baseUrl)); throw $e; } if ($response->getStatus() != 200) { $this->messageManager->addError(__('The URL "%1" is invalid.', $baseUrl)); throw new \Magento\Framework\Model\Exception(__('Response from the server is invalid.')); } }
/** * Retrieve URL for media catalog * * If we use Database file storage and server doesn't support rewrites (.htaccess in media folder) * we have to put name of fetching media script exactly into URL * * @param \Magento\Framework\App\Filesystem $filesystem * @param bool $secure * @return string|bool */ protected function _getMediaScriptUrl(\Magento\Framework\App\Filesystem $filesystem, $secure) { if (!$this->_getConfig(self::XML_PATH_USE_REWRITES) && $this->_coreFileStorageDatabase->checkDbUsage()) { return $this->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_WEB, $secure) . $filesystem->getUri(\Magento\Framework\App\Filesystem::PUB_DIR) . '/' . self::MEDIA_REWRITE_SCRIPT; } return false; }
/** * Validate uploaded file * * @return $this * @throws \Magento\Framework\Model\Exception */ protected function _validateUploadedFile() { $option = $this->getOption(); $processingParams = $this->_getProcessingParams(); /** * Upload init */ $upload = new \Zend_File_Transfer_Adapter_Http(); $file = $processingParams->getFilesPrefix() . 'options_' . $option->getId() . '_file'; $maxFileSize = $this->getFileSizeService()->getMaxFileSize(); try { $runValidation = $option->getIsRequire() || $upload->isUploaded($file); if (!$runValidation) { $this->setUserValue(null); return $this; } $fileInfo = $upload->getFileInfo($file); $fileInfo = $fileInfo[$file]; $fileInfo['title'] = $fileInfo['name']; } catch (\Exception $e) { // when file exceeds the upload_max_filesize, $_FILES is empty if (isset($_SERVER['CONTENT_LENGTH']) && $_SERVER['CONTENT_LENGTH'] > $maxFileSize) { $this->setIsValid(false); $value = $this->getFileSizeService()->getMaxFileSizeInMb(); throw new Exception(__("The file you uploaded is larger than %1 Megabytes allowed by server", $value)); } else { switch ($this->getProcessMode()) { case \Magento\Catalog\Model\Product\Type\AbstractType::PROCESS_MODE_FULL: throw new Exception(__('Please specify the product\'s required option(s).')); break; default: $this->setUserValue(null); break; } return $this; } } /** * Option Validations */ // Image dimensions $_dimentions = array(); if ($option->getImageSizeX() > 0) { $_dimentions['maxwidth'] = $option->getImageSizeX(); } if ($option->getImageSizeY() > 0) { $_dimentions['maxheight'] = $option->getImageSizeY(); } if (count($_dimentions) > 0) { $upload->addValidator('ImageSize', false, $_dimentions); } // File extension $_allowed = $this->_parseExtensionsString($option->getFileExtension()); if ($_allowed !== null) { $upload->addValidator('Extension', false, $_allowed); } else { $_forbidden = $this->_parseExtensionsString($this->getConfigData('forbidden_extensions')); if ($_forbidden !== null) { $upload->addValidator('ExcludeExtension', false, $_forbidden); } } // Maximum filesize $upload->addValidator('FilesSize', false, array('max' => $maxFileSize)); /** * Upload process */ $this->_initFilesystem(); if ($upload->isUploaded($file) && $upload->isValid($file)) { $extension = pathinfo(strtolower($fileInfo['name']), PATHINFO_EXTENSION); $fileName = \Magento\Core\Model\File\Uploader::getCorrectFileName($fileInfo['name']); $dispersion = \Magento\Core\Model\File\Uploader::getDispretionPath($fileName); $filePath = $dispersion; $tmpDirectory = $this->_filesystem->getDirectoryRead(\Magento\Framework\App\Filesystem::SYS_TMP_DIR); $fileHash = md5($tmpDirectory->readFile($tmpDirectory->getRelativePath($fileInfo['tmp_name']))); $filePath .= '/' . $fileHash . '.' . $extension; $fileFullPath = $this->_mediaDirectory->getAbsolutePath($this->_quotePath . $filePath); $upload->addFilter('Rename', array('target' => $fileFullPath, 'overwrite' => true)); $this->getProduct()->getTypeInstance()->addFileQueue(array('operation' => 'receive_uploaded_file', 'src_name' => $file, 'dst_name' => $fileFullPath, 'uploader' => $upload, 'option' => $this)); $_width = 0; $_height = 0; if ($tmpDirectory->isReadable($tmpDirectory->getRelativePath($fileInfo['tmp_name']))) { $_imageSize = getimagesize($fileInfo['tmp_name']); if ($_imageSize) { $_width = $_imageSize[0]; $_height = $_imageSize[1]; } } $uri = $this->_filesystem->getUri(\Magento\Framework\App\Filesystem::MEDIA_DIR); $this->setUserValue(array('type' => $fileInfo['type'], 'title' => $fileInfo['name'], 'quote_path' => $uri . $this->_quotePath . $filePath, 'order_path' => $uri . $this->_orderPath . $filePath, 'fullpath' => $fileFullPath, 'size' => $fileInfo['size'], 'width' => $_width, 'height' => $_height, 'secret_key' => substr($fileHash, 0, 20))); } elseif ($upload->getErrors()) { $errors = $this->_getValidatorErrors($upload->getErrors(), $fileInfo); if (count($errors) > 0) { $this->setIsValid(false); throw new Exception(implode("\n", $errors)); } } else { $this->setIsValid(false); throw new Exception(__('Please specify the product\'s required option(s).')); } return $this; }