const PT_FETCHED = 75;
const PT_PERSONAL_NOTE = 128;
+ const LOCK_INSERT = 'item-insert';
+
// Field list that is used to display the items
const DISPLAY_FIELDLIST = [
'uid', 'id', 'parent', 'uri-id', 'uri', 'thr-parent', 'parent-uri', 'guid', 'network', 'gravity',
'commented', 'created', 'edited', 'received', 'verb', 'object-type', 'postopts', 'plink',
- 'wall', 'private', 'starred', 'origin', 'title', 'body', 'file', 'attach', 'language',
+ 'wall', 'private', 'starred', 'origin', 'title', 'body', 'file', 'language',
'content-warning', 'location', 'coord', 'app', 'rendered-hash', 'rendered-html', 'object',
'allow_cid', 'allow_gid', 'deny_cid', 'deny_gid', 'item_id',
'author-id', 'author-link', 'author-name', 'author-avatar', 'author-network',
const DELIVER_FIELDLIST = ['uid', 'id', 'parent', 'uri-id', 'uri', 'thr-parent', 'parent-uri', 'guid',
'parent-guid', 'created', 'edited', 'verb', 'object-type', 'object', 'target',
'private', 'title', 'body', 'location', 'coord', 'app',
- 'attach', 'deleted', 'extid', 'post-type', 'gravity',
+ 'deleted', 'extid', 'post-type', 'gravity',
'allow_cid', 'allow_gid', 'deny_cid', 'deny_gid',
'author-id', 'author-link', 'owner-link', 'contact-uid',
'signed_text', 'signature', 'signer', 'network'];
'guid', 'uri-id', 'parent-uri-id', 'thr-parent-id', 'vid',
'contact-id', 'type', 'wall', 'gravity', 'extid', 'icid', 'psid',
'created', 'edited', 'commented', 'received', 'changed', 'verb',
- 'postopts', 'plink', 'resource-id', 'event-id', 'attach', 'inform',
+ 'postopts', 'plink', 'resource-id', 'event-id', 'inform',
'file', 'allow_cid', 'allow_gid', 'deny_cid', 'deny_gid', 'post-type',
'private', 'pubmail', 'moderated', 'visible', 'starred', 'bookmark',
'unseen', 'deleted', 'origin', 'forum_mode', 'mention', 'global', 'network',
'guid', 'uri-id', 'parent-uri-id', 'thr-parent-id', 'vid', 'causer-id',
'contact-id', 'owner-id', 'author-id', 'type', 'wall', 'gravity', 'extid',
'created', 'edited', 'commented', 'received', 'changed', 'psid',
- 'resource-id', 'event-id', 'attach', 'post-type', 'file',
+ 'resource-id', 'event-id', 'post-type', 'file',
'private', 'pubmail', 'moderated', 'visible', 'starred', 'bookmark',
'unseen', 'deleted', 'origin', 'forum_mode', 'mention', 'global',
'id' => 'item_id', 'network', 'icid',
while ($item = DBA::fetch($items)) {
if (empty($content_fields['verb']) || !in_array($content_fields['verb'], self::ACTIVITIES)) {
+ if (!empty($content_fields['body'])) {
+ $content_fields['raw-body'] = trim($content_fields['raw-body'] ?? $content_fields['body']);
+
+ // Remove all media attachments from the body and store them in the post-media table
+ $content_fields['raw-body'] = Post\Media::insertFromBody($item['uri-id'], $content_fields['raw-body']);
+ $content_fields['raw-body'] = self::setHashtags($content_fields['raw-body']);
+ }
+
self::updateContent($content_fields, ['uri-id' => $item['uri-id']]);
if (empty($item['icid'])) {
}
}
+ if (!empty($fields['attach'])) {
+ Post\Media::insertFromAttachment($item['uri-id'], $fields['attach']);
+ }
+
Post\DeliveryData::update($item['uri-id'], $delivery_data);
self::updateThread($item['id']);
Logger::info('Mark item for deletion by id', ['id' => $item_id, 'callstack' => System::callstack()]);
// locate item to be deleted
$fields = ['id', 'uri', 'uri-id', 'uid', 'parent', 'parent-uri', 'origin',
- 'deleted', 'file', 'resource-id', 'event-id', 'attach',
+ 'deleted', 'file', 'resource-id', 'event-id',
'verb', 'object-type', 'object', 'target', 'contact-id',
'icid', 'psid', 'gravity'];
$item = self::selectFirst($fields, ['id' => $item_id]);
}
// If item has attachments, drop them
- /// @TODO: this should first check if attachment is used elsewhere
- foreach (explode(",", $item['attach']) as $attach) {
- preg_match("|attach/(\d+)|", $attach, $matches);
- if (is_array($matches) && count($matches) > 1) {
+ $attachments = Post\Media::getByURIId($item['uri-id'], [Post\Media::DOCUMENT]);
+ foreach($attachments as $attachment) {
+ if (preg_match("|attach/(\d+)|", $attachment['url'], $matches)) {
Attach::delete(['id' => $matches[1], 'uid' => $item['uid']]);
}
}
$item['private'] = intval($item['private'] ?? self::PUBLIC);
$item['body'] = trim($item['body'] ?? '');
$item['raw-body'] = trim($item['raw-body'] ?? $item['body']);
- $item['attach'] = trim($item['attach'] ?? '');
$item['app'] = trim($item['app'] ?? '');
$item['origin'] = intval($item['origin'] ?? 0);
$item['postopts'] = trim($item['postopts'] ?? '');
}
// Remove all media attachments from the body and store them in the post-media table
- $item['raw-body'] = Post\Media::addAttachmentsFromBody($item['uri-id'], $item['raw-body']);
+ $item['raw-body'] = Post\Media::insertFromBody($item['uri-id'], $item['raw-body']);
$item['raw-body'] = self::setHashtags($item['raw-body']);
// Check for hashtags in the body and repair or add hashtag links
$item['body'] = self::setHashtags($item['body']);
+ if (!empty($item['attach'])) {
+ Post\Media::insertFromAttachment($item['uri-id'], $item['attach']);
+ }
+
// Fill the cache field
self::putInCache($item);
}
}
- DBA::lock('item');
-
- $condition = ['uri-id' => $item['uri-id'], 'uid' => $item['uid'], 'network' => $item['network']];
- if (DBA::exists('item', $condition)) {
- DBA::unlock();
- Logger::notice('Item is already inserted - aborting', $condition);
- return 0;
- }
+ if (DI::lock()->acquire(self::LOCK_INSERT, 0)) {
+ $condition = ['uri-id' => $item['uri-id'], 'uid' => $item['uid'], 'network' => $item['network']];
+ if (DBA::exists('item', $condition)) {
+ DI::lock()->release(self::LOCK_INSERT);
+ Logger::notice('Item is already inserted - aborting', $condition);
+ return 0;
+ }
- $ret = DBA::insert('item', $item);
+ $result = DBA::insert('item', $item);
- // When the item was successfully stored we fetch the ID of the item.
- $current_post = DBA::lastInsertId();
- DBA::unlock();
+ // When the item was successfully stored we fetch the ID of the item.
+ $current_post = DBA::lastInsertId();
+ DI::lock()->release(self::LOCK_INSERT);
+ } else {
+ Logger::warning('Item lock had not been acquired');
+ $result = false;
+ $current_post = 0;
+ }
- if (!DBA::isResult($ret) || ($current_post == 0)) {
+ if (empty($current_post) || !DBA::isResult($result)) {
// On failure store the data into a spool file so that the "SpoolPost" worker can try again later.
- Logger::warning('Could not store item. it will be spooled', ['ret' => $ret, 'id' => $current_post]);
+ Logger::warning('Could not store item. it will be spooled', ['result' => $result, 'id' => $current_post]);
self::spool($orig_item);
return 0;
}
}
if ($contact['network'] != Protocol::FEED) {
+ $old_uri_id = $datarray["uri-id"] ?? 0;
$datarray["guid"] = System::createUUID();
unset($datarray["plink"]);
$datarray["uri"] = self::newURI($contact['uid'], $datarray["guid"]);
+ $datarray["uri-id"] = ItemURI::getIdByURI($datarray["uri"]);
$datarray["parent-uri"] = $datarray["uri"];
$datarray["thr-parent"] = $datarray["uri"];
$datarray["extid"] = Protocol::DFRN;
$urlpart = parse_url($datarray2['author-link']);
$datarray["app"] = $urlpart["host"];
+ if (!empty($old_uri_id)) {
+ Post\Media::copy($old_uri_id, $datarray["uri-id"]);
+ }
} else {
$datarray['private'] = self::PUBLIC;
}
}
}
+ /**
+ * Fetch the SQL condition for the given user id
+ *
+ * @param integer $owner_id User ID for which the permissions should be fetched
+ * @return array condition
+ */
+ public static function getPermissionsConditionArrayByUserId(int $owner_id)
+ {
+ $local_user = local_user();
+ $remote_user = Session::getRemoteContactID($owner_id);
+
+ // default permissions - anonymous user
+ $condition = ["`private` != ?", self::PRIVATE];
+
+ if ($local_user && ($local_user == $owner_id)) {
+ // Profile owner - everything is visible
+ $condition = [];
+ } elseif ($remote_user) {
+ // Authenticated visitor - fetch the matching permissionsets
+ $set = PermissionSet::get($owner_id, $remote_user);
+ if (!empty($set)) {
+ $condition = ["(`private` != ? OR (`private` = ? AND `wall`
+ AND `psid` IN (" . implode(', ', array_fill(0, count($set), '?')) . ")))",
+ Item::PRIVATE, Item::PRIVATE];
+ $condition = array_merge($condition, $set);
+ }
+ }
+
+ return $condition;
+ }
+
public static function getPermissionsSQLByUserId($owner_id)
{
$local_user = local_user();
$as = '';
$vhead = false;
- $matches = [];
- preg_match_all('|\[attach\]href=\"(.*?)\" length=\"(.*?)\" type=\"(.*?)\"(?: title=\"(.*?)\")?|', $item['attach'], $matches, PREG_SET_ORDER);
- foreach ($matches as $mtch) {
- $mime = $mtch[3];
+ foreach (Post\Media::getByURIId($item['uri-id'], [Post\Media::DOCUMENT, Post\Media::TORRENT, Post\Media::UNKNOWN]) as $attachment) {
+ $mime = $attachment['mimetype'];
- $the_url = Contact::magicLinkById($item['author-id'], $mtch[1]);
+ $the_url = Contact::magicLinkById($item['author-id'], $attachment['url']);
if (strpos($mime, 'video') !== false) {
if (!$vhead) {
$filesubtype = 'unkn';
}
- $title = Strings::escapeHtml(trim(($mtch[4] ?? '') ?: $mtch[1]));
- $title .= ' ' . $mtch[2] . ' ' . DI::l10n()->t('bytes');
+ $title = Strings::escapeHtml(trim(($attachment['description'] ?? '') ?: $attachment['url']));
+ $title .= ' ' . ($attachment['size'] ?? 0) . ' ' . 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" rel="noopener noreferrer" >' . $icon . '</a>';
$uid = $item['uid'] ?? 0;
// first try to fetch the item via the GUID. This will work for all reshares that had been created on this system
- $shared_item = self::selectFirst(['title', 'body', 'attach'], ['guid' => $shared['guid'], 'uid' => [0, $uid]]);
+ $shared_item = self::selectFirst(['title', 'body'], ['guid' => $shared['guid'], 'uid' => [0, $uid]]);
if (!DBA::isResult($shared_item)) {
if (empty($shared['link'])) {
return $item;
return $item;
}
- $shared_item = self::selectFirst(['title', 'body', 'attach'], ['id' => $id]);
+ $shared_item = self::selectFirst(['title', 'body'], ['id' => $id]);
if (!DBA::isResult($shared_item)) {
return $item;
}