]> git.mxchange.org Git - friendica.git/blobdiff - src/Contact/Avatar.php
Merge remote-tracking branch 'upstream/develop' into restricted-access
[friendica.git] / src / Contact / Avatar.php
index d63ebe5b6d6cb80f2eac8f039551886244c2eb58..fc4b7e38cba62279c37d05ca04877e5fa71f5163 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /**
- * @copyright Copyright (C) 2010-2022, the Friendica project
+ * @copyright Copyright (C) 2010-2023, the Friendica project
  *
  * @license GNU AGPL version 3 or any later version
  *
@@ -32,7 +32,6 @@ use Friendica\Util\HTTPSignature;
 use Friendica\Util\Images;
 use Friendica\Util\Network;
 use Friendica\Util\Proxy;
-use Friendica\Util\Strings;
 
 /**
  * functions for handling contact avatar caching
@@ -74,7 +73,12 @@ class Avatar
                        return $fields;
                }
 
-               $fetchResult = HTTPSignature::fetchRaw($avatar, 0, [HttpClientOptions::ACCEPT_CONTENT => [HttpClientAccept::IMAGE]]);
+               try {
+                       $fetchResult = HTTPSignature::fetchRaw($avatar, 0, [HttpClientOptions::ACCEPT_CONTENT => [HttpClientAccept::IMAGE]]);
+               } catch (\Exception $exception) {
+                       Logger::notice('Avatar is invalid', ['avatar' => $avatar, 'exception' => $exception]);
+                       return $fields;
+               }
 
                $img_str = $fetchResult->getBody();
                if (empty($img_str)) {
@@ -88,9 +92,11 @@ class Avatar
                        return $fields;
                }
 
-               $filename  = self::getFilename($contact['url']);
+               $filename  = self::getFilename($contact['url'], $avatar);
                $timestamp = time();
 
+               $fields['blurhash'] = $image->getBlurHash();
+
                $fields['photo'] = self::storeAvatarCache($image, $filename, Proxy::PIXEL_SMALL, $timestamp);
                $fields['thumb'] = self::storeAvatarCache($image, $filename, Proxy::PIXEL_THUMB, $timestamp);
                $fields['micro'] = self::storeAvatarCache($image, $filename, Proxy::PIXEL_MICRO, $timestamp);
@@ -114,7 +120,7 @@ class Avatar
                        return $fields;
                }
 
-               $filename  = self::getFilename($contact['url']);
+               $filename  = self::getFilename($contact['url'], $contact['avatar']);
                $timestamp = time();
 
                $fields['photo'] = self::storeAvatarCache($image, $filename, Proxy::PIXEL_SMALL, $timestamp);
@@ -124,9 +130,9 @@ class Avatar
                return $fields;
        }
 
-       private static function getFilename(string $url)
+       private static function getFilename(string $url, string $host): string
        {
-               $guid = Item::guidFromUri($url, parse_url($url, PHP_URL_HOST));
+               $guid = Item::guidFromUri($url, $host);
 
                return substr($guid, 0, 2) . '/' . substr($guid, 3, 2) . '/' . substr($guid, 5, 3) . '/' .
                        substr($guid, 9, 2) .'/' . substr($guid, 11, 2) . '/' . substr($guid, 13, 4). '/' . substr($guid, 18) . '-';
@@ -139,20 +145,18 @@ class Avatar
                        return '';
                }
 
-               $path = self::BASE_PATH . $filename . $size . '.' . $image->getExt();
+               $path = $filename . $size . '.' . $image->getExt();
 
-               $filepath = DI::basePath() . $path;
+               $basepath = self::basePath();
+               if (empty($basepath)) {
+                       return '';
+               }
 
-               $dirpath = DI::basePath() . self::BASE_PATH;
+               $filepath = $basepath . $path;
 
-               DI::profiler()->startRecording('file');
+               $dirpath = $basepath;
 
-               if (!file_exists($dirpath)) {
-                       if (!mkdir($dirpath, 0775)) {
-                               Logger::warning('Base directory could not be created', ['directory' => $dirpath]);
-                               return '';
-                       }
-               }
+               DI::profiler()->startRecording('file');
 
                // Fetch the permission and group ownership of the "avatar" path and apply to all files
                $dir_perm  = fileperms($dirpath) & 0777;
@@ -164,15 +168,15 @@ class Avatar
                        $dirpath .= $part . '/';
 
                        if (!file_exists($dirpath)) {
-                               if (!mkdir($dirpath, $dir_perm)) {
+                               if (!@mkdir($dirpath, $dir_perm) && !file_exists($dirpath)) {
                                        Logger::warning('Directory could not be created', ['directory' => $dirpath]);
                                }
                        } elseif ((($old_perm = fileperms($dirpath) & 0777) != $dir_perm) && !chmod($dirpath, $dir_perm)) {
-                               Logger::notice('Directory permissions could not be changed', ['directory' => $dirpath, 'old' => $old_perm, 'new' => $dir_perm]);
+                               Logger::warning('Directory permissions could not be changed', ['directory' => $dirpath, 'old' => $old_perm, 'new' => $dir_perm]);
                        }
 
                        if ((($old_group = filegroup($dirpath)) != $group) && !chgrp($dirpath, $group)) {
-                               Logger::notice('Directory group could not be changed', ['directory' => $dirpath, 'old' => $old_group, 'new' => $group]);
+                               Logger::warning('Directory group could not be changed', ['directory' => $dirpath, 'old' => $old_group, 'new' => $group]);
                        }
                }
 
@@ -184,11 +188,11 @@ class Avatar
                $old_group = filegroup($filepath);
 
                if (($old_perm != $file_perm) && !chmod($filepath, $file_perm)) {
-                       Logger::notice('File permissions could not be changed', ['file' => $filepath, 'old' => $old_perm, 'new' => $file_perm]);
+                       Logger::warning('File permissions could not be changed', ['file' => $filepath, 'old' => $old_perm, 'new' => $file_perm]);
                }
 
                if (($old_group != $group) && !chgrp($filepath, $group)) {
-                       Logger::notice('File group could not be changed', ['file' => $filepath, 'old' => $old_group, 'new' => $group]);
+                       Logger::warning('File group could not be changed', ['file' => $filepath, 'old' => $old_group, 'new' => $group]);
                }
 
                DI::profiler()->stopRecording();
@@ -198,7 +202,7 @@ class Avatar
                        return '';
                }
 
-               return DI::baseUrl() . $path . '?ts=' . $timestamp;
+               return self::baseUrl() . $path . '?ts=' . $timestamp;
        }
 
        /**
@@ -221,16 +225,17 @@ class Avatar
        private static function getCacheFile(string $avatar): string
        {
                $parts = parse_url($avatar);
-               if (empty($parts['host']) || ($parts['host'] != DI::baseUrl()->getHostname())) {
+               if (empty($parts['host']) || ($parts['host'] != parse_url(self::baseUrl(), PHP_URL_HOST))) {
                        return '';
                }
 
-               $pos = strpos($parts['path'], DI::baseUrl()->getUrlPath() . self::BASE_PATH);
+               $avatarpath = parse_url(self::baseUrl(), PHP_URL_PATH);
+               $pos = strpos($parts['path'], $avatarpath);
                if ($pos !== 0) {
                        return '';
                }
 
-               $filename = DI::basePath() . $parts['path'];
+               $filename = self::basePath() . substr($parts['path'], strlen($avatarpath));
 
                DI::profiler()->startRecording('file');
                $exists = file_exists($filename);
@@ -246,13 +251,16 @@ class Avatar
         * Delete locally cached avatar pictures of a contact
         *
         * @param string $avatar
-        * @return void
+        * @return bool
         */
