diff --git a/RestControllers/SettingsController.php b/RestControllers/SettingsController.php index dc4ebdc..6cc5f12 100644 --- a/RestControllers/SettingsController.php +++ b/RestControllers/SettingsController.php @@ -257,6 +257,32 @@ class SettingsController { return array("success" => "The account image has been deleted!"); } + /** + * Update the visibility of user account image + * + * @url POST /settings/set_account_image_visibility + */ + public function set_account_image_visibility(){ + + //Login required + user_login_required(); + + //Get the list of API visibility levels + $levels = array_flip(self::AccountImageVisibilityLevels); + $post_visibility = postString("visibility"); + + //Get visibility level + if(!isset($levels[$post_visibility])){ + Rest_fatal_error(401, "Unrecognized visibility level !"); + } + + //Save visibility level + components()->accountImage->setVisibilityLevel(userID, $levels[$post_visibility]); + + //Success + return array("success" => "The visibility level of the account image has been updated!"); + } + /** * Turn a GeneralSettings object into a valid API object * diff --git a/classes/components/AccountImage.php b/classes/components/AccountImage.php index ff46731..257db75 100644 --- a/classes/components/AccountImage.php +++ b/classes/components/AccountImage.php @@ -105,7 +105,7 @@ class AccountImage { * @return int The visibility level of the account image */ public function getVisibilityLevel(int $userID) : int { - $filePath = path_user_data(cs()->config->get(self::accountImageDirConfItem)."adresse_avatars/limit_view_".$userID.".txt", TRUE); + $filePath = $this->getPathVisibilityFile($userID); //Check restriction file if(!file_exists($filePath)) @@ -118,6 +118,31 @@ class AccountImage { return $fileContent; } + /** + * Set (update) the visibility level of a an account image + * + * @param int $userID Target user ID + * @param int $level New visibility level + */ + public function setVisibilityLevel(int $userID, int $level){ + + //Get the name of the file that contains visibility levels + $file = $this->getPathVisibilityFile($userID); + + //Check if the account image is publicy visible + if($level == AccountImageSettings::VISIBILITY_OPEN){ + + //We do not need visibility file + if(file_exists($file)) + unlink($file); + + } + + //Else append the new value to the file + file_put_contents($file, $level); + + } + /** * Get AccountImageSettings for an account * @@ -207,6 +232,17 @@ class AccountImage { private function getPathMetadataFile(int $userID) : string { return path_user_data(cs()->config->get(self::accountImageDirConfItem)."adresse_avatars/".$userID.".txt", TRUE); } + + /** + * Get the system path of the file that contains visibility level restrictions + * + * @param int $userID The ID of the target user + * @return string The sys path pointing on the file + */ + private function getPathVisibilityFile(int $userID) : string { + return path_user_data(cs()->config->get( + self::accountImageDirConfItem)."adresse_avatars/limit_view_".$userID.".txt", TRUE); + } } //Register class