diff --git a/app/code/community/Cloudinary/Cloudinary/Helper/Cms/Wysiwyg/Images.php b/app/code/community/Cloudinary/Cloudinary/Helper/Cms/Wysiwyg/Images.php index 1d038da..2a256ce 100644 --- a/app/code/community/Cloudinary/Cloudinary/Helper/Cms/Wysiwyg/Images.php +++ b/app/code/community/Cloudinary/Cloudinary/Helper/Cms/Wysiwyg/Images.php @@ -22,4 +22,24 @@ public function getStorageRoot() } return $this->_storageRoot; } + + /** + * Return URL based on current selected directory or root directory for startup + * + * @return string + */ + public function getCurrentUrl() + { + if (!Mage::getModel('cloudinary_cloudinary/configuration')->isEnabled()) { + return parent::getCurrentUrl(); + } + if (!$this->_currentUrl) { + $mediaPath = Mage::getConfig()->getOptions()->getMediaDir(); + $path = str_replace($mediaPath, '', $this->getCurrentPath()); + $path = trim($path, DS); + $this->_currentUrl = Mage::app()->getStore($this->_storeId)->getBaseUrl('media') . + $this->convertPathToUrl($path) . '/'; + } + return $this->_currentUrl; + } } diff --git a/app/code/community/Cloudinary/Cloudinary/Model/Cms/Adminhtml/Template/Filter.php b/app/code/community/Cloudinary/Cloudinary/Model/Cms/Adminhtml/Template/Filter.php index 58b8401..ad33c83 100644 --- a/app/code/community/Cloudinary/Cloudinary/Model/Cms/Adminhtml/Template/Filter.php +++ b/app/code/community/Cloudinary/Cloudinary/Model/Cms/Adminhtml/Template/Filter.php @@ -42,7 +42,9 @@ public function mediaDirective($construction) if (ini_get('allow_url_fopen')) { $image = $this->imageFactory->build( $this->imagePath($construction), - function() use($construction) { return parent::mediaDirective($construction); } + function () use ($construction) { + return parent::mediaDirective($construction); + } ); return $this->urlGenerator->generateFor($image); diff --git a/app/code/community/Cloudinary/Cloudinary/Model/Cms/Wysiwyg/Images/Storage.php b/app/code/community/Cloudinary/Cloudinary/Model/Cms/Wysiwyg/Images/Storage.php index d4c3f24..9ef65f9 100644 --- a/app/code/community/Cloudinary/Cloudinary/Model/Cms/Wysiwyg/Images/Storage.php +++ b/app/code/community/Cloudinary/Cloudinary/Model/Cms/Wysiwyg/Images/Storage.php @@ -40,17 +40,101 @@ public function __construct() ); } + /** + * Return files + * + * @param string $path Parent directory path + * @param string $type Type of storage, e.g. image, media etc. + * @return Varien_Data_Collection_Filesystem + */ + public function getFilesCollection($path, $type = null) + { + if (!$this->_configuration->isEnabled()) { + return parent::getFilesCollection($path, $type); + } + + if (Mage::helper('core/file_storage_database')->checkDbUsage()) { + $files = Mage::getModel('core/file_storage_database')->getDirectoryFiles($path); + + $fileStorageModel = Mage::getModel('core/file_storage_file'); + foreach ($files as $file) { + $fileStorageModel->saveFile($file); + } + } + + $collection = $this->getCollection($path) + ->setCollectDirs(false) + ->setCollectFiles(true) + ->setCollectRecursively(false) + ->setOrder('mtime', Varien_Data_Collection::SORT_ORDER_ASC); + + // Add files extension filter + if ($allowed = $this->getAllowedExtensions($type)) { + $collection->setFilesFilter('/\.(' . implode('|', $allowed). ')$/i'); + } + + $helper = $this->getHelper(); + + // prepare items + foreach ($collection as $item) { + $item->setId($helper->idEncode($item->getBasename())); + $item->setName($item->getBasename()); + $item->setShortName($helper->getShortFilename($item->getBasename())); + $item->setUrl($helper->getCurrentUrl() . $item->getBasename()); + + if ($this->isImage($item->getBasename())) { + $thumbUrl = $this->getThumbnailUrl( + Mage_Core_Model_File_Uploader::getCorrectFileName($item->getFilename()), + true, + $item->getFilename() + ); + // generate thumbnail "on the fly" if it does not exists + if (! $thumbUrl) { + $thumbUrl = Mage::getSingleton('adminhtml/url')->getUrl('*/*/thumbnail', array('file' => $item->getId())); + } + + $size = @getimagesize($item->getFilename()); + + if (is_array($size)) { + $item->setWidth($size[0]); + $item->setHeight($size[1]); + } + } else { + $thumbUrl = Mage::getDesign()->getSkinBaseUrl() . self::THUMB_PLACEHOLDER_PATH_SUFFIX; + } + + $item->setThumbUrl($thumbUrl); + } + + return $collection; + } + /** * @param string $filePath * @param bool $checkFile * @return string */ - public function getThumbnailUrl($filePath, $checkFile = false) + public function getThumbnailUrl($filePath, $checkFile = false, $origFilePath = null) { + $_origUrl = $origUrl = parent::getThumbnailUrl($filePath, $checkFile); + + if (!$this->_configuration->isEnabled()) { + return $_origUrl; + } + + if (!$_origUrl && !is_null($origFilePath)) { + $filePath = $origFilePath; + $origUrl = parent::getThumbnailUrl($filePath, $checkFile); + } + + if (!$origUrl) { + return $_origUrl; + } + $image = $this->_imageFactory->build( $filePath, - function() use($filePath, $checkFile) { - return parent::getThumbnailUrl($filePath, $checkFile); + function () use ($_origUrl) { + return (string) $_origUrl; } ); @@ -71,7 +155,7 @@ function() use($filePath, $checkFile) { public function uploadFile($targetPath, $type = null) { if (!$this->_configuration->isEnabled()) { - return parent::uploadFile($targetPath, $type); + return parent::uploadFile($targetPath, $type); } $uploader = new Cloudinary_Cloudinary_Model_Cms_Uploader('image'); diff --git a/app/code/community/Cloudinary/Cloudinary/Model/Configuration.php b/app/code/community/Cloudinary/Cloudinary/Model/Configuration.php index c8c90f1..9d2c360 100644 --- a/app/code/community/Cloudinary/Cloudinary/Model/Configuration.php +++ b/app/code/community/Cloudinary/Cloudinary/Model/Configuration.php @@ -201,7 +201,7 @@ private function setStoreConfig($configPath, $value) /** * @return CloudinaryEnvironmentVariable */ - private function getEnvironmentVariable() + public function getEnvironmentVariable() { if (is_null($this->environmentVariable)) { if (Mage::registry('cloudinaryEnvironmentVariable')) { diff --git a/app/code/community/Cloudinary/Cloudinary/Model/Observer/Config.php b/app/code/community/Cloudinary/Cloudinary/Model/Observer/Config.php index b9bd31b..ea5de65 100644 --- a/app/code/community/Cloudinary/Cloudinary/Model/Observer/Config.php +++ b/app/code/community/Cloudinary/Cloudinary/Model/Observer/Config.php @@ -16,26 +16,6 @@ class Cloudinary_Cloudinary_Model_Observer_Config extends Mage_Core_Model_Abstra const AUTO_UPLOAD_SETUP_FAIL_MESSAGE = 'error. Unable to setup auto upload mapping.'; const AUTO_UPLOAD_SETUP_SUCCESS_MESSAGE = 'auto upload mapping configured: %s'; - /** - * @param Varien_Event_Observer $observer - */ - public function configSave(Varien_Event_Observer $observer) - { - $config = $observer->getEvent()->getObject(); - if ($config->getSection() != self::CLOUDINARY_CONFIG_SECTION) { - return; - } - - $data = Mage::helper('cloudinary_cloudinary/config')->flatten('cloudinary', $config->getGroups()); - if ($data[self::ENABLED_FIELD] == '1') { - Mage::register('cloudinaryIsEnabled', true); - $this->validateEnvironmentVariable($data); - $this->logConfigChange($data); - } else { - Mage::register('cloudinaryIsEnabled', false); - } - } - /** * @param Varien_Event_Observer $observer */ @@ -44,9 +24,10 @@ public function cloudinaryConfigChanged(Varien_Event_Observer $observer) //Clear config cache before mapping Mage::app()->getCacheInstance()->cleanType("config"); Mage::dispatchEvent('adminhtml_cache_refresh_type', array('type' => "config")); + Mage::getConfig()->reinit(); - if (!Mage::registry('cloudinaryIsEnabled')) { - return; + if (!Mage::getModel('cloudinary_cloudinary/configuration')->isEnabled()) { + return $this; } if (!$this->autoUploadRequestProcessor()->handle('media', Mage::getBaseUrl('media'), true)) { @@ -60,54 +41,6 @@ public function cloudinaryConfigChanged(Varien_Event_Observer $observer) } } - /** - * @param array $data - */ - private function validateEnvironmentVariable(array $data) - { - $value = (string) $data[self::ENVIRONMENT_FIELD]; - if (preg_match('/^\*+$/', $value)) { - $value = Mage::helper('core')->decrypt($value); - } - $credentialValidator = new CredentialValidator(); - $environmentVariable = CloudinaryEnvironmentVariable::fromString($value); - - if (!$credentialValidator->validate($environmentVariable->getCredentials())) { - Mage::register('cloudinaryEnvironmentVariableIsValid', false); - throw new Mage_Core_Exception(self::ERROR_WRONG_CREDENTIALS); - } - Mage::register('cloudinaryEnvironmentVariableIsValid', true); - } - - /** - * @param array $data - */ - private function logConfigChange(array $data) - { - $data[self::ENVIRONMENT_FIELD] = md5($data[self::ENVIRONMENT_FIELD]); - Mage::getModel('cloudinary_cloudinary/logger')->notice( - sprintf(self::CONFIG_CHANGE_MESSAGE, $this->formatConfigData($data)) - ); - } - - /** - * @param array $data - * @return string - */ - private function formatConfigData(array $data) - { - return implode( - ', ', - array_map( - function ($key, $value) { - return sprintf('(%s: %s)', $key, $value); - }, - array_keys($data), - array_values($data) - ) - ); - } - /** * @return RequestProcessor */ diff --git a/app/code/community/Cloudinary/Cloudinary/Model/System/Config/Credentials.php b/app/code/community/Cloudinary/Cloudinary/Model/System/Config/Credentials.php index e35210f..ddf1d7e 100644 --- a/app/code/community/Cloudinary/Cloudinary/Model/System/Config/Credentials.php +++ b/app/code/community/Cloudinary/Cloudinary/Model/System/Config/Credentials.php @@ -1,41 +1,144 @@ configuration = Mage::getModel('cloudinary_cloudinary/configuration'); + $this->configurationBuilder = new ConfigurationBuilder($this->configuration); + $this->api = new Api(); + return parent::_init($resourceModel); + } + /** * Encrypt value before saving * */ protected function _beforeSave() { - $value = (string)$this->getValue(); - if (preg_match('/^\*+$/', $value)) { - $value = $this->getOldValue(); + $rawValue = (string)$this->getValue(); + + $isSaveAllowed = true; + if (preg_match('/^\*+$/', $rawValue)) { + $isSaveAllowed = false; + $rawValue = $this->getOldValue(); } - $isValid = false; - try { - $credentialValidator = new CredentialValidator(); - $environmentVariable = CloudinaryEnvironmentVariable::fromString($value); - $isValid = (bool) $credentialValidator->validate($environmentVariable->getCredentials()); - } catch (\Exception $e) { - //Ignore errors + parent::_beforeSave(); + + //Clear config cache before mapping + Mage::app()->getCacheInstance()->cleanType("config"); + Mage::dispatchEvent('adminhtml_cache_refresh_type', array('type' => "config")); + Mage::getConfig()->reinit(); + + if ($rawValue || $this->configuration->isEnabled()) { + if (!$rawValue) { + throw new Mage_Core_Exception(__(self::CREDENTIALS_CHECK_MISSING)); + } + if ($isSaveAllowed) { + $this->validate($this->getCredentialsFromEnvironmentVariable($rawValue)); + } else { + $this->validate($this->getCredentialsFromConfig()); + } } - if (!$isValid) { + Mage::register('cloudinaryEnvironmentVariable', $this->getValue()); + + return $this; + } + + + /** + * @param array $credentials + * @throws Mage_Core_Exception + */ + private function validate(array $credentials) + { + $this->_authorise($credentials); + $pingValidation = $this->api->ping(); + if (!(isset($pingValidation["status"]) && $pingValidation["status"] === "ok")) { $this->setValue(null); - Mage::getSingleton('core/session')->addError(Cloudinary_Cloudinary_Model_Observer_Config::ERROR_WRONG_CREDENTIALS); + throw new Mage_Core_Exception(__(self::CREDENTIALS_CHECK_UNSURE)); } + } - parent::_beforeSave(); + /** + * @param string $environmentVariable + * @throws Mage_Core_Exception + * @return array + */ + private function getCredentialsFromEnvironmentVariable($environmentVariable) + { + try { + Cloudinary::config_from_url(str_replace('CLOUDINARY_URL=', '', $environmentVariable)); + $credentials = [ + "cloud_name" => Cloudinary::config_get('cloud_name'), + "api_key" => Cloudinary::config_get('api_key'), + "api_secret" => Cloudinary::config_get('api_secret') + ]; + if (Cloudinary::config_get('private_cdn')) { + $credentials["private_cdn"] = Cloudinary::config_get('private_cdn'); + } + return $credentials; + } catch (\Exception $e) { + throw new Mage_Core_Exception(__(self::CREDENTIALS_CHECK_FAILED)); + } + } - Mage::register('cloudinaryEnvironmentVariable', $this->getValue()); + /** + * @throws ValidatorException + * @return array + */ + private function getCredentialsFromConfig() + { + try { + return $this->getCredentialsFromEnvironmentVariable($this->configuration->getEnvironmentVariable()->__toString()); + } catch (InvalidCredentials $e) { + throw new Mage_Core_Exception(__(self::CREDENTIALS_CHECK_FAILED)); + } + } - return $this; + /** + * @param array $credentials + */ + private function _authorise(array $credentials) + { + Cloudinary::config($credentials); + Cloudinary::$USER_PLATFORM = $this->configuration->getUserPlatform(); } } diff --git a/app/code/community/Cloudinary/Cloudinary/Model/System/Config/Free.php b/app/code/community/Cloudinary/Cloudinary/Model/System/Config/Free.php index 1622d5f..937e9d0 100644 --- a/app/code/community/Cloudinary/Cloudinary/Model/System/Config/Free.php +++ b/app/code/community/Cloudinary/Cloudinary/Model/System/Config/Free.php @@ -30,6 +30,11 @@ protected function _init($resourceModel) */ protected function _beforeSave() { + //Clear config cache before mapping + Mage::app()->getCacheInstance()->cleanType("config"); + Mage::dispatchEvent('adminhtml_cache_refresh_type', array('type' => "config")); + Mage::getConfig()->reinit(); + if (!$this->hasAccountConfigured()) { return parent::_beforeSave(); } @@ -98,7 +103,7 @@ public function httpRequest($url) */ public function hasAccountConfigured() { - return (Mage::registry('cloudinaryEnvironmentVariableIsValid') || (is_null(Mage::registry('cloudinaryEnvironmentVariableIsValid')) && (string)$this->configuration->getCloud() !== ''))? true : false; + return (string)$this->configuration->getCloud() !== ''; } /** diff --git a/app/code/community/Cloudinary/Cloudinary/Model/System/Config/Source/Dropdown/Gravity.php b/app/code/community/Cloudinary/Cloudinary/Model/System/Config/Source/Dropdown/Gravity.php index be71112..31669ba 100644 --- a/app/code/community/Cloudinary/Cloudinary/Model/System/Config/Source/Dropdown/Gravity.php +++ b/app/code/community/Cloudinary/Cloudinary/Model/System/Config/Source/Dropdown/Gravity.php @@ -9,14 +9,6 @@ public function toOptionArray() 'value' => '', 'label' => 'Magento\'s Default', ), - array( - 'value' => 'face', - 'label' => 'Face', - ), - array( - 'value' => 'faces', - 'label' => 'Faces', - ), array( 'value' => 'north_west', 'label' => 'North West', @@ -53,14 +45,6 @@ public function toOptionArray() 'value' => 'south_east', 'label' => 'South East', ), - array( - 'value' => 'face:center', - 'label' => 'Face (Center)', - ), - array( - 'value' => 'faces:center', - 'label' => 'Faces (Center)', - ), ); } } diff --git a/app/code/community/Cloudinary/Cloudinary/etc/config.xml b/app/code/community/Cloudinary/Cloudinary/etc/config.xml index a7e7c90..cad3e20 100644 --- a/app/code/community/Cloudinary/Cloudinary/etc/config.xml +++ b/app/code/community/Cloudinary/Cloudinary/etc/config.xml @@ -2,7 +2,7 @@ - 2.9.3 + 2.9.5 @@ -134,14 +134,6 @@ - - - - cloudinary_cloudinary/observer_config - configSave - - - diff --git a/app/code/community/Cloudinary/Cloudinary/etc/system.xml b/app/code/community/Cloudinary/Cloudinary/etc/system.xml index 9d12174..2a8f268 100644 --- a/app/code/community/Cloudinary/Cloudinary/etc/system.xml +++ b/app/code/community/Cloudinary/Cloudinary/etc/system.xml @@ -37,26 +37,26 @@ 0 0 - - - Set the credentials of your Cloudinary account. Copy the "Environment variable" string from the dashboard of Cloudinary's Management Console. - password - cloudinary_cloudinary/system_config_credentials - 1 - 1 - 0 - 0 - When Cloudinary is enabled, images will be served from Cloudinary (where available). When disabled, images will be served locally. select - 2 + 1 1 0 0 adminhtml/system_config_source_yesno + + + Format should be: cloudinary://API_Key:API_Secret@Cloud_Name]]> + password + cloudinary_cloudinary/system_config_credentials + 2 + 1 + 0 + 0 + diff --git a/lib/CloudinaryExtension/CloudinaryImageProvider.php b/lib/CloudinaryExtension/CloudinaryImageProvider.php index a9372dd..f932f71 100644 --- a/lib/CloudinaryExtension/CloudinaryImageProvider.php +++ b/lib/CloudinaryExtension/CloudinaryImageProvider.php @@ -22,21 +22,15 @@ class CloudinaryImageProvider implements ImageProvider * @var ConfigurationBuilder */ private $configurationBuilder; - /** - * @var CredentialValidator - */ - private $credentialValidator; public function __construct( ConfigurationInterface $configuration, ConfigurationBuilder $configurationBuilder, - UploadResponseValidator $uploadResponseValidator, - CredentialValidator $credentialValidator + UploadResponseValidator $uploadResponseValidator ) { $this->configuration = $configuration; $this->uploadResponseValidator = $uploadResponseValidator; $this->configurationBuilder = $configurationBuilder; - $this->credentialValidator = $credentialValidator; $this->authorise(); } @@ -45,8 +39,7 @@ public static function fromConfiguration(ConfigurationInterface $configuration) return new CloudinaryImageProvider( $configuration, new ConfigurationBuilder($configuration), - new UploadResponseValidator(), - new CredentialValidator() + new UploadResponseValidator() ); } @@ -99,9 +92,21 @@ public function delete(Image $image) Uploader::destroy($image->getIdWithoutExtension()); } + /** + * @return bool + */ public function validateCredentials() { - return $this->credentialValidator->validate($this->configuration->getCredentials()); + try { + $pingValidation = $this->api->ping(); + if (!(isset($pingValidation["status"]) && $pingValidation["status"] === "ok")) { + return false; + //throw new ValidatorException(__(self::CREDENTIALS_CHECK_UNSURE)); + } + } catch (\Exception $e) { + return false; + } + return true; } private function authorise() diff --git a/var/connect/Cloudinary_Cloudinary.xml b/var/connect/Cloudinary_Cloudinary.xml index e00eb65..fa2e15a 100644 --- a/var/connect/Cloudinary_Cloudinary.xml +++ b/var/connect/Cloudinary_Cloudinary.xml @@ -1,5 +1,5 @@ <_> - pHIzpC9UKidsX254 + DkXucloppn3lEGP4 Cloudinary_Cloudinary community @@ -9,7 +9,7 @@ Cloudinary supercharges your images! Upload images to the cloud, deliver optimized via a fast CDN, perform smart resizing and apply effects. MIT License (MITL) - 2.9.3 + 2.9.5 stable - Match MEQP1 coding standards @@ -41,7 +41,7 @@ - + diff --git a/var/package/package.xml b/var/package/package.xml index 2474eb1..60aa9bc 100644 --- a/var/package/package.xml +++ b/var/package/package.xml @@ -1,7 +1,7 @@ Cloudinary_Cloudinary - 2.9.3 + 2.9.5 stable MIT License (MITL) community @@ -11,9 +11,9 @@ - Match MEQP1 coding standards Cloudinarycloudinaryaccounts+magento@cloudinary.com - 2018-12-27 - - + 2018-12-31 + + 5.4.07.2.2