use Friendica\Util\Images;
use Friendica\Util\Network;
use Friendica\Util\Proxy;
-use Friendica\Util\Strings;
/**
* functions for handling contact avatar caching
return $fields;
}
- private static function getFilename(string $url)
+ private static function getFilename(string $url): string
{
$guid = Item::guidFromUri($url, parse_url($url, PHP_URL_HOST));
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;
+
+ $dirpath = $basepath;
DI::profiler()->startRecording('file');
return '';
}
- return DI::baseUrl() . $path . '?ts=' . $timestamp;
+ return self::baseUrl() . $path . '?ts=' . $timestamp;
}
/**
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);
{
$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;
+ }
}