+ /**
+ * Returns the origin uid of a post if the given user is allowed to see it.
+ *
+ * @param int $uriid
+ * @param int $uid
+ * @return int
+ */
+ private static function GetOriginUidForUriId(int $uriid, int $uid)
+ {
+ if (Post::exists(['uri-id' => $uriid, 'uid' => $uid])) {
+ return $uid;
+ }
+
+ $post = Post::selectFirst(['uid', 'allow_cid', 'allow_gid', 'deny_cid', 'deny_gid', 'private'], ['uri-id' => $uriid, 'origin' => true]);
+ if (!empty($post)) {
+ if (in_array($post['private'], [Item::PUBLIC, Item::UNLISTED])) {
+ return $post['uid'];
+ }
+
+ $pcid = Contact::getPublicIdByUserId($uid);
+ if (empty($pcid)) {
+ return null;
+ }
+
+ foreach (Item::enumeratePermissions($post, true) as $receiver) {
+ if ($receiver == $pcid) {
+ return $post['uid'];
+ }
+ }
+
+ return null;
+ }
+
+ if (Post::exists(['uri-id' => $uriid, 'uid' => 0])) {
+ return 0;
+ }
+
+ // When the post belongs to a a forum then all forum users are allowed to access it
+ foreach (Tag::getByURIId($uriid, [Tag::MENTION, Tag::EXCLUSIVE_MENTION]) as $tag) {
+ if (DBA::exists('contact', ['uid' => $uid, 'nurl' => Strings::normaliseLink($tag['url']), 'contact-type' => Contact::TYPE_COMMUNITY])) {
+ $target_uid = User::getIdForURL($tag['url']);
+ if (!empty($target_uid)) {
+ return $target_uid;
+ }
+ }
+ }
+
+ return null;
+ }
+