-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.php
130 lines (78 loc) · 3.18 KB
/
server.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
<?php
# ==================== BEGIN OF THE CONFIGURATION ==================== #
# The username and password to authenticate to the server.
# Replace "change-me" for each variable ("USERNAME" and "PASSWORD")
define ("USERNAME", "change-me");
define ("PASSWORD", "change-me");
# If you want put your ShareX server in maintenance mode.
# Replace the value of "MAINTENANCE" variable.
# "true" => Enable maintenance mode
# "false" => Disable maintenance mode
# DO NOT PUT QUOTATION MARKS AROUND VALUES !
define("MAINTENANCE", false);
# ==================== END OF THE CONFIGURATION ==================== #
function response ($http_code, $status, $url, $deletion_url) {
switch ($http_code) {
case 200:
header("{$_SERVER['SERVER_PROTOCOL']} 200 OK");
break;
case 400:
header("{$_SERVER['SERVER_PROTOCOL']} 400 Bad Request");
break;
case 401:
header("{$_SERVER['SERVER_PROTOCOL']} 401 Unauthorized");
break;
case 403:
header("{$_SERVER['SERVER_PROTOCOL']} 403 Forbidden");
break;
case 500:
header("{$_SERVER['SERVER_PROTOCOL']} 500 Internal Server Error");
break;
case 503:
header("{$_SERVER['SERVER_PROTOCOL']} 503 Service Unavailable");
break;
}
$response["http_code"] = $http_code;
if ($status != "Success") {
$response["status"] = $status;
}
if ($url != null) {
$response["url"] = $url;
}
if ($deletion_url != null) {
$response["deletion_url"] = $deletion_url;
}
echo json_encode ($response);
return true;
}
if (MAINTENANCE) {
return response (503, "Server under maintenance", null, null);
}
if (!isset($_GET["username"]) AND !isset($_POST["username"])) {
return response (401, "Missing authentication information", null, null);
}
if (!isset($_GET["password"]) AND !isset($_POST["password"])) {
return response (401, "Missing authentication information", null, null);
}
$username= htmlspecialchars($_GET["username"]) ?: htmlspecialchars($_POST["username"]);
$password = htmlspecialchars($_GET["password"]) ?: htmlspecialchars($_POST["password"]);
if ($username != USERNAME OR $password != PASSWORD) {
return response (403, "Invalid credential", null, null);
}
if (!file_exists("files")) {
return response (500, "Incomplete ShareX server", null, null);
}
if (!isset($_FILES["file"])) {
return response (400, "File not existing", null, null);
}
$characters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789";
$generateFileName = str_shuffle($characters);
$generateFileName = substr($generateFileName, 0, 7);
$fileExtension = strtolower(substr(strrchr($_FILES["file"]['name'], '.'), 1));
if (file_exists("files/{$generateFileName}.{$fileExtension}")) {
return response (500, "One file with this name already exists", null, null);
}
if (!move_uploaded_file($_FILES["file"]["tmp_name"], "files/{$generateFileName}.{$fileExtension}")) {
return response (500, "Error when uploading", null, null);
}
response (200, "Success", "https://{$_SERVER['SERVER_NAME']}/{$generateFileName}.{$fileExtension}", null);