/** * Create .htaccess file and deny backups directory access from web * * @return void */ protected function _hideBackupsForApache() { $filename = '.htaccess'; if (!$this->_varDirectory->isFile($filename)) { $this->_varDirectory->writeFile($filename, 'deny from all'); } }
/** * Delete Expired Captcha Images for specific website * * @param \Magento\Captcha\Helper\Data $helper * @param \Magento\Store\Model\Website|null $website * @param \Magento\Store\Model\Store|null $store * @return void */ protected function _deleteExpiredImagesForWebsite(\Magento\Captcha\Helper\Data $helper, \Magento\Store\Model\Website $website = null, \Magento\Store\Model\Store $store = null) { $expire = time() - $helper->getConfig('timeout', $store) * 60; $imageDirectory = $this->_mediaDirectory->getRelativePath($helper->getImgDir($website)); foreach ($this->_mediaDirectory->read($imageDirectory) as $filePath) { if ($this->_mediaDirectory->isFile($filePath) && pathinfo($filePath, PATHINFO_EXTENSION) == 'png' && $this->_mediaDirectory->stat($filePath)['mtime'] < $expire) { $this->_mediaDirectory->delete($filePath); } } }
/** * Check if file exist in filesystem and try to re-create it from database record if negative. * @param string $file * @return bool|int */ public function ensureFileInFilesystem($file) { $result = true; if (!$this->_mediaDirectory->isFile($file)) { $result = $this->_coreFileStorageDatabase->saveFileToFilesystem($file); } return $result; }
/** * Move files from TMP folder into destination folder * * @param string $tmpPath * @param string $destPath * @return bool */ protected function _moveFile($tmpPath, $destPath) { if ($this->_directory->isFile($tmpPath)) { return $this->_directory->copyFile($tmpPath, $destPath); } else { return false; } }
/** * Download sample file action * * @return \Magento\Framework\Controller\Result\Raw */ public function execute() { $fileName = $this->getRequest()->getParam('filename') . '.csv'; $filePath = self::SAMPLE_FILES_DIRECTORY . $fileName; if (!$this->fileDirectory->isFile($filePath)) { /** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */ $this->messageManager->addError(__('There is no sample file for this entity.')); $resultRedirect = $this->resultRedirectFactory->create(); $resultRedirect->setPath('*/import'); return $resultRedirect; } $fileSize = isset($this->fileDirectory->stat($filePath)['size']) ? $this->fileDirectory->stat($filePath)['size'] : null; $this->fileFactory->create($fileName, null, DirectoryList::VAR_DIR, 'application/octet-stream', $fileSize); /** @var \Magento\Framework\Controller\Result\Raw $resultRaw */ $resultRaw = $this->resultRawFactory->create(); $resultRaw->setContents($this->fileDirectory->readFile($filePath)); return $resultRaw; }
/** * Directory structure initializing * * @return void */ protected function _initFilesystem() { $this->_mediaDirectory->create($this->_path); $this->_mediaDirectory->create($this->_quotePath); $this->_mediaDirectory->create($this->_orderPath); // Directory listing and hotlink secure $path = $this->_path . '/.htaccess'; if (!$this->_mediaDirectory->isFile($path)) { $this->_mediaDirectory->writeFile($path, "Order deny,allow\nDeny from all"); } }
/** * Move files from TMP folder into destination folder * * @param string $tmpPath * @param string $destPath * @return bool */ protected function _moveFile($tmpPath, $destPath) { if ($this->_directory->isFile($tmpPath)) { $tmpRealPath = $this->_directory->getDriver()->getRealPath($this->_directory->getAbsolutePath($tmpPath)); $destinationRealPath = $this->_directory->getDriver()->getRealPath($this->_directory->getAbsolutePath($destPath)); $isSameFile = $tmpRealPath === $destinationRealPath; return $isSameFile ?: $this->_directory->copyFile($tmpPath, $destPath); } else { return false; } }
/** * Add image to media gallery and return new filename * * @param \Magento\Catalog\Model\Product $product * @param string $file file path of image in file system * @param string|string[] $mediaAttribute code of attribute with type 'media_image', * leave blank if image should be only in gallery * @param boolean $move if true, it will move source file * @param boolean $exclude mark image as disabled in product page view * @return string * @throws \Magento\Framework\Exception\LocalizedException * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) */ public function addImage(\Magento\Catalog\Model\Product $product, $file, $mediaAttribute = null, $move = false, $exclude = true) { $file = $this->mediaDirectory->getRelativePath($file); if (!$this->mediaDirectory->isFile($file)) { throw new LocalizedException(__('The image does not exist.')); } $pathinfo = pathinfo($file); $imgExtensions = ['jpg', 'jpeg', 'gif', 'png']; if (!isset($pathinfo['extension']) || !in_array(strtolower($pathinfo['extension']), $imgExtensions)) { throw new LocalizedException(__('Please correct the image file type.')); } $fileName = \Magento\MediaStorage\Model\File\Uploader::getCorrectFileName($pathinfo['basename']); $dispretionPath = \Magento\MediaStorage\Model\File\Uploader::getDispretionPath($fileName); $fileName = $dispretionPath . '/' . $fileName; $fileName = $this->getNotDuplicatedFilename($fileName, $dispretionPath); $destinationFile = $this->mediaConfig->getTmpMediaPath($fileName); try { /** @var $storageHelper \Magento\MediaStorage\Helper\File\Storage\Database */ $storageHelper = $this->fileStorageDb; if ($move) { $this->mediaDirectory->renameFile($file, $destinationFile); //If this is used, filesystem should be configured properly $storageHelper->saveFile($this->mediaConfig->getTmpMediaShortUrl($fileName)); } else { $this->mediaDirectory->copyFile($file, $destinationFile); $storageHelper->saveFile($this->mediaConfig->getTmpMediaShortUrl($fileName)); $this->mediaDirectory->changePermissions($destinationFile, DriverInterface::WRITEABLE_FILE_MODE); } } catch (\Exception $e) { throw new LocalizedException(__('We couldn\'t move this file: %1.', $e->getMessage())); } $fileName = str_replace('\\', '/', $fileName); $attrCode = $this->getAttribute()->getAttributeCode(); $mediaGalleryData = $product->getData($attrCode); $position = 0; if (!is_array($mediaGalleryData)) { $mediaGalleryData = ['images' => []]; } foreach ($mediaGalleryData['images'] as &$image) { if (isset($image['position']) && $image['position'] > $position) { $position = $image['position']; } } $position++; $mediaGalleryData['images'][] = ['file' => $fileName, 'position' => $position, 'label' => '', 'disabled' => (int) $exclude]; $product->setData($attrCode, $mediaGalleryData); if ($mediaAttribute !== null) { $this->setMediaAttribute($product, $mediaAttribute, $fileName); } return $fileName; }
/** * Check thumbnail and create it if need. * * @return bool */ public function checkAndCreate() { if ($this->_mediaDirectory->isFile($this->_cacheDir . $this->_post->getImageUrl())) { return true; } if (!$this->_mediaDirectory->isFile(Upload::UPLOAD_POST_IMAGE_DIR . $this->_post->getImageUrl())) { return false; } $image = $this->_imageFactory->create(array('file' => $this->_mediaDirectory->getAbsolutePath(Upload::UPLOAD_POST_IMAGE_DIR . $this->_post->getImageUrl()))); $image->setWH(75, 75); $image->resize(); $image->save(DirectoryList::PUB . DIRECTORY_SEPARATOR . DirectoryList::MEDIA . DIRECTORY_SEPARATOR . $this->_cacheDir . $this->_post->getImageUrl()); return true; }
/** * Copy image and return new filename. * * @param string $file * @return string * @throws \Magento\Framework\Exception\LocalizedException */ protected function _copyImage($file) { try { $destinationFile = $this->_getUniqueFileName($file); if (!$this->_mediaDirectory->isFile($this->_mediaConfig->getMediaPath($file))) { throw new \Exception(); } if ($this->_fileStorageDb->checkDbUsage()) { $this->_fileStorageDb->copyFile($this->_mediaDirectory->getAbsolutePath($this->_mediaConfig->getMediaShortUrl($file)), $this->_mediaConfig->getMediaShortUrl($destinationFile)); $this->_mediaDirectory->delete($this->_mediaConfig->getMediaPath($destinationFile)); } else { $this->_mediaDirectory->copyFile($this->_mediaConfig->getMediaPath($file), $this->_mediaConfig->getMediaPath($destinationFile)); } return str_replace('\\', '/', $destinationFile); } catch (\Exception $e) { $file = $this->_mediaConfig->getMediaPath($file); throw new LocalizedException(__('We couldn\'t copy file %1. Please delete media with non-existing images and try again.', $file)); } }
/** * Open backup file (write or read mode) * * @param bool $write * @return $this * @throws \Magento\Framework\Exception\InputException * @throws \Magento\Framework\Backup\Exception\NotEnoughPermissions */ public function open($write = false) { if ($this->getPath() === null) { throw new \Magento\Framework\Exception\InputException(__('The backup file path was not specified.')); } if ($write && $this->varDirectory->isFile($this->_getFilePath())) { $this->varDirectory->delete($this->_getFilePath()); } if (!$write && !$this->varDirectory->isFile($this->_getFilePath())) { throw new \Magento\Framework\Exception\InputException(__('The backup file "%1" does not exist.', $this->getFileName())); } $mode = $write ? 'wb' . self::COMPRESS_RATE : 'rb'; try { /** @var \Magento\Framework\Filesystem\Directory\WriteInterface $varDirectory */ $varDirectory = $this->_filesystem->getDirectoryWrite(DirectoryList::VAR_DIR, DriverPool::ZLIB); $this->_stream = $varDirectory->openFile($this->_getFilePath(), $mode); } catch (\Magento\Framework\Exception\FileSystemException $e) { throw new \Magento\Framework\Backup\Exception\NotEnoughPermissions(__('Sorry, but we cannot read from or write to backup file "%1".', $this->getFileName())); } return $this; }
/** * Insert logo to pdf page * * @param \Zend_Pdf_Page &$page * @param null $store * @return void * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ protected function insertLogo(&$page, $store = null) { $this->y = $this->y ? $this->y : 815; $image = $this->_scopeConfig->getValue('sales/identity/logo', \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $store); if ($image) { $imagePath = '/sales/store/logo/' . $image; if ($this->_mediaDirectory->isFile($imagePath)) { $image = \Zend_Pdf_Image::imageWithPath($this->_mediaDirectory->getAbsolutePath($imagePath)); $top = 830; //top border of the page $widthLimit = 270; //half of the page width $heightLimit = 270; //assuming the image is not a "skyscraper" $width = $image->getPixelWidth(); $height = $image->getPixelHeight(); //preserving aspect ratio (proportions) $ratio = $width / $height; if ($ratio > 1 && $width > $widthLimit) { $width = $widthLimit; $height = $width / $ratio; } elseif ($ratio < 1 && $height > $heightLimit) { $height = $heightLimit; $width = $height * $ratio; } elseif ($ratio == 1 && $height > $heightLimit) { $height = $heightLimit; $width = $widthLimit; } $y1 = $top - $height; $y2 = $top; $x1 = 25; $x2 = $x1 + $width; //coordinates after transformation are rounded by Zend $page->drawImage($image, $x1, $y1, $x2, $y2); $this->y = $y1 - 10; } } }
/** * @param string $relativePath * @return bool */ protected function _isCanProcessed($relativePath) { $filePath = $this->_rootDir->getAbsolutePath($relativePath); return (!$this->_rootDir->isFile($relativePath) || !$this->_rootDir->isReadable($relativePath)) && !$this->_processDatabaseFile($filePath); }
/** * Checks imported file exists. * * @param string $filename * @return bool */ public function importFileExists($filename) { return $this->varDirectory->isFile($this->getFilePath($filename)); }
/** * @param string $relativePath * @return bool */ protected function _isCanProcessed($relativePath) { $filePath = $this->_rootDir->getAbsolutePath($relativePath); return strpos($this->_rootDir->getDriver()->getRealPath($filePath), $relativePath) !== false && $this->_rootDir->isFile($relativePath) && $this->_rootDir->isReadable($relativePath) || $this->_processDatabaseFile($filePath, $relativePath); }
/** * Return resized product image information * * @return array */ public function getResizedImageInfo() { $fileInfo = null; if ($this->_newFile === true) { $asset = $this->_assetRepo->createAsset("Magento_Catalog::images/product/placeholder/{$this->getDestinationSubdir()}.jpg"); $img = $asset->getSourceFile(); $fileInfo = getimagesize($img); } else { if ($this->_mediaDirectory->isFile($this->_mediaDirectory->getAbsolutePath($this->_newFile))) { $fileInfo = getimagesize($this->_mediaDirectory->getAbsolutePath($this->_newFile)); } } return $fileInfo; }
/** * First check this file on FS * If it doesn't exist - try to download it from DB * * @param string $filename * @return bool */ protected function _fileExists($filename) { if ($this->_mediaDirectory->isFile($filename)) { return true; } else { return $this->_coreFileStorageDatabase->saveFileToFilesystem($this->_mediaDirectory->getAbsolutePath($filename)); } }
/** * @param string $dbCode * @return bool */ public function isDbAvailable($dbCode) { $dbPath = $this->getDbPath($dbCode); return $dbPath && $this->directory->isFile($dbPath) && $this->directory->isReadable($dbPath); }
/** * @param string $relativePath * @return bool */ protected function _isCanProcessed($relativePath) { $filePath = $this->_rootDir->getAbsolutePath($relativePath); $pathWithFixedSeparator = str_replace('\\', '/', $this->_rootDir->getDriver()->getRealPath($filePath)); return strpos($pathWithFixedSeparator, $relativePath) !== false && $this->_rootDir->isFile($relativePath) && $this->_rootDir->isReadable($relativePath) || $this->_processDatabaseFile($filePath, $relativePath); }