-       public static function deleteCache(array $contact)
+       public static function deleteCache(array $contact): bool
        {
+               $existed = (self::isCacheFile($contact['photo']) || self::isCacheFile($contact['thumb']) || self::isCacheFile($contact['micro']));
                self::deleteCacheFile($contact['photo']);
                self::deleteCacheFile($contact['thumb']);
                self::deleteCacheFile($contact['micro']);
+
+               return $existed;
        }
 
        /**
@@ -265,8 +273,51 @@ class Avatar
        {
                $localFile = self::getCacheFile($avatar);
                if (!empty($localFile)) {
-                       unlink($localFile);
+                       @unlink($localFile);
                        Logger::debug('Unlink avatar', ['avatar' => $avatar]);
                }
        }
+
+       /**
+        * Fetch the avatar base path
+        *
+        * @return string
+        */
+       private static function basePath(): string
+       {
+               $basepath = DI::config()->get('system', 'avatar_cache_path');
+               if (empty($basepath)) {
+                       $basepath = DI::basePath() . self::BASE_PATH;
+               }
+               $basepath = rtrim($basepath, '/') . '/';
+
+               if (!file_exists($basepath)) {
+                       // We only automatically create the folder when it is in the web root
+                       if (strpos($basepath, DI::basePath()) !== 0) {
+                               Logger::warning('Base directory does not exist', ['directory' => $basepath]);
+                               return '';
+                       }
+                       if (!mkdir($basepath, 0775)) {
+                               Logger::warning('Base directory could not be created', ['directory' => $basepath]);
+                               return '';
+                       }
+               }
+
+               return $basepath;
+       }
+
+       /**
+        * Fetch the avatar base url
+        *
+        * @return string
+        */
+       private static function baseUrl(): string
+       {
+               $baseurl = DI::config()->get('system', 'avatar_cache_url');
+               if (!empty($baseurl)) {
+                       return rtrim($baseurl, '/') . '/';
+               }
+
+               return DI::baseUrl() . self::BASE_PATH;
+       }
 }