]> git.mxchange.org Git - friendica.git/blobdiff - src/Module/Photo.php
Some more API functions moved
[friendica.git] / src / Module / Photo.php
index 39a8353b5d87f34bf0afb80d00ffbe0164ca9896..399313e3f5c428816d89404b1dbc47837376343c 100644 (file)
@@ -29,13 +29,15 @@ use Friendica\Model\Contact;
 use Friendica\Model\Photo as MPhoto;
 use Friendica\Model\Post;
 use Friendica\Model\Profile;
-use Friendica\Model\Storage\ExternalResource;
-use Friendica\Model\Storage\SystemResource;
+use Friendica\Core\Storage\Type\ExternalResource;
+use Friendica\Core\Storage\Type\SystemResource;
 use Friendica\Model\User;
 use Friendica\Network\HTTPException;
+use Friendica\Network\HTTPException\NotModifiedException;
 use Friendica\Object\Image;
 use Friendica\Util\Images;
 use Friendica\Util\Network;
+use Friendica\Util\ParseUrl;
 use Friendica\Util\Proxy;
 
 /**
@@ -54,7 +56,6 @@ class Photo extends BaseModule
                $totalstamp = microtime(true);
 
                if (isset($_SERVER["HTTP_IF_MODIFIED_SINCE"])) {
-                       header("HTTP/1.1 304 Not Modified");
                        header("Last-Modified: " . gmdate("D, d M Y H:i:s", time()) . " GMT");
                        if (!empty($_SERVER["HTTP_IF_NONE_MATCH"])) {
                                header("Etag: " . $_SERVER["HTTP_IF_NONE_MATCH"]);
@@ -66,7 +67,7 @@ class Photo extends BaseModule
                                header_remove("Expires");
                                header_remove("Cache-Control");
                        }
-                       exit;
+                       throw new NotModifiedException();
                }
 
                Profile::addVisitorCookieForHTTPSigner();
@@ -82,24 +83,29 @@ class Photo extends BaseModule
                                $square_resize = !in_array($parameters['type'], ['media', 'preview']);
                        }
 
-                       if (!empty($parameters['nickname_ext'])) {
-                               if (in_array($parameters['type'], ['contact', 'header'])) {
-                                       $guid = pathinfo($parameters['nickname_ext'], PATHINFO_FILENAME);
-                                       $account = DBA::selectFirst('account-user-view', ['id'], ['guid' => $guid], ['order' => ['uid' => true]]);
-                                       if (empty($account)) {
-                                               throw new HTTPException\NotFoundException();
-                                       }
+                       if (!empty($parameters['guid'])) {
+                               $guid = $parameters['guid'];
+                               $account = DBA::selectFirst('account-user-view', ['id'], ['guid' => $guid], ['order' => ['uid' => true]]);
+                               if (empty($account)) {
+                                       throw new HTTPException\NotFoundException();
+                               }
 
-                                       $id = $account['id'];
-                               } else {
-                                       $nickname = pathinfo($parameters['nickname_ext'], PATHINFO_FILENAME);
-                                       $user = User::getByNickname($nickname, ['uid']);
-                                       if (empty($user)) {
-                                               throw new HTTPException\NotFoundException();
-                                       }
+                               $id = $account['id'];
+                       }
+
+                       // Contact Id Fallback, to remove after version 2021.12
+                       if (isset($parameters['contact_id'])) {
+                               $id = intval($parameters['contact_id']);
+                       }
 
-                                       $id = $user['uid'];
+                       if (!empty($parameters['nickname_ext'])) {
+                               $nickname = pathinfo($parameters['nickname_ext'], PATHINFO_FILENAME);
+                               $user = User::getByNickname($nickname, ['uid']);
+                               if (empty($user)) {
+                                       throw new HTTPException\NotFoundException();
                                }
+
+                               $id = $user['uid'];
                        }
 
                        // User Id Fallback, to remove after version 2021.12
@@ -279,14 +285,30 @@ class Photo extends BaseModule
                                        $url = $contact['avatar'];
                                } elseif (!empty($contact['avatar'])) {
                                        $url = $contact['avatar'];
-                               } elseif ($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);
+                               $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)) {