]> git.mxchange.org Git - friendica.git/blobdiff - src/Model/Photo.php
Use "received" instead of "created" when displaying posts in creation order
[friendica.git] / src / Model / Photo.php
index 6f2bbd0039dab4e04ac86d5e2fb1726b63889f10..0e3661b0f33e4110915908a8542353e35823112b 100644 (file)
@@ -10,11 +10,13 @@ use Friendica\BaseObject;
 use Friendica\Core\Cache;
 use Friendica\Core\Config;
 use Friendica\Core\L10n;
-use Friendica\Core\System;
 use Friendica\Core\StorageManager;
+use Friendica\Core\System;
 use Friendica\Database\DBA;
 use Friendica\Database\DBStructure;
+use Friendica\Model\Storage\IStorage;
 use Friendica\Object\Image;
+use Friendica\Protocol\DFRN;
 use Friendica\Util\DateTimeFormat;
 use Friendica\Util\Network;
 use Friendica\Util\Security;
@@ -41,7 +43,7 @@ class Photo extends BaseObject
        public static function select(array $fields = [], array $conditions = [], array $params = [])
        {
                if (empty($fields)) {
-                       $selected = self::getFields();
+                       $fields = self::getFields();
                }
 
                $r = DBA::select("photo", $fields, $conditions, $params);
@@ -128,12 +130,23 @@ class Photo extends BaseObject
         */
        public static function getPhoto($resourceid, $scale = 0)
        {
-               $r = self::selectFirst(["uid"], ["resource-id" => $resourceid]);
+               $r = self::selectFirst(["uid", "allow_cid", "allow_gid", "deny_cid", "deny_gid"], ["resource-id" => $resourceid]);
                if ($r === false) {
                        return false;
                }
+               $uid = $r["uid"];
+
+               // This is the first place, when retrieving just a photo, that we know who owns the photo.
+               // Check if the photo is public (empty allow and deny means public), if so, skip auth attempt, if not
+               // make sure that the requester's session is appropriately authenticated to that user
+               // otherwise permissions checks done by getPermissionsSQLByUserId() won't work correctly
+               if (!empty($r["allow_cid"]) || !empty($r["allow_gid"]) || !empty($r["deny_cid"]) || !empty($r["deny_gid"])) {
+                       $r = DBA::selectFirst("user", ["nickname"], ["uid" => $uid], []);
+                       // this will either just return (if auth all ok) or will redirect and exit (starting over)
+                       DFRN::autoRedir(self::getApp(), $r["nickname"]);
+               }
 
-               $sql_acl = Security::getPermissionsSQLByUserId($r["uid"]);
+               $sql_acl = Security::getPermissionsSQLByUserId($uid);
 
                $conditions = [
                        "`resource-id` = ? AND `scale` <= ? " . $sql_acl,
@@ -173,6 +186,7 @@ class Photo extends BaseObject
        public static function getImageForPhoto(array $photo)
        {
                $data = "";
+
                if ($photo["backend-class"] == "") {
                        // legacy data storage in "data" column
                        $i = self::selectFirst(["data"], ["id" => $photo["id"]]);
@@ -189,6 +203,7 @@ class Photo extends BaseObject
                if ($data === "") {
                        return null;
                }
+
                return new Image($data, $photo["type"]);
        }
 
@@ -200,7 +215,7 @@ class Photo extends BaseObject
         */
        private static function getFields()
        {
-               $allfields = DBStructure::definition(false);
+               $allfields = DBStructure::definition(self::getApp()->getBasePath(), false);
                $fields = array_keys($allfields["photo"]["fields"]);
                array_splice($fields, array_search("data", $fields), 1);
                return $fields;
@@ -219,11 +234,13 @@ class Photo extends BaseObject
        {
                $fields = self::getFields();
                $values = array_fill(0, count($fields), "");
+
                $photo = array_combine($fields, $values);
                $photo["backend-class"] = Storage\SystemResource::class;
                $photo["backend-ref"] = $filename;
                $photo["type"] = $mimetype;
                $photo["cacheable"] = false;
+
                return $photo;
        }
 
@@ -268,14 +285,15 @@ class Photo extends BaseObject
                // if is an existing photo, reuse same backend
                $data = "";
                $backend_ref = "";
-               $backend_class = "";
 
+               /** @var IStorage $backend_class */
                if (DBA::isResult($existing_photo)) {
                        $backend_ref = (string)$existing_photo["backend-ref"];
                        $backend_class = (string)$existing_photo["backend-class"];
                } else {
                        $backend_class = StorageManager::getBackend();
                }
+
                if ($backend_class === "") {
                        $data = $Image->asString();
                } else {
@@ -335,6 +353,7 @@ class Photo extends BaseObject
                $photos = self::select(["backend-class","backend-ref"], $conditions);
 
                foreach($photos as $photo) {
+                       /** @var IStorage $backend_class */
                        $backend_class = (string)$photo["backend-class"];
                        if ($backend_class !== "") {
                                $backend_class::delete($photo["backend-ref"]);
@@ -364,6 +383,7 @@ class Photo extends BaseObject
                        $photos = self::select(["backend-class","backend-ref"], $conditions);
 
                        foreach($photos as $photo) {
+                               /** @var IStorage $backend_class */
                                $backend_class = (string)$photo["backend-class"];
                                if ($backend_class !== "") {
                                        $fields["backend-ref"] = $backend_class::put($img->asString(), $photo["backend-ref"]);
@@ -405,13 +425,22 @@ class Photo extends BaseObject
                $photo_failure = false;
 
                $filename = basename($image_url);
-               $img_str = Network::fetchUrl($image_url, true);
+               if (!empty($image_url)) {
+                       $ret = Network::curl($image_url, true);
+                       $img_str = $ret->getBody();
+                       $type = $ret->getContentType();
+               } else {
+                       $img_str = '';
+               }
 
                if ($quit_on_error && ($img_str == "")) {
                        return false;
                }
 
-               $type = Image::guessType($image_url, true);
+               if (empty($type)) {
+                       $type = Image::guessType($image_url, true);
+               }
+
                $Image = new Image($img_str, $type);
                if ($Image->isValid()) {
                        $Image->scaleToSquare(300);
@@ -480,7 +509,7 @@ class Photo extends BaseObject
        }
 
        /**
-        * @param string $exifCoord coordinate
+        * @param array $exifCoord coordinate
         * @param string $hemi      hemi
         * @return float
         */