]> git.mxchange.org Git - friendica.git/blobdiff - src/Model/Item.php
Use rawContent for Special Options to avoid a protected options() method
[friendica.git] / src / Model / Item.php
index 8327252abc7acc2f69bb062cb732e06884edd2e9..f923fe2691d84109e6ec20f0e918fed26a4d2da6 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /**
- * @copyright Copyright (C) 2010-2021, the Friendica project
+ * @copyright Copyright (C) 2010-2022, the Friendica project
  *
  * @license GNU AGPL version 3 or any later version
  *
@@ -88,7 +88,7 @@ class Item
                'writable', 'self', 'cid', 'alias',
                'event-created', 'event-edited', 'event-start', 'event-finish',
                'event-summary', 'event-desc', 'event-location', 'event-type',
-               'event-nofinish', 'event-adjust', 'event-ignore', 'event-id',
+               'event-nofinish', 'event-ignore', 'event-id',
                'delivery_queue_count', 'delivery_queue_done', 'delivery_queue_failed'
        ];
 
@@ -103,7 +103,7 @@ class Item
                        'thr-parent-id', 'parent-uri-id', 'postopts', 'pubmail',
                        'event-created', 'event-edited', 'event-start', 'event-finish',
                        'event-summary', 'event-desc', 'event-location', 'event-type',
-                       'event-nofinish', 'event-adjust', 'event-ignore', 'event-id'];
+                       'event-nofinish', 'event-ignore', 'event-id'];
 
        // All fields in the item table
        const ITEM_FIELDLIST = ['id', 'uid', 'parent', 'uri', 'parent-uri', 'thr-parent',
@@ -366,7 +366,7 @@ class Item
        public static function guid($item, $notify)
        {
                if (!empty($item['guid'])) {
-                       return Strings::escapeTags(trim($item['guid']));
+                       return trim($item['guid']);
                }
 
                if ($notify) {
@@ -448,7 +448,7 @@ class Item
                // We use "microtime" to keep the arrival order and "mt_rand" to avoid duplicates
                $file = 'item-' . round(microtime(true) * 10000) . '-' . mt_rand() . '.msg';
 
-               $spoolpath = get_spoolpath();
+               $spoolpath = System::getSpoolPath();
                if ($spoolpath != "") {
                        $spool = $spoolpath . '/' . $file;
 
@@ -949,7 +949,9 @@ class Item
                        $item['parent'] = $parent_id;
 
                        // Trigger automatic reactions for addons
-                       $item['api_source'] = true;
+                       if (!isset($item['api_source'])) {
+                               $item['api_source'] = true;
+                       }
 
                        // We have to tell the hooks who we are - this really should be improved
                        if (!local_user()) {
@@ -971,7 +973,7 @@ class Item
                }
 
                if (!empty($item['cancel'])) {
-                       Logger::log('post cancelled by addon.');
+                       Logger::notice('post cancelled by addon.');
                        return 0;
                }
 
@@ -980,13 +982,14 @@ class Item
                }
 
                // Creates or assigns the permission set
-               $item['psid'] = PermissionSet::getIdFromACL(
-                       $item['uid'],
-                       $item['allow_cid'],
-                       $item['allow_gid'],
-                       $item['deny_cid'],
-                       $item['deny_gid']
-               );
+               $item['psid'] = DI::permissionSet()->selectOrCreate(
+                       DI::permissionSetFactory()->createFromString(
+                               $item['uid'],
+                               $item['allow_cid'],
+                               $item['allow_gid'],
+                               $item['deny_cid'],
+                               $item['deny_gid']
+               ))->id;
 
                if (!empty($item['extid'])) {
                        $item['external-id'] = ItemURI::getIdByURI($item['extid']);
@@ -1766,15 +1769,15 @@ class Item
                        } else {
                                $condition = ['id' => $arr['contact-id'], 'self' => false];
                        }
-                       DBA::update('contact', ['failed' => false, 'success_update' => $arr['received'], 'last-item' => $arr['received']], $condition);
+                       Contact::update(['failed' => false, 'success_update' => $arr['received'], 'last-item' => $arr['received']], $condition);
                }
                // Now do the same for the system wide contacts with uid=0
                if ($arr['private'] != self::PRIVATE) {
-                       DBA::update('contact', ['failed' => false, 'success_update' => $arr['received'], 'last-item' => $arr['received']],
+                       Contact::update(['failed' => false, 'success_update' => $arr['received'], 'last-item' => $arr['received']],
                                ['id' => $arr['owner-id']]);
 
                        if ($arr['owner-id'] != $arr['author-id']) {
-                               DBA::update('contact', ['failed' => false, 'success_update' => $arr['received'], 'last-item' => $arr['received']],
+                               Contact::update(['failed' => false, 'success_update' => $arr['received'], 'last-item' => $arr['received']],
                                        ['id' => $arr['author-id']]);
                        }
                }
@@ -1884,7 +1887,7 @@ class Item
                        foreach ($matches as $mtch) {
                                if (Strings::compareLink($link, $mtch[1]) || Strings::compareLink($dlink, $mtch[1])) {
                                        $mention = true;
-                                       Logger::log('mention found: ' . $mtch[2]);
+                                       Logger::notice('mention found', ['mention' => $mtch[2]]);
                                }
                        }
                }
@@ -1952,18 +1955,19 @@ class Item
                        $private = self::PUBLIC;
                }
 
-               $psid = PermissionSet::getIdFromACL(
-                       $user['uid'],
-                       $user['allow_cid'],
-                       $user['allow_gid'],
-                       $user['deny_cid'],
-                       $user['deny_gid']
-               );
+               $permissionSet = DI::permissionSet()->selectOrCreate(
+                       DI::permissionSetFactory()->createFromString(
+                               $user['uid'],
+                               $user['allow_cid'],
+                               $user['allow_gid'],
+                               $user['deny_cid'],
+                               $user['deny_gid']
+                       ));
 
                $forum_mode = ($prvgroup ? 2 : 1);
 
                $fields = ['wall' => true, 'origin' => true, 'forum_mode' => $forum_mode, 'contact-id' => $self['id'],
-                       'owner-id' => $owner_id, 'private' => $private, 'psid' => $psid];
+                       'owner-id' => $owner_id, 'private' => $private, 'psid' => $permissionSet->id];
                self::update($fields, ['id' => $item['id']]);
 
                Worker::add(['priority' => PRIORITY_HIGH, 'dont_fork' => true], 'Notifier', Delivery::POST, (int)$item['uri-id'], (int)$item['uid']);
@@ -2256,8 +2260,8 @@ class Item
                        $condition[] = $network;
                }
 
-               $condition[0] .= " AND `received` < UTC_TIMESTAMP() - INTERVAL ? DAY";
-               $condition[] = $days;
+               $condition[0] .= " AND `received` < ?";
+               $condition[] = DateTimeFormat::utc('now - ' . $days . ' day');
 
                $items = Post::select(['resource-id', 'starred', 'id', 'post-type', 'uid', 'uri-id'], $condition);
 
@@ -2303,7 +2307,7 @@ class Item
                        ++$expired;
                }
                DBA::close($items);
-               Logger::log('User ' . $uid . ": expired $expired items; expire items: $expire_items, expire notes: $expire_notes, expire starred: $expire_starred, expire photos: $expire_photos");
+               Logger::notice('User ' . $uid . ": expired $expired items; expire items: $expire_items, expire notes: $expire_notes, expire starred: $expire_starred, expire photos: $expire_photos");
        }
 
        public static function firstPostDate($uid, $wall = false)
@@ -2352,7 +2356,7 @@ class Item
 
                $item = Post::selectFirst(self::ITEM_FIELDLIST, ['id' => $item_id]);
                if (!DBA::isResult($item)) {
-                       Logger::log('like: unknown item ' . $item_id);
+                       Logger::notice('like: unknown item', ['id' => $item_id]);
                        return false;
                }
 
@@ -2549,12 +2553,12 @@ class Item
                        $condition = [];
                } elseif ($remote_user) {
                         // Authenticated visitor - fetch the matching permissionsets
-                       $set = PermissionSet::get($owner_id, $remote_user);
+                       $permissionSets = DI::permissionSet()->selectByContactId($remote_user, $owner_id);
                        if (!empty($set)) {
                                $condition = ["(`private` != ? OR (`private` = ? AND `wall`
                                        AND `psid` IN (" . implode(', ', array_fill(0, count($set), '?')) . ")))",
                                        self::PRIVATE, self::PRIVATE];
-                               $condition = array_merge($condition, $set);
+                               $condition = array_merge($condition, $permissionSets->column('id'));
                        }
                }
 
@@ -2595,10 +2599,10 @@ class Item
                         * If pre-verified, the caller is expected to have already
                         * done this and passed the groups into this function.
                         */
