diff --git a/RestControllers/conversationsController.php b/RestControllers/conversationsController.php index b761754..4215940 100644 --- a/RestControllers/conversationsController.php +++ b/RestControllers/conversationsController.php @@ -109,7 +109,7 @@ class conversationsController{ Rest_fatal_error(400, "Please specify a conversation ID !"); $conversationID = toInt($_POST["conversationID"]); - //Check if the user is a conversation moderator or not + //Check if the user belongs to the conversation if(!CS::get()->components->conversations->userBelongsTo(userID, $conversationID)) Rest_fatal_error("401", "Specified user doesn't belongs to the conversation !"); @@ -210,4 +210,44 @@ class conversationsController{ //Success return array("conversationsID" => $results); } + + /** + * Send a new message + * + * @url POST /conversations/sendMessage + */ + public function sendMessage(){ + user_login_required(); + + //First, check a conversation ID was specified + if(!isset($_POST["conversationID"])) + Rest_fatal_errror(400, "Please speicify a conversation ID !"); + + //Extract conversation ID + $conversationID = toInt($_POST["conversationID"]); + + //Check if the user belongs to the conversation + if(!CS::get()->components->conversations->userBelongsTo(userID, $conversationID)) + Rest_fatal_error(401, "Specified user doesn't belongs to the conversation !"); + + //Check if informations were specified about the new message or not + if(!isset($_POST['message']) AND !isset($_POST['image'])) + Rest_fatal_error(401, "Nothing to be sent with the new message !"); + + //Else extract informations + $message = (isset($_POST['message']) ? $_POST['message'] : ""); + $image = (isset($_POST['image']) ? $_POST['image'] : false); + + //Check message validity + if(!check_string_before_insert($message) && !$image) + Rest_fatal_error(401, "Invalid message sending request !"); + + //Process images NOT IMPLEMENTED YET + + //Insert the new message + if(!CS::get()->components->conversations->sendMessage(userID, $conversationID, $message)) + Rest_fatal_error(500, "Couldn't send the message !"); + + Rest_fatal_error("200", "All right now"); + } } \ No newline at end of file diff --git a/classes/components/conversations.php b/classes/components/conversations.php index 78e8f50..2b73209 100644 --- a/classes/components/conversations.php +++ b/classes/components/conversations.php @@ -411,6 +411,23 @@ class conversations { return $conversationsID; } + /** + * Send a new message + * + * @param Integer $userID The ID of the user sending the message + * @param Integer $conversationID The ID of the target conversation + * @param String $message The message + * @return Boolean True for a success + */ + public function sendMessage($userID, $conversationID, $message){ + + //GUIDE LINE : this method act like a "controller" : it doesn't perform any database operation + //But it manage all operations (insert message; save image; inform other users; ...) + + //Success + return true; + } + } //Register component diff --git a/functions/requests.php b/functions/requests.php index 6aef9fb..60c5fd4 100644 --- a/functions/requests.php +++ b/functions/requests.php @@ -64,4 +64,42 @@ function users_list_to_array($list) : array{ */ function toInt($input){ return floor($input*1); +} + +/** + * Remove HTML markup codes (<, >) + * + * @param String $input The string to change + * @return String The updated string + */ +function removeHTMLnodes($input){ + $output = str_replace("<", "<", $input); + return str_replace(">", ">", $output); +} + +/** + * Check a string before inserting it + * + * @param String $string The string to check + * @return Boolean True if the string is valid / false else + */ +function check_string_before_insert($string){ + + //First, empty string are invalid + if($string == "") + return false; + + //Remove HTML tags before continuing + $string = str_replace(array("<", ">"), "", $string); + + //Check string size + if(strlen($string)<5) + return false; + + //Check if the string has at least three different characters + if(strlen(count_chars($string,3)) < 3) + return false; + + //Success + return true; } \ No newline at end of file diff --git a/functions/strings.php b/functions/strings.php index 0f8e3c2..cd67998 100644 --- a/functions/strings.php +++ b/functions/strings.php @@ -1,6 +1,6 @@