Compare commits

...

17 Commits

11 changed files with 282 additions and 12 deletions

View File

@ -34,6 +34,33 @@ use ReflectionObject;
use ReflectionMethod;
use DOMDocument;
////////////////////////////////////////////////////////////////////////////////
////////// THESE FUNCTIONS FIXE ENCODING ISSUES DUE TO PREVIOUS ENCODING ///////
/////////// CHOICES ////////////////////////////////////////////////////////////
//////////// THEY HAVE NOT BEEN TESTED ENOUGH, USE WITH CAUTION !!!!! //////////
////////////////////////////////////////////////////////////////////////////////
function do_fix_utf8($input){
if(\json_encode($input) == FALSE)
return utf8_encode($input);
return $input;
}
function check_utf8($input) {
if(is_array($input)) {
$out = array();
foreach($input as $key => $value)
$out[$key] = check_utf8($value);
return $out;
}
else
return mb_detect_encoding($input) == "UTF-8" ? do_fix_utf8($input) : $input;
}
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
/**
* Description of RestServer
*
@ -432,7 +459,13 @@ class RestServer
$options = JSON_PRETTY_PRINT;
}
$options = $options | JSON_UNESCAPED_UNICODE;
echo json_encode($data, $options);
// Return data
$output = json_encode($data, $options);
if($output === FALSE)
$output = json_encode(check_utf8($data), $options);
echo $output;
}
}

View File

@ -491,6 +491,29 @@ class PostsController {
}
/**
* Get the list of targets (pages) where the current user can create
* posts
*
* @url POST /posts/getAvailableTargets
*/
public function getAvailableTargets() {
user_login_required();
// Get the list of friends of the user where the user
// can create posts
$friends = components()->friends->getListThatAllowPostsFromUser(userID);
// Get the list of groups where the user can create posts
$groups = components()->groups->getListUserWhereCanCreatePosts(userID);
//Return result
return array(
"friends" => $friends,
"groups" => $groups
);
}
/**
* Get the visibility level specified in a POST request
*

View File

@ -0,0 +1,81 @@
<?php
/**
* Web application controller
*
* Methods specifically targetting the web application
*
* @author Pierre HUBERT
*/
class WebAppController {
// Kins of membership
const MEMBERSHIP_FRIEND = "friend";
const MEMBERSHIP_GROUP = "group";
public function __construction() {
user_login_required();
}
/**
* Get all the memberships of the user, sorted by last activity order
*
* @url POST /webApp/getMemberships
*/
public function getMemberships() {
// Get the list of friends of the user
$friends = components()->friends->getList(userID);
// Get the list of groups of the user
$groups = components()->groups->getListUser(userID);
// Get last activities of groups
$groups_activity = array();
foreach($groups as $group)
$groups_activity[components()->groups->getLastActivity($group)] = $group;
krsort($groups_activity);
$groups = array();
foreach($groups_activity as $activity => $id)
$groups[] = array("id" => $id, "activity" => $activity);
$out = array();
while(count($friends) != 0 || count($groups) != 0) {
if(count($friends) == 0)
$type = self::MEMBERSHIP_GROUP;
else if(count($groups) == 0)
$type = self::MEMBERSHIP_FRIEND;
else if($friends[0]->getLastActivityTime() > $groups[0]["activity"])
$type = self::MEMBERSHIP_FRIEND;
else
$type = self::MEMBERSHIP_GROUP;
// In case of friend
if($type == self::MEMBERSHIP_FRIEND){
$out[] = array(
"type" => $type,
"friend" => friendsController::parseFriendAPI(array_shift($friends))
);
}
// In case of group
else {
$info = array_shift($groups);
$out[] = array(
"type" => $type,
"id" => (int)$info["id"],
"last_activity" => $info["activity"]
);
}
}
return $out;
}
}

View File

