diff --git a/api/newPost.php b/api/newPost.php index 1379743..bf8ced0 100644 --- a/api/newPost.php +++ b/api/newPost.php @@ -20,14 +20,21 @@ $postImage = $_FILES["postImage"]; $imageHash = hash_file("sha256", $postImage["tmp_name"]); - $fileExtension = "png"; + $fileExtension = pathinfo($postImage["name"], PATHINFO_EXTENSION); $uniqueFileName = $imageHash . "." . $fileExtension; - $target_file = $_SERVER['DOCUMENT_ROOT'] . "/" . TARGET_UPLOAD_PATH . $uniqueFileName; + // normalize filesystem target directory and URL + $docRoot = rtrim($_SERVER['DOCUMENT_ROOT'], "/\\"); + $uploadPath = trim(TARGET_UPLOAD_PATH, "/\\"); + $targetDirFs = $docRoot . DIRECTORY_SEPARATOR . $uploadPath; + if (!is_dir($targetDirFs)) { + mkdir($targetDirFs, 0755, true); + } + $target_file = $targetDirFs . DIRECTORY_SEPARATOR . $uniqueFileName; $uploadOk = 1; $imageFileType = strtolower(pathinfo($target_file, PATHINFO_EXTENSION)); - - $fileUrl = TARGET_UPLOAD_DIR . $uniqueFileName; + + $fileUrl = rtrim(TARGET_UPLOAD_DIR, "/\\") . '/' . rawurlencode($uniqueFileName); $check = getimagesize($postImage["tmp_name"]); @@ -50,22 +57,30 @@ if ($uploadOk == 0) { echo "
Sorry, the file was not uploaded.
"; + exit(); } else { - // Resize the uploaded image - $resizedImage = imagecreatefromstring(file_get_contents($postImage["tmp_name"])); - $newWidth = POST_IMAGE_WIDTH; // Adjust this value as needed - $aspectRatio = imagesx($resizedImage) / imagesy($resizedImage); - $newHeight = $newWidth / $aspectRatio; - if ($newHeight > ($newWidth * 3)){ - $newHeight = $newWidth * 3; - } - $resizedImage = imagescale($resizedImage, $newWidth, $newHeight); - imagejpeg($resizedImage, $target_file); - imagedestroy($resizedImage); - - if($resizedImage == false){ - echo "error with resizing image."; - exit(); + // If the uploaded file is a GIF, move it as-is to preserve animation + if (strtolower($fileExtension) === 'gif') { + if (!move_uploaded_file($postImage['tmp_name'], $target_file)) { + echo "Error saving uploaded GIF file.
"; + exit(); + } + } else { + // Resize the uploaded image using GD for non-GIF formats + $resizedImage = imagecreatefromstring(file_get_contents($postImage["tmp_name"])); + $newWidth = POST_IMAGE_WIDTH; // Adjust this value as needed + $aspectRatio = imagesx($resizedImage) / imagesy($resizedImage); + $newHeight = $newWidth / $aspectRatio; + if ($newHeight > ($newWidth * 3)) { + $newHeight = $newWidth * 3; + } + $resizedImage = imagescale($resizedImage, $newWidth, $newHeight); + imagejpeg($resizedImage, $target_file); + imagedestroy($resizedImage); + + if ($resizedImage == false) { + echo "error with resizing image."; + } } $PostContent .= "

$uniqueFileName"; @@ -75,8 +90,7 @@ $db = new Database(); -$sanitizedPostContent = strip_tags($PostContent, implode('', ALLOWED_POST_BALISE)); - +$sanitizedPostContent = strip_tags($PostContent, implode('', ALLOWED_POST_BALISES)); // transform content link into html $url = "/http(s)?:\/\/([\w-]+\.)+[\w-]+(\/[\w\- .\/?%&=]*)?/"; $sanitizedPostContent = preg_replace($url, "$0", $sanitizedPostContent); @@ -84,7 +98,7 @@ $cleanPostContent = $db->escapeStrings($sanitizedPostContent); -if (strlen(strip_tags($PostContent)) < 2 || strlen(strip_tags($PostContent)) > MAX_POSTS_LENGTH) { +if (strlen(strip_tags($PostContent)) > MAX_POSTS_LENGTH) { echo "Error with post length
"; echo strlen($PostContent); exit(); @@ -97,4 +111,3 @@ $db->query($insertNewPostSqlPrompt); header('Location: ../index.php?p=home'); -?> diff --git a/api/saveSettings.php b/api/saveSettings.php index 4b81086..a323ccd 100644 --- a/api/saveSettings.php +++ b/api/saveSettings.php @@ -86,11 +86,11 @@ $fileExtension = "png"; $uniqueFileName = "pp_$userId." . $fileExtension; - $target_file = $_SERVER['DOCUMENT_ROOT'] . "/" . TARGET_UPLOAD_DIR . $uniqueFileName; + $target_file = $_SERVER['DOCUMENT_ROOT'] . "/" . TARGET_UPLOAD_PATH . $uniqueFileName; $uploadOk = 1; $imageFileType = strtolower(pathinfo($target_file, PATHINFO_EXTENSION)); - $fileUrl = $fullDomain . "/" . TARGET_UPLOAD_DIR . $uniqueFileName; + $fileUrl = $fullDomain . "/" . TARGET_UPLOAD_PATH . $uniqueFileName; $check = getimagesize($profilePict["tmp_name"]); diff --git a/api/utils/database.php b/api/utils/database.php index 46a8f51..24e74c4 100644 --- a/api/utils/database.php +++ b/api/utils/database.php @@ -6,7 +6,7 @@ class Database { private $conn; public function __construct() { - include_once(dirname(__FILE__) . "/../../config.php"); + include_once(dirname(__FILE__) . "/../config.php"); $this->conn = new mysqli(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_NAME); if ($this->conn->connect_error) {