ComunicAPI/classes/components/AccountImage.php

112 lines
3.0 KiB
PHP
Raw Normal View History

<?php
/**
* Account image class
*
* @author Pierre HUBERT
*/
2018-04-29 12:09:55 +00:00
class AccountImage{
2017-05-26 08:23:55 +00:00
/**
* @var String $accountImageURL URL path pointing on image accounts
*/
private $accountImageURL;
/**
* @var String $accountImagePath URL path pointing on image accounts
*/
private $accountImagePath;
/**
* @var String $defaultAccountImage name of the default account image
*/
private $defaultAccountImage = "0Reverse.png";
/**
* @var String $defaultAccountImage name of the error account image
*/
private $errorAccountImage = "0Red.png";
/**
* Public constructor
*/
public function __construct(){
2017-05-26 08:23:55 +00:00
//Set values
$this->accountImageURL = path_user_data(CS::get()->config->get("imageAccountPath"), false);
$this->accountImagePath = path_user_data(CS::get()->config->get("imageAccountPath"), true);
}
2017-05-26 07:50:20 +00:00
/**
* Returns the path of an account image
*
* @param Integer $userID The ID of the user on which we perform research
2017-05-26 08:23:55 +00:00
* @return String The URL pointing on the account image
2017-05-26 07:50:20 +00:00
*/
public function getPath($userID){
2017-05-26 08:23:55 +00:00
//First, check if the account image exists
$accountImageFileName = $this->accountImagePath."adresse_avatars/".$userID.".txt";
if(file_exists($accountImageFileName)){
//Get account image path
$accountImageFile = $this->accountImageURL.file_get_contents($accountImageFileName);
//Get account image visibility level
$visibilityLevel = $this->visibilityLevel($userID);
//If account image is open or if the user signed in is the user making the request
if($visibilityLevel == 3 || userID == $userID)
2017-05-26 08:23:55 +00:00
//Account image is OPEN
return $accountImageFile;
//If the user just requires user to be in
if($visibilityLevel == 2){
if(userID != 0)
return $accountImageFile;
else
return $this->accountImageURL.$this->errorAccountImage;
}
//Else users must be friends
if($visibilityLevel == 1){
//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 $accountImageFile;
else
return $this->accountImageURL.$this->errorAccountImage;
2017-05-26 08:23:55 +00:00
}
}
else
//Return default account image
return $this->accountImageURL.$this->defaultAccountImage;
}
/**
* Get visibilty level of the account image
*
* 1. The user and his friend ONLY
* 2. User, friends, and logged in users
* 3. Everybody
*
* @param Integer $userID The ID of the user on which we perform researchs
* @return Integer The visibility level of the account image
*/
private function visibilityLevel($userID){
$filePath = $this->accountImagePath."adresse_avatars/limit_view_".$userID.".txt";
//Check restriction file
if(!file_exists($filePath))
return 3; //Everybody by default
//Check for personnalized level
$fileContent = file_get_contents($filePath);
if($fileContent == 1 || $fileContent == 2){
//Return new visibility level
return $fileContent*1;
}
else
return 3; //Everybody by default
}
}
//Register class
2018-04-29 12:09:55 +00:00
Components::register("accountImage", new AccountImage());