From b79bb0d2cb3e6da43d7e6296f5afa29987be88e4 Mon Sep 17 00:00:00 2001
From: Michael <heluecht@pirati.ca>
Date: Tue, 28 Jul 2020 12:58:19 +0000
Subject: [PATCH 1/2] Fetch photo fields, ensuring that they are filled

---
 mod/common.php                   |  4 +-
 mod/match.php                    |  3 +-
 mod/network.php                  |  6 +--
 mod/ping.php                     |  7 +---
 src/Model/Contact.php            | 69 +++++++++++++++++++++++++++++++-
 src/Module/AllFriends.php        |  3 +-
 src/Module/Contact.php           |  6 +--
 src/Module/Contact/Hovercard.php |  4 +-
 src/Module/Directory.php         |  3 +-
 src/Module/Profile/Contacts.php  |  3 +-
 src/Module/Search/Acl.php        |  4 +-
 src/Util/Proxy.php               |  2 +-
 12 files changed, 82 insertions(+), 32 deletions(-)

diff --git a/mod/common.php b/mod/common.php
index 0ff523b3f7..b59b36ee7d 100644
--- a/mod/common.php
+++ b/mod/common.php
@@ -26,8 +26,8 @@ use Friendica\Core\Renderer;
 use Friendica\Database\DBA;
 use Friendica\DI;
 use Friendica\Model;
+use Friendica\Model\Contact;
 use Friendica\Module;
-use Friendica\Util\Proxy as ProxyUtils;
 use Friendica\Util\Strings;
 
 function common_content(App $a)
@@ -136,7 +136,7 @@ function common_content(App $a)
 			'url'          => Model\Contact::magicLink($common_friend['url']),
 			'itemurl'      => ($contact_details['addr'] ?? '') ?: $common_friend['url'],
 			'name'         => $contact_details['name'],
-			'thumb'        => ProxyUtils::proxifyUrl($contact_details['thumb'], false, ProxyUtils::SIZE_THUMB),
+			'thumb'        => Contact::getThumb($contact_details),
 			'img_hover'    => $contact_details['name'],
 			'details'      => $contact_details['location'],
 			'tags'         => $contact_details['keywords'],
diff --git a/mod/match.php b/mod/match.php
index 3b24c4097a..f50a454ba9 100644
--- a/mod/match.php
+++ b/mod/match.php
@@ -27,7 +27,6 @@ use Friendica\Database\DBA;
 use Friendica\DI;
 use Friendica\Model\Contact;
 use Friendica\Model\Profile;
-use Friendica\Util\Proxy as ProxyUtils;
 
 /**
  * Controller for /match.
@@ -111,7 +110,7 @@ function match_content(App $a)
 				'tags'         => $contact_details['keywords'] ?? '',
 				'about'        => $contact_details['about'] ?? '',
 				'account_type' => Contact::getAccountType($contact_details),
-				'thumb'        => ProxyUtils::proxifyUrl($profile->photo, false, ProxyUtils::SIZE_THUMB),
+				'thumb'        => Contact::getThumb($contact_details, $profile->photo),
 				'conntxt'      => DI::l10n()->t('Connect'),
 				'connlnk'      => $connlnk,
 				'img_hover'    => $profile->tags,
diff --git a/mod/network.php b/mod/network.php
index a3eb169839..d82072754c 100644
--- a/mod/network.php
+++ b/mod/network.php
@@ -20,7 +20,6 @@
  */
 
 use Friendica\App;
-use Friendica\Content\Feature;
 use Friendica\Content\ForumManager;
 use Friendica\Content\Nav;
 use Friendica\Content\Pager;
@@ -29,9 +28,7 @@ use Friendica\Content\Text\HTML;
 use Friendica\Core\ACL;
 use Friendica\Core\Hook;
 use Friendica\Core\Logger;
-use Friendica\Core\Protocol;
 use Friendica\Core\Renderer;
-use Friendica\Core\Session;
 use Friendica\Database\DBA;
 use Friendica\DI;
 use Friendica\Model\Contact;
@@ -41,7 +38,6 @@ use Friendica\Model\Post\Category;
 use Friendica\Model\Profile;
 use Friendica\Module\Security\Login;
 use Friendica\Util\DateTimeFormat;
-use Friendica\Util\Proxy as ProxyUtils;
 use Friendica\Util\Strings;
 
 function network_init(App $a)
@@ -587,7 +583,7 @@ function networkThreadedView(App $a, $update, $parent)
 				'id' => 'network',
 				'name' => $contact['name'],
 				'itemurl' => ($contact['addr'] ?? '') ?: $contact['nurl'],
