]> git.mxchange.org Git - friendica.git/blobdiff - src/Model/Item.php
Check null for acl-fields
[friendica.git] / src / Model / Item.php
index ff0f46676f0e6d560991a0551505c9fb0a9db599..e298cc1605288a57df3cf1794ce699167f0a9fda 100644 (file)
@@ -17,13 +17,15 @@ use Friendica\Core\Logger;
 use Friendica\Core\PConfig;
 use Friendica\Core\Protocol;
 use Friendica\Core\Renderer;
-use Friendica\Core\System;
 use Friendica\Core\Session;
+use Friendica\Core\System;
 use Friendica\Core\Worker;
 use Friendica\Database\DBA;
+use Friendica\Protocol\Activity;
 use Friendica\Protocol\ActivityPub;
 use Friendica\Protocol\Diaspora;
 use Friendica\Protocol\OStatus;
+use Friendica\Util\ACLFormatter;
 use Friendica\Util\DateTimeFormat;
 use Friendica\Util\Map;
 use Friendica\Util\Network;
@@ -95,7 +97,11 @@ class Item extends BaseObject
 
        // Never reorder or remove entries from this list. Just add new ones at the end, if needed.
        // The item-activity table only stores the index and needs this array to know the matching activity.
-       const ACTIVITIES = [ACTIVITY_LIKE, ACTIVITY_DISLIKE, ACTIVITY_ATTEND, ACTIVITY_ATTENDNO, ACTIVITY_ATTENDMAYBE, ACTIVITY_FOLLOW, ACTIVITY2_ANNOUNCE];
+       const ACTIVITIES = [
+               Activity::LIKE, Activity::DISLIKE,
+               Activity::ATTEND, Activity::ATTENDNO, Activity::ATTENDMAYBE,
+               Activity::FOLLOW,
+               Activity::ANNOUNCE];
 
        private static $legacy_mode = null;
 
@@ -208,9 +214,9 @@ class Item extends BaseObject
                                $row['object'] = '';
                        }
                        if (array_key_exists('object-type', $row)) {
-                               $row['object-type'] = ACTIVITY_OBJ_NOTE;
+                               $row['object-type'] = Activity\ObjectType::NOTE;
                        }
-               } elseif (array_key_exists('verb', $row) && in_array($row['verb'], ['', ACTIVITY_POST, ACTIVITY_SHARE])) {
+               } elseif (array_key_exists('verb', $row) && in_array($row['verb'], ['', Activity::POST, Activity::SHARE])) {
                        // Posts don't have an object or target - but having tags or files.
                        // We safe some performance by building tag and file strings only here.
                        // We remove object and target since they aren't used for this type.
@@ -222,7 +228,7 @@ class Item extends BaseObject
                        }
                }
 
