From 916aa1c9a9d5c513bfac5c2107246ef936ab5a5e Mon Sep 17 00:00:00 2001 From: Michael Date: Mon, 9 May 2022 17:36:46 +0000 Subject: [PATCH 1/4] Inherit avatar cache file permissions --- src/Contact/Avatar.php | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/src/Contact/Avatar.php b/src/Contact/Avatar.php index 13a99f9106..8aa72bf9ba 100644 --- a/src/Contact/Avatar.php +++ b/src/Contact/Avatar.php @@ -109,18 +109,32 @@ class Avatar $filepath = DI::basePath() . $path; - $dirpath = dirname($filepath); + $dirpath = DI::basePath() . '/avatar/'; DI::profiler()->startRecording('file'); - if (!file_exists($dirpath)) { - mkdir($dirpath, 0775, true); - } else { - chmod($dirpath, 0775); + // Fetch the permission and group ownership of the "avatar" path and apply to all files + $dir_perm = fileperms($dirpath) & 0777; + $file_perm = fileperms($dirpath) & 0666; + $group = filegroup($dirpath); + + // Check directory permissions of all parts of the path + foreach (explode('/', dirname($filename)) as $part) { + $dirpath .= $part . '/'; + if (!file_exists($dirpath)) { + mkdir($dirpath, $dir_perm); + } elseif (fileperms($dirpath) & 0777 != $dir_perm) { + chmod($dirpath, $dir_perm); + } + + if (filegroup($dirpath) != $group) { + chgrp($dirpath, $group); + } } file_put_contents($filepath, $image->asString()); - chmod($filepath, 0664); + chmod($filepath, $file_perm); + chgrp($filepath, $group); DI::profiler()->stopRecording(); From 34030a736d5e0aa2195c0f472cf69f863c161d83 Mon Sep 17 00:00:00 2001 From: Michael Date: Mon, 9 May 2022 19:16:14 +0000 Subject: [PATCH 2/4] Use a constant for the avatar base path --- src/Contact/Avatar.php | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/Contact/Avatar.php b/src/Contact/Avatar.php index 8aa72bf9ba..6377560962 100644 --- a/src/Contact/Avatar.php +++ b/src/Contact/Avatar.php @@ -39,6 +39,8 @@ use Friendica\Util\Strings; */ class Avatar { + const BASE_PATH = '/avatar/'; + /** * Returns a field array with locally cached avatar pictures * @@ -105,11 +107,11 @@ class Avatar return ''; } - $path = '/avatar/' . $filename . $size . '.' . $image->getExt(); + $path = self::BASE_PATH . $filename . $size . '.' . $image->getExt(); $filepath = DI::basePath() . $path; - $dirpath = DI::basePath() . '/avatar/'; + $dirpath = DI::basePath() . self::BASE_PATH; DI::profiler()->startRecording('file'); @@ -169,13 +171,13 @@ class Avatar return ''; } - $path = Strings::normaliseLink(DI::baseUrl() . '/avatar'); + $path = Strings::normaliseLink(DI::baseUrl() . self::BASE_PATH); if (Network::getUrlMatch($path, $avatar) != $path) { return ''; } - $filename = str_replace($path, DI::basePath(). '/avatar/', Strings::normaliseLink($avatar)); + $filename = str_replace($path, DI::basePath(). self::BASE_PATH, Strings::normaliseLink($avatar)); DI::profiler()->startRecording('file'); $exists = file_exists($filename); From 548bf469cacf389e939f06eb80675c18d1352601 Mon Sep 17 00:00:00 2001 From: Michael Date: Mon, 9 May 2022 22:36:25 +0000 Subject: [PATCH 3/4] Added logging --- src/Contact/Avatar.php | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/src/Contact/Avatar.php b/src/Contact/Avatar.php index 6377560962..a69ccdff85 100644 --- a/src/Contact/Avatar.php +++ b/src/Contact/Avatar.php @@ -124,19 +124,33 @@ class Avatar foreach (explode('/', dirname($filename)) as $part) { $dirpath .= $part . '/'; if (!file_exists($dirpath)) { - mkdir($dirpath, $dir_perm); + if (!mkdir($dirpath, $dir_perm)) { + Logger::warning('Directory could not be created', ['directory' => $dirpath]); + } } elseif (fileperms($dirpath) & 0777 != $dir_perm) { - chmod($dirpath, $dir_perm); + if (!chmod($dirpath, $dir_perm)) { + Logger::warning('Directory permissions could not be changed', ['directory' => $dirpath]); + } } if (filegroup($dirpath) != $group) { - chgrp($dirpath, $group); + if (!chgrp($dirpath, $group)) { + Logger::warning('Directory group could not be changed', ['directory' => $dirpath]); + } } } - file_put_contents($filepath, $image->asString()); - chmod($filepath, $file_perm); - chgrp($filepath, $group); + if (!file_put_contents($filepath, $image->asString())) { + Logger::warning('File could not be created', ['file' => $filepath]); + } + + if (!chmod($filepath, $file_perm)) { + Logger::warning('File permissions could not be changed', ['file' => $filepath]); + } + + if (!chgrp($filepath, $group)) { + Logger::warning('File group could not be changed', ['file' => $filepath]); + } DI::profiler()->stopRecording(); From 22da88b43f770c69d78d059165b333459e7f88d7 Mon Sep 17 00:00:00 2001 From: Michael Date: Tue, 10 May 2022 01:14:27 +0000 Subject: [PATCH 4/4] Changed log level --- src/Contact/Avatar.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Contact/Avatar.php b/src/Contact/Avatar.php index a69ccdff85..9d06995ec5 100644 --- a/src/Contact/Avatar.php +++ b/src/Contact/Avatar.php @@ -129,13 +129,13 @@ class Avatar } } elseif (fileperms($dirpath) & 0777 != $dir_perm) { if (!chmod($dirpath, $dir_perm)) { - Logger::warning('Directory permissions could not be changed', ['directory' => $dirpath]); + Logger::info('Directory permissions could not be changed', ['directory' => $dirpath]); } } if (filegroup($dirpath) != $group) { if (!chgrp($dirpath, $group)) { - Logger::warning('Directory group could not be changed', ['directory' => $dirpath]); + Logger::info('Directory group could not be changed', ['directory' => $dirpath]); } } }