2023-04-09 10:58:09 +00:00
|
|
|
<?php
|
|
|
|
$before = microtime(true); // Start time of the script
|
2023-04-09 11:21:39 +00:00
|
|
|
declare(strict_types=1);
|
2023-04-09 10:58:09 +00:00
|
|
|
error_reporting(E_ERROR); // Hide PHP errors
|
2023-04-09 11:21:39 +00:00
|
|
|
header('Content-type:application/json;charset=utf-8'); // Set the content type to JSON
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Configuration
|
|
|
|
*/
|
2023-04-09 10:58:09 +00:00
|
|
|
$tokens = array("set me"); // Your secret keys
|
2023-04-09 11:04:17 +00:00
|
|
|
$uploadDir = "./"; // The upload directory
|
2023-04-09 11:17:17 +00:00
|
|
|
$useRandomFileNames = false; // Use random file names instead of the original file name
|
|
|
|
$fileNameLength = 8; // The length of the random file name
|
2023-04-09 10:58:09 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if the token is valid
|
|
|
|
*/
|
2023-04-09 11:21:39 +00:00
|
|
|
function checkToken($token): bool {
|
2023-04-09 10:58:09 +00:00
|
|
|
global $tokens;
|
2023-04-09 11:21:39 +00:00
|
|
|
return isset($token) && in_array($token, $tokens);
|
2023-04-09 10:58:09 +00:00
|
|
|
}
|
|
|
|
|
2023-04-09 11:17:17 +00:00
|
|
|
/**
|
|
|
|
* Generate a random string
|
|
|
|
*/
|
2023-04-09 11:21:39 +00:00
|
|
|
function generateRandomString($length = 10): string {
|
2023-04-09 11:17:17 +00:00
|
|
|
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
|
|
|
|
$charactersLength = strlen($characters);
|
|
|
|
$randomString = '';
|
|
|
|
for ($i = 0; $i < $length; $i++) {
|
|
|
|
$randomString .= $characters[rand(0, $charactersLength - 1)];
|
|
|
|
}
|
|
|
|
return $randomString;
|
|
|
|
}
|
|
|
|
|
2023-04-09 10:58:09 +00:00
|
|
|
/**
|
|
|
|
* Return a JSON response
|
|
|
|
*/
|
|
|
|
function returnJson($status, $message, $timeTaken = null) {
|
2023-04-09 11:21:39 +00:00
|
|
|
$json = array('status' => $status, 'url' => $message, 'processingTime' => round($timeTaken ?? 0, 2) . "ms");
|
2023-04-09 10:58:09 +00:00
|
|
|
echo(json_encode($json));
|
|
|
|
die();
|
|
|
|
}
|
|
|
|
|
2023-04-09 11:21:39 +00:00
|
|
|
try {
|
|
|
|
$token = $_POST['secret']; // The provided secret key
|
|
|
|
$file = $_FILES['sharex']; // The uploaded file
|
2023-04-09 10:58:09 +00:00
|
|
|
|
2023-04-09 11:21:39 +00:00
|
|
|
// Check if the token is valid
|
|
|
|
if (!checkToken($token)) {
|
|
|
|
$timeTaken = microtime(true) - $before;
|
|
|
|
returnJson('ERROR', 'Invalid or missing secret key', $timeTaken);
|
|
|
|
die();
|
|
|
|
}
|
2023-04-09 10:58:09 +00:00
|
|
|
|
2023-04-09 11:21:39 +00:00
|
|
|
// Check if the file was uploaded
|
|
|
|
if (!isset($file)) {
|
|
|
|
$timeTaken = microtime(true) - $before;
|
|
|
|
returnJson('ERROR', 'No file was uploaded', $timeTaken);
|
|
|
|
die();
|
|
|
|
}
|
2023-04-09 10:58:09 +00:00
|
|
|
|
2023-04-09 11:21:39 +00:00
|
|
|
$target_file = $_FILES["sharex"]["name"]; // File name
|
|
|
|
$fileType = pathinfo($target_file, PATHINFO_EXTENSION); // File extension (e.g. png, jpg, etc.)
|
2023-04-09 10:58:09 +00:00
|
|
|
|
2023-04-09 11:21:39 +00:00
|
|
|
// Check if the file already exists
|
|
|
|
if (file_exists($uploadDir . $target_file)) {
|
|
|
|
$timeTaken = microtime(true) - $before;
|
|
|
|
returnJson('ERROR', 'File already exists', $timeTaken);
|
|
|
|
die();
|
|
|
|
}
|
2023-04-09 11:05:53 +00:00
|
|
|
|
2023-04-09 11:21:39 +00:00
|
|
|
$shouldSave = true; // Whether or not the file should be saved
|
|
|
|
$finalName = $target_file; // The final name of the file
|
|
|
|
if ($useRandomFileNames) { // Generate a random file name if enabled
|
|
|
|
$finalName = generateRandomString($fileNameLength) . "." . $fileType;
|
|
|
|
}
|
2023-04-09 11:17:17 +00:00
|
|
|
|
2023-04-09 11:21:39 +00:00
|
|
|
// Convert the image to webp if applicable
|
|
|
|
if (in_array($fileType, array("png", "jpeg", "jpg"))) {
|
|
|
|
$image = imagecreatefromstring(file_get_contents($_FILES["sharex"]["tmp_name"]));
|
|
|
|
$webp_file = pathinfo($finalName, PATHINFO_FILENAME) . ".webp";
|
|
|
|
imagewebp($image, $webp_file, 90);
|
|
|
|
imagedestroy($image);
|
|
|
|
$finalName = $webp_file;
|
|
|
|
$shouldSave = false;
|
|
|
|
}
|
2023-04-09 10:58:09 +00:00
|
|
|
|
2023-04-09 11:21:39 +00:00
|
|
|
if ($shouldSave) {
|
|
|
|
// Move the file to the uploads folder
|
|
|
|
if (move_uploaded_file($_FILES["sharex"]["tmp_name"], $uploadDir . $finalName)) {
|
|
|
|
$timeTaken = microtime(true) - $before;
|
|
|
|
returnJson('OK', $finalName, $timeTaken);
|
|
|
|
} else {
|
|
|
|
$timeTaken = microtime(true) - $before;
|
2023-04-09 11:22:39 +00:00
|
|
|
returnJson('ERROR', 'File upload failed. Does the upload folder exist and did you CHMOD the folder?', $timeTaken);
|
2023-04-09 11:21:39 +00:00
|
|
|
}
|
|
|
|
die();
|
2023-04-09 11:17:17 +00:00
|
|
|
}
|
2023-04-09 11:21:39 +00:00
|
|
|
returnJson('OK', $finalName, $timeTaken);
|
2023-04-09 11:22:39 +00:00
|
|
|
} catch (Exception $e) { // Handle any errors
|
2023-04-09 11:21:39 +00:00
|
|
|
$timeTaken = microtime(true) - $before;
|
|
|
|
returnJson('ERROR', $e->getMessage(), $timeTaken);
|
2023-04-09 11:17:17 +00:00
|
|
|
die();
|
2023-04-09 10:58:09 +00:00
|
|
|
}
|
|
|
|
?>
|