ComunicAPI/classes/components/AccountImage.php

213 lines
5.7 KiB
PHP
Raw Normal View History

<?php
/**
* Account image class
*
* @author Pierre HUBERT
*/
2018-04-29 12:31:52 +00:00
class AccountImage {
2017-05-26 08:23:55 +00:00
/**
2018-04-29 12:31:52 +00:00
* @var string accountImageDirConfItem The name of the configuration item that contains the path
* to the images
2017-05-26 08:23:55 +00:00
*/
2018-04-29 12:31:52 +00:00
const accountImageDirConfItem = "imageAccountPath";
2017-05-26 08:23:55 +00:00
/**
2018-04-29 12:31:52 +00:00
* @var string defaultAccountImage name of the default account image
2017-05-26 08:23:55 +00:00
*/
2018-04-29 12:31:52 +00:00
const defaultAccountImage = "0Reverse.png";
2017-05-26 08:23:55 +00:00
/**
2018-04-29 12:31:52 +00:00
* @var string defaultAccountImage name of the error account image
2017-05-26 08:23:55 +00:00
*/
2018-04-29 12:31:52 +00:00
const errorAccountImage = "0Red.png";
2017-05-26 08:23:55 +00:00
/**
2018-05-01 07:33:52 +00:00
* Returns the file name of an account image
2017-05-26 07:50:20 +00:00
*
2018-04-29 12:31:52 +00:00
* @param int $userID The ID of the user on which we perform research
* @return string The URL pointing on the account image
2017-05-26 07:50:20 +00:00
*/
2018-04-29 12:31:52 +00:00
public function getFile(int $userID) : string {
2017-05-26 08:23:55 +00:00
//First, check if the account image exists
2018-04-29 12:31:52 +00:00
$accountImageFileName = $this->getFileAccountImage($userID);
if($accountImageFileName != ""){
2017-05-26 08:23:55 +00:00
//Get account image visibility level
$visibilityLevel = $this->getVisibilityLevel($userID);
2017-05-26 08:23:55 +00:00
//If account image is open or if the user signed in is the user making the request
2018-04-29 12:31:52 +00:00
if($visibilityLevel == AccountImageSettings::VISIBILITY_OPEN || userID == $userID)
2017-05-26 08:23:55 +00:00
//Account image is OPEN
2018-04-29 12:31:52 +00:00
return $accountImageFileName;
2017-05-26 08:23:55 +00:00
//If the user just requires user to be in
2018-04-29 12:31:52 +00:00
if($visibilityLevel == AccountImageSettings::VISIBILITY_PUBLIC){
2017-05-26 08:23:55 +00:00
if(userID != 0)
2018-04-29 12:31:52 +00:00
return $accountImageFileName;
2017-05-26 08:23:55 +00:00
else
2018-04-29 12:31:52 +00:00
return self::errorAccountImage;
2017-05-26 08:23:55 +00:00
}
//Else users must be friends
2018-04-29 12:31:52 +00:00
//Check the two persons are friend or not
if(CS::get()->components->friends->are_friend($userID, userID))
//User is allowed to access the image
return $accountImageFileName;
else
return self::errorAccountImage;
2017-05-26 08:23:55 +00:00
}
else
//Return default account image
2018-04-29 12:31:52 +00:00
return self::defaultAccountImage;
}
/**
* Returns the path to an account image
*
* @param int $userID Target user ID
* @return string $url The URL of the account image
*/
public function getPath(int $userID) : string {
return cs()->config->get(self::accountImageDirConfItem).$this->getFile($userID);
}
2018-04-29 12:31:52 +00:00
/**
* Returns the URL to an account image
*
* @param int $userID Target user ID
* @return string $url The URL of the account image
*/
public function getURL(int $userID) : string {
return path_user_data($this->getPath($userID));
}
/**
* Check whether a user has an account image or not
*
* @param int $userID Target user ID
* @return bool TRUE if the user has an account image / FALSE else
*/
public function has(int $userID) : bool {
return $this->getFileAccountImage($userID) != "";
2017-05-26 08:23:55 +00:00
}
/**
* Get visibilty level of the account image
*
* 1. The user and his friend ONLY
* 2. User, friends, and logged in users
* 3. Everybody
*
2018-04-29 12:31:52 +00:00
* @param int $userID The ID of the user on which we perform researchs
* @return int The visibility level of the account image
2017-05-26 08:23:55 +00:00
*/
public function getVisibilityLevel(int $userID) : int {
2018-04-29 12:31:52 +00:00
$filePath = path_user_data(cs()->config->get(self::accountImageDirConfItem)."adresse_avatars/limit_view_".$userID.".txt", TRUE);
2017-05-26 08:23:55 +00:00
//Check restriction file
if(!file_exists($filePath))
2018-04-29 12:31:52 +00:00
return AccountImageSettings::VISIBILITY_OPEN; //Everybody by default
2017-05-26 08:23:55 +00:00
//Check for personnalized level
$fileContent = file_get_contents($filePath);
2018-04-29 12:31:52 +00:00
//Return visibility level
return $fileContent;
}
/**
* Get AccountImageSettings for an account
*
* @param int $userID ID of the target user
* @return AccountImageSettings Generated account settings object
*/
public function getSettings(int $userID) : AccountImageSettings {
//Create and fill UserAccountImage object
$settings = new AccountImageSettings();
//Add user account image (if any)
if($this->has(userID)){
$settings->set_image_path($this->getPath(userID));
}
$settings->set_visibility_level($this->getVisibilityLevel(userID));
return $settings;
}
2018-05-01 07:33:52 +00:00
/**
* Update the account image of a user
*
* @param int $userID The ID of the target user
* @param string $fileName The name of the new account image
* @return bool TRUE for a success / FALSE else
*/
public function update(int $userID, string $fileName) : bool {
//First, we must delete previous account image (if any)
if($this->has($userID)){
if(!$this->delete($userID))
return FALSE;
}
//Save image file name into metadata file
return (bool) file_put_contents($this->getPathMetadataFile($userID), $fileName);
}
/**
* Delete user account image
*
* @param int $userID The ID of the target user
* @return bool TRUE for a success / FALSE else
*/
public function delete(int $userID) : bool {
$path = $this->getFileAccountImage($userID);
if($path != ""){
$syspath = path_user_data($this->getPath($userID), TRUE);
if(file_exists($syspath))
unlink($syspath);
//Delete metadata file
unlink($this->getPathMetadataFile($userID));
}
//Success by default
return TRUE;
}
2018-04-29 12:31:52 +00:00
/**
* Get the file to the user account image, if the user has any
*
* @param int $userID Target user ID
* @return string The path to the user account image, empty string else
*/
private function getFileAccountImage(int $userID) : string {
2018-05-01 07:33:52 +00:00
$fileName = $this->getPathMetadataFile($userID);
2018-04-29 12:31:52 +00:00
if(file_exists($fileName))
return file_get_contents($fileName);
2017-05-26 08:23:55 +00:00
else
2018-04-29 12:31:52 +00:00
return "";
}
2018-05-01 07:33:52 +00:00
/**
* Get the system path of the file that contains the name of the account
* image file
*
* @param int $userID The ID of the target user
* @return string The sys path pointing of the file that contains user account
* image
*/
private function getPathMetadataFile(int $userID) : string {
return path_user_data(cs()->config->get(self::accountImageDirConfItem)."adresse_avatars/".$userID.".txt", TRUE);
}
}
//Register class
2018-04-29 12:09:55 +00:00
Components::register("accountImage", new AccountImage());