]> git.mxchange.org Git - friendica.git/blobdiff - src/Model/Photo.php
spelling: resource
[friendica.git] / src / Model / Photo.php
index 126bc152b4ddf2d9ce5d275e73c6a94a4327f1bc..29ae473abc49b57ee72a4b43d246d0ce4cc659b2 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
  *
@@ -36,11 +36,12 @@ use Friendica\Object\Image;
 use Friendica\Util\DateTimeFormat;
 use Friendica\Util\Images;
 use Friendica\Security\Security;
+use Friendica\Util\Network;
 use Friendica\Util\Proxy;
 use Friendica\Util\Strings;
 
 /**
- * Class to handle photo dabatase table
+ * Class to handle photo database table
  */
 class Photo
 {
@@ -100,7 +101,7 @@ class Photo
         * Get photos for user id
         *
         * @param integer $uid        User id
-        * @param string  $resourceid Rescource ID of the photo
+        * @param string  $resourceid Resource ID of the photo
         * @param array   $conditions Array of fields for conditions
         * @param array   $params     Array of several parameters
         *
@@ -121,7 +122,7 @@ class Photo
         * Get a photo for user id
         *
         * @param integer $uid        User id
-        * @param string  $resourceid Rescource ID of the photo
+        * @param string  $resourceid Resource ID of the photo
         * @param integer $scale      Scale of the photo. Defaults to 0
         * @param array   $conditions Array of fields for conditions
         * @param array   $params     Array of several parameters
@@ -147,13 +148,14 @@ class Photo
         * on success, "no sign" image info, if user has no permission,
         * false if photo does not exists
         *
-        * @param string  $resourceid Rescource ID of the photo
-        * @param integer $scale      Scale of the photo. Defaults to 0
+        * @param string  $resourceid  Resource ID of the photo
+        * @param integer $scale       Scale of the photo. Defaults to 0
+        * @param integer $visitor_uid UID of the visitor
         *
         * @return boolean|array
         * @throws \Exception
         */
-       public static function getPhoto(string $resourceid, int $scale = 0)
+       public static function getPhoto(string $resourceid, int $scale = 0, int $visitor_uid = 0)
        {
                $r = self::selectFirst(['uid'], ['resource-id' => $resourceid]);
                if (!DBA::isResult($r)) {
@@ -164,7 +166,11 @@ class Photo
 
                $accessible = $uid ? (bool)DI::pConfig()->get($uid, 'system', 'accessible-photos', false) : false;
 
-               $sql_acl = Security::getPermissionsSQLByUserId($uid, $accessible);
+               if (!empty($visitor_uid) && ($uid == $visitor_uid)) {
+                       $sql_acl = '';
+               } else {
+                       $sql_acl = Security::getPermissionsSQLByUserId($uid, $accessible);
+               }
 
                $conditions = ["`resource-id` = ? AND `scale` <= ? " . $sql_acl, $resourceid, $scale];
                $params = ['order' => ['scale' => true]];
@@ -225,7 +231,7 @@ class Photo
                        DBA::p(
                                "SELECT `resource-id`, ANY_VALUE(`id`) AS `id`, ANY_VALUE(`filename`) AS `filename`, ANY_VALUE(`type`) AS `type`,
                                        min(`scale`) AS `hiq`, max(`scale`) AS `loq`, ANY_VALUE(`desc`) AS `desc`, ANY_VALUE(`created`) AS `created`
-                                       FROM `photo` WHERE `uid` = ? AND NOT `photo-type` IN (?, ?) $sqlExtra 
+                                       FROM `photo` WHERE `uid` = ? AND NOT `photo-type` IN (?, ?) $sqlExtra
                                        GROUP BY `resource-id` $sqlExtra2",
                                $values
                        ));
@@ -313,6 +319,28 @@ class Photo
                return $fields;
        }
 
+       /**
+        * Construct a photo array for a given image data string
+        *
+        * @param string $image_data Image data
+        * @param string $mimetype   Image mime type. Is guessed by file name when empty.
+        *
+        * @return array
+        * @throws \Exception
+        */
+       public static function createPhotoForImageData(string $image_data, string $mimetype = ''): array
+       {
+               $fields = self::getFields();
+               $values = array_fill(0, count($fields), '');
+
+               $photo                  = array_combine($fields, $values);
+               $photo['data']          = $image_data;
+               $photo['type']          = $mimetype ?: Images::getMimeTypeByData($image_data);
+               $photo['cacheable']     = false;
+
+               return $photo;
+       }
+
        /**
         * Construct a photo array for a system resource image
         *
@@ -346,11 +374,14 @@ class Photo
         * @param string $url      Image URL
         * @param int    $uid      User ID of the requesting person
         * @param string $mimetype Image mime type. Is guessed by file name when empty.
+        * @param string $blurhash The blurhash that will be used to generate a picture when the original picture can't be fetched
+        * @param int    $width    Image width
+        * @param int    $height   Image height
         *
         * @return array
         * @throws \Exception
         */
-       public static function createPhotoForExternalResource(string $url, int $uid = 0, string $mimetype = ''): array
+       public static function createPhotoForExternalResource(string $url, int $uid = 0, string $mimetype = '', string $blurhash = null, int $width = null, int $height = null): array
        {
                if (empty($mimetype)) {
                        $mimetype = Images::guessTypeByExtension($url);
@@ -364,6 +395,9 @@ class Photo
                $photo['backend-ref']   = json_encode(['url' => $url, 'uid' => $uid]);
                $photo['type']          = $mimetype;
                $photo['cacheable']     = true;
+               $photo['blurhash']      = $blurhash;
+               $photo['width']         = $width;
+               $photo['height']        = $height;
 
                return $photo;
        }
@@ -382,7 +416,7 @@ class Photo
         * @param string  $allow_cid Permissions, allowed contacts. optional, default = ""
         * @param string  $allow_gid Permissions, allowed groups. optional, default = ""
         * @param string  $deny_cid  Permissions, denied contacts.optional, default = ""
-        * @param string  $deny_gid  Permissions, denied greoup.optional, default = ""
+        * @param string  $deny_gid  Permissions, denied group.optional, default = ""
         * @param string  $desc      Photo caption. optional, default = ""
         *
         * @return boolean True on success
@@ -436,6 +470,7 @@ class Photo
                        'height' => $image->getHeight(),
                        'width' => $image->getWidth(),
                        'datasize' => strlen($image->asString()),
+                       'blurhash' => $image->getBlurHash(),
                        'data' => $data,
                        'scale' => $scale,
                        'photo-type' => $type,
@@ -501,7 +536,7 @@ class Photo
         * @param Image $image      Image to update. Optional, default null.
         * @param array $old_fields Array with the old field values that are about to be replaced (true = update on duplicate)
         *
-        * @return boolean  Was the update successfull?
+        * @return boolean  Was the update successful?
         *
         * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         * @see   \Friendica\Database\DBA::update
@@ -553,6 +588,11 @@ class Photo
 
                $photo_failure = false;
 
+               if (!Network::isValidHttpUrl($image_url)) {
+                       Logger::warning('Invalid image url', ['image_url' => $image_url, 'uid' => $uid, 'cid' => $cid, 'callstack' => System::callstack(20)]);
+                       return false;
+               }
+
                $filename = basename($image_url);
                if (!empty($image_url)) {
                        $ret = DI::httpClient()->get($image_url, HttpClientAccept::IMAGE);
@@ -640,7 +680,11 @@ class Photo
                        $micro = Contact::getDefaultAvatar($contact, Proxy::SIZE_MICRO);
                }
 
-               return [$image_url, $thumb, $micro];
+               $photo = DBA::selectFirst(
+                       'photo', ['blurhash'], ['uid' => $uid, 'contact-id' => $cid, 'scale' => 4, 'photo-type' => self::CONTACT_AVATAR]
+               );
+
+               return [$image_url, $thumb, $micro, $photo['blurhash']];
        }
 
        /**
@@ -837,14 +881,14 @@ class Photo
                         * Then set the permissions to public.
                         */
 
-                       self::setPermissionForRessource($image_rid, $uid, $str_contact_allow, $str_group_allow, $str_contact_deny, $str_group_deny);
+                       self::setPermissionForResource($image_rid, $uid, $str_contact_allow, $str_group_allow, $str_contact_deny, $str_group_deny);
                }
 
                return true;
        }
 
        /**
-        * Add permissions to photo ressource
+        * Add permissions to photo resource
         * @todo mix with previous photo permissions
         *
         * @param string $image_rid
@@ -855,7 +899,7 @@ class Photo
         * @param string $str_group_deny
         * @return void
         */
