-
Notifications
You must be signed in to change notification settings - Fork 0
/
cImgur.class.php
190 lines (137 loc) · 4.31 KB
/
cImgur.class.php
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
<?php
/*
cImgur v1.0
(c) 2017 by Thielicious
cImgur (abbr: c = custom) is a customized imgur remote uploader using cURL
and imgur API endpoints. Made for anonymous execution.
*/
class cImgur {
public
$clientID = null, $debug = 0;
private
$imgWidth, $imgHeight, $uploadSize,
$errors = [], $data;
const
RETURN_JSON = 1, RETURN_ARRAY = 2, RETURN_OBJECT = 3;
public function __construct(string $cid = null) {
if ($cid != null) {
$this->clientID = $cid;
}
}
public function clientID(string $cid) {
$this->clientID = $cid;
}
public function debug() {
$this->debug = 1;
}
private static function error(array $file) {
return $file["error"] !== 0 ?
true : false;
}
public function getErrors() {
if (count($this->errors) > 0) {
return $this->errors;
}
}
public function setUploadSize(int $usize) {
$this->uploadSize = $usize;
}
public function setImageSize(int $width, int $height) {
$this->imgWidth = $width;
$this->imgHeight = $height;
}
private static function checkMIME(array $file) {
$filetype = explode("/",mime_content_type($file["tmp_name"]));
return $filetype[0] !== "image" ?
false : true;
}
private function connectToImgur(array $file) {
$image = @file_get_contents($file["tmp_name"]);
$curl = curl_init();
curl_setopt_array(
$curl, [
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_URL => "https://api.imgur.com/3/image.json",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => [
"image" => base64_encode($image)
],
CURLOPT_HTTPHEADER => [
"Authorization: Client-ID ".$this->clientID
]
]
);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
$this->errors[] = "Upload Error: ".$err;
} else {
if (@count(json_decode($response)->data->error,true) > 0) {
$this->errors[] = "Sorry, this image file does not meet the required standards at imgur.com to be uploaded. <br> Upload denied.";
}
$this->data = $response;
}
return true;
}
public function data($type = null) {
if ($type == self::RETURN_JSON || null) {
return $this->data;
} elseif ($type == self::RETURN_OBJECT) {
$data = json_decode($this->data);
return $data->data;
} elseif ($type == self::RETURN_ARRAY) {
$data = json_decode($this->data,true);
return $data["data"];
}
}
public function upload(array $file) {
if ($this->debug == 1) {
echo "<strong>DEBUG:</strong><br>
<pre>".print_r($file,true)."</pre>";
$this->connectToImgur($file);
echo "<pre>".print_r(json_decode($this->data)->data,true)."</pre>";
exit;
} elseif ($file["error"] != 4) {
if (static::error($file) == false) {
if (strlen($this->uploadSize) != 0) {
if ($file["size"] <= $this->uploadSize) {
if (static::checkMIME($file) == true) {
$size = getimagesize($file["tmp_name"]);
if (strlen($this->imgWidth && $this->imgHeight) != 0) {
if ($size[0] == $this->imgWidth && $size[1] == $this->imgHeight) {
$this->connectToImgur($file);
} else {
$this->errors[] = "Image size does not match (allowed: ".$this->imgWidth."x".$this->imgHeight." pixel)";
}
} else {
$this->connectToImgur($file);
}
} else {
$this->errors[] = "File is not an image.";
}
} else {
$this->errors[] = "Image is too large. (max ".floor(($this->uploadSize/1024))." KB)";
}
} else {
$this->errors[] = "Upload size not set.";
}
} else {
$this->errors[] = "File error :( Could be incorrectly formatted or is damaged.";
}
} else {
$this->errors[] = "No image selected.";
}
if (count($this->errors) > 0) {
throw new Exception;
} else {
return true;
}
}
}
?>