2017-05-17 12:43:12 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Main user controller file
|
|
|
|
*
|
|
|
|
* @author Pierre HUBERT
|
|
|
|
*/
|
|
|
|
|
|
|
|
//Enable access to exceptions handler
|
|
|
|
use \Jacwright\RestServer\RestException;
|
|
|
|
|
|
|
|
class userController
|
|
|
|
{
|
|
|
|
|
|
|
|
/**
|
2017-05-27 10:12:21 +00:00
|
|
|
* Get informations about a user
|
2017-05-17 12:43:12 +00:00
|
|
|
*
|
2017-05-27 10:12:21 +00:00
|
|
|
* @url POST /user/getInfos
|
2017-05-27 12:09:05 +00:00
|
|
|
* @url POST /user/getInfosMultiple
|
2018-04-15 12:26:26 +00:00
|
|
|
* @url POST /user/getInfo
|
|
|
|
* @url POST /user/getInfoMultiple
|
2017-05-27 12:09:05 +00:00
|
|
|
* @return array The result
|
|
|
|
*/
|
2018-04-15 12:26:26 +00:00
|
|
|
public function getUserInfo() : array{
|
2017-05-27 12:09:05 +00:00
|
|
|
|
|
|
|
//Determine userID
|
2017-05-28 12:09:20 +00:00
|
|
|
if(isset($_POST['userID'])){
|
2017-06-18 08:07:52 +00:00
|
|
|
$usersID = array(toInt($_POST['userID']));
|
2017-05-27 12:09:05 +00:00
|
|
|
}
|
2017-05-28 12:09:20 +00:00
|
|
|
elseif(isset($_POST['usersID'])){
|
|
|
|
//Generate users ID list
|
2017-06-25 16:09:18 +00:00
|
|
|
$usersID = numbers_list_to_array($_POST['usersID']);
|
2017-06-18 08:07:52 +00:00
|
|
|
|
2017-05-28 12:09:20 +00:00
|
|
|
//Check for errors
|
2017-06-18 08:07:52 +00:00
|
|
|
if(count($usersID) == 0)
|
2017-05-28 12:09:20 +00:00
|
|
|
Rest_fatal_error(400, "No user ID were specified!");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
//No ID specified
|
|
|
|
Rest_fatal_error(400, "Please specify at least one user ID !");
|
2017-05-27 13:11:17 +00:00
|
|
|
|
2017-07-01 08:49:07 +00:00
|
|
|
//Check if it is a wide request or not
|
|
|
|
if(count($usersID) <= 10)
|
|
|
|
//Try to get user infos
|
2018-04-15 12:39:17 +00:00
|
|
|
$usersInfo = CS::get()->components->user->getMultipleUserInfos($usersID);
|
2017-07-01 08:49:07 +00:00
|
|
|
else {
|
|
|
|
//Divide request in multiples ones
|
2018-04-15 12:39:17 +00:00
|
|
|
$usersInfo = array();
|
2017-07-01 08:49:07 +00:00
|
|
|
foreach(array_chunk($usersID, 10) as $process_users_ID){
|
|
|
|
|
|
|
|
//Get informations about the IDS
|
|
|
|
foreach(CS::get()->components->user->getMultipleUserInfos($process_users_ID) as $key=>$val){
|
2018-04-15 12:39:17 +00:00
|
|
|
$usersInfo[$key] = $val;
|
2017-07-01 08:49:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-05-27 12:09:05 +00:00
|
|
|
|
|
|
|
//Check if response is empty
|
2018-04-15 12:39:17 +00:00
|
|
|
if(count($usersInfo) == 0)
|
2017-05-28 12:09:20 +00:00
|
|
|
throw new RestException(401, "Couldn't get user data !");
|
2017-05-27 12:09:05 +00:00
|
|
|
|
2018-04-15 12:39:17 +00:00
|
|
|
//Parse User objects into API-readable objects
|
|
|
|
foreach($usersInfo as $num=>$info){
|
|
|
|
$usersInfo[$num] = $this->userToAPI($info);
|
|
|
|
}
|
|
|
|
|
2017-05-27 12:09:05 +00:00
|
|
|
//Return result
|
2018-04-15 12:39:17 +00:00
|
|
|
return $usersInfo;
|
2017-05-27 12:09:05 +00:00
|
|
|
}
|
|
|
|
|
2017-12-16 14:30:04 +00:00
|
|
|
/**
|
|
|
|
* Get advanced user informations
|
|
|
|
*
|
|
|
|
* @url POST /user/getAdvancedUserInfos
|
2018-04-15 12:26:26 +00:00
|
|
|
* @url POST /user/getAdvancedUserInfo
|
2017-12-16 14:30:04 +00:00
|
|
|
*/
|
2018-04-15 12:26:26 +00:00
|
|
|
public function getAdvancedInfo(){
|
2017-12-16 14:30:04 +00:00
|
|
|
|
|
|
|
//Get the ID of the target user
|
2017-12-25 08:21:54 +00:00
|
|
|
$userID = getPostUserID("userID");
|
2017-12-17 11:30:05 +00:00
|
|
|
|
2017-12-16 14:30:04 +00:00
|
|
|
//Check if the user is allowed to get advanced user infromations
|
|
|
|
if(!CS::get()->components->user->userAllowed(userID, $userID))
|
|
|
|
Rest_fatal_error(401, "You are not allowed to access these information !");
|
|
|
|
|
2017-12-16 15:26:42 +00:00
|
|
|
//Get user informations
|
2018-04-15 12:26:26 +00:00
|
|
|
$userInfos = CS::get()->components->user->getUserAdvancedInfo($userID, true);
|
2017-12-16 15:26:42 +00:00
|
|
|
|
|
|
|
//Check if we got a response
|
2018-04-15 12:26:26 +00:00
|
|
|
if(!$userInfos->isValid())
|
2017-12-16 15:26:42 +00:00
|
|
|
Rest_fatal_error(500, "Couldn't get informations about the user !");
|
|
|
|
|
2018-04-15 12:26:26 +00:00
|
|
|
//Parse user information for the API
|
|
|
|
$data = $this->advancedUserToAPI($userInfos);
|
2017-12-23 17:03:05 +00:00
|
|
|
|
|
|
|
//Get the number of friends (if allowed)
|
2018-04-15 12:26:26 +00:00
|
|
|
if($userInfos->is_friendListPublic()){
|
|
|
|
$data['number_friends'] = CS::get()->components->friends->count_all($userID);
|
2017-12-23 17:03:05 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
//User friends won't be displayed
|
2018-04-15 12:26:26 +00:00
|
|
|
$data["number_friends"] = 0;
|
2017-12-29 07:14:38 +00:00
|
|
|
|
2018-01-04 16:51:21 +00:00
|
|
|
//User can not post text on this page by default
|
2018-04-15 12:26:26 +00:00
|
|
|
$data["can_post_texts"] = FALSE;
|
2018-01-04 16:51:21 +00:00
|
|
|
|
2017-12-29 07:14:38 +00:00
|
|
|
//Get some informations only is user is signed in
|
|
|
|
if(user_signed_in()){
|
2018-04-15 12:26:26 +00:00
|
|
|
$data["user_like_page"] = CS::get()->components->likes->is_liking(userID, $userID, Likes::LIKE_USER);
|
2018-01-04 16:51:21 +00:00
|
|
|
|
|
|
|
//Check if the user can post texts on this page
|
2018-04-15 12:26:26 +00:00
|
|
|
$data["can_post_texts"] =
|
2018-01-04 16:51:21 +00:00
|
|
|
//If it is his page, yes by default
|
|
|
|
userID == $userID ? TRUE :
|
|
|
|
//Else check friendship status
|
2018-01-06 17:26:02 +00:00
|
|
|
CS::get()->components->user->canCreatePosts(userID, $userID);
|
2017-12-29 07:14:38 +00:00
|
|
|
}
|
2017-12-23 17:03:05 +00:00
|
|
|
|
2017-12-16 15:26:42 +00:00
|
|
|
//Return user informations
|
2018-04-15 12:26:26 +00:00
|
|
|
return $data;
|
2017-12-16 14:30:04 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-05-17 12:43:12 +00:00
|
|
|
/**
|
|
|
|
* Get current user infos using tokens
|
|
|
|
*
|
|
|
|
* @url POST /user/getCurrentUserID
|
|
|
|
*/
|
2017-05-27 10:12:21 +00:00
|
|
|
public function getCurrentUserID(){
|
2017-05-19 16:07:52 +00:00
|
|
|
user_login_required();
|
2017-05-17 12:43:12 +00:00
|
|
|
|
2017-06-03 12:53:47 +00:00
|
|
|
//Update last user activity
|
|
|
|
CS::get()->components->user->updateLastActivity(userID);
|
|
|
|
|
2017-05-19 16:07:52 +00:00
|
|
|
//Return userID
|
|
|
|
return array("userID" => userID);
|
2017-05-17 12:43:12 +00:00
|
|
|
}
|
2017-12-10 10:38:23 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Find user ID by a specified folder name
|
|
|
|
*
|
|
|
|
* @url POST /user/findbyfolder
|
|
|
|
*/
|
|
|
|
public function findUserByFolder(){
|
|
|
|
|
|
|
|
//Check for domain name
|
|
|
|
if(!isset($_POST['subfolder']))
|
|
|
|
Rest_fatal_error(400, "No subfolder specified!");
|
|
|
|
|
|
|
|
$input = safe_for_sql($_POST['subfolder']);
|
|
|
|
|
|
|
|
if(!check_string_before_insert($input))
|
|
|
|
Rest_fatal_error(401, "The request was cancelled because the query is unsafe !");
|
|
|
|
|
|
|
|
//Search user ID in the database
|
|
|
|
$id = CS::get()->components->user->findByFolder($input);
|
|
|
|
|
|
|
|
//Check for error
|
|
|
|
if($id === 0)
|
|
|
|
Rest_fatal_error(404, "No user was found with the specifed subfolder!");
|
|
|
|
|
|
|
|
//Return result
|
|
|
|
return array("userID" => $id);
|
|
|
|
|
|
|
|
}
|
2018-04-15 12:26:26 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Turn a User object into an API array
|
|
|
|
*
|
|
|
|
* @param User $user Information about the user
|
|
|
|
* @return array Information about the user compatible with the API
|
|
|
|
*/
|
|
|
|
public function userToAPI(User $user) : array {
|
|
|
|
|
|
|
|
$data = array();
|
|
|
|
|
|
|
|
$data['userID'] = $user->get_id();
|
|
|
|
$data['firstName'] = $user->get_firstName();
|
|
|
|
$data['lastName'] = $user->get_lastName();
|
|
|
|
$data['publicPage'] = $user->is_publicPage() ? "true" : "false";
|
|
|
|
$data['openPage'] = $user->is_openPage() ? "true" : "false";
|
|
|
|
$data['virtualDirectory'] = $user->has_virtualDirectory() ? $user->get_virtualDirectory() : "";
|
|
|
|
$data['accountImage'] = $user->get_accountImageURL();
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Turn an AdvancedUser object into an API array
|
|
|
|
*
|
|
|
|
* @param AdvancedUser $user Information about the user
|
|
|
|
* @return array Data compatible with the API
|
|
|
|
*/
|
|
|
|
private function advancedUserToAPI(AdvancedUser $user) : array {
|
|
|
|
|
|
|
|
$data = $this->userToAPI($user);
|
|
|
|
|
|
|
|
$data['friend_list_public'] = $user->is_friendListPublic();
|
|
|
|
$data['personnalWebsite'] = $user->has_personnalWebsite() ? $user->get_personnalWebsite() : "";
|
2018-05-05 09:17:48 +00:00
|
|
|
$data['publicNote'] = $user->has_publicNote() ? $user->get_publicNote() : "";
|
2018-04-15 12:26:26 +00:00
|
|
|
$data['noCommentOnHisPage'] = $user->is_disallowComments();
|
|
|
|
$data['allowPostFromFriendOnHisPage'] = $user->is_allowPostFromFriends();
|
|
|
|
$data['account_creation_time'] = $user->get_creation_time();
|
|
|
|
$data['backgroundImage'] = $user->get_backgroundImage();
|
|
|
|
$data['pageLikes'] = $user->get_pageLikes();
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
}
|
2017-05-17 12:43:12 +00:00
|
|
|
}
|