-               if (!array_key_exists('verb', $row) || in_array($row['verb'], ['', ACTIVITY_POST, ACTIVITY_SHARE])) {
+               if (!array_key_exists('verb', $row) || in_array($row['verb'], ['', Activity::POST, Activity::SHARE])) {
                        // Build the tag string out of the term entries
                        if (array_key_exists('tag', $row) && empty($row['tag'])) {
                                $row['tag'] = Term::tagTextFromItemId($row['internal-iid']);
@@ -1151,14 +1157,14 @@ class Item extends BaseObject
 
        private static function deleteTagsFromItem($item)
        {
-               if (($item["verb"] != ACTIVITY_TAG) || ($item["object-type"] != ACTIVITY_OBJ_TAGTERM)) {
+               if (($item["verb"] != Activity::TAG) || ($item["object-type"] != Activity\ObjectType::TAGTERM)) {
                        return;
                }
 
                $xo = XML::parseString($item["object"], false);
                $xt = XML::parseString($item["target"], false);
 
-               if ($xt->type != ACTIVITY_OBJ_NOTE) {
+               if ($xt->type != Activity\ObjectType::NOTE) {
                        return;
                }
 
@@ -1357,13 +1363,16 @@ class Item extends BaseObject
                        $item['parent-uri'] = $item['thr-parent'];
                }
 
+               /** @var Activity $activity */
+               $activity = self::getClass(Activity::class);
+
                if (isset($item['gravity'])) {
                        $item['gravity'] = intval($item['gravity']);
                } elseif ($item['parent-uri'] === $item['uri']) {
                        $item['gravity'] = GRAVITY_PARENT;
-               } elseif (activity_match($item['verb'], ACTIVITY_POST)) {
+               } elseif ($activity->match($item['verb'], Activity::POST)) {
                        $item['gravity'] = GRAVITY_COMMENT;
-               } elseif (activity_match($item['verb'], ACTIVITY_FOLLOW)) {
+               } elseif ($activity->match($item['verb'], Activity::FOLLOW)) {
                        $item['gravity'] = GRAVITY_ACTIVITY;
                } else {
                        $item['gravity'] = GRAVITY_UNKNOWN;   // Should not happen
@@ -1559,14 +1568,14 @@ class Item extends BaseObject
                        return 0;
                }
 
-               if ($item['verb'] == ACTIVITY_FOLLOW) {
+               if ($item['verb'] == Activity::FOLLOW) {
                        if (!$item['origin'] && ($item['author-id'] == Contact::getPublicIdByUserId($uid))) {
                                // Our own follow request can be relayed to us. We don't store it to avoid notification chaos.
                                Logger::log("Follow: Don't store not origin follow request from us for " . $item['parent-uri'], Logger::DEBUG);
                                return 0;
                        }
 
-                       $condition = ['verb' => ACTIVITY_FOLLOW, 'uid' => $item['uid'],
+                       $condition = ['verb' => Activity::FOLLOW, 'uid' => $item['uid'],
                                'parent-uri' => $item['parent-uri'], 'author-id' => $item['author-id']];
                        if (self::exists($condition)) {
                                // It happens that we receive multiple follow requests by the same author - we only store one.
@@ -1673,7 +1682,7 @@ class Item extends BaseObject
                        }
                }
 
-               if (stristr($item['verb'], ACTIVITY_POKE)) {
+               if (stristr($item['verb'], Activity::POKE)) {
                        $notify_type = Delivery::POKE;
                }
 
@@ -2686,7 +2695,7 @@ class Item extends BaseObject
                }
 
                // Only forward posts
-               if ($datarray["verb"] != ACTIVITY_POST) {
+               if ($datarray["verb"] != Activity::POST) {
                        Logger::log('No post', Logger::DEBUG);
                        return false;
                }
@@ -2892,10 +2901,13 @@ class Item extends BaseObject
         */
        public static function enumeratePermissions(array $obj, bool $check_dead = false)
        {
-               $allow_people = expand_acl($obj['allow_cid']);
-               $allow_groups = Group::expand($obj['uid'], expand_acl($obj['allow_gid']), $check_dead);
-               $deny_people  = expand_acl($obj['deny_cid']);
-               $deny_groups  = Group::expand($obj['uid'], expand_acl($obj['deny_gid']), $check_dead);
+               /** @var ACLFormatter $aclFormater */
+               $aclFormater = self::getClass(ACLFormatter::class);
+
+               $allow_people = $aclFormater->expand($obj['allow_cid'] ?? '');
+               $allow_groups = Group::expand($obj['uid'], $aclFormater->expand($obj['allow_gid'] ?? ''), $check_dead);
+               $deny_people  = $aclFormater->expand($obj['deny_cid'] ?? '');
+               $deny_groups  = Group::expand($obj['uid'], $aclFormater->expand($obj['deny_gid'] ?? ''), $check_dead);
                $recipients   = array_unique(array_merge($allow_people, $allow_groups));
                $deny         = array_unique(array_merge($deny_people, $deny_groups));
                $recipients   = array_diff($recipients, $deny);
@@ -3036,23 +3048,23 @@ class Item extends BaseObject
                switch ($verb) {
                        case 'like':
                        case 'unlike':
-                               $activity = ACTIVITY_LIKE;
+                               $activity = Activity::LIKE;
                                break;
                        case 'dislike':
                        case 'undislike':
-                               $activity = ACTIVITY_DISLIKE;
+                               $activity = Activity::DISLIKE;
                                break;
                        case 'attendyes':
                        case 'unattendyes':
-                               $activity = ACTIVITY_ATTEND;
+                               $activity = Activity::ATTEND;
                                break;
                        case 'attendno':
                        case 'unattendno':
-                               $activity = ACTIVITY_ATTENDNO;
+                               $activity = Activity::ATTENDNO;
                                break;
                        case 'attendmaybe':
                        case 'unattendmaybe':
-                               $activity = ACTIVITY_ATTENDMAYBE;
+                               $activity = Activity::ATTENDMAYBE;
                                break;
                        default:
                                Logger::log('like: unknown verb ' . $verb . ' for item ' . $item_id);
@@ -3060,7 +3072,7 @@ class Item extends BaseObject
                }
 
                // Enable activity toggling instead of on/off
-               $event_verb_flag = $activity === ACTIVITY_ATTEND || $activity === ACTIVITY_ATTENDNO || $activity === ACTIVITY_ATTENDMAYBE;
+               $event_verb_flag = $activity === Activity::ATTEND || $activity === Activity::ATTENDNO || $activity === Activity::ATTENDMAYBE;
 
                Logger::log('like: verb ' . $verb . ' item ' . $item_id);
 
@@ -3114,7 +3126,7 @@ class Item extends BaseObject
                // event participation are essentially radio toggles. If you make a subsequent choice,
                // we need to eradicate your first choice.
                if ($event_verb_flag) {
-                       $verbs = [ACTIVITY_ATTEND, ACTIVITY_ATTENDNO, ACTIVITY_ATTENDMAYBE];
+                       $verbs = [Activity::ATTEND, Activity::ATTENDNO, Activity::ATTENDMAYBE];
 
                        // Translate to the index based activity index
                        $activities = [];
@@ -3144,7 +3156,7 @@ class Item extends BaseObject
                        return true;
                }
 
-               $objtype = $item['resource-id'] ? ACTIVITY_OBJ_IMAGE : ACTIVITY_OBJ_NOTE;
+               $objtype = $item['resource-id'] ? Activity\ObjectType::IMAGE : Activity\ObjectType::NOTE;
 
                $new_item = [
                        'guid'          => System::createUUID(),
@@ -3310,7 +3322,7 @@ class Item extends BaseObject
                        return L10n::t('event');
                } elseif (!empty($item['resource-id'])) {
                        return L10n::t('photo');
-               } elseif (!empty($item['verb']) && $item['verb'] !== ACTIVITY_POST) {
+               } elseif (!empty($item['verb']) && $item['verb'] !== Activity::POST) {
                        return L10n::t('activity');
                } elseif ($item['id'] != $item['parent']) {
                        return L10n::t('comment');
@@ -3342,10 +3354,9 @@ class Item extends BaseObject
                        || $rendered_hash != hash("md5", $item["body"])
                        || Config::get("system", "ignore_cache")
                ) {
-                       $a = self::getApp();
-                       redir_private_images($a, $item);
+                       self::addRedirToImageTags($item);
 
-                       $item["rendered-html"] = prepare_text($item["body"]);
+                       $item["rendered-html"] = BBCode::convert($item["body"]);
                        $item["rendered-hash"] = hash("md5", $item["body"]);
 
                        $hook_data = ['item' => $item, 'rendered-html' => $item['rendered-html'], 'rendered-hash' => $item['rendered-hash']];
@@ -3378,6 +3389,31 @@ class Item extends BaseObject
                $item["body"] = $body;
        }
 
+       /**
+        * @brief Find any non-embedded images in private items and add redir links to them
+        *
+        * @param array &$item The field array of an item row
+        */
+       private static function addRedirToImageTags(array &$item)
+       {
+               $app = self::getApp();
+
+               $matches = [];
+               $cnt = preg_match_all('|\[img\](http[^\[]*?/photo/[a-fA-F0-9]+?(-[0-9]\.[\w]+?)?)\[\/img\]|', $item['body'], $matches, PREG_SET_ORDER);
+               if ($cnt) {
+                       foreach ($matches as $mtch) {
+                               if (strpos($mtch[1], '/redir') !== false) {
+                                       continue;
+                               }
+
+                               if ((local_user() == $item['uid']) && ($item['private'] == 1) && ($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']);
+                               }
+                       }
+               }
+       }
+
        /**
         * @brief Given an item array, convert the body element from bbcode to html and add smilie icons.
         * If attach is true, also add icons for item attachments.
@@ -3400,7 +3436,7 @@ class Item extends BaseObject
 
                // In order to provide theme developers more possibilities, event items
                // are treated differently.
-               if ($item['object-type'] === ACTIVITY_OBJ_EVENT && isset($item['event-id'])) {
+               if ($item['object-type'] === Activity\ObjectType::EVENT && isset($item['event-id'])) {
                        $ev = Event::getItemHTML($item);
                        return $ev;
                }