]> git.mxchange.org Git - friendica.git/blobdiff - src/Model/Item.php
Raw content is now stored with announce messages as well
[friendica.git] / src / Model / Item.php
index 050e79de50eed567a5e496f508710dbe851fa4c2..eac3b7028623dac5bdee844b471eb1721c43b793 100644 (file)
@@ -1,16 +1,29 @@
 <?php
-
 /**
- * @file src/Model/Item.php
+ * @copyright Copyright (C) 2020, Friendica
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <https://www.gnu.org/licenses/>.
+ *
  */
 
 namespace Friendica\Model;
 
 use Friendica\Content\Text\BBCode;
 use Friendica\Content\Text\HTML;
-use Friendica\Core\Config;
 use Friendica\Core\Hook;
-use Friendica\Core\L10n;
 use Friendica\Core\Logger;
 use Friendica\Core\Protocol;
 use Friendica\Core\Renderer;
@@ -100,12 +113,16 @@ class Item
                Activity::FOLLOW,
                Activity::ANNOUNCE];
 
+       const PUBLIC = 0;
+       const PRIVATE = 1;
+       const UNLISTED = 2;
+
        private static $legacy_mode = null;
 
        public static function isLegacyMode()
        {
                if (is_null(self::$legacy_mode)) {
-                       self::$legacy_mode = (Config::get("system", "post_update_version") < 1279);
+                       self::$legacy_mode = (DI::config()->get("system", "post_update_version") < 1279);
                }
 
                return self::$legacy_mode;
@@ -1099,6 +1116,7 @@ class Item
         */
        public static function deleteById($item_id, $priority = PRIORITY_HIGH)
        {
+               Logger::notice('Delete item by id', ['id' => $item_id, 'callstack' => System::callstack()]);
                // locate item to be deleted
                $fields = ['id', 'uri', 'uid', 'parent', 'parent-uri', 'origin',
                        'deleted', 'file', 'resource-id', 'event-id', 'attach',
@@ -1452,7 +1470,7 @@ class Item
                $uid = intval($item['uid']);
 
                // check for create date and expire time
-               $expire_interval = Config::get('system', 'dbclean-expire-days', 0);
+               $expire_interval = DI::config()->get('system', 'dbclean-expire-days', 0);
 
                $user = DBA::selectFirst('user', ['expire'], ['uid' => $uid]);
                if (DBA::isResult($user) && ($user['expire'] > 0) && (($user['expire'] < $expire_interval) || ($expire_interval == 0))) {
@@ -1528,7 +1546,7 @@ class Item
                $item['allow_gid']     = trim($item['allow_gid'] ?? '');
                $item['deny_cid']      = trim($item['deny_cid'] ?? '');
                $item['deny_gid']      = trim($item['deny_gid'] ?? '');
-               $item['private']       = intval($item['private'] ?? 0);
+               $item['private']       = intval($item['private'] ?? self::PUBLIC);
                $item['body']          = trim($item['body'] ?? '');
                $item['tag']           = trim($item['tag'] ?? '');
                $item['attach']        = trim($item['attach'] ?? '');
@@ -1724,8 +1742,8 @@ class Item
                                 * The original author commented, but as this is a comment, the permissions
                                 * weren't fixed up so it will still show the comment as private unless we fix it here.
                                 */
-                               if ((intval($parent['forum_mode']) == 1) && $parent['private']) {
-                                       $item['private'] = 0;
+                               if ((intval($parent['forum_mode']) == 1) && ($parent['private'] != self::PUBLIC)) {
+                                       $item['private'] = self::PUBLIC;
                                }
 
                                // If its a post that originated here then tag the thread as "mention"
@@ -1795,7 +1813,7 @@ class Item
 
                // ACL settings
                if (strlen($allow_cid) || strlen($allow_gid) || strlen($deny_cid) || strlen($deny_gid)) {
-                       $private = 1;
+                       $private = self::PRIVATE;
                } else {
                        $private = $item['private'];
                }
@@ -1858,7 +1876,18 @@ class Item
                }
 
                // Creates or assigns the permission set
-               $item['psid'] = PermissionSet::fetchIDForPost($item);
+               $item['psid'] = PermissionSet::getIdFromACL(
+                       $item['uid'],
+                       $item['allow_cid'],
+                       $item['allow_gid'],
+                       $item['deny_cid'],
+                       $item['deny_gid']
+               );
+
+               $item['allow_cid'] = null;
+               $item['allow_gid'] = null;
+               $item['deny_cid'] = null;
+               $item['deny_gid'] = null;
 
                // We are doing this outside of the transaction to avoid timing problems
                if (!self::insertActivity($item)) {
@@ -1880,7 +1909,7 @@ class Item
                unset($item['owner-name']);
                unset($item['owner-avatar']);
 
-               $like_no_comment = Config::get('system', 'like_no_comment');
+               $like_no_comment = DI::config()->get('system', 'like_no_comment');
 
                DBA::transaction();
                $ret = DBA::insert('item', $item);
@@ -1910,7 +1939,7 @@ class Item
 
                if ($entries > 1) {
                        // There are duplicates. We delete our just created entry.
-                       Logger::log('Duplicated post occurred. uri = ' . $item['uri'] . ' uid = ' . $item['uid']);
+                       Logger::notice('Delete duplicated item', ['id' => $current_post, 'uri' => $item['uri'], 'uid' => $item['uid']]);
 
                        // Yes, we could do a rollback here - but we are having many users with MyISAM.
                        DBA::delete('item', ['id' => $current_post]);
@@ -2193,7 +2222,7 @@ class Item
                // Only distribute public items from native networks
                $condition = ['id' => $itemid, 'uid' => 0,
                        'network' => array_merge(Protocol::FEDERATED ,['']),
-                       'visible' => true, 'deleted' => false, 'moderated' => false, 'private' => false];
+                       'visible' => true, 'deleted' => false, 'moderated' => false, 'private' => [self::PUBLIC, self::UNLISTED]];
                $item = self::selectFirst(self::ITEM_FIELDLIST, $condition);
                if (!DBA::isResult($item)) {
                        return;
@@ -2343,7 +2372,7 @@ class Item
                }
 
                // Is it a visible public post?
-               if (!$item["visible"] || $item["deleted"] || $item["moderated"] || $item["private"]) {
+               if (!$item["visible"] || $item["deleted"] || $item["moderated"] || ($item["private"] == Item::PRIVATE)) {
                        return;
                }
 
@@ -2534,7 +2563,7 @@ class Item
                        Contact::unmarkForArchival($contact);
                }
 
-               $update = (!$arr['private'] && ((($arr['author-link'] ?? '') === ($arr['owner-link'] ?? '')) || ($arr["parent-uri"] === $arr["uri"])));
+               $update = (($arr['private'] != self::PRIVATE) && ((($arr['author-link'] ?? '') === ($arr['owner-link'] ?? '')) || ($arr["parent-uri"] === $arr["uri"])));
 
                // Is it a forum? Then we don't care about the rules from above
                if (!$update && in_array($arr["network"], [Protocol::ACTIVITYPUB, Protocol::DFRN]) && ($arr["parent-uri"] === $arr["uri"])) {
@@ -2548,7 +2577,7 @@ class Item
                                ['id' => $arr['contact-id']]);
                }
                // Now do the same for the system wide contacts with uid=0
-               if (!$arr['private']) {
+               if ($arr['private'] != self::PRIVATE) {
                        DBA::update('contact', ['success_update' => $arr['received'], 'last-item' => $arr['received']],
                                ['id' => $arr['owner-id']]);
 
@@ -2586,7 +2615,7 @@ class Item
                $URLSearchString = "^\[\]";
 
                // All hashtags should point to the home server if "local_tags" is activated
-               if (Config::get('system', 'local_tags')) {
+               if (DI::config()->get('system', 'local_tags')) {
                        $item["body"] = preg_replace("/#\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/ism",
                                        "#[url=".DI::baseUrl()."/search?tag=$2]$2[/url]", $item["body"]);
 
@@ -2693,9 +2722,7 @@ class Item
                if (!$mention) {
                        if (($community_page || $prvgroup) &&
                                  !$item['wall'] && !$item['origin'] && ($item['id'] == $item['parent'])) {
-                               // mmh.. no mention.. community page or private group... no wall.. no origin.. top-post (not a comment)
-                               // delete it!
-                               Logger::log("no-mention top-level post to community or private group. delete.");
+                               Logger::notice('Delete private group/communiy top-level item without mention', ['id' => $item_id]);
                                DBA::delete('item', ['id' => $item_id]);
                                return true;
                        }
@@ -2729,9 +2756,15 @@ class Item
 
                // also reset all the privacy bits to the forum default permissions
 
-               $private = ($user['allow_cid'] || $user['allow_gid'] || $user['deny_cid'] || $user['deny_gid']) ? 1 : 0;
+               $private = ($user['allow_cid'] || $user['allow_gid'] || $user['deny_cid'] || $user['deny_gid']) ? self::PRIVATE : self::PUBLIC;
 
-               $psid = PermissionSet::fetchIDForPost($user);
+               $psid = PermissionSet::getIdFromACL(
+                       $user['uid'],
+                       $user['allow_cid'],
+                       $user['allow_gid'],
+                       $user['deny_cid'],
+                       $user['deny_gid']
+               );
 
                $forum_mode = ($prvgroup ? 2 : 1);
 
@@ -2770,7 +2803,7 @@ class Item
                        return false;
                }
 
-               if (($contact['network'] != Protocol::FEED) && $datarray['private']) {
+               if (($contact['network'] != Protocol::FEED) && ($datarray['private'] == self::PRIVATE)) {
                        Logger::log('Not public', Logger::DEBUG);
                        return false;
                }
@@ -2808,7 +2841,7 @@ class Item
                                $urlpart = parse_url($datarray2['author-link']);
                                $datarray["app"] = $urlpart["host"];
                        } else {
-                               $datarray['private'] = 0;
+                               $datarray['private'] = self::PUBLIC;
                        }
                }
 
@@ -2843,7 +2876,7 @@ class Item
         */
        public static function fixPrivatePhotos($s, $uid, $item = null, $cid = 0)
        {
-               if (Config::get('system', 'disable_embedded')) {
+               if (DI::config()->get('system', 'disable_embedded')) {
                        return $s;
                }
 
@@ -3337,7 +3370,7 @@ class Item
                        $condition = ["`uri` = ? AND NOT `deleted` AND NOT (`uid` IN (?, 0))", $itemuri, $item["uid"]];
                        if (!self::exists($condition)) {
                                DBA::delete('item', ['uri' => $itemuri, 'uid' => 0]);
-                               Logger::log("deleteThread: Deleted shadow for item ".$itemuri, Logger::DEBUG);
+                               Logger::debug('Deleted shadow item', ['id' => $itemid, 'uri' => $itemuri]);
                        }
                }
        }
@@ -3352,7 +3385,7 @@ class Item
                 *
                 * default permissions - anonymous user
                 */
-               $sql = " AND NOT `item`.`private`";
+               $sql = sprintf(" AND `item`.`private` != %d", self::PRIVATE);
 
                // Profile owner - everything is visible
                if ($local_user && ($local_user == $owner_id)) {
@@ -3368,12 +3401,12 @@ class Item
                        $set = PermissionSet::get($owner_id, $remote_user);
 
                        if (!empty($set)) {
-                               $sql_set = " OR (`item`.`private` IN (1,2) AND `item`.`wall` AND `item`.`psid` IN (" . implode(',', $set) . "))";
+                               $sql_set = sprintf(" OR (`item`.`private` = %d AND `item`.`wall` AND `item`.`psid` IN (", self::PRIVATE) . implode(',', $set) . "))";
                        } else {
                                $sql_set = '';
                        }
 
-                       $sql = " AND (NOT `item`.`private`" . $sql_set . ")";
+                       $sql = sprintf(" AND (`item`.`private` != %d", self::PRIVATE) . $sql_set . ")";
                }
 
                return $sql;
@@ -3421,7 +3454,7 @@ class Item
                if ($rendered_hash == ''
                        || $rendered_html == ""
                        || $rendered_hash != hash("md5", $item["body"])
-                       || Config::get("system", "ignore_cache")
+                       || DI::config()->get("system", "ignore_cache")
                ) {
                        self::addRedirToImageTags($item);
 
@@ -3440,7 +3473,7 @@ class Item
                        }
 
                        // Only compare the HTML when we forcefully ignore the cache
-                       if (Config::get("system", "ignore_cache") && ($rendered_html != $item["rendered-html"])) {
+                       if (DI::config()->get("system", "ignore_cache") && ($rendered_html != $item["rendered-html"])) {
                                $update = true;
                        }
 
@@ -3475,7 +3508,7 @@ class Item
                                        continue;
                                }
 
-                               if ((local_user() == $item['uid']) && ($item['private'] == 1) && ($item['contact-id'] != $app->contact['id']) && ($item['network'] == Protocol::DFRN)) {
+                               if ((local_user() == $item['uid']) && ($item['private'] == self::PRIVATE) && ($item['contact-id'] != $app->contact['id']) && ($item['network'] == Protocol::DFRN)) {
                                        $img_url = 'redir/' . $item['contact-id'] . '?url=' . urlencode($mtch[1]);
                                        $item['body'] = str_replace($mtch[0], '[img]' . $img_url . '[/img]', $item['body']);
                                }
@@ -3600,7 +3633,7 @@ class Item
                        $title .= ' ' . $mtch[2] . ' ' . DI::l10n()->t('bytes');
 
                        $icon = '<div class="attachtype icon s22 type-' . $filetype . ' subtype-' . $filesubtype . '"></div>';
-                       $as .= '<a href="' . strip_tags($the_url) . '" title="' . $title . '" class="attachlink" target="_blank" >' . $icon . '</a>';
+                       $as .= '<a href="' . strip_tags($the_url) . '" title="' . $title . '" class="attachlink" target="_blank" rel="noopener noreferrer" >' . $icon . '</a>';
                }
 
                if ($as != '') {
@@ -3653,7 +3686,7 @@ class Item
                                $ret["title"] = DI::l10n()->t('link to source');
                        }
 
-               } elseif (!empty($item['plink']) && ($item['private'] != 1)) {
+               } elseif (!empty($item['plink']) && ($item['private'] != self::PRIVATE)) {
                        $ret = [
                                'href' => $item['plink'],
                                'orig' => $item['plink'],
@@ -3729,6 +3762,36 @@ class Item
                return 0;
        }
 
+       /**
+        * Return the URI for a link to the post 
+        * 
+        * @param string $uri URI or link to post
+        *
+        * @return string URI
+        */
+       public static function getURIByLink(string $uri)
+       {
+               $ssl_uri = str_replace('http://', 'https://', $uri);
+               $uris = [$uri, $ssl_uri, Strings::normaliseLink($uri)];
+
+               $item = DBA::selectFirst('item', ['uri'], ['uri' => $uris]);
+               if (DBA::isResult($item)) {
+                       return $item['uri'];
+               }
+
+               $itemcontent = DBA::selectFirst('item-content', ['uri-id'], ['plink' => $uris]);
+               if (!DBA::isResult($itemcontent)) {
+                       return '';
+               }
+
+               $itemuri = DBA::selectFirst('item-uri', ['uri'], ['id' => $itemcontent['uri-id']]);
+               if (DBA::isResult($itemuri)) {
+                       return $itemuri['uri'];
+               }
+
+               return '';
+       }
+
        /**
         * Fetches item for given URI or plink
         *
@@ -3744,8 +3807,8 @@ class Item
                        return $item_id;
                }
 
-               if (ActivityPub\Processor::fetchMissingActivity($uri)) {
-                       $item_id = self::searchByLink($uri, $uid);
+               if ($fetched_uri = ActivityPub\Processor::fetchMissingActivity($uri)) {
+                       $item_id = self::searchByLink($fetched_uri, $uid);
                } else {
                        $item_id = Diaspora::fetchByURL($uri);
                }