You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
118 lines
4.7 KiB
118 lines
4.7 KiB
<?php
|
|
// Imeji Uploader Core Code -- Version 2.1 Revision 58 (Return Proper Error Codes Update)
|
|
//
|
|
// Core Variables. Don't change unless you know what you're doing.
|
|
$imejicoreversion = "Imeji-Caddy_V2.2_Droploader_R58";
|
|
$target_dir = "public/";
|
|
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
|
|
$target_filenameonly = basename($_FILES["fileToUpload"]["name"]);
|
|
$uploadOk = 1;
|
|
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
|
|
// Check if uploaded image file is a actual image or fake image with masked extension.
|
|
if(isset($_POST["submit"])) {
|
|
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
|
|
if($check !== false) {
|
|
$uploadOk = 1;
|
|
} else {
|
|
$uploadOk = 0;
|
|
header('HTTP/1.1 415 Unsupported Media Type', true, 415);
|
|
http_response_code(415);
|
|
return;
|
|
}
|
|
}
|
|
// Check whether a file with the same name already exists on server or not for security reasons.
|
|
if (file_exists($target_file)) {
|
|
$uploadOk = 0;
|
|
header('HTTP/1.1 409 Conflict', true, 409);
|
|
http_response_code(409);
|
|
return;
|
|
}
|
|
// Check file size. (Imeji Standard Max is 6MB)
|
|
if ($_FILES["fileToUpload"]["size"] > 6291456) {
|
|
$uploadOk = 0;
|
|
header('HTTP/1.1 413 Payload Too Large', true, 413);
|
|
http_response_code(413);
|
|
return;
|
|
}
|
|
// Allow only certain file formats.
|
|
// 1.1a -- Fixed uppercase rejection bug and allows them.
|
|
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
|
|
&& $imageFileType != "JPG" && $imageFileType != "PNG" && $imageFileType != "JPEG"
|
|
&& $imageFileType != "gif" && $imageFileType != "svg"
|
|
&& $imageFileType != "GIF" && $imageFileType != "SVG" ) {
|
|
$uploadOk = 0;
|
|
header('HTTP/1.1 415 Unsupported Media Type', true, 415);
|
|
http_response_code(415);
|
|
return;
|
|
}
|
|
// Check if $uploadOk is set to 0 by an error, if so go to failure condition.
|
|
if ($uploadOk == 0) {
|
|
header('HTTP/1.1 400 Bad Request', true, 400);
|
|
http_response_code(400);
|
|
return;
|
|
// If everything is ok, try to upload file.
|
|
} else {
|
|
// 1.2a --- Changed some case-confusing characters such as I and O to web safe symbols.
|
|
// 1.3 ---- Removed some problematic symbol characters that can break CMS such as + and $.
|
|
// 1.3_0 -- Changed the filename randomizer algorithm to compensate for characters pool reduction to 62 from 64.
|
|
$characters = 'abcdefghjklmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ1234567890-_!*';
|
|
$random_string_length = 11;
|
|
$randomFilename = '';
|
|
for ($i = 0; $i < $random_string_length; $i++) {
|
|
$randomFilename .= $characters[rand(0, strlen($characters) - 1)];
|
|
}
|
|
// Day-base identifiers (Implemented in v1.3_0)
|
|
if (date('D') == 'Sun') {
|
|
$dayId = 'Q';
|
|
} else if (date('D') == 'Mon') {
|
|
$dayId = 'a';
|
|
} else if (date('D') == 'Tue') {
|
|
$dayId = 'z';
|
|
} else if (date('D') == 'Wed') {
|
|
$dayId = '2';
|
|
} else if (date('D') == 'Thu') {
|
|
$dayId = 'W';
|
|
} else if (date('D') == 'Fri') {
|
|
$dayId = 's';
|
|
} else if (date('D') == 'Sat') {
|
|
$dayId = 'x';
|
|
} else {
|
|
$dayId = '0';
|
|
}
|
|
// Hour-base identifiers (Implemented in v1.3_0 / Changed in v1.3_2)
|
|
if ((date('H') >= '00') && (date('H') <= '03')) {
|
|
$hourId = '3';
|
|
} else if ((date('H') >= '04') && (date('H') <= '07')) {
|
|
$hourId = 'E';
|
|
} else if ((date('H') >= '08') && (date('H') <= '11')) {
|
|
$hourId = 'd';
|
|
} else if ((date('H') >= '12') && (date('H') <= '15')) {
|
|
$hourId = '4';
|
|
} else if ((date('H') >= '16') && (date('H') <= '19')) {
|
|
$hourId = 'R';
|
|
} else if ((date('H') >= '20') && (date('H') <= '23')) {
|
|
$hourId = 'f';
|
|
} else {
|
|
$hourId = 'z';
|
|
}
|
|
// Finalize file upload and return output to user.
|
|
// Old line: $newfilename = $randomFilename . '.' . end(explode(".",$_FILES["fileToUpload"]["name"])); --prior to v1.3, kept for instaneous rollback safety.
|
|
$newfilename = $dayId . $hourId . $randomFilename . '.' . end(explode(".",$_FILES["fileToUpload"]["name"]));
|
|
$newtarget = $target_dir . $newfilename;
|
|
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $newtarget)) {
|
|
chmod("$newtarget", 0775); // Set read and write permissions on file
|
|
$uploaderlog = "" . date('U') . ", ". date('Y-m-d H:i:s e') . ", $_SERVER[REMOTE_ADDR], $newfilename" . ", " . $imejicoreversion . ", $_SERVER[HTTP_USER_AGENT]";
|
|
file_put_contents('uploads.log', $uploaderlog . PHP_EOL, FILE_APPEND);
|
|
header('Content-type: application/json');
|
|
echo json_encode([
|
|
"location" => "https://i.yandols.xyz/{$newfilename}"
|
|
]);
|
|
return;
|
|
} else {
|
|
header('HTTP/1.1 422 Unprocessable Entity', true, 422);
|
|
http_response_code(422);
|
|
return;
|
|
}
|
|
}
|
|
?>
|