]> git.mxchange.org Git - friendica.git/blobdiff - src/Model/Item.php
Merge pull request #9621 from MrPetovan/bug/9611-bbcode-convert-html-purify
[friendica.git] / src / Model / Item.php
index 4e0cc5cd0d9964606a2b44efe39c89f6fa9fa940..d41e84c5b9f44c5e6ff0b1d61cb15c9d542c56a3 100644 (file)
@@ -233,6 +233,8 @@ class Item
                        return $row;
                }
 
+               $row = DBA::castFields('item', $row);
+
                // ---------------------- Transform item structure data ----------------------
 
                // We prefer the data from the user's contact over the public one
@@ -1065,9 +1067,6 @@ class Item
                        // "Deleting" global items just means hiding them
                        if ($item['uid'] == 0) {
                                DBA::update('user-item', ['hidden' => true], ['iid' => $item['id'], 'uid' => $uid], true);
-
-                               // Delete notifications
-                               DBA::delete('notify', ['iid' => $item['id'], 'uid' => $uid]);
                        } elseif ($item['uid'] == $uid) {
                                self::markForDeletionById($item['id'], PRIORITY_HIGH);
                        } else {
@@ -1155,9 +1154,6 @@ class Item
                        }
                }
 
-               // Delete notifications
-               DBA::delete('notify', ['iid' => $item['id'], 'uid' => $item['uid']]);
-
                // Set the item to "deleted"
                $item_fields = ['deleted' => true, 'edited' => DateTimeFormat::utcNow(), 'changed' => DateTimeFormat::utcNow()];
                DBA::update('item', $item_fields, ['id' => $item['id']]);
@@ -1171,9 +1167,6 @@ class Item
 
                Post\DeliveryData::delete($item['uri-id']);
 
-               if (!empty($item['icid']) && !self::exists(['icid' => $item['icid'], 'deleted' => false])) {
-                       DBA::delete('item-content', ['id' => $item['icid']], ['cascade' => false]);
-               }
                // When the permission set will be used in photo and events as well,
                // this query here needs to be extended.
                // @todo Currently deactivated. We need the permission set in the deletion process.
@@ -1188,12 +1181,14 @@ class Item
                }
 
                // Is it our comment and/or our thread?