-       public static function setPermissionForRessource(string $image_rid, int $uid, string $str_contact_allow, string $str_group_allow, string $str_contact_deny, string $str_group_deny)
+       public static function setPermissionForResource(string $image_rid, int $uid, string $str_contact_allow, string $str_group_allow, string $str_contact_deny, string $str_group_deny)
        {
                $fields = ['allow_cid' => $str_contact_allow, 'allow_gid' => $str_group_allow,
                'deny_cid' => $str_contact_deny, 'deny_gid' => $str_group_deny,
@@ -874,7 +918,7 @@ class Photo
         */
        public static function getResourceData(string $name): array
        {
-               $base = DI::baseUrl()->get();
+               $base = DI::baseUrl();
 
                $guid = str_replace([Strings::normaliseLink($base), '/photo/'], '', Strings::normaliseLink($name));
 
@@ -938,7 +982,7 @@ class Photo
         */
        public static function isLocalPage(string $name): bool
        {
-               $base = DI::baseUrl()->get();
+               $base = DI::baseUrl();
 
                $guid = str_replace(Strings::normaliseLink($base), '', Strings::normaliseLink($name));
                $guid = preg_replace("=/photos/.*/image/(.*)=ism", '$1', $guid);
@@ -1318,7 +1362,7 @@ class Photo
                        logger::warning('profile banner upload with scale 3 (960) failed');
                }
 
-               logger::info('new profile banner upload ended');
+               logger::info('new profile banner upload ended', ['uid' => $uid, 'resource_id' => $resource_id, 'filename' => $filename]);
 
                $condition = ["`photo-type` = ? AND `resource-id` != ? AND `uid` = ?", self::USER_BANNER, $resource_id, $uid];
                self::update(['photo-type' => self::DEFAULT], $condition);