'ERROR', 'url' => 'Invalid or missing upload secret', // Remove this if you don't want to show the support URL 'support' => "For support, visit - https://git.fascinated.cc/Fascinated/sharex-php-uploader", 'timeTaken' => getTimeTaken() )); die(); } // Check if the secret is the default one, and if so, tell the user to change it if ($secret == $defaultSecretKey) { returnJson(array( 'status' => 'ERROR', 'url' => 'You need to set your upload secret in the configuration section of the upload.php file', 'timeTaken' => getTimeTaken() )); die(); } // Check if the file was uploaded if (!isset($file)) { returnJson(array( 'status' => 'ERROR', 'url' => 'No file was uploaded', 'timeTaken' => getTimeTaken() )); die(); } $target_file = preg_replace("/[^A-Za-z0-9_.]/", '', $_FILES["sharex"]["name"]); // Remove unwanted characters $fileType = pathinfo($target_file, PATHINFO_EXTENSION); // File extension (e.g. png, jpg, etc.) // Check if the file already exists if (file_exists($uploadDir . $target_file)) { returnJson(array( 'status' => 'ERROR', 'url' => 'File already exists', 'timeTaken' => getTimeTaken() )); die(); } $finalName = $target_file; // The final name of the file if ($useRandomFileNames) { // Generate a random file name if enabled $finalName = generateRandomString($fileNameLength) . "." . $fileType; } $needsToBeSaved = true; // Whether the file needs to be saved // Check the file type and size if ($shouldConvertToWebp && in_array($fileType, ["png", "jpeg", "jpg"]) && $_FILES["sharex"]["size"] > $webpThreadhold) { // Create an Imagick object from the uploaded file $image = new Imagick($_FILES["sharex"]["tmp_name"]); // Convert the image to WebP $image->setImageFormat("webp"); $image->setImageCompressionQuality($webpQuality); // Set the output filename $webp_file = pathinfo($finalName, PATHINFO_FILENAME) . ".webp"; // Save the converted image $image->writeImage($webp_file); // Free up memory $image->clear(); $image->destroy(); // Update the final filename $finalName = $webp_file; $needsToBeSaved = false; } if ($needsToBeSaved) { // Save the file if it has not been saved yet // Move the file to the uploads folder $success = move_uploaded_file($_FILES["sharex"]["tmp_name"], $uploadDir . $finalName); if (!$success) { returnJson(array( 'status' => 'ERROR', 'url' => 'Failed to save file. Check the permissions of the upload directory.', 'timeTaken' => getTimeTaken() )); die(); } } returnJson(array( 'status' => 'OK', 'url' => $finalName, 'timeTaken' => getTimeTaken() )); die(); } catch (Exception $e) { // Handle any errors returnJson(array( 'status' => 'ERROR', 'url' => $e->getMessage(), 'timeTaken' => getTimeTaken() )); die(); }