-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.php
72 lines (66 loc) · 2.28 KB
/
api.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
<?php
include "src/constants.php";
include "src/image.php";
include "src/config.php";
if (!empty($_GET)) {
global $CONFIG;
switch ($_GET['c']) {
//Serve an image
case 'get_image':
if (array_key_exists('filename', $_GET)) {
$sanitized_filename = htmlspecialchars($_GET['filename'], ENT_QUOTES);
return serveImage($sanitized_filename);
} else {
retFailure(400, 'Parameter filename not specified');
}
break;
//Upload an image
case 'upload':
//Check if category exist
if (array_key_exists('cat', $_GET) && array_key_exists($_GET['cat'], $CONFIG['CATEGORIES'])) {
$cat = $CONFIG['CATEGORIES'][$_GET['cat']];
//Check if filename exist
if (array_key_exists('filename', $_GET) && file_exists($CONFIG['SOURCE_DIR'] . $_GET['filename'])) {
$filename = $_GET['filename'];
if (!storeFile($_FILES, $cat, $filename)) {
retFailure(500, 'Could not store/move files. Is SOURCE_DIR and TARGET_DIR writeable?');
}
} else {
retFailure(400, 'Invalid file or not specified');
}
} else {
retFailure(400, 'Invalid category or not specified');
}
break;
//Get next image info
case 'img_info':
getNextImageData();
break;
//Duplicate image on disk
case 'duplicate':
if (array_key_exists('filename', $_GET)) {
duplicateImage(htmlspecialchars($_GET['filename']));
} else {
retFailure(400, 'No filename specified');
}
}
}
/**
* @param int $status_code HTTP Status code
* @param string $msg Error message which is returned as JSON array
*/
function retFailure(int $status_code, string $msg)
{
header('Content-type:application/json;charset=utf-8');
http_response_code($status_code);
$data = array(
'error' => $msg
);
echo json_encode($data);
}
function getNextImageData()
{
$next_image = getNextFile();
header('Content-type:application/json;charset=utf-8');
echo json_encode($next_image);
}