2023-04-09 10:58:09 +00:00
|
|
|
<?php
|
|
|
|
$before = microtime(true); // Start time of the script
|
|
|
|
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
|
2023-04-09 15:56:05 +00:00
|
|
|
$shouldConvertToWebp = true; // Should the script convert images to webp?
|
|
|
|
$webpQuality = 90; // The quality of the webp image (0-100)
|
2023-04-09 11:17:17 +00:00
|
|
|
$fileNameLength = 8; // The length of the random file name
|
2023-04-09 15:54:20 +00:00
|
|
|
$webpThreadhold = 1048576; // 1MB - The minimum file size for converting to webp (in bytes)
|
2023-04-09 10:58:09 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if the token is valid
|
|
|
|
*/
|
2023-04-09 16:30:23 +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 16:30:23 +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 16:36:53 +00:00
|
|
|
/**
|
|
|
|
* Get the time taken to execute the script
|
|
|
|
*/
|
|
|
|
function getTimeTaken()
|
|
|
|
{
|
|
|
|
global $before;
|
|
|
|
return round(microtime(true) - $before, 3) . "ms";
|
|
|
|
}
|
|
|
|
|
2023-04-09 10:58:09 +00:00
|
|
|
/**
|
|
|
|
* Return a JSON response
|
|
|
|
*/
|
2023-04-09 16:30:23 +00:00
|
|
|
function returnJson($data): void
|
|
|
|
{
|
|
|
|
echo (json_encode($data));
|
2023-04-09 10:58:09 +00:00
|
|
|
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)) {
|
2023-04-09 16:30:23 +00:00
|
|
|
returnJson(array(
|
|
|
|
'status' => 'ERROR',
|
2023-04-09 16:37:56 +00:00
|
|
|
'url' => 'Invalid or missing upload secret',
|
2023-04-09 16:36:53 +00:00
|
|
|
// Remove this if you don't want to show the support URL
|
2023-04-09 16:31:08 +00:00
|
|
|
'support' => "For support, visit - https://git.fascinated.cc/Fascinated/sharex-php-uploader",
|
2023-04-09 16:36:53 +00:00
|
|
|
'timeTaken' => getTimeTaken()
|
2023-04-09 16:30:23 +00:00
|
|
|
));
|
2023-04-09 11:21:39 +00:00
|
|
|
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)) {
|
2023-04-09 16:30:23 +00:00
|
|
|
returnJson(array(
|
|
|
|
'status' => 'ERROR',
|
2023-04-09 16:37:56 +00:00
|
|
|
'url' => 'No file was uploaded',
|
2023-04-09 16:36:53 +00:00
|
|
|
'timeTaken' => getTimeTaken()
|
2023-04-09 16:30:23 +00:00
|
|
|
));
|
2023-04-09 11:21:39 +00:00
|
|
|
die();
|
|
|
|
}
|
2023-04-09 10:58:09 +00:00
|
|
|
|
2023-04-09 15:51:05 +00:00
|
|
|
$target_file = preg_replace("/[^A-Za-z0-9_.]/", '', $_FILES["sharex"]["name"]); // Remove unwanted characters
|
2023-04-09 11:21:39 +00:00
|
|
|
$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)) {
|
2023-04-09 16:30:23 +00:00
|
|
|
returnJson(array(
|
|
|
|
'status' => 'ERROR',
|
2023-04-09 16:37:56 +00:00
|
|
|
'url' => 'File already exists',
|
2023-04-09 16:36:53 +00:00
|
|
|
'timeTaken' => getTimeTaken()
|
2023-04-09 16:30:23 +00:00
|
|
|
));
|
2023-04-09 11:21:39 +00:00
|
|
|
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
|
2023-04-09 15:56:05 +00:00
|
|
|
if (in_array($fileType, array("png", "jpeg", "jpg")) && $_FILES["sharex"]["size"] > $webpThreadhold && $shouldConvertToWebp) {
|
2023-04-09 11:21:39 +00:00
|
|
|
$image = imagecreatefromstring(file_get_contents($_FILES["sharex"]["tmp_name"]));
|
|
|
|
$webp_file = pathinfo($finalName, PATHINFO_FILENAME) . ".webp";
|
2023-04-09 15:56:05 +00:00
|
|
|
imagewebp($image, $webp_file, $webpQuality); // Convert the image and save it
|
2023-04-09 11:24:47 +00:00
|
|
|
imagedestroy($image); // Free up memory
|
2023-04-09 11:21:39 +00:00
|
|
|
$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)) {
|
2023-04-09 16:30:23 +00:00
|
|
|
returnJson(array(
|
|
|
|
'status' => 'OK',
|
2023-04-09 16:39:19 +00:00
|
|
|
'url' => $finalName,
|
2023-04-09 16:36:53 +00:00
|
|
|
'timeTaken' => getTimeTaken()
|
2023-04-09 16:30:23 +00:00
|
|
|
));
|
2023-04-09 11:21:39 +00:00
|
|
|
} else {
|
2023-04-09 16:30:23 +00:00
|
|
|
returnJson(array(
|
|
|
|
'status' => 'ERROR',
|
2023-04-09 16:37:56 +00:00
|
|
|
'url' => 'Failed to save file. Check the permissions of the upload directory.',
|
2023-04-09 16:36:53 +00:00
|
|
|
'timeTaken' => getTimeTaken()
|
2023-04-09 16:30:23 +00:00
|
|
|
));
|
2023-04-09 11:21:39 +00:00
|
|
|
}
|
|
|
|
die();
|
2023-04-09 11:17:17 +00:00
|
|
|
}
|
2023-04-09 16:30:23 +00:00
|
|
|
returnJson(array(
|
|
|
|
'status' => 'OK',
|
2023-04-09 16:37:56 +00:00
|
|
|
'url' => 'File uploaded successfully',
|
2023-04-09 16:36:53 +00:00
|
|
|
'timeTaken' => getTimeTaken()
|
2023-04-09 16:30:23 +00:00
|
|
|
));
|
2023-04-09 15:54:20 +00:00
|
|
|
die();
|
2023-04-09 11:22:39 +00:00
|
|
|
} catch (Exception $e) { // Handle any errors
|
2023-04-09 16:30:23 +00:00
|
|
|
returnJson(array(
|
|
|
|
'status' => 'ERROR',
|
2023-04-09 16:37:56 +00:00
|
|
|
'url' => $e->getMessage(),
|
2023-04-09 16:36:53 +00:00
|
|
|
'timeTaken' => getTimeTaken()
|
2023-04-09 16:30:23 +00:00
|
|
|
));
|
2023-04-09 11:17:17 +00:00
|
|
|
die();
|
2023-04-09 10:58:09 +00:00
|
|
|
}
|