123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Cms\Helper\Wysiwyg;
- use Magento\Framework\App\Filesystem\DirectoryList;
- /**
- * Wysiwyg Images Helper.
- */
- class Images extends \Magento\Framework\App\Helper\AbstractHelper
- {
- /**
- * Image directory subpath relative to media directory
- *
- * @var string
- */
- private $imageDirectorySubpath;
- /**
- * Current directory path
- * @var string
- */
- protected $_currentPath;
- /**
- * Current directory URL
- * @var string
- */
- protected $_currentUrl;
- /**
- * Currently selected store ID if applicable
- *
- * @var int
- */
- protected $_storeId;
- /**
- * @var \Magento\Framework\Filesystem\Directory\Write
- */
- protected $_directory;
- /**
- * Adminhtml data
- *
- * @var \Magento\Backend\Helper\Data
- */
- protected $_backendData;
- /**
- * Store manager
- *
- * @var \Magento\Store\Model\StoreManagerInterface
- */
- protected $_storeManager;
- /**
- * String escaper
- *
- * @var \Magento\Framework\Escaper
- */
- protected $escaper;
- /**
- * Construct
- *
- * @param \Magento\Framework\App\Helper\Context $context
- * @param \Magento\Backend\Helper\Data $backendData
- * @param \Magento\Framework\Filesystem $filesystem
- * @param \Magento\Store\Model\StoreManagerInterface $storeManager
- * @param \Magento\Framework\Escaper $escaper
- */
- public function __construct(
- \Magento\Framework\App\Helper\Context $context,
- \Magento\Backend\Helper\Data $backendData,
- \Magento\Framework\Filesystem $filesystem,
- \Magento\Store\Model\StoreManagerInterface $storeManager,
- \Magento\Framework\Escaper $escaper
- ) {
- parent::__construct($context);
- $this->_backendData = $backendData;
- $this->_storeManager = $storeManager;
- $this->escaper = $escaper;
- $this->_directory = $filesystem->getDirectoryWrite(DirectoryList::MEDIA);
- $this->_directory->create($this->getStorageRoot());
- }
- /**
- * Set a specified store ID value
- *
- * @param int $store
- * @return $this
- */
- public function setStoreId($store)
- {
- $this->_storeId = $store;
- return $this;
- }
- /**
- * Images Storage root directory
- *
- * @return string
- */
- public function getStorageRoot()
- {
- return $this->_directory->getAbsolutePath($this->getStorageRootSubpath());
- }
- /**
- * Get image storage root subpath. User is unable to traverse outside of this subpath in media gallery
- *
- * @return string
- */
- public function getStorageRootSubpath()
- {
- return '';
- }
- /**
- * Images Storage base URL
- *
- * @return string
- */
- public function getBaseUrl()
- {
- return $this->_storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA);
- }
- /**
- * Ext Tree node key name
- *
- * @return string
- */
- public function getTreeNodeName()
- {
- return 'node';
- }
- /**
- * Encode path to HTML element id
- *
- * @param string $path Path to file/directory
- * @return string
- */
- public function convertPathToId($path)
- {
- $path = str_replace($this->getStorageRoot(), '', $path);
- return $this->idEncode($path);
- }
- /**
- * Decode HTML element id.
- *
- * @param string $id
- * @return string
- * @throws \InvalidArgumentException When path contains restricted symbols.
- */
- public function convertIdToPath($id)
- {
- if ($id === \Magento\Theme\Helper\Storage::NODE_ROOT) {
- return $this->getStorageRoot();
- } else {
- $path = $this->getStorageRoot() . $this->idDecode($id);
- if (preg_match('/\.\.(\\\|\/)/', $path)) {
- throw new \InvalidArgumentException('Path is invalid');
- }
- return $path;
- }
- }
- /**
- * Check whether using static URLs is allowed
- *
- * @return bool
- */
- public function isUsingStaticUrlsAllowed()
- {
- $checkResult = (object) [];
- $checkResult->isAllowed = false;
- $this->_eventManager->dispatch(
- 'cms_wysiwyg_images_static_urls_allowed',
- ['result' => $checkResult, 'store_id' => $this->_storeId]
- );
- return $checkResult->isAllowed;
- }
- /**
- * Prepare Image insertion declaration for Wysiwyg or textarea(as_is mode)
- *
- * @param string $filename Filename transferred via Ajax
- * @param bool $renderAsTag Leave image HTML as is or transform it to controller directive
- * @return string
- */
- public function getImageHtmlDeclaration($filename, $renderAsTag = false)
- {
- $fileUrl = $this->getCurrentUrl() . $filename;
- $mediaUrl = $this->_storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA);
- $mediaPath = str_replace($mediaUrl, '', $fileUrl);
- $directive = sprintf('{{media url="%s"}}', $mediaPath);
- if ($renderAsTag) {
- $src = $this->isUsingStaticUrlsAllowed() ? $fileUrl : $this->escaper->escapeHtml($directive);
- $html = sprintf('<img src="%s" alt="" />', $src);
- } else {
- if ($this->isUsingStaticUrlsAllowed()) {
- $html = $fileUrl;
- } else {
- $directive = $this->urlEncoder->encode($directive);
- $html = $this->_backendData->getUrl(
- 'cms/wysiwyg/directive',
- [
- '___directive' => $directive,
- '_escape_params' => false,
- ]
- );
- }
- }
- return $html;
- }
- /**
- * Return path of the current selected directory or root directory for startup
- * Try to create target directory if it doesn't exist
- *
- * @return string
- * @throws \Magento\Framework\Exception\LocalizedException
- */
- public function getCurrentPath()
- {
- if (!$this->_currentPath) {
- $currentPath = $this->getStorageRoot();
- $path = $this->_getRequest()->getParam($this->getTreeNodeName());
- if ($path) {
- $path = $this->convertIdToPath($path);
- if ($this->_directory->isDirectory($this->_directory->getRelativePath($path))) {
- $currentPath = $path;
- }
- }
- try {
- $currentDir = $this->_directory->getRelativePath($currentPath);
- if (!$this->_directory->isExist($currentDir)) {
- $this->_directory->create($currentDir);
- }
- } catch (\Magento\Framework\Exception\FileSystemException $e) {
- $message = __('The directory %1 is not writable by server.', $currentPath);
- throw new \Magento\Framework\Exception\LocalizedException($message);
- }
- $this->_currentPath = $currentPath;
- }
- return $this->_currentPath;
- }
- /**
- * Return URL based on current selected directory or root directory for startup
- *
- * @return string
- */
- public function getCurrentUrl()
- {
- if (!$this->_currentUrl) {
- $path = $this->getCurrentPath();
- $mediaUrl = $this->_storeManager->getStore(
- $this->_storeId
- )->getBaseUrl(
- \Magento\Framework\UrlInterface::URL_TYPE_MEDIA
- );
- $this->_currentUrl = rtrim($mediaUrl . $this->_directory->getRelativePath($path), '/') . '/';
- }
- return $this->_currentUrl;
- }
- /**
- * Encode string to valid HTML id element, based on base64 encoding
- *
- * @param string $string
- * @return string
- */
- public function idEncode($string)
- {
- return strtr(base64_encode($string), '+/=', ':_-');
- }
- /**
- * Revert operation to idEncode
- *
- * @param string $string
- * @return string
- */
- public function idDecode($string)
- {
- $string = strtr($string, ':_-', '+/=');
- return base64_decode($string);
- }
- /**
- * Reduce filename by replacing some characters with dots
- *
- * @param string $filename
- * @param int $maxLength Maximum filename
- * @return string Truncated filename
- */
- public function getShortFilename($filename, $maxLength = 20)
- {
- if (strlen($filename) <= $maxLength) {
- return $filename;
- }
- return substr($filename, 0, $maxLength) . '...';
- }
- /**
- * Set user-traversable image directory subpath relative to media directory and relative to nested storage root
- *
- * @var string $subpath
- * @return void
- */
- public function setImageDirectorySubpath($subpath)
- {
- $this->imageDirectorySubpath = $subpath;
- }
- }
|