-               if ($item['origin'] || $parent['origin']) {
+               if (($item['origin'] || $parent['origin']) && ($item['uid'] != 0)) {
                        // When we delete the original post we will delete all existing copies on the server as well
                        self::markForDeletion(['uri' => $item['uri'], 'deleted' => false], $priority);
 
                        // send the notification upstream/downstream
-                       Worker::add(['priority' => $priority, 'dont_fork' => true], "Notifier", Delivery::DELETION, intval($item['id']));
+                       if ($priority) {
+                               Worker::add(['priority' => $priority, 'dont_fork' => true], "Notifier", Delivery::DELETION, intval($item['id']));
+                       }
                } elseif ($item['uid'] != 0) {
                        Post\User::update($item['uri-id'], $item['uid'], ['hidden' => true]);
 
@@ -1389,27 +1384,6 @@ class Item
                        return false;
                }
 
-               // check for create date and expire time
-               $expire_interval = DI::config()->get('system', 'dbclean-expire-days', 0);
-
-               $user = DBA::selectFirst('user', ['expire'], ['uid' => $item['uid']]);
-               if (DBA::isResult($user) && ($user['expire'] > 0) && (($user['expire'] < $expire_interval) || ($expire_interval == 0))) {
-                       $expire_interval = $user['expire'];
-               }
-
-               if (($expire_interval > 0) && !empty($item['created'])) {
-                       $expire_date = time() - ($expire_interval * 86400);
-                       $created_date = strtotime($item['created']);
-                       if ($created_date < $expire_date) {
-                               Logger::notice('Item created before expiration interval.', [
-                                       'created' => date('c', $created_date),
-                                       'expired' => date('c', $expire_date),
-                                       '$item' => $item
-                               ]);
-                               return false;
-                       }
-               }
-
                if (!empty($item['author-id']) && Contact::isBlocked($item['author-id'])) {
                        Logger::notice('Author is blocked node-wide', ['author-link' => $item['author-link'], 'item-uri' => $item['uri']]);
                        return false;
@@ -1453,6 +1427,38 @@ class Item
                return true;
        }
 
+       /**
+        * Check if the item array is too old
+        *
+        * @param array $item
+        * @return boolean item is too old
+        */
+       public static function isTooOld(array $item)
+       {
+               // check for create date and expire time
+               $expire_interval = DI::config()->get('system', 'dbclean-expire-days', 0);
+
+               $user = DBA::selectFirst('user', ['expire'], ['uid' => $item['uid']]);
+               if (DBA::isResult($user) && ($user['expire'] > 0) && (($user['expire'] < $expire_interval) || ($expire_interval == 0))) {
+                       $expire_interval = $user['expire'];
+               }
+
+               if (($expire_interval > 0) && !empty($item['created'])) {
+                       $expire_date = time() - ($expire_interval * 86400);
+                       $created_date = strtotime($item['created']);
+                       if ($created_date < $expire_date) {
+                               Logger::notice('Item created before expiration interval.', [
+                                       'created' => date('c', $created_date),
+                                       'expired' => date('c', $expire_date),
+                                       '$item' => $item
+                               ]);
+                               return true;
+                       }
+               }
+
+               return false;
+       }
+
        /**
         * Return the id of the given item array if it has been stored before
         *
@@ -1560,7 +1566,7 @@ class Item
                        $item['wall'] = 1;
                        $item['origin'] = 1;
                        $item['network'] = Protocol::DFRN;
-                       $item['protocol'] = Conversation::PARCEL_DFRN;
+                       $item['protocol'] = Conversation::PARCEL_DIRECT;
 
                        if (is_int($notify)) {
                                $priority = $notify;
@@ -1886,14 +1892,14 @@ class Item
                        Tag::storeFromBody($item['uri-id'], $body);
                }
 
-               // Remove all fields that aren't part of the item table
-               foreach ($item as $field => $value) {
-                       if (!in_array($field, $structure['item'])) {
-                               unset($item[$field]);
+               if (Post\User::insert($item['uri-id'], $item['uid'], $item)) {
+                       // Remove all fields that aren't part of the item table
+                       foreach ($item as $field => $value) {
+                               if (!in_array($field, $structure['item'])) {
+                                       unset($item[$field]);
+                               }
                        }
-               }
 
-               if (Post\User::insert($item['uri-id'], $item['uid'], $item)) {
                        $condition = ['uri-id' => $item['uri-id'], 'uid' => $item['uid'], 'network' => $item['network']];
                        if (DBA::exists('item', $condition)) {
                                Logger::notice('Item is already inserted - aborting', $condition);
@@ -1986,6 +1992,9 @@ class Item
                // Distribute items to users who subscribed to their tags
                self::distributeByTags($item);
 
+               // Automatically reshare the item if the "remote_self" option is selected
+               self::autoReshare($item);
+
                $transmit = $notify || ($item['visible'] && ($parent_origin || $item['origin']));
 
                if ($transmit) {
@@ -2790,6 +2799,31 @@ class Item
                return false;
        }
 
+       /**
+        * Automatically reshare the item if the "remote_self" option is selected
+        *
+        * @param array $item
+        * @return void
+        */
+       private static function autoReshare(array $item)
+       {
+               if ($item['gravity'] != GRAVITY_PARENT) {
+                       return;
+               }
+
+               if (!DBA::exists('contact', ['id' => $item['contact-id'], 'remote_self' => Contact::MIRROR_NATIVE_RESHARE])) {
+                       return;
+               }
+
+               if (!in_array($item['network'], [Protocol::ACTIVITYPUB, Protocol::DFRN])) {
+                       return;
+               }
+
+               Logger::info('Automatically reshare item', ['uid' => $item['uid'], 'id' => $item['id'], 'guid' => $item['guid'], 'uri-id' => $item['uri-id']]);
+
+               Item::performActivity($item['id'], 'announce', $item['uid']);
+       }
+
        public static function isRemoteSelf($contact, &$datarray)
        {
                if (!$contact['remote_self']) {
@@ -2821,7 +2855,7 @@ class Item
 
                $datarray2 = $datarray;
                Logger::info('remote-self start', ['contact' => $contact['url'], 'remote_self'=> $contact['remote_self'], 'item' => $datarray]);
-               if ($contact['remote_self'] == 2) {
+               if ($contact['remote_self'] == Contact::MIRROR_OWN_POST) {
                        $self = DBA::selectFirst('contact', ['id', 'name', 'url', 'thumb'],
                                        ['uid' => $contact['uid'], 'self' => true]);
                        if (DBA::isResult($self)) {
@@ -3033,7 +3067,7 @@ class Item
                return $recipients;
        }
 
-       public static function expire($uid, $days, $network = "", $force = false)
+       public static function expire(int $uid, int $days, string $network = "", bool $force = false)
        {
                if (!$uid || ($days < 1)) {
                        return;
@@ -3079,6 +3113,8 @@ class Item
 
                $expired = 0;
 
+               $priority = DI::config()->get('system', 'expire-notify-priority');
+
                while ($item = Item::fetch($items)) {
                        // don't expire filed items
 
@@ -3098,7 +3134,7 @@ class Item
                                continue;
                        }
 
-                       self::markForDeletionById($item['id'], PRIORITY_LOW);
+                       self::markForDeletionById($item['id'], $priority);
 
                        ++$expired;
                }
@@ -3493,20 +3529,21 @@ class Item
         */
        public static function putInCache(&$item, $update = false)
        {
-               $body = $item["body"];
+               // Save original body to prevent addons to modify it
+               $body = $item['body'];
 
                $rendered_hash = $item['rendered-hash'] ?? '';
                $rendered_html = $item['rendered-html'] ?? '';
 
                if ($rendered_hash == ''
-                       || $rendered_html == ""
-                       || $rendered_hash != hash("md5", $item["body"])
-                       || DI::config()->get("system", "ignore_cache")
+                       || $rendered_html == ''
+                       || $rendered_hash != hash('md5', BBCode::VERSION . '::' . $body)
+                       || DI::config()->get('system', 'ignore_cache')
                ) {
                        self::addRedirToImageTags($item);
 
-                       $item["rendered-html"] = BBCode::convert($item["body"]);
-                       $item["rendered-hash"] = hash("md5", $item["body"]);
+                       $item['rendered-html'] = BBCode::convert($item['body']);
+                       $item['rendered-hash'] = hash('md5', BBCode::VERSION . '::' . $body);
 
                        $hook_data = ['item' => $item, 'rendered-html' => $item['rendered-html'], 'rendered-hash' => $item['rendered-hash']];
                        Hook::callAll('put_item_in_cache', $hook_data);
@@ -3515,27 +3552,27 @@ class Item
                        unset($hook_data);
 
                        // Force an update if the generated values differ from the existing ones
-                       if ($rendered_hash != $item["rendered-hash"]) {
+                       if ($rendered_hash != $item['rendered-hash']) {
                                $update = true;
                        }
 
                        // Only compare the HTML when we forcefully ignore the cache
-                       if (DI::config()->get("system", "ignore_cache") && ($rendered_html != $item["rendered-html"])) {
+                       if (DI::config()->get('system', 'ignore_cache') && ($rendered_html != $item['rendered-html'])) {
                                $update = true;
                        }
 
-                       if ($update && !empty($item["id"])) {
+                       if ($update && !empty($item['id'])) {
                                self::update(
                                        [
-                                               'rendered-html' => $item["rendered-html"],
-                                               'rendered-hash' => $item["rendered-hash"]
+                                               'rendered-html' => $item['rendered-html'],
+                                               'rendered-hash' => $item['rendered-hash']
                                        ],
-                                       ['id' => $item["id"]]
+                                       ['id' => $item['id']]
                                );
                        }
                }
 
-               $item["body"] = $body;
+               $item['body'] = $body;
        }
 
        /**