-                       $set = PermissionSet::get($owner_id, $remote_user);
+                       $permissionSets = DI::permissionSet()->selectByContactId($remote_user, $owner_id);
 
                        if (!empty($set)) {
-                               $sql_set = sprintf(" OR (" . $table . "`private` = %d AND " . $table . "`wall` AND " . $table . "`psid` IN (", self::PRIVATE) . implode(',', $set) . "))";
+                               $sql_set = sprintf(" OR (" . $table . "`private` = %d AND " . $table . "`wall` AND " . $table . "`psid` IN (", self::PRIVATE) . implode(',', $permissionSets->column('id')) . "))";
                        } else {
                                $sql_set = '';
                        }
@@ -2988,8 +2992,9 @@ class Item
        private static function addLinkAttachment(int $uriid, array $attachments, string $body, string $content, bool $shared, array $ignore_links)
        {
                DI::profiler()->startRecording('rendering');
-               // @ToDo Check only for audio and video
-               $preview = empty($attachments['visual']);
+               // Don't show a preview when there is a visual attachment (audio or video)
+               $types = array_column($attachments['visual'], 'type');
+               $preview = !in_array(Post\Media::IMAGE, $types) && !in_array(Post\Media::VIDEO, $types);
 
                if (!empty($attachments['link'])) {
                        foreach ($attachments['link'] as $link) {
@@ -3260,6 +3265,18 @@ class Item
                        return $item_id;
                }
 
+               $hookData = [
+                       'uri'     => $uri,
+                       'uid'     => $uid,
+                       'item_id' => null,
+               ];
+
+               Hook::callAll('item_by_link', $hookData);
+
+               if (isset($hookData['item_id'])) {
+                       return is_numeric($hookData['item_id']) ? $hookData['item_id'] : 0;
+               }
+
                if ($fetched_uri = ActivityPub\Processor::fetchMissingActivity($uri)) {
                        $item_id = self::searchByLink($fetched_uri, $uid);
                } else {
@@ -3352,7 +3369,7 @@ class Item
                        $body = $shared_item['body'];
                }
 
-               $item['body'] = preg_replace("/\[share ([^\[\]]*)\].*\[\/share\]/ism", '[share $1]' . $body . '[/share]', $item['body']);
+               $item['body'] = preg_replace("/\[share ([^\[\]]*)\].*\[\/share\]/ism", '[share $1]' . str_replace('$', '\$', $body) . '[/share]', $item['body']);
                unset($shared_item['body']);
 
                return array_merge($item, $shared_item);