public function testCl_upload_url() { Cloudinary::config_from_url(CLOUDINARY_URL); $ext = new CloudinaryExtension(); $cloudName = Cloudinary::option_get(Cloudinary::config(), 'cloud_name'); $this->assertEquals('https://api.cloudinary.com/v1_1/' . $cloudName . '/auto/upload', $ext->cl_upload_url()); }
function cl_form_tag($callback_url, $options = array()) { $form_options = Cloudinary::option_get($options, "form", array()); $options["callback_url"] = $callback_url; $params = Cloudinary\Uploader::build_upload_params($options); $params = Cloudinary::sign_request($params, $options); $api_url = Cloudinary::cloudinary_api_url("upload", $options); $form = "<form enctype='multipart/form-data' action='" . $api_url . "' method='POST' " . Cloudinary::html_attrs($form_options) . ">\n"; foreach ($params as $key => $value) { $form .= "<input " . Cloudinary::html_attrs(array("name" => $key, "value" => $value, "type" => "hidden")) . "/>\n"; } $form .= "</form>\n"; return $form; }
public static function call_api($action, $params, $options = array(), $file = NULL) { $return_error = \Cloudinary::option_get($options, "return_error"); $params = \Cloudinary::sign_request($params, $options); $api_url = \Cloudinary::cloudinary_api_url($action, $options); # Serialize params $api_url .= "?" . preg_replace("/%5B\\d+%5D/", "%5B%5D", http_build_query($params)); $ch = curl_init($api_url); $post_params = array(); if ($file) { if (file_exists($file) && function_exists("curl_file_create")) { $post_params['file'] = curl_file_create($file); } else { if (!preg_match('/^@|^https?:|^s3:|^data:[^;]*;base64,([a-zA-Z0-9\\/+\\n=]+)$/', $file)) { $post_params["file"] = "@" . $file; } else { $post_params["file"] = $file; } } } curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_TIMEOUT, 60); curl_setopt($ch, CURLOPT_POSTFIELDS, $post_params); curl_setopt($ch, CURLOPT_CAINFO, realpath(dirname(__FILE__)) . "/cacert.pem"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $response = curl_exec($ch); $curl_error = NULL; if (curl_errno($ch)) { $curl_error = curl_error($ch); } $code = curl_getinfo($ch, CURLINFO_HTTP_CODE); $response_data = $response; curl_close($ch); if ($curl_error != NULL) { throw new \Exception("Error in sending request to server - " . $curl_error); } if ($code != 200 && $code != 400 && $code != 500 && $code != 401 && $code != 404) { throw new \Exception("Server returned unexpected status code - " . $code . " - " . $response_data); } $result = json_decode($response_data, TRUE); if ($result == NULL) { throw new \Exception("Error parsing server response (" . $code . ") - " . $response_data); } if (isset($result["error"])) { if ($return_error) { $result["error"]["http_code"] = $code; } else { throw new \Exception($result["error"]["message"]); } } return $result; }
public static function sign_request($params, &$options) { $api_key = Cloudinary::option_get($options, "api_key", Cloudinary::config_get("api_key")); if (!$api_key) { throw new \InvalidArgumentException("Must supply api_key"); } $api_secret = Cloudinary::option_get($options, "api_secret", Cloudinary::config_get("api_secret")); if (!$api_secret) { throw new \InvalidArgumentException("Must supply api_secret"); } # Remove blank parameters $params = array_filter($params, function ($v) { return isset($v) && $v !== ""; }); $params["signature"] = Cloudinary::api_sign_request($params, $api_secret); $params["api_key"] = $api_key; return $params; }
public static function call_api($action, $params, $options = array(), $file = NULL) { $return_error = \Cloudinary::option_get($options, "return_error"); if (!\Cloudinary::option_get($options, "unsigned")) { $params = \Cloudinary::sign_request($params, $options); } $api_url = \Cloudinary::cloudinary_api_url($action, $options); $ch = curl_init($api_url); $post_params = array(); foreach ($params as $key => $value) { if (is_array($value)) { $i = 0; foreach ($value as $item) { $post_params[$key . "[{$i}]"] = $item; $i++; } } else { $post_params[$key] = $value; } } if ($file) { if (!preg_match('/^@|^ftp:|^https?:|^s3:|^data:[^;]*;base64,([a-zA-Z0-9\\/+\\n=]+)$/', $file)) { if (function_exists("curl_file_create")) { $post_params['file'] = curl_file_create($file); $post_params['file']->setPostFilename($file); } else { $post_params["file"] = "@" . $file; } } else { $post_params["file"] = $file; } } curl_setopt($ch, CURLOPT_POST, true); $timeout = \Cloudinary::option_get($options, "timeout", \Cloudinary::config_get("timeout", 60)); curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); curl_setopt($ch, CURLOPT_POSTFIELDS, $post_params); curl_setopt($ch, CURLOPT_CAINFO, realpath(dirname(__FILE__)) . DIRECTORY_SEPARATOR . "cacert.pem"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); # no effect since PHP 5.1.3 curl_setopt($ch, CURLOPT_USERAGENT, \Cloudinary::userAgent()); curl_setopt($ch, CURLOPT_PROXY, \Cloudinary::option_get($options, "api_proxy", \Cloudinary::config_get("api_proxy"))); $range = \Cloudinary::option_get($options, "content_range"); if ($range != NULL) { curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Range: ' . $range)); } $response = curl_exec($ch); $curl_error = NULL; if (curl_errno($ch)) { $curl_error = curl_error($ch); } $code = curl_getinfo($ch, CURLINFO_HTTP_CODE); $response_data = $response; curl_close($ch); if ($curl_error != NULL) { throw new \Cloudinary\Error("Error in sending request to server - " . $curl_error); } if ($code != 200 && $code != 400 && $code != 500 && $code != 401 && $code != 404) { throw new \Cloudinary\Error("Server returned unexpected status code - " . $code . " - " . $response_data, $code); } $result = json_decode($response_data, TRUE); if ($result == NULL) { throw new \Cloudinary\Error("Error parsing server response (" . $code . ") - " . $response_data); } if (isset($result["error"])) { if ($return_error) { $result["error"]["http_code"] = $code; } else { throw new \Cloudinary\Error($result["error"]["message"], $code); } } return $result; }
public static function cloudinary_api_url($action = 'upload', $options = array()) { $cloudinary = Cloudinary::option_get($options, "upload_prefix", Cloudinary::config_get("upload_prefix", "https://api.cloudinary.com")); $cloud_name = Cloudinary::config_get("cloud_name"); if (!$cloud_name) { throw new InvalidArgumentException("Must supply cloud_name in tag or in configuration"); } $resource_type = Cloudinary::option_get($options, "resource_type", "image"); return implode("/", array($cloudinary, "v1_1", $cloud_name, $resource_type, $action)); }
protected function call_api($method, $uri, $params, &$options) { $prefix = Cloudinary::option_get($options, "upload_prefix", Cloudinary::config_get("upload_prefix", "https://api.cloudinary.com")); $cloud_name = Cloudinary::option_get($options, "cloud_name", Cloudinary::config_get("cloud_name")); if (!$cloud_name) { throw new InvalidArgumentException("Must supply cloud_name"); } $api_key = Cloudinary::option_get($options, "api_key", Cloudinary::config_get("api_key")); if (!$api_key) { throw new InvalidArgumentException("Must supply api_key"); } $api_secret = Cloudinary::option_get($options, "api_secret", Cloudinary::config_get("api_secret")); if (!$api_secret) { throw new InvalidArgumentException("Must supply api_secret"); } $api_url = implode("/", array_merge(array($prefix, "v1_1", $cloud_name), $uri)); $api_url .= "?" . preg_replace("/%5B\\d+%5D/", "%5B%5D", http_build_query($params)); $ch = curl_init($api_url); curl_setopt($ch, CURLOPT_HEADER, 1); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, strtoupper($method)); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_TIMEOUT, 60); curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); curl_setopt($ch, CURLOPT_USERPWD, $api_key . ":" . $api_secret); curl_setopt($ch, CURLOPT_CAINFO, realpath(dirname(__FILE__)) . "/cacert.pem"); $response = $this->execute($ch); curl_close($ch); if ($response->responseCode == 200) { return new CloudinaryApiResponse($response); } else { $exception_class = Cloudinary::option_get(self::$CLOUDINARY_API_ERROR_CLASSES, $response->responseCode); if (!$exception_class) { throw new CloudinaryApiGeneralError("Server returned unexpected status code - {$response->responseCode} - {$response->body}"); } $json = $this->parse_json_response($response); throw new $exception_class($json["error"]["message"]); } }
function call_api($method, $uri, $params, &$options) { $prefix = \Cloudinary::option_get($options, "upload_prefix", \Cloudinary::config_get("upload_prefix", "https://api.cloudinary.com")); $cloud_name = \Cloudinary::option_get($options, "cloud_name", \Cloudinary::config_get("cloud_name")); if (!$cloud_name) { throw new \InvalidArgumentException("Must supply cloud_name"); } $api_key = \Cloudinary::option_get($options, "api_key", \Cloudinary::config_get("api_key")); if (!$api_key) { throw new \InvalidArgumentException("Must supply api_key"); } $api_secret = \Cloudinary::option_get($options, "api_secret", \Cloudinary::config_get("api_secret")); if (!$api_secret) { throw new \InvalidArgumentException("Must supply api_secret"); } $api_url = implode("/", array_merge(array($prefix, "v1_1", $cloud_name), $uri)); if ($method == "get") { $api_url .= "?" . preg_replace("/%5B\\d+%5D/", "%5B%5D", http_build_query($params)); } $ch = curl_init($api_url); if ($method != "get") { $post_params = array(); foreach ($params as $key => $value) { if (is_array($value)) { $i = 0; foreach ($value as $item) { $post_params[$key . "[{$i}]"] = $item; $i++; } } else { $post_params[$key] = $value; } } curl_setopt($ch, CURLOPT_POSTFIELDS, $post_params); } curl_setopt($ch, CURLOPT_HEADER, 1); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, strtoupper($method)); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_TIMEOUT, 60); curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); curl_setopt($ch, CURLOPT_USERPWD, "{$api_key}:{$api_secret}"); curl_setopt($ch, CURLOPT_CAINFO, realpath(dirname(__FILE__)) . DIRECTORY_SEPARATOR . "cacert.pem"); curl_setopt($ch, CURLOPT_USERAGENT, \Cloudinary::userAgent()); curl_setopt($ch, CURLOPT_PROXY, \Cloudinary::option_get($options, "api_proxy", \Cloudinary::config_get("api_proxy"))); $response = $this->execute($ch); $curl_error = NULL; if (curl_errno($ch)) { $curl_error = curl_error($ch); } curl_close($ch); if ($curl_error != NULL) { throw new \Cloudinary\Api\GeneralError("Error in sending request to server - " . $curl_error); } if ($response->responseCode == 200) { return new \Cloudinary\Api\Response($response); } else { $exception_class = \Cloudinary::option_get(self::$CLOUDINARY_API_ERROR_CLASSES, $response->responseCode); if (!$exception_class) { throw new \Cloudinary\Api\GeneralError("Server returned unexpected status code - {$response->responseCode} - {$response->body}"); } $json = $this->parse_json_response($response); throw new $exception_class($json["error"]["message"]); } }
function media_lib_upload_notices() { global $post_type, $pagenow; if ($pagenow == 'upload.php' && $post_type == 'attachment' && Cloudinary::option_get($_REQUEST, 'cloudinary_message')) { $message = htmlentities($_REQUEST['cloudinary_message'], ENT_NOQUOTES); echo "<div class=\"updated\"><p>{$message}</p></div>"; } if ($pagenow == 'upload.php' && Cloudinary::option_get($_REQUEST, 'cloud_upload')) { $message = "Sorry, this file format is not supported."; echo "<div class=\"updated\"><p>{$message}</p></div>"; } }