@ -393,7 +393,7 @@ class accountController {
user_login_required();
check_post_password(userID, "password");
//Try to delet the account
//Try to delete the account
if(!components()->account->delete(userID))
Rest_fatal_error(500, "An error occurred while trying to delete your account!");

View File

@ -330,7 +330,7 @@ class friendsController{
//Update status
if(!components()->friends->set_can_post_texts(userID, $friendID, $can_post_texts))
Rest_fatal_error(500, "Coudl not update friendship status !");
Rest_fatal_error(500, "Could not update friendship status !");
//Success
return array("success" => "Updated authorization status !");

View File

@ -409,8 +409,12 @@ class AccountComponent {
*/
public function delete(int $userID) : bool {
/*//Delete all group memberships
if(!components()->groups->deleteAllUsersGroups($userID))
return FALSE;
//Delete user comments
/*if(!components()->comments->deleteAllUser($userID))
if(!components()->comments->deleteAllUser($userID))
return false;
//Delete user posts
@ -449,6 +453,10 @@ class AccountComponent {
if(!components()->accountImage->delete($userID))
return FALSE;
//Delete all the likes on the user page
if(!components()->likes->delete_all($userID, Likes::LIKE_USER))
return FALSE;
if(!components()->backgroundImage->delete($userID))
return FALSE;

View File

@ -115,7 +115,7 @@ class AccountImage {
$fileContent = file_get_contents($filePath);
//Return visibility level
return $fileContent;
return (int)$fileContent;
}
/**

View File

@ -91,6 +91,32 @@ class GroupsComponent {
return $info;
}
/**
* Get the list of groups of a user where the users can create
* posts
*
* @param int $userID The ID of the target user
* @return array The list of the groups the user can participate to
*/
public function getListUserWhereCanCreatePosts(int $userID) : array {
$list = db()->select(self::GROUPS_MEMBERS_TABLE." m, ".self::GROUPS_LIST_TABLE." g",
"WHERE user_id = ?
AND m.groups_id = g.id
AND (
level = ".GroupMember::ADMINISTRATOR." OR
level = ".GroupMember::MODERATOR." OR
(level = ".GroupMember::MEMBER." AND posts_level = ".GroupInfo::POSTS_LEVEL_ALL_MEMBERS.")
)
",
array($userID),
array("g.id"));
foreach($list as $num => $info)
$list[$num] = (int)$info["id"];
return $list;
}
/**
* Get the visibility level of a group
*
@ -172,6 +198,25 @@ class GroupsComponent {
return $this->dbToAdvancedGroupInfo($info[0], null, TRUE);
}
/**
* Get the timestamp of the estimated last activity on the group
*
* @param int $id The ID of the target group
* @return int The time of last activity on the group
*/
public function getLastActivity(int $id) : int {
// Query the database
$posts = components()->posts->getGroupPosts($id, true, 0, 1);
if(count($posts) == 0)
return 0;
else
return $posts[0]->get_time_sent();
}
/**
* Get a group settings
*
@ -521,6 +566,20 @@ class GroupsComponent {
== GroupMember::ADMINISTRATOR;
}
/**
* Check out whether a user is the last administrator of a group
* or not
*
* @param int $userID The ID of the user to check
* @param int $groupID The ID of the target group
* @return bool TRUE if the user is an admin and the last one of the group
* and FALSE else
*/
public function isLastAdmin(int $userID, int $groupID) : bool {
return $this->isAdmin($userID, $groupID)
&& $this->countMembersAtLevel($groupID, GroupMember::ADMINISTRATOR) === 1;
}
/**
* Check whether a group is open or not
*
@ -672,7 +731,7 @@ class GroupsComponent {
* @param int $groupID The ID of the target group
* @return bool TRUE if the directory is available / FALSE
*/
public function checkDirectoryAvailability(string $directory, int $groupID) : int {
public function checkDirectoryAvailability(string $directory, int $groupID) : bool {
$currID = $this->findByVirtualDirectory($directory);
//Check if the domain has not been allocated
@ -708,6 +767,10 @@ class GroupsComponent {
*/
public function delete_group(int $groupID) : bool {
// Delete all the likes of the group
if(!components()->likes->delete_all($groupID, Likes::LIKE_GROUP))
return FALSE;
//Delete group image
if(!$this->deleteLogo($groupID))
return FALSE;
@ -732,6 +795,36 @@ class GroupsComponent {
return TRUE;
}
/**
* Delete all the groups a user belongs to
*
* @param int $userID The ID of the target user
* @return bool TRUE in case of success / FALSE else
*/
public function deleteAllUsersGroups(int $userID) : bool {
//Get all user gropus
foreach($this->getListUser($userID) as $groupID){
//Get information about user membership to determine whether the group has to be
// deleted or not, to do so we check whether the user is the last administrator
// of the group or not
if($this->isLastAdmin($userID, $groupID)) {
if(!$this->delete_group($groupID))
return FALSE;
}
else
//Make the user leave the group
if(!$this->deleteMembershipWithStatus(
$userID, $groupID, $this->getMembershipLevel($userID, $groupID)))
return FALSE;
}
//Success
return TRUE;
}
/**
* Turn a database entry into a GroupInfo object
*

View File

@ -76,6 +76,28 @@ class friends {
}
/**
* Get the list of friends of a given user that allows him to
* create posts on their page
*
* @param $userID The ID of the target user
* @return array The list of friends of a user that allows him
* to create posts
*/
public function getListThatAllowPostsFromUser(int $userID) : array {
$list = db()->select(
$this->friendsTable,
"WHERE autoriser_post_page = 1 AND ID_amis = ?",
array($userID),
array("ID_personne")
);
foreach($list as $num=>$info)
$list[$num] = (int)$info["ID_personne"];
return $list;
}
/**
* Respond to a friendship request
*

View File

@ -21,7 +21,7 @@ class SearchResult {
* @param int $kind The kind of result (group, user...)
* @param int $kind_id The ID of the result
*/
public function SearchResult(int $kind, int $kind_id){
public function __construct(int $kind, int $kind_id){
$this->set_kind($kind);
$this->set_kind_id($kind_id);
}

View File

@ -130,6 +130,7 @@ CREATE TABLE `comunic_conversations_list` (
`name` varchar(50) DEFAULT NULL,
`last_active` int(11) DEFAULT NULL,
`creation_time` int(11) DEFAULT NULL,
`can_everyone_add_members` tinyint(4) DEFAULT '1',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
@ -522,10 +523,10 @@ CREATE TABLE `utilisateurs` (
`affiche_chat` int(11) NOT NULL DEFAULT '0',
`public` int(11) NOT NULL DEFAULT '0',
`pageouverte` int(11) NOT NULL DEFAULT '0',
`question1` varchar(255) DEFAULT NULL,
`reponse1` varchar(255) DEFAULT NULL,
`question2` varchar(255) DEFAULT NULL,
`reponse2` varchar(255) DEFAULT NULL,
`question1` varchar(255) CHARACTER SET utf8mb4 DEFAULT NULL,
`reponse1` varchar(255) CHARACTER SET utf8mb4 DEFAULT NULL,
`question2` varchar(255) CHARACTER SET utf8mb4 DEFAULT NULL,
`reponse2` varchar(255) CHARACTER SET utf8mb4 DEFAULT NULL,
`bloquecommentaire` int(11) NOT NULL DEFAULT '0',
`last_activity` int(11) NOT NULL DEFAULT '1',
`bloquenotification` int(11) NOT NULL DEFAULT '1',
@ -558,3 +559,12 @@ CREATE TABLE `utilisateurs` (
`lang` varchar(4) DEFAULT 'en',
PRIMARY KEY (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
DROP TABLE IF EXISTS `comunic_custom_emojis`;
CREATE TABLE `comunic_custom_emojis` (
`id` INT NOT NULL AUTO_INCREMENT,
`user_id` INT NULL,
`shortcut` VARCHAR(45) NULL,
`path` VARCHAR(255) NULL,
PRIMARY KEY (`id`));