+ case "preview":
+ $media = DBA::selectFirst('post-media', ['preview', 'url', 'mimetype', 'type', 'uri-id'], ['id' => $id]);
+ if (empty($media)) {
+ return false;
+ }
+ $url = $media['preview'];
+
+ if (empty($url) && ($media['type'] == Post\Media::IMAGE)) {
+ $url = $media['url'];
+ }
+
+ if (empty($url)) {
+ return false;
+ }
+
+ if (Network::isLocalLink($url) && preg_match('|.*?/photo/(.*[a-fA-F0-9])\-(.*[0-9])\..*[\w]|', $url, $matches)) {
+ return MPhoto::getPhoto($matches[1], $matches[2]);
+ }
+
+ return MPhoto::createPhotoForExternalResource($url, (int)local_user(), $media['mimetype']);
+ case "media":
+ $media = DBA::selectFirst('post-media', ['url', 'mimetype', 'uri-id'], ['id' => $id, 'type' => Post\Media::IMAGE]);
+ if (empty($media)) {
+ return false;
+ }
+
+ if (Network::isLocalLink($media['url']) && preg_match('|.*?/photo/(.*[a-fA-F0-9])\-(.*[0-9])\..*[\w]|', $media['url'], $matches)) {
+ return MPhoto::getPhoto($matches[1], $matches[2]);
+ }
+
+ return MPhoto::createPhotoForExternalResource($media['url'], (int)local_user(), $media['mimetype']);
+ case "link":
+ $link = DBA::selectFirst('post-link', ['url', 'mimetype'], ['id' => $id]);
+ if (empty($link)) {
+ return false;
+ }
+
+ return MPhoto::createPhotoForExternalResource($link['url'], (int)local_user(), $link['mimetype']);
+ case "contact":
+ $contact = Contact::getById($id, ['uid', 'url', 'avatar', 'photo', 'xmpp', 'addr']);
+ if (empty($contact)) {
+ return false;
+ }
+ If (($contact['uid'] != 0) && empty($contact['photo']) && empty($contact['avatar'])) {
+ $contact = Contact::getByURL($contact['url'], false, ['avatar', 'photo', 'xmpp', 'addr']);
+ }
+ if (!empty($contact['photo']) && !empty($contact['avatar'])) {
+ // Fetch photo directly
+ $resourceid = MPhoto::ridFromURI($contact['photo']);
+ if (!empty($resourceid)) {
+ $photo = MPhoto::selectFirst([], ['resource-id' => $resourceid], ['order' => ['scale']]);
+ if (!empty($photo)) {
+ return $photo;
+ }
+ }
+ // We continue with the avatar link when the photo link is invalid
+ $url = $contact['avatar'];
+ } elseif (!empty($contact['avatar'])) {
+ $url = $contact['avatar'];
+ }
+ $mimetext = '';
+ if (!empty($url)) {
+ $mime = ParseUrl::getContentType($url);
+ if (!empty($mime)) {
+ $mimetext = $mime[0] . '/' . $mime[1];
+ } else {
+ Logger::info('Invalid file', ['url' => $url]);
+ }
+ if (!empty($mimetext) && ($mime[0] != 'image') && ($mimetext != 'application/octet-stream')) {
+ Logger::info('Unexpected Content-Type', ['mime' => $mimetext, 'url' => $url]);
+ $mimetext = '';
+ }
+ }
+ if (empty($mimetext)) {
+ if ($customsize <= Proxy::PIXEL_MICRO) {
+ $url = Contact::getDefaultAvatar($contact, Proxy::SIZE_MICRO);
+ } elseif ($customsize <= Proxy::PIXEL_THUMB) {
+ $url = Contact::getDefaultAvatar($contact, Proxy::SIZE_THUMB);
+ } else {
+ $url = Contact::getDefaultAvatar($contact, Proxy::SIZE_SMALL);
+ }
+ }
+ return MPhoto::createPhotoForExternalResource($url, 0, $mimetext);
+ case "header":
+ $contact = Contact::getById($id, ['uid', 'url', 'header']);
+ if (empty($contact)) {
+ return false;
+ }
+ If (($contact['uid'] != 0) && empty($contact['header'])) {
+ $contact = Contact::getByURL($contact['url'], false, ['header']);
+ }
+ if (!empty($contact['header'])) {
+ $url = $contact['header'];
+ } else {
+ $url = DI::baseUrl() . '/images/blank.png';
+ }
+ return MPhoto::createPhotoForExternalResource($url);