-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupload.php
189 lines (163 loc) · 4.6 KB
/
upload.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
<?php
namespace Log1x\Pomf;
use Carbon\Carbon;
use Bulletproof\Image;
use Hashids\Hashids;
use Exception;
class Pomf
{
/**
* Default Configuration
*
* @var array
*/
protected $config = [
'token' => '',
'dir' => 'screenshots/',
'size' => 100,
'slug' => 'Screenshot_',
'timestamp' => 'Y-m-d_H-i-s'
];
/**
* Create a new Pomf instance.
*
* @return void
*/
public function __construct()
{
if (! $_SERVER['REQUEST_METHOD'] == 'POST') {
return $this->error('Invalid request method.', 401);
}
$config = file_exists($config = __DIR__ . '/config.php') ? require_once($config) : [];
$this->config = (object) array_merge($this->config, $config);
$this->verifyToken();
$this->upload();
}
/**
* Responds with a JSON body that the request was invalid.
*
* @param array $value
* @param int $code
* @return mixed
*/
protected function error($value, $code = 500)
{
header('Content-Type: application/json; charset=UTF-8');
http_response_code((int) $code);
echo json_encode([
'success' => false,
'errorcode' => $code,
'description' => $value
], JSON_PRETTY_PRINT);
exit();
}
/**
* Responds with a JSON body that the request was successful.
*
* @param string $value
* @return mixed
*/
protected function success($value)
{
header('Content-Type: application/json; charset=UTF-8');
http_response_code(200);
echo json_encode([
'success' => true,
'files' => $value
], JSON_PRETTY_PRINT);
exit();
}
/**
* Returns the current host URL.
*
* @return string
*/
protected function getHost()
{
return (empty($_SERVER['HTTPS']) ? 'http://' : 'https://') . $_SERVER['HTTP_HOST'] . strtok($_SERVER['REQUEST_URI'], '?');
}
/**
* Generates the filename based on the format.
*
* @return string
*/
protected function generateFilename()
{
if (empty($this->config->timestamp)) {
return $this->config->slug . (new Hashids(random_bytes(24)))->encode(1, 2, 3);
}
return $this->config->slug . Carbon::now()->format($this->config->timestamp);
}
/**
* Returns the full relative path to the uploaded file with extension.
*
* @param object $image
* @return string
*/
protected function getFile($image = null)
{
if (! $image instanceof Image) {
return $this->error('Unable to process uploaded file.');
}
return $this->getHost() . $this->config->dir . $image->getName() . '.' . $image->getMime();
}
/**
* Verifies API Token
*
* @return mixed
*/
protected function verifyToken()
{
if (empty($_GET['token']) || $_GET['token'] !== $this->config->token) {
return $this->error('Invalid Token', 401);
}
}
/**
* Passes multiple images through Bulletproof and bundles them into an array.
*
* @param array $images
* @return array
*/
protected function bundle($images)
{
return array_map(function ($image) {
$image = new Image($image);
$image->setName($this->generateFilename())
->setSize(100, $this->config->size * (1024 * 1024))
->setMime(['gif', 'jpg', 'jpeg', 'png'])
->setLocation($this->config->dir ? $this->config->dir : '.', 0755);
return $image;
}, $images);
}
/**
* Processes and uploads an image passed through the POST method.
*
* @param array $images
* @return void
*/
protected function upload($images = [])
{
if (empty($_FILES)) {
return $this->error('No input file(s) found.');
}
foreach ($this->bundle($_FILES) as $image) {
if ($image->upload()) {
$images[] = [
'name' => $image->getName(),
'url' => $this->getFile($image),
'hash' => sha1_file($image->getFullPath()),
'size' => $image->getSize(),
];
}
}
if (empty($images)) {
return $this->error('Images uploaded are not valid.');
}
return $this->success($images);
}
}
if (! file_exists($composer = __DIR__ . '/vendor/autoload.php')) {
return;
}
require_once $composer;
return new Pomf;