forked from cloudinary/cloudinary_php
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPreloadedFile.php
More file actions
53 lines (45 loc) · 2.2 KB
/
Copy pathPreloadedFile.php
File metadata and controls
53 lines (45 loc) · 2.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<?php
namespace Cloudinary {
class PreloadedFile {
public static $PRELOADED_CLOUDINARY_PATH = "/^([^\/]+)\/([^\/]+)\/v(\d+)\/([^#]+)#([^\/]+)$/";
public $filename, $version, $public_id, $signature, $resource_type, $type;
public function __construct($file_info) {
if (preg_match(\Cloudinary\PreloadedFile::$PRELOADED_CLOUDINARY_PATH, $file_info, $matches)) {
$this->resource_type = $matches[1];
$this->type = $matches[2];
$this->version = $matches[3];
$this->filename = $matches[4];
$this->signature = $matches[5];
$public_id_and_format = $this->split_format($this->filename);
$this->public_id = $public_id_and_format[0];
$this->format = $public_id_and_format[1];
} else {
throw new \InvalidArgumentException("Invalid preloaded file info");
}
}
public function is_valid() {
$public_id = $this->resource_type == "raw" ? $this->filename : $this->public_id;
$expected_signature = \Cloudinary::api_sign_request(array("public_id" => $public_id, "version" => $this->version), \Cloudinary::config_get("api_secret"));
return $this->signature == $expected_signature;
}
protected function split_format($identifier) {
$last_dot = strrpos($identifier, ".");
if ($last_dot === false) {
return array($identifier, NULL);
}
$public_id = substr($identifier, 0, $last_dot);
$format = substr($identifier, $last_dot+1);
return array($public_id, $format);
}
public function identifier() {
return "v" . $this->version . "/" . $this->filename;
}
public function extended_identifier() {
return $this->resource_type . "/" . $this->type . "/" . $this->identifier();
}
public function __toString() {
return $this->resource_type . "/" . $this->type . "/v" . $this->version . "/" . $this->filename . "#" . $this->signature;
}
}
}
?>