Skip to content
This repository was archived by the owner on Dec 10, 2025. It is now read-only.

Commit 1005f12

Browse files
author
pini-girit
committed
CLOUDINARY-68
1 parent 1017dca commit 1005f12

File tree

7 files changed

+154
-116
lines changed

7 files changed

+154
-116
lines changed

app/code/community/Cloudinary/Cloudinary/Model/Configuration.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ private function setStoreConfig($configPath, $value)
201201
/**
202202
* @return CloudinaryEnvironmentVariable
203203
*/
204-
private function getEnvironmentVariable()
204+
public function getEnvironmentVariable()
205205
{
206206
if (is_null($this->environmentVariable)) {
207207
if (Mage::registry('cloudinaryEnvironmentVariable')) {

app/code/community/Cloudinary/Cloudinary/Model/Observer/Config.php

Lines changed: 3 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -16,26 +16,6 @@ class Cloudinary_Cloudinary_Model_Observer_Config extends Mage_Core_Model_Abstra
1616
const AUTO_UPLOAD_SETUP_FAIL_MESSAGE = 'error. Unable to setup auto upload mapping.';
1717
const AUTO_UPLOAD_SETUP_SUCCESS_MESSAGE = 'auto upload mapping configured: %s';
1818

19-
/**
20-
* @param Varien_Event_Observer $observer
21-
*/
22-
public function configSave(Varien_Event_Observer $observer)
23-
{
24-
$config = $observer->getEvent()->getObject();
25-
if ($config->getSection() != self::CLOUDINARY_CONFIG_SECTION) {
26-
return;
27-
}
28-
29-
$data = Mage::helper('cloudinary_cloudinary/config')->flatten('cloudinary', $config->getGroups());
30-
if ($data[self::ENABLED_FIELD] == '1') {
31-
Mage::register('cloudinaryIsEnabled', true);
32-
$this->validateEnvironmentVariable($data);
33-
$this->logConfigChange($data);
34-
} else {
35-
Mage::register('cloudinaryIsEnabled', false);
36-
}
37-
}
38-
3919
/**
4020
* @param Varien_Event_Observer $observer
4121
*/
@@ -44,9 +24,10 @@ public function cloudinaryConfigChanged(Varien_Event_Observer $observer)
4424
//Clear config cache before mapping
4525
Mage::app()->getCacheInstance()->cleanType("config");
4626
Mage::dispatchEvent('adminhtml_cache_refresh_type', array('type' => "config"));
27+
Mage::getConfig()->reinit();
4728

48-
if (!Mage::registry('cloudinaryIsEnabled')) {
49-
return;
29+
if (!Mage::getModel('cloudinary_cloudinary/configuration')->isEnabled()) {
30+
return $this;
5031
}
5132

5233
if (!$this->autoUploadRequestProcessor()->handle('media', Mage::getBaseUrl('media'), true)) {
@@ -60,54 +41,6 @@ public function cloudinaryConfigChanged(Varien_Event_Observer $observer)
6041
}
6142
}
6243

63-
/**
64-
* @param array $data
65-
*/
66-
private function validateEnvironmentVariable(array $data)
67-
{
68-
$value = (string) $data[self::ENVIRONMENT_FIELD];
69-
if (preg_match('/^\*+$/', $value)) {
70-
$value = Mage::helper('core')->decrypt($value);
71-
}
72-
$credentialValidator = new CredentialValidator();
73-
$environmentVariable = CloudinaryEnvironmentVariable::fromString($value);
74-
75-
if (!$credentialValidator->validate($environmentVariable->getCredentials())) {
76-
Mage::register('cloudinaryEnvironmentVariableIsValid', false);
77-
throw new Mage_Core_Exception(self::ERROR_WRONG_CREDENTIALS);
78-
}
79-
Mage::register('cloudinaryEnvironmentVariableIsValid', true);
80-
}
81-
82-
/**
83-
* @param array $data
84-
*/
85-
private function logConfigChange(array $data)
86-
{
87-
$data[self::ENVIRONMENT_FIELD] = md5($data[self::ENVIRONMENT_FIELD]);
88-
Mage::getModel('cloudinary_cloudinary/logger')->notice(
89-
sprintf(self::CONFIG_CHANGE_MESSAGE, $this->formatConfigData($data))
90-
);
91-
}
92-
93-
/**
94-
* @param array $data
95-
* @return string
96-
*/
97-
private function formatConfigData(array $data)
98-
{
99-
return implode(
100-
', ',
101-
array_map(
102-
function ($key, $value) {
103-
return sprintf('(%s: %s)', $key, $value);
104-
},
105-
array_keys($data),
106-
array_values($data)
107-
)
108-
);
109-
}
110-
11144
/**
11245
* @return RequestProcessor
11346
*/
Lines changed: 118 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,144 @@
11
<?php
22
use CloudinaryExtension\CredentialValidator;
33
use CloudinaryExtension\Security\CloudinaryEnvironmentVariable;
4+
use Cloudinary;
5+
use Cloudinary\Api;
6+
use CloudinaryExtension\ConfigurationBuilder;
7+
use CloudinaryExtension\ConfigurationInterface;
8+
use Magento\Framework\Exception\ValidatorException;
9+
use CloudinaryExtension\Credentials as CredentialsValue;
10+
use CloudinaryExtension\Exception\InvalidCredentials;
411

512
/**
613
* Cloudinary_Cloudinary_Model_System_Config_Credentials
714
*/
815
class Cloudinary_Cloudinary_Model_System_Config_Credentials extends Mage_Adminhtml_Model_System_Config_Backend_Encrypted
916
{
17+
const CREDENTIALS_CHECK_MISSING = 'You must provide Cloudinary credentials.';
18+
const CREDENTIALS_CHECK_FAILED = 'Your Cloudinary credentials are not correct.';
19+
const CREDENTIALS_CHECK_UNSURE = 'There was a problem validating your Cloudinary credentials.';
20+
const CLOUDINARY_ENABLED_PATH = 'groups/cloud/fields/cloudinary_enabled/value';
21+
22+
/**
23+
* @var ConfigurationInterface
24+
*/
25+
private $configuration;
26+
27+
/**
28+
* @var ConfigurationBuilder
29+
*/
30+
private $configurationBuilder;
31+
32+
/**
33+
* @var Cloudinary\Api
34+
*/
35+
private $api;
36+
37+
/**
38+
* @param string $resourceModel
39+
*/
40+
protected function _init($resourceModel)
41+
{
42+
$this->configuration = Mage::getModel('cloudinary_cloudinary/configuration');
43+
$this->configurationBuilder = new ConfigurationBuilder($this->configuration);
44+
$this->api = new Api();
45+
return parent::_init($resourceModel);
46+
}
47+
1048
/**
1149
* Encrypt value before saving
1250
*
1351
*/
1452
protected function _beforeSave()
1553
{
16-
$value = (string)$this->getValue();
17-
if (preg_match('/^\*+$/', $value)) {
18-
$value = $this->getOldValue();
54+
$rawValue = (string)$this->getValue();
55+
56+
$isSaveAllowed = true;
57+
if (preg_match('/^\*+$/', $rawValue)) {
58+
$isSaveAllowed = false;
59+
$rawValue = $this->getOldValue();
1960
}
2061

21-
$isValid = false;
22-
try {
23-
$credentialValidator = new CredentialValidator();
24-
$environmentVariable = CloudinaryEnvironmentVariable::fromString($value);
25-
$isValid = (bool) $credentialValidator->validate($environmentVariable->getCredentials());
26-
} catch (\Exception $e) {
27-
//Ignore errors
62+
parent::_beforeSave();
63+
64+
//Clear config cache before mapping
65+
Mage::app()->getCacheInstance()->cleanType("config");
66+
Mage::dispatchEvent('adminhtml_cache_refresh_type', array('type' => "config"));
67+
Mage::getConfig()->reinit();
68+
69+
if ($rawValue || $this->configuration->isEnabled()) {
70+
if (!$rawValue) {
71+
throw new Mage_Core_Exception(__(self::CREDENTIALS_CHECK_MISSING));
72+
}
73+
if ($isSaveAllowed) {
74+
$this->validate($this->getCredentialsFromEnvironmentVariable($rawValue));
75+
} else {
76+
$this->validate($this->getCredentialsFromConfig());
77+
}
2878
}
2979

30-
if (!$isValid) {
80+
Mage::register('cloudinaryEnvironmentVariable', $this->getValue());
81+
82+
return $this;
83+
}
84+
85+
86+
/**
87+
* @param array $credentials
88+
* @throws Mage_Core_Exception
89+
*/
90+
private function validate(array $credentials)
91+
{
92+
$this->_authorise($credentials);
93+
$pingValidation = $this->api->ping();
94+
if (!(isset($pingValidation["status"]) && $pingValidation["status"] === "ok")) {
3195
$this->setValue(null);
32-
Mage::getSingleton('core/session')->addError(Cloudinary_Cloudinary_Model_Observer_Config::ERROR_WRONG_CREDENTIALS);
96+
throw new Mage_Core_Exception(__(self::CREDENTIALS_CHECK_UNSURE));
3397
}
98+
}
3499

35-
parent::_beforeSave();
100+
/**
101+
* @param string $environmentVariable
102+
* @throws Mage_Core_Exception
103+
* @return array
104+
*/
105+
private function getCredentialsFromEnvironmentVariable($environmentVariable)
106+
{
107+
try {
108+
Cloudinary::config_from_url(str_replace('CLOUDINARY_URL=', '', $environmentVariable));
109+
$credentials = [
110+
"cloud_name" => Cloudinary::config_get('cloud_name'),
111+
"api_key" => Cloudinary::config_get('api_key'),
112+
"api_secret" => Cloudinary::config_get('api_secret')
113+
];
114+
if (Cloudinary::config_get('private_cdn')) {
115+
$credentials["private_cdn"] = Cloudinary::config_get('private_cdn');
116+
}
117+
return $credentials;
118+
} catch (\Exception $e) {
119+
throw new Mage_Core_Exception(__(self::CREDENTIALS_CHECK_FAILED));
120+
}
121+
}
36122

37-
Mage::register('cloudinaryEnvironmentVariable', $this->getValue());
123+
/**
124+
* @throws ValidatorException
125+
* @return array
126+
*/
127+
private function getCredentialsFromConfig()
128+
{
129+
try {
130+
return $this->getCredentialsFromEnvironmentVariable($this->configuration->getEnvironmentVariable()->__toString());
131+
} catch (InvalidCredentials $e) {
132+
throw new Mage_Core_Exception(__(self::CREDENTIALS_CHECK_FAILED));
133+
}
134+
}
38135

39-
return $this;
136+
/**
137+
* @param array $credentials
138+
*/
139+
private function _authorise(array $credentials)
140+
{
141+
Cloudinary::config($credentials);
142+
Cloudinary::$USER_PLATFORM = $this->configuration->getUserPlatform();
40143
}
41144
}

app/code/community/Cloudinary/Cloudinary/Model/System/Config/Free.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ protected function _init($resourceModel)
3030
*/
3131
protected function _beforeSave()
3232
{
33+
//Clear config cache before mapping
34+
Mage::app()->getCacheInstance()->cleanType("config");
35+
Mage::dispatchEvent('adminhtml_cache_refresh_type', array('type' => "config"));
36+
Mage::getConfig()->reinit();
37+
3338
if (!$this->hasAccountConfigured()) {
3439
return parent::_beforeSave();
3540
}
@@ -98,7 +103,7 @@ public function httpRequest($url)
98103
*/
99104
public function hasAccountConfigured()
100105
{
101-
return (Mage::registry('cloudinaryEnvironmentVariableIsValid') || (is_null(Mage::registry('cloudinaryEnvironmentVariableIsValid')) && (string)$this->configuration->getCloud() !== ''))? true : false;
106+
return (string)$this->configuration->getCloud() !== '';
102107
}
103108

104109
/**

app/code/community/Cloudinary/Cloudinary/etc/config.xml

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -134,14 +134,6 @@
134134
</global>
135135
<adminhtml>
136136
<events>
137-
<model_config_data_save_before>
138-
<observers>
139-
<config_save>
140-
<class>cloudinary_cloudinary/observer_config</class>
141-
<method>configSave</method>
142-
</config_save>
143-
</observers>
144-
</model_config_data_save_before>
145137
<admin_system_config_changed_section_cloudinary>
146138
<observers>
147139
<cloudinary_config_changed>

app/code/community/Cloudinary/Cloudinary/etc/system.xml

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,26 +37,26 @@
3737
<show_in_website>0</show_in_website>
3838
<show_in_store>0</show_in_store>
3939
<fields>
40-
<cloudinary_environment_variable translate="label comment">
41-
<label>Cloudinary Account Credentials</label>
42-
<comment>Set the credentials of your Cloudinary account. Copy the "Environment variable" string from the dashboard of Cloudinary's Management Console.</comment>
43-
<frontend_type>password</frontend_type>
44-
<backend_model>cloudinary_cloudinary/system_config_credentials</backend_model>
45-
<sort_order>1</sort_order>
46-
<show_in_default>1</show_in_default>
47-
<show_in_website>0</show_in_website>
48-
<show_in_store>0</show_in_store>
49-
</cloudinary_environment_variable>
5040
<cloudinary_enabled>
5141
<label>Enable Cloudinary</label>
5242
<comment>When Cloudinary is enabled, images will be served from Cloudinary (where available). When disabled, images will be served locally.</comment>
5343
<frontend_type>select</frontend_type>
54-
<sort_order>2</sort_order>
44+
<sort_order>1</sort_order>
5545
<show_in_default>1</show_in_default>
5646
<show_in_website>0</show_in_website>
5747
<show_in_store>0</show_in_store>
5848
<source_model>adminhtml/system_config_source_yesno</source_model>
5949
</cloudinary_enabled>
50+
<cloudinary_environment_variable translate="label comment">
51+
<label>Cloudinary Account Credentials</label>
52+
<comment>Set the credentials of your Cloudinary account. Copy the "Environment variable" string from the dashboard of Cloudinary's Management Console.</comment>
53+
<frontend_type>password</frontend_type>
54+
<backend_model>cloudinary_cloudinary/system_config_credentials</backend_model>
55+
<sort_order>2</sort_order>
56+
<show_in_default>1</show_in_default>
57+
<show_in_website>0</show_in_website>
58+
<show_in_store>0</show_in_store>
59+
</cloudinary_environment_variable>
6060
</fields>
6161
</setup>
6262
<configuration translate="label comment">

lib/CloudinaryExtension/CloudinaryImageProvider.php

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,15 @@ class CloudinaryImageProvider implements ImageProvider
2222
* @var ConfigurationBuilder
2323
*/
2424
private $configurationBuilder;
25-
/**
26-
* @var CredentialValidator
27-
*/
28-
private $credentialValidator;
2925

3026
public function __construct(
3127
ConfigurationInterface $configuration,
3228
ConfigurationBuilder $configurationBuilder,
33-
UploadResponseValidator $uploadResponseValidator,
34-
CredentialValidator $credentialValidator
29+
UploadResponseValidator $uploadResponseValidator
3530
) {
3631
$this->configuration = $configuration;
3732
$this->uploadResponseValidator = $uploadResponseValidator;
3833
$this->configurationBuilder = $configurationBuilder;
39-
$this->credentialValidator = $credentialValidator;
4034
$this->authorise();
4135
}
4236

@@ -45,8 +39,7 @@ public static function fromConfiguration(ConfigurationInterface $configuration)
4539
return new CloudinaryImageProvider(
4640
$configuration,
4741
new ConfigurationBuilder($configuration),
48-
new UploadResponseValidator(),
49-
new CredentialValidator()
42+
new UploadResponseValidator()
5043
);
5144
}
5245

@@ -99,9 +92,21 @@ public function delete(Image $image)
9992
Uploader::destroy($image->getIdWithoutExtension());
10093
}
10194

95+
/**
96+
* @return bool
97+
*/
10298
public function validateCredentials()
10399
{
104-
return $this->credentialValidator->validate($this->configuration->getCredentials());
100+
try {
101+
$pingValidation = $this->api->ping();
102+
if (!(isset($pingValidation["status"]) && $pingValidation["status"] === "ok")) {
103+
return false;
104+
//throw new ValidatorException(__(self::CREDENTIALS_CHECK_UNSURE));
105+
}
106+
} catch (\Exception $e) {
107+
return false;
108+
}
109+
return true;
105110
}
106111

107112
private function authorise()

0 commit comments

Comments
 (0)