Skip to content

Commit

Permalink
CLOUDINARY-103: Fixed curl on validateImageUrl() & made if less stric…
Browse files Browse the repository at this point in the history
…t on configuration save (show message without exception)
  • Loading branch information
pini-girit committed Mar 17, 2019
1 parent 62796fe commit ca45f81
Showing 1 changed file with 34 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

class Cloudinary_Cloudinary_Model_System_Config_Free extends Mage_Core_Model_Config_Data
{
const ERROR_FORMAT = 'Incorrect custom transform - %s';
const ERROR_FORMAT = 'Incorrect Cloudinary Transformation - %s';
const ERROR_DEFAULT = 'please update';

/**
Expand Down Expand Up @@ -43,25 +43,41 @@ protected function _beforeSave()
->getDefaultTransformation()
->withFreeform(Freeform::fromString($this->getValue()));

$this->validateImageUrl($this->sampleImageUrl($transform));
$this->validateImageUrl($this->sampleImageUrl($transform), false);

return $this;
}

/**
* @param string $url
* @param bool $strict Throw exception on errors
* @return bool
*/
public function validateImageUrl($url)
public function validateImageUrl($url, $strict = true)
{
try {
$response = $this->httpRequest($url);
} catch (Exception $e) {
throw new Mage_Core_Exception(sprintf(self::ERROR_FORMAT, self::ERROR_DEFAULT));
$this->setValue(null);
if ($strict) {
throw new Mage_Core_Exception(sprintf(self::ERROR_FORMAT, self::ERROR_DEFAULT));
} else {
Mage::getSingleton('adminhtml/session')->addError(sprintf(self::ERROR_FORMAT, self::ERROR_DEFAULT));
}
return false;
}

if ($response->isError()) {
throw new Mage_Core_Exception($this->formatError($response));
if (is_object($response) && ($response->error || !in_array($response->code, [200,301,302]))) {
$this->setValue(null);
if ($strict) {
throw new Mage_Core_Exception($this->formatError($response));
} else {
Mage::getSingleton('adminhtml/session')->addError($this->formatError($response));
}
return false;
}

return true;
}

/**
Expand All @@ -79,11 +95,11 @@ public function defaultTransform($freeTransform)
* @param Zend_Http_Response $response
* @return string
*/
public function formatError(Zend_Http_Response $response)
public function formatError($response)
{
return sprintf(
self::ERROR_FORMAT,
$response->getStatus() == 400 ? $response->getHeader('x-cld-error') : self::ERROR_DEFAULT
(is_object($response) && isset($response->headers['x-cld-error']) && $response->headers['x-cld-error']) ? $response->headers['x-cld-error'] : self::ERROR_DEFAULT
);
}

Expand All @@ -93,9 +109,16 @@ public function formatError(Zend_Http_Response $response)
*/
public function httpRequest($url)
{
$client = new Varien_Http_Client($url);
$client->setMethod(Varien_Http_Client::GET);
return $client->request();
$curl = new Varien_Http_Adapter_Curl();
$curl->write(Zend_Http_Client::GET, $url);
$response = $curl->read();
$response = (object)[
"code" => Zend_Http_Response::extractCode($response),
"body" => Zend_Http_Response::extractBody($response),
"headers" => (array) Zend_Http_Response::extractHeaders($response),
"error" => $curl->getError()
];
return $response;
}

/**
Expand Down

0 comments on commit ca45f81

Please sign in to comment.