-				'thumb' => ProxyUtils::proxifyUrl($contact['thumb'], false, ProxyUtils::SIZE_THUMB),
+				'thumb' => Contact::getThumb($contact),
 				'details' => $contact['location'],
 			];
 
diff --git a/mod/ping.php b/mod/ping.php
index 848f2f0ecf..c6e32a9314 100644
--- a/mod/ping.php
+++ b/mod/ping.php
@@ -34,7 +34,6 @@ use Friendica\Model\Verb;
 use Friendica\Protocol\Activity;
 use Friendica\Util\DateTimeFormat;
 use Friendica\Util\Temporal;
-use Friendica\Util\Proxy as ProxyUtils;
 use Friendica\Util\XML;
 
 /**
@@ -329,11 +328,7 @@ function ping_init(App $a)
 		if (DBA::isResult($notifs)) {
 			foreach ($notifs as $notif) {
 				$contact = Contact::getByURL($notif['url'], false, ['micro']);
-				if (isset($contact['micro'])) {
-					$notif['photo'] = ProxyUtils::proxifyUrl($contact['micro'], false, ProxyUtils::SIZE_MICRO);
-				} else {
-					$notif['photo'] = ProxyUtils::proxifyUrl($notif['photo'], false, ProxyUtils::SIZE_MICRO);
-				}
+				$notif['photo'] = Contact::getMicro($contact, $notif['photo']);
 
 				$local_time = DateTimeFormat::local($notif['date']);
 
diff --git a/src/Model/Contact.php b/src/Model/Contact.php
index 9e89fc30a5..bb87640542 100644
--- a/src/Model/Contact.php
+++ b/src/Model/Contact.php
@@ -43,6 +43,7 @@ use Friendica\Protocol\Salmon;
 use Friendica\Util\DateTimeFormat;
 use Friendica\Util\Images;
 use Friendica\Util\Network;
+use Friendica\Util\Proxy;
 use Friendica\Util\Strings;
 
 /**
@@ -1787,13 +1788,79 @@ class Contact
 		self::updateAvatar($cid, $contact['avatar'], true);
 	}
 
+	/**
+	 * Return the photo path for a given contact array in the given size
+	 *
+	 * @param array $contact  contact array
+	 * @param string $field   Fieldname of the photo in the contact array
+	 * @param string $default Default path when no picture had been found
+	 * @param string $size    Size of the avatar picture
+	 * @param string $avatar  Avatar path that is displayed when no photo had been found
+	 * @return string photo path
+	 */
+	private static function getAvatarPath(array $contact, string $field, string $default, string $size, string $avatar)
+	{
+		if (!empty($contact)) {
+			$contact = self::checkAvatarCacheByArray($contact);
+			if (!empty($contact[$field])) {
+				$avatar = $contact[$field];
+			}
+		}
+
+		if (empty($avatar)) {
+			return $default;
+		}
+
+		if (Proxy::isLocalImage($avatar)) {
+			return $avatar;
+		} else {
+			return Proxy::proxifyUrl($avatar, false, $size);
+		}
+	}
+
+	/**
+	 * Return the photo path for a given contact array
+	 *
+	 * @param array $contact Contact array
+	 * @param string $avatar  Avatar path that is displayed when no photo had been found
+	 * @return string photo path
+	 */
+	public static function getPhoto(array $contact, string $avatar = '')
+	{
+		return self::getAvatarPath($contact, 'photo', DI::baseUrl() . '/images/person-300.jpg', Proxy::SIZE_SMALL, $avatar);
+	}
+
+	/**
+	 * Return the photo path (thumb size) for a given contact array
+	 *
+	 * @param array $contact Contact array
+	 * @param string $avatar  Avatar path that is displayed when no photo had been found
+	 * @return string photo path
+	 */
+	public static function getThumb(array $contact, string $avatar = '')
+	{
+		return self::getAvatarPath($contact, 'thumb', DI::baseUrl() . '/images/person-80.jpg', Proxy::SIZE_THUMB, $avatar);
+	}
+
+	/**
+	 * Return the photo path (micro size) for a given contact array
+	 *
+	 * @param array $contact Contact array
+	 * @param string $avatar  Avatar path that is displayed when no photo had been found
+	 * @return string photo path
+	 */
+	public static function getMicro(array $contact, string $avatar = '')
+	{
+		return self::getAvatarPath($contact, 'micro', DI::baseUrl() . '/images/person-48.jpg', Proxy::SIZE_MICRO, $avatar);
+	}
+
 	/**
 	 * Check the given contact array for avatar cache fields
 	 *
 	 * @param array $contact
 	 * @return array contact array with avatar cache fields
 	 */
-	public static function checkAvatarCacheByArray(array $contact)
+	private static function checkAvatarCacheByArray(array $contact)
 	{
 		$update = false;
 		$contact_fields = [];
diff --git a/src/Module/AllFriends.php b/src/Module/AllFriends.php
index 0a95256172..1c254b2094 100644
--- a/src/Module/AllFriends.php
+++ b/src/Module/AllFriends.php
@@ -28,7 +28,6 @@ use Friendica\Core\Renderer;
 use Friendica\DI;
 use Friendica\Model;
 use Friendica\Network\HTTPException;
-use Friendica\Util\Proxy as ProxyUtils;
 
 /**
  * This module shows all public friends of the selected contact
@@ -99,7 +98,7 @@ class AllFriends extends BaseModule
 				'url'          => Model\Contact::magicLinkbyId($friend['id'], $friend['url']),
 				'itemurl'      => ($contactDetails['addr'] ?? '') ?: $friend['url'],
 				'name'         => $contactDetails['name'],
-				'thumb'        => ProxyUtils::proxifyUrl($contactDetails['thumb'], false, ProxyUtils::SIZE_THUMB),
+				'thumb'        => Model\Contact::getThumb($contactDetails),
 				'img_hover'    => $contactDetails['name'],
 				'details'      => $contactDetails['location'],
 				'tags'         => $contactDetails['keywords'],
diff --git a/src/Module/Contact.php b/src/Module/Contact.php
index 5a4d0b1e86..ed6fe728a1 100644
--- a/src/Module/Contact.php
+++ b/src/Module/Contact.php
@@ -279,8 +279,6 @@ class Contact extends BaseModule
 			if ($contact['network'] == Protocol::PHANTOM) {
 				$contact = false;
 			}
-
-			$contact = ModelContact::checkAvatarCacheByArray($contact);
 		}
 
 		if (DBA::isResult($contact)) {
@@ -318,7 +316,7 @@ class Contact extends BaseModule
 
 			$vcard_widget = Renderer::replaceMacros(Renderer::getMarkupTemplate('widget/vcard.tpl'), [
 				'$name'         => $contact['name'],
-				'$photo'        => $contact['photo'],
+				'$photo'        => Model\Contact::getPhoto($contact),
 				'$url'          => Model\Contact::magicLinkByContact($contact, $contact['url']),
 				'$addr'         => $contact['addr'] ?? '',
 				'$network_link' => $network_link,
@@ -614,7 +612,7 @@ class Contact extends BaseModule
 				'$notify'         => ['notify', DI::l10n()->t('Notification for new posts'), ($contact['notify_new_posts'] == 1), DI::l10n()->t('Send a notification of every new post of this contact')],
 				'$fetch_further_information' => $fetch_further_information,
 				'$ffi_keyword_denylist' => ['ffi_keyword_denylist', DI::l10n()->t('Keyword Deny List'), $contact['ffi_keyword_denylist'], DI::l10n()->t('Comma separated list of keywords that should not be converted to hashtags, when "Fetch information and keywords" is selected')],
-				'$photo'          => $contact['photo'],
+				'$photo'          => Model\Contact::getPhoto($contact),
 				'$name'           => $contact['name'],
 				'$dir_icon'       => $dir_icon,
 				'$sparkle'        => $sparkle,
diff --git a/src/Module/Contact/Hovercard.php b/src/Module/Contact/Hovercard.php
index 750b856bc3..1d2fe85676 100644
--- a/src/Module/Contact/Hovercard.php
+++ b/src/Module/Contact/Hovercard.php
@@ -27,10 +27,8 @@ use Friendica\Core\Session;
 use Friendica\Database\DBA;
 use Friendica\DI;
 use Friendica\Model\Contact;
-use Friendica\Model\GContact;
 use Friendica\Network\HTTPException;
 use Friendica\Util\Strings;
-use Friendica\Util\Proxy;
 
 /**
  * Asynchronous HTML fragment provider for frio contact hovercards
@@ -88,7 +86,7 @@ class Hovercard extends BaseModule
 				'name'         => $contact['name'],
 				'nick'         => $contact['nick'],
 				'addr'         => $contact['addr'] ?: $contact['url'],
-				'thumb'        => Proxy::proxifyUrl($contact['thumb'], false, Proxy::SIZE_THUMB),
+				'thumb'        => Contact::getThumb($contact),
 				'url'          => Contact::magicLink($contact['url']),
 				'nurl'         => $contact['nurl'],
 				'location'     => $contact['location'],
diff --git a/src/Module/Directory.php b/src/Module/Directory.php
index 507da6b942..38be89b93c 100644
--- a/src/Module/Directory.php
+++ b/src/Module/Directory.php
@@ -32,7 +32,6 @@ use Friendica\DI;
 use Friendica\Model\Contact;
 use Friendica\Model\Profile;
 use Friendica\Network\HTTPException;
-use Friendica\Util\Proxy as ProxyUtils;
 use Friendica\Util\Strings;
 
 /**
@@ -168,7 +167,7 @@ class Directory extends BaseModule
 			'id'           => $contact['id'],
 			'url'          => Contact::magicLink($profile_link),
 			'itemurl'      => $itemurl,
-			'thumb'        => ProxyUtils::proxifyUrl($contact[$photo_size], false, ProxyUtils::SIZE_THUMB),
+			'thumb'        => Contact::getThumb($contact),
 			'img_hover'    => $contact['name'],
 			'name'         => $contact['name'],
 			'details'      => $details,
diff --git a/src/Module/Profile/Contacts.php b/src/Module/Profile/Contacts.php
index 4cd97b4097..e7931bdb02 100644
--- a/src/Module/Profile/Contacts.php
+++ b/src/Module/Profile/Contacts.php
@@ -32,7 +32,6 @@ use Friendica\DI;
 use Friendica\Model\Contact;
 use Friendica\Model\Profile;
 use Friendica\Module\BaseProfile;
-use Friendica\Util\Proxy as ProxyUtils;
 
 class Contacts extends BaseProfile
 {
@@ -109,7 +108,7 @@ class Contacts extends BaseProfile
 				'id'           => $contact['id'],
 				'img_hover'    => DI::l10n()->t('Visit %s\'s profile [%s]', $contact_details['name'], $contact['url']),
 				'photo_menu'   => Contact::photoMenu($contact),
-				'thumb'        => ProxyUtils::proxifyUrl($contact_details['thumb'], false, ProxyUtils::SIZE_THUMB),
+				'thumb'        => Contact::getThumb($contact_details),
 				'name'         => substr($contact_details['name'], 0, 20),
 				'username'     => $contact_details['name'],
 				'details'      => $contact_details['location'],
diff --git a/src/Module/Search/Acl.php b/src/Module/Search/Acl.php
index 8a5c9faf58..2c0cc967c0 100644
--- a/src/Module/Search/Acl.php
+++ b/src/Module/Search/Acl.php
@@ -294,7 +294,7 @@ class Acl extends BaseModule
 			foreach ($r as $g) {
 				$entry = [
 					'type'    => 'c',
-					'photo'   => ProxyUtils::proxifyUrl($g['micro'], false, ProxyUtils::SIZE_MICRO),
+					'photo'   => Contact::getMicro($g),
 					'name'    => htmlspecialchars($g['name']),
 					'id'      => intval($g['id']),
 					'network' => $g['network'],
@@ -355,7 +355,7 @@ class Acl extends BaseModule
 				if (count($contact) > 0) {
 					$unknown_contacts[] = [
 						'type'    => 'c',
-						'photo'   => ProxyUtils::proxifyUrl($contact['micro'], false, ProxyUtils::SIZE_MICRO),
+						'photo'   => Contact::getMicro($contact),
 						'name'    => htmlspecialchars($contact['name']),
 						'id'      => intval($contact['cid']),
 						'network' => $contact['network'],
diff --git a/src/Util/Proxy.php b/src/Util/Proxy.php
index e104073f03..87f7c983e1 100644
--- a/src/Util/Proxy.php
+++ b/src/Util/Proxy.php
@@ -170,7 +170,7 @@ class Proxy
 	 * @return boolean
 	 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
 	 */
-	private static function isLocalImage($url)
+	public static function isLocalImage($url)
 	{
 		if (substr($url, 0, 1) == '/') {
 			return true;

From d5a3ef6c2a2426a9504bd6d681716912f3465ebb Mon Sep 17 00:00:00 2001
From: Michael <heluecht@pirati.ca>
Date: Tue, 28 Jul 2020 15:04:39 +0000
Subject: [PATCH 2/2] Removed unused "use"

---
 src/Module/Contact.php | 1 -
 1 file changed, 1 deletion(-)

diff --git a/src/Module/Contact.php b/src/Module/Contact.php
index ed6fe728a1..d21524a793 100644
--- a/src/Module/Contact.php
+++ b/src/Module/Contact.php
@@ -36,7 +36,6 @@ use Friendica\Core\Worker;
 use Friendica\Database\DBA;
 use Friendica\DI;
 use Friendica\Model;
-use Friendica\Model\Contact as ModelContact;
 use Friendica\Module\Security\Login;
 use Friendica\Network\HTTPException\BadRequestException;
 use Friendica\Network\HTTPException\NotFoundException;