ComunicAPI/RestControllers/userController.php

208 lines
5.6 KiB
PHP
Raw Normal View History

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
* @url POST /user/getInfo
* @url POST /user/getInfoMultiple
2017-05-27 12:09:05 +00:00
* @return array The result
*/
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'])){
$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
$usersID = numbers_list_to_array($_POST['usersID']);
2017-05-28 12:09:20 +00:00
//Check for errors
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
//Check if it is a wide request or not
if(count($usersID) <= 10)
//Try to get user infos
$usersInfo = CS::get()->components->user->getMultipleUserInfos($usersID);
else {
//Divide request in multiples ones
$usersInfo = array();
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){
$usersInfo[$key] = $val;
}
}
}
2017-05-27 12:09:05 +00:00
//Check if response is empty
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
//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
return $usersInfo;
2017-05-27 12:09:05 +00:00
}
/**
* Get advanced user informations
*
* @url POST /user/getAdvancedUserInfos
* @url POST /user/getAdvancedUserInfo
*/
public function getAdvancedInfo(){
//Get the ID of the target user
$userID = getPostUserID("userID");
2017-12-17 11:30:05 +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 !");
//Get user informations
$userInfos = CS::get()->components->user->getUserAdvancedInfo($userID, true);
//Check if we got a response
if(!$userInfos->isValid())
Rest_fatal_error(500, "Couldn't get informations about the user !");
//Parse user information for the API
$data = $this->advancedUserToAPI($userInfos);
//Get the number of friends (if allowed)
if($userInfos->is_friendListPublic()){
$data['number_friends'] = CS::get()->components->friends->count_all($userID);
}
else
//User friends won't be displayed
$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
$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()){
$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
$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
}
//Return user informations
return $data;
}
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
2019-01-11 11:03:59 +00:00
update_last_user_activity_if_allowed();
2017-06-03 12:53:47 +00:00
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);
}
/**
* 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
*/
2018-05-13 18:15:11 +00:00
public static 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
*/
2018-05-13 18:15:11 +00:00
public static function advancedUserToAPI(AdvancedUser $user) : array {
2018-05-13 18:15:11 +00:00
$data = self::userToAPI($user);
$data['friend_list_public'] = $user->is_friendListPublic();
$data['personnalWebsite'] = $user->has_personnalWebsite() ? $user->get_personnalWebsite() : "";
$data['publicNote'] = $user->has_publicNote() ? $user->get_publicNote() : "";
$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
}