2017-06-10 08:07:03 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Requests functions
|
|
|
|
*
|
|
|
|
* @author Pierre HUBERT
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check $_POST parametres associated to a request
|
|
|
|
*
|
|
|
|
* @param Array $varList The list of variables to check
|
|
|
|
* @return Boolean True or false depending of the success of the operation
|
|
|
|
*/
|
|
|
|
function check_post_parametres(array $varList){
|
|
|
|
|
|
|
|
//Check each fields
|
|
|
|
foreach($varList as $process){
|
|
|
|
|
|
|
|
//Check variable existence
|
|
|
|
if(!isset($_POST[$process]))
|
|
|
|
return false; //The variable does not exists
|
|
|
|
|
|
|
|
//Check variable content
|
|
|
|
if($_POST[$process] == "")
|
|
|
|
return false; //The variable is empty
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
//If we arrive there, it is a success
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-06-25 16:09:18 +00:00
|
|
|
* Convert a list of numbers (anything with IDs) comma-separated to an array
|
2017-06-10 08:07:03 +00:00
|
|
|
*
|
|
|
|
* @param String $list The input list
|
|
|
|
* @return Array The list of user / an empty list in case of errors
|
|
|
|
*/
|
2017-06-25 16:09:18 +00:00
|
|
|
function numbers_list_to_array($list) : array{
|
2017-06-10 08:07:03 +00:00
|
|
|
//Split the list into an array
|
|
|
|
$array = explode(",", $list);
|
|
|
|
$usersList = array();
|
|
|
|
|
|
|
|
foreach($array as $process){
|
|
|
|
|
|
|
|
//Check the entry is valid
|
2017-06-16 17:08:58 +00:00
|
|
|
if(toInt($process) < 1)
|
2017-06-18 08:07:52 +00:00
|
|
|
//return array();
|
|
|
|
continue; //Ignore entry
|
2017-06-10 08:07:03 +00:00
|
|
|
|
|
|
|
//Add the entry to the list
|
2017-06-16 17:08:58 +00:00
|
|
|
$usersList[toInt($process)] = toInt($process);
|
2017-06-10 08:07:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//Return the result
|
|
|
|
return $usersList;
|
2017-06-16 17:08:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Securely transform user given number (mixed) to integer (int)
|
|
|
|
*
|
|
|
|
* @param Mixed $input The input variable (mixed)
|
2017-12-29 17:03:41 +00:00
|
|
|
* @return int $output The output (safe integer)
|
2017-06-16 17:08:58 +00:00
|
|
|
*/
|
2017-12-29 17:03:41 +00:00
|
|
|
function toInt($input) : int{
|
2017-06-16 17:08:58 +00:00
|
|
|
return floor($input*1);
|
2017-06-23 17:00:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
2017-06-24 08:05:16 +00:00
|
|
|
if(strlen($string)<3)
|
2017-06-23 17:00:40 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
//Check if the string has at least three different characters
|
|
|
|
if(strlen(count_chars($string,3)) < 3)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
//Success
|
|
|
|
return true;
|
2017-12-10 10:38:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Make a string safe to be used to perform a query on a database
|
|
|
|
*
|
|
|
|
* @param string $input The string to process
|
|
|
|
* @return string The result string
|
|
|
|
*/
|
|
|
|
function safe_for_sql(string $input) : string {
|
|
|
|
|
|
|
|
//Perform safe adapation
|
|
|
|
$input = str_ireplace("\\", "\\\\", $input);
|
|
|
|
$input = str_ireplace("'", "\\'", $input);
|
|
|
|
$input = str_ireplace('"', "\\\"", $input);
|
|
|
|
|
|
|
|
return $input;
|
|
|
|
|
2017-12-24 15:48:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check a given user ID
|
|
|
|
*
|
|
|
|
* @param int $userID The user ID to check
|
|
|
|
* @return bool True if userID is valid, false else
|
|
|
|
*/
|
|
|
|
function check_user_id(int $userID) : bool {
|
|
|
|
|
|
|
|
if($userID < 1)
|
|
|
|
return false; //Invalid
|
|
|
|
|
|
|
|
return true; //Valid
|
2017-12-24 16:45:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get userID posted in a request and return it if there
|
|
|
|
* isn't any error
|
|
|
|
*
|
|
|
|
* @param string $name Optionnal, the name of the post field
|
|
|
|
* @return int User ID
|
|
|
|
* @throws RestError in case of error
|
|
|
|
*/
|
|
|
|
function getPostUserID(string $name = "userID") : int {
|
|
|
|
|
|
|
|
//Get userID post
|
|
|
|
if(!isset($_POST[$name]))
|
|
|
|
Rest_fatal_error(400, "Please specify a userID in '".$name."' !");
|
|
|
|
|
|
|
|
$userID = toInt($_POST[$name]);
|
|
|
|
|
|
|
|
//Check userID validity
|
|
|
|
if(!check_user_id($userID))
|
|
|
|
Rest_fatal_error(400, "Invalid userID in '".$name."' !");
|
|
|
|
|
|
|
|
//Check if user exits
|
|
|
|
if(!CS::get()->components->user->exists($userID))
|
|
|
|
Rest_fatal_error(404, "Specified user in '".$name."' not found !");
|
|
|
|
|
|
|
|
return $userID;
|
2017-12-29 17:03:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the ID of a conversation posted in a request and return
|
|
|
|
* if it is a valid ID
|
|
|
|
*
|
|
|
|
* @param string $name Optionnal, the name of the post field
|
|
|
|
* @return int $convID The ID of the conversation
|
|
|
|
*/
|
|
|
|
function getPostConversationID(string $name = "conversationID") : int {
|
|
|
|
|
|
|
|
//Get conversationID
|
|
|
|
if(!isset($_POST[$name]))
|
2018-01-04 12:59:48 +00:00
|
|
|
Rest_fatal_error(400, "Excepted conversation ID in '".$name."' !");
|
2017-12-29 17:03:41 +00:00
|
|
|
$conversationID = toInt($_POST[$name]);
|
|
|
|
|
|
|
|
//Check conversationID validity
|
|
|
|
if($conversationID < 1)
|
|
|
|
Rest_fatal_error(400, "Invalid conversation ID !");
|
|
|
|
|
|
|
|
//Check if conversation exists
|
|
|
|
if(!CS::get()->components->conversations->exist($conversationID))
|
|
|
|
Rest_fatal_error(404, "Specified conversation not found!");
|
|
|
|
|
|
|
|
return $conversationID;
|
|
|
|
|
2018-01-04 12:59:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the ID of a post in a rest request
|
|
|
|
*
|
|
|
|
* @param string $name Optionnal, the name of the post id field
|
|
|
|
* @return int $postID The ID of the post
|
|
|
|
*/
|
|
|
|
function getPostPostID(string $name = "postID") : int {
|
|
|
|
|
|
|
|
//Get postID
|
|
|
|
if(!isset($_POST[$name]))
|
|
|
|
Rest_fatal_error(400, "Excepted post ID in '".$name."' !");
|
|
|
|
$postID = toInt($_POST[$name]);
|
|
|
|
|
|
|
|
//Check post ID validity
|
|
|
|
if($postID < 1)
|
|
|
|
Rest_fatal_error(400, "Invalid post ID!");
|
|
|
|
|
|
|
|
//Check if the post exists
|
|
|
|
if(!CS::get()->components->posts->exist($postID))
|
|
|
|
Rest_fatal_error(404, "Specified post does not exists!");
|
|
|
|
|
|
|
|
return $postID;
|
2018-01-06 17:25:27 +00:00
|
|
|
}
|
|
|
|
|
2018-01-06 18:03:26 +00:00
|
|
|
/**
|
|
|
|
* Get the ID of a movie in a rest request
|
|
|
|
*
|
|
|
|
* @param string $name Optionnal, the name of the post ID field
|
|
|
|
* @return int $movieID The ID of the movie
|
|
|
|
*/
|
|
|
|
function getPostMovieId(string $name = "movieID") : int {
|
|
|
|
|
|
|
|
//Get movieID
|
|
|
|
if(!isset($_POST[$name]))
|
|
|
|
Rest_fatal_error(400, "Excepted movie ID in '".$name."' !");
|
|
|
|
$movieID = toInt($_POST[$name]);
|
|
|
|
|
|
|
|
//Check movie ID validity
|
|
|
|
if($movieID < 1)
|
|
|
|
Rest_fatal_error(400, "Invalid movie ID in '".$name."' !");
|
|
|
|
|
|
|
|
//Check if the movie exists
|
|
|
|
if(!CS::get()->components->movies->exist($movieID))
|
|
|
|
Rest_fatal_error(404, "Specified movie does not exists!");
|
|
|
|
|
|
|
|
return $movieID;
|
|
|
|
}
|
|
|
|
|
2018-01-06 17:25:27 +00:00
|
|
|
/**
|
|
|
|
* Check the validity of an file posted in a request
|
|
|
|
*
|
|
|
|
* @param string $name The name of the $_FILES entry
|
|
|
|
* @return bool True if the file is valid / false else
|
|
|
|
*/
|
|
|
|
function check_post_file(string $name) : bool {
|
|
|
|
|
|
|
|
//Check if image exists
|
|
|
|
if(!isset($_FILES[$name]))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
//Check for errors
|
|
|
|
if($_FILES[$name]['error'] != 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
//Check if the file is empty
|
|
|
|
if($_FILES[$name]['size'] < 1)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
2018-01-06 18:03:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check the validity of a Youtube video ID
|
|
|
|
*
|
|
|
|
* @param string $id The ID of the YouTube video
|
|
|
|
* @return bool True if the ID is valid / false else
|
|
|
|
*/
|
|
|
|
function check_youtube_id(string $id) : bool {
|
|
|
|
|
|
|
|
//Check length
|
|
|
|
if(strlen($id) < 5)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
//Check for illegal characters
|
|
|
|
if($id !== str_replace(array("/", "\\", "@", "&", "?", ".", "'", '"'), "", $id))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
//The video is considered as valid
|
|
|
|
return TRUE;
|
|
|
|
|
2017-06-10 08:07:03 +00:00
|
|
|
}
|