3 * @copyright Copyright (C) 2020, Friendica
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 * This is the POST destination for most all locally posted
21 * text stuff. This function handles status, wall-to-wall status,
22 * local comments, and remote coments that are posted on this site
23 * (as opposed to being delivered in a feed).
24 * Also processed here are posts and comments coming through the
25 * statusnet/twitter API.
27 * All of these become an "item" which is our basic unit of
32 use Friendica\Content\Item as ItemHelper;
33 use Friendica\Content\Text\BBCode;
34 use Friendica\Core\Hook;
35 use Friendica\Core\Logger;
36 use Friendica\Core\Protocol;
37 use Friendica\Core\Session;
38 use Friendica\Core\System;
39 use Friendica\Core\Worker;
40 use Friendica\Database\DBA;
42 use Friendica\Model\Attach;
43 use Friendica\Model\Contact;
44 use Friendica\Model\Conversation;
45 use Friendica\Model\FileTag;
46 use Friendica\Model\Item;
47 use Friendica\Model\Notify\Type;
48 use Friendica\Model\Photo;
49 use Friendica\Model\Tag;
50 use Friendica\Network\HTTPException;
51 use Friendica\Object\EMail\ItemCCEMail;
52 use Friendica\Protocol\Activity;
53 use Friendica\Protocol\Diaspora;
54 use Friendica\Util\DateTimeFormat;
55 use Friendica\Util\Security;
56 use Friendica\Util\Strings;
57 use Friendica\Worker\Delivery;
59 require_once __DIR__ . '/../include/items.php';
61 function item_post(App $a) {
62 if (!Session::isAuthenticated()) {
63 throw new HTTPException\ForbiddenException();
68 if (!empty($_REQUEST['dropitems'])) {
69 $arr_drop = explode(',', $_REQUEST['dropitems']);
70 drop_items($arr_drop);
71 $json = ['success' => 1];
72 System::jsonExit($json);
75 Hook::callAll('post_local_start', $_REQUEST);
77 Logger::debug('postvars', ['_REQUEST' => $_REQUEST]);
79 $api_source = $_REQUEST['api_source'] ?? false;
81 $message_id = ((!empty($_REQUEST['message_id']) && $api_source) ? strip_tags($_REQUEST['message_id']) : '');
83 $return_path = $_REQUEST['return'] ?? '';
84 $preview = intval($_REQUEST['preview'] ?? 0);
87 * Check for doubly-submitted posts, and reject duplicates
88 * Note that we have to ignore previews, otherwise nothing will post
89 * after it's been previewed
91 if (!$preview && !empty($_REQUEST['post_id_random'])) {
92 if (!empty($_SESSION['post-random']) && $_SESSION['post-random'] == $_REQUEST['post_id_random']) {
93 Logger::info('item post: duplicate post');
94 item_post_return(DI::baseUrl(), $api_source, $return_path);
96 $_SESSION['post-random'] = $_REQUEST['post_id_random'];
100 // Is this a reply to something?
101 $toplevel_item_id = intval($_REQUEST['parent'] ?? 0);
102 $thr_parent_uri = trim($_REQUEST['parent_uri'] ?? '');
104 $toplevel_item = null;
108 $profile_uid = ($_REQUEST['profile_uid'] ?? 0) ?: local_user();
109 $posttype = ($_REQUEST['post_type'] ?? '') ?: Item::PT_ARTICLE;
111 if ($toplevel_item_id || $thr_parent_uri) {
112 if ($toplevel_item_id) {
113 $toplevel_item = Item::selectFirst([], ['id' => $toplevel_item_id]);
114 } elseif ($thr_parent_uri) {
115 $toplevel_item = Item::selectFirst([], ['uri' => $thr_parent_uri, 'uid' => $profile_uid]);
118 // if this isn't the top-level parent of the conversation, find it
119 if (DBA::isResult($toplevel_item)) {
120 // The URI and the contact is taken from the direct parent which needn't to be the top parent
121 $thr_parent_uri = $toplevel_item['uri'];
123 if ($toplevel_item['gravity'] != GRAVITY_PARENT) {
124 $toplevel_item = Item::selectFirst([], ['id' => $toplevel_item['parent']]);
128 if (!DBA::isResult($toplevel_item)) {
129 notice(DI::l10n()->t('Unable to locate original post.'));
131 DI::baseUrl()->redirect($return_path);
133 throw new HTTPException\NotFoundException(DI::l10n()->t('Unable to locate original post.'));
136 $toplevel_item_id = $toplevel_item['id'];
137 $parent_user = $toplevel_item['uid'];
139 $objecttype = Activity\ObjectType::COMMENT;
142 if ($toplevel_item_id) {
143 Logger::info('mod_item: item_post', ['parent' => $toplevel_item_id]);
146 $post_id = intval($_REQUEST['post_id'] ?? 0);
147 $app = strip_tags($_REQUEST['source'] ?? '');
148 $extid = strip_tags($_REQUEST['extid'] ?? '');
149 $object = $_REQUEST['object'] ?? '';
151 // Don't use "defaults" here. It would turn 0 to 1
152 if (!isset($_REQUEST['wall'])) {
155 $wall = $_REQUEST['wall'];
158 // Ensure that the user id in a thread always stay the same
159 if (!is_null($parent_user) && in_array($parent_user, [local_user(), 0])) {
160 $profile_uid = $parent_user;
163 // Check for multiple posts with the same message id (when the post was created via API)
164 if (($message_id != '') && ($profile_uid != 0)) {
165 if (Item::exists(['uri' => $message_id, 'uid' => $profile_uid])) {
166 Logger::info('Message already exists for user', ['uri' => $message_id, 'uid' => $profile_uid]);
171 // Allow commenting if it is an answer to a public post
172 $allow_comment = local_user() && ($profile_uid == 0) && $toplevel_item_id && in_array($toplevel_item['network'], Protocol::FEDERATED);
174 // Now check that valid personal details have been provided
175 if (!Security::canWriteToUserWall($profile_uid) && !$allow_comment) {
176 notice(DI::l10n()->t('Permission denied.'));
178 DI::baseUrl()->redirect($return_path);
181 throw new HTTPException\ForbiddenException(DI::l10n()->t('Permission denied.'));
184 // Init post instance
187 // is this an edited post?
189 $orig_post = Item::selectFirst(Item::ITEM_FIELDLIST, ['id' => $post_id]);
192 $user = DBA::selectFirst('user', [], ['uid' => $profile_uid]);
194 if (!DBA::isResult($user) && !$toplevel_item_id) {
201 $body = $_REQUEST['body'] ?? '';
202 $has_attachment = $_REQUEST['has_attachment'] ?? 0;
204 // If we have a speparate attachment, we need to add it to the body.
205 if (!empty($has_attachment)) {
206 $attachment_type = $_REQUEST['attachment_type'] ?? '';
207 $attachment_title = $_REQUEST['attachment_title'] ?? '';
208 $attachment_text = $_REQUEST['attachment_text'] ?? '';
210 $attachment_url = hex2bin($_REQUEST['attachment_url'] ?? '');
211 $attachment_img_src = hex2bin($_REQUEST['attachment_img_src'] ?? '');
213 $attachment_img_width = $_REQUEST['attachment_img_width'] ?? 0;
214 $attachment_img_height = $_REQUEST['attachment_img_height'] ?? 0;
216 'type' => $attachment_type,
217 'title' => $attachment_title,
218 'text' => $attachment_text,
219 'url' => $attachment_url,
222 if (!empty($attachment_img_src)) {
223 $attachment['images'] = [
225 'src' => $attachment_img_src,
226 'width' => $attachment_img_width,
227 'height' => $attachment_img_height
232 $att_bbcode = add_page_info_data($attachment);
233 $body .= $att_bbcode;
236 // Convert links with empty descriptions to links without an explicit description
237 $body = preg_replace('#\[url=([^\]]*?)\]\[/url\]#ism', '[url]$1[/url]', $body);
239 if (!empty($orig_post)) {
240 $str_group_allow = $orig_post['allow_gid'];
241 $str_contact_allow = $orig_post['allow_cid'];
242 $str_group_deny = $orig_post['deny_gid'];
243 $str_contact_deny = $orig_post['deny_cid'];
244 $location = $orig_post['location'];
245 $coord = $orig_post['coord'];
246 $verb = $orig_post['verb'];
247 $objecttype = $orig_post['object-type'];
248 $app = $orig_post['app'];
249 $categories = $orig_post['file'] ?? '';
250 $title = Strings::escapeTags(trim($_REQUEST['title']));
252 $private = $orig_post['private'];
253 $pubmail_enabled = $orig_post['pubmail'];
254 $network = $orig_post['network'];
255 $guid = $orig_post['guid'];
256 $extid = $orig_post['extid'];
258 $str_contact_allow = '';
259 $str_group_allow = '';
260 $str_contact_deny = '';
261 $str_group_deny = '';
263 if (($_REQUEST['visibility'] ?? '') !== 'public') {
264 $aclFormatter = DI::aclFormatter();
265 $str_contact_allow = isset($_REQUEST['contact_allow']) ? $aclFormatter->toString($_REQUEST['contact_allow']) : $user['allow_cid'] ?? '';
266 $str_group_allow = isset($_REQUEST['group_allow']) ? $aclFormatter->toString($_REQUEST['group_allow']) : $user['allow_gid'] ?? '';
267 $str_contact_deny = isset($_REQUEST['contact_deny']) ? $aclFormatter->toString($_REQUEST['contact_deny']) : $user['deny_cid'] ?? '';
268 $str_group_deny = isset($_REQUEST['group_deny']) ? $aclFormatter->toString($_REQUEST['group_deny']) : $user['deny_gid'] ?? '';
271 $title = Strings::escapeTags(trim($_REQUEST['title'] ?? ''));
272 $location = Strings::escapeTags(trim($_REQUEST['location'] ?? ''));
273 $coord = Strings::escapeTags(trim($_REQUEST['coord'] ?? ''));
274 $verb = Strings::escapeTags(trim($_REQUEST['verb'] ?? ''));
275 $emailcc = Strings::escapeTags(trim($_REQUEST['emailcc'] ?? ''));
277 $network = Strings::escapeTags(trim(($_REQUEST['network'] ?? '') ?: Protocol::DFRN));
278 $guid = System::createUUID();
280 $postopts = $_REQUEST['postopts'] ?? '';
282 if (strlen($str_group_allow) || strlen($str_contact_allow) || strlen($str_group_deny) || strlen($str_contact_deny)) {
283 $private = Item::PRIVATE;
284 } elseif (DI::pConfig()->get($profile_uid, 'system', 'unlisted')) {
285 $private = Item::UNLISTED;
287 $private = Item::PUBLIC;
290 // If this is a comment, set the permissions from the parent.
292 if ($toplevel_item) {
293 // for non native networks use the network of the original post as network of the item
294 if (($toplevel_item['network'] != Protocol::DIASPORA)
295 && ($toplevel_item['network'] != Protocol::OSTATUS)
296 && ($network == "")) {
297 $network = $toplevel_item['network'];
300 $str_contact_allow = $toplevel_item['allow_cid'] ?? '';
301 $str_group_allow = $toplevel_item['allow_gid'] ?? '';
302 $str_contact_deny = $toplevel_item['deny_cid'] ?? '';
303 $str_group_deny = $toplevel_item['deny_gid'] ?? '';
304 $private = $toplevel_item['private'];
306 $wall = $toplevel_item['wall'];
309 $pubmail_enabled = ($_REQUEST['pubmail_enable'] ?? false) && !$private;
311 // if using the API, we won't see pubmail_enable - figure out if it should be set
312 if ($api_source && $profile_uid && $profile_uid == local_user() && !$private) {
313 if (function_exists('imap_open') && !DI::config()->get('system', 'imap_disabled')) {
314 $pubmail_enabled = DBA::exists('mailacct', ["`uid` = ? AND `server` != ? AND `pubmail`", local_user(), '']);
318 if (!strlen($body)) {
320 System::jsonExit(['preview' => '']);
323 info(DI::l10n()->t('Empty post discarded.'));
325 DI::baseUrl()->redirect($return_path);
328 throw new HTTPException\BadRequestException(DI::l10n()->t('Empty post discarded.'));
332 if (!empty($categories)) {
333 // get the "fileas" tags for this post
334 $filedas = FileTag::fileToArray($categories);
337 // save old and new categories, so we can determine what needs to be deleted from pconfig
338 $categories_old = $categories;
339 $categories = FileTag::listToFile(trim($_REQUEST['category'] ?? ''), 'category');
340 $categories_new = $categories;
342 if (!empty($filedas) && is_array($filedas)) {
343 // append the fileas stuff to the new categories list
344 $categories .= FileTag::arrayToFile($filedas);
347 // get contact info for poster
353 if (local_user() && ((local_user() == $profile_uid) || $allow_comment)) {
355 $author = DBA::selectFirst('contact', [], ['uid' => local_user(), 'self' => true]);
356 } elseif (!empty(Session::getRemoteContactID($profile_uid))) {
357 $author = DBA::selectFirst('contact', [], ['id' => Session::getRemoteContactID($profile_uid)]);
360 if (DBA::isResult($author)) {
361 $contact_id = $author['id'];
364 // get contact info for owner
365 if ($profile_uid == local_user() || $allow_comment) {
366 $contact_record = $author;
368 $contact_record = DBA::selectFirst('contact', [], ['uid' => $profile_uid, 'self' => true]);
371 // Look for any tags and linkify them
373 $private_forum = false;
375 $only_to_forum = false;
378 BBCode::performWithEscapedTags($body, ['noparse', 'pre', 'code'], function ($body) use ($profile_uid, $network, $str_contact_allow, &$inform, &$private_forum, &$private_id, &$only_to_forum, &$forum_contact) {
379 $tags = BBCode::getTags($body);
383 foreach ($tags as $tag) {
384 $tag_type = substr($tag, 0, 1);
386 if ($tag_type == Tag::TAG_CHARACTER[Tag::HASHTAG]) {
390 /* If we already tagged 'Robert Johnson', don't try and tag 'Robert'.
391 * Robert Johnson should be first in the $tags array
393 foreach ($tagged as $nextTag) {
394 if (stristr($nextTag, $tag . ' ')) {
399 $success = ItemHelper::replaceTag($body, $inform, local_user() ? local_user() : $profile_uid, $tag, $network);
400 if ($success['replaced']) {
403 // When the forum is private or the forum is addressed with a "!" make the post private
404 if (!empty($success['contact']['prv']) || ($tag_type == Tag::TAG_CHARACTER[Tag::EXCLUSIVE_MENTION])) {
405 $private_forum = $success['contact']['prv'];
406 $only_to_forum = ($tag_type == Tag::TAG_CHARACTER[Tag::EXCLUSIVE_MENTION]);
407 $private_id = $success['contact']['id'];
408 $forum_contact = $success['contact'];
409 } elseif (!empty($success['contact']['forum']) && ($str_contact_allow == '<' . $success['contact']['id'] . '>')) {
410 $private_forum = false;
411 $only_to_forum = true;
412 $private_id = $success['contact']['id'];
413 $forum_contact = $success['contact'];
421 $original_contact_id = $contact_id;
423 if (!$toplevel_item_id && count($forum_contact) && ($private_forum || $only_to_forum)) {
424 // we tagged a forum in a top level post. Now we change the post
425 $private = $private_forum;
427 $str_group_allow = '';
428 $str_contact_deny = '';
429 $str_group_deny = '';
430 if ($private_forum) {
431 $str_contact_allow = '<' . $private_id . '>';
433 $str_contact_allow = '';
435 $contact_id = $private_id;
436 $contact_record = $forum_contact;
437 $_REQUEST['origin'] = false;
442 * When a photo was uploaded into the message using the (profile wall) ajax
443 * uploader, The permissions are initially set to disallow anybody but the
444 * owner from seeing it. This is because the permissions may not yet have been
445 * set for the post. If it's private, the photo permissions should be set
446 * appropriately. But we didn't know the final permissions on the post until
447 * now. So now we'll look for links of uploaded messages that are in the
448 * post and set them to the same permissions as the post itself.
453 if (!$preview && Photo::setPermissionFromBody($body, $uid, $original_contact_id, $str_contact_allow, $str_group_allow, $str_contact_deny, $str_group_deny)) {
454 $objecttype = Activity\ObjectType::IMAGE;
458 * Next link in any attachment references we find in the post.
462 /// @todo these lines should be moved to Model/Attach (Once it exists)
463 if (!$preview && preg_match_all("/\[attachment\](.*?)\[\/attachment\]/", $body, $match)) {
464 $attaches = $match[1];
465 if (count($attaches)) {
466 foreach ($attaches as $attach) {
467 // Ensure to only modify attachments that you own
468 $srch = '<' . intval($original_contact_id) . '>';
470 $condition = ['allow_cid' => $srch, 'allow_gid' => '', 'deny_cid' => '', 'deny_gid' => '',
472 if (!Attach::exists($condition)) {
476 $fields = ['allow_cid' => $str_contact_allow, 'allow_gid' => $str_group_allow,
477 'deny_cid' => $str_contact_deny, 'deny_gid' => $str_group_deny];
478 $condition = ['id' => $attach];
479 Attach::update($fields, $condition);
484 // embedded bookmark or attachment in post? set bookmark flag
486 $data = BBCode::getAttachmentData($body);
487 if ((preg_match_all("/\[bookmark\=([^\]]*)\](.*?)\[\/bookmark\]/ism", $body, $match, PREG_SET_ORDER) || isset($data["type"]))
488 && ($posttype != Item::PT_PERSONAL_NOTE)) {
489 $posttype = Item::PT_PAGE;
490 $objecttype = Activity\ObjectType::BOOKMARK;
493 $body = DI::bbCodeVideo()->transform($body);
495 $body = BBCode::scaleExternalImages($body);
497 // Setting the object type if not defined before
499 $objecttype = Activity\ObjectType::NOTE; // Default value
500 $objectdata = BBCode::getAttachedData($body);
502 if ($objectdata["type"] == "link") {
503 $objecttype = Activity\ObjectType::BOOKMARK;
504 } elseif ($objectdata["type"] == "video") {
505 $objecttype = Activity\ObjectType::VIDEO;
506 } elseif ($objectdata["type"] == "photo") {
507 $objecttype = Activity\ObjectType::IMAGE;
515 if (preg_match_all('/(\[attachment\]([0-9]+)\[\/attachment\])/',$body,$match)) {
516 foreach ($match[2] as $mtch) {
517 $fields = ['id', 'filename', 'filesize', 'filetype'];
518 $attachment = Attach::selectFirst($fields, ['id' => $mtch]);
519 if ($attachment !== false) {
520 if (strlen($attachments)) {
523 $attachments .= '[attach]href="' . DI::baseUrl() . '/attach/' . $attachment['id'] .
524 '" length="' . $attachment['filesize'] . '" type="' . $attachment['filetype'] .
525 '" title="' . ($attachment['filename'] ? $attachment['filename'] : '') . '"[/attach]';
527 $body = str_replace($match[1],'',$body);
531 if (!strlen($verb)) {
532 $verb = Activity::POST;
535 if ($network == "") {
536 $network = Protocol::DFRN;
539 $gravity = ($toplevel_item_id ? GRAVITY_COMMENT : GRAVITY_PARENT);
541 // even if the post arrived via API we are considering that it
542 // originated on this site by default for determining relayability.
544 // Don't use "defaults" here. It would turn 0 to 1
545 if (!isset($_REQUEST['origin'])) {
548 $origin = $_REQUEST['origin'];
551 $uri = ($message_id ? $message_id : Item::newURI($api_source ? $profile_uid : $uid, $guid));
553 // Fallback so that we alway have a parent uri
554 if (!$thr_parent_uri || !$toplevel_item_id) {
555 $thr_parent_uri = $uri;
559 $datarray['uid'] = $profile_uid;
560 $datarray['wall'] = $wall;
561 $datarray['gravity'] = $gravity;
562 $datarray['network'] = $network;
563 $datarray['contact-id'] = $contact_id;
564 $datarray['owner-name'] = $contact_record['name'];
565 $datarray['owner-link'] = $contact_record['url'];
566 $datarray['owner-avatar'] = $contact_record['thumb'];
567 $datarray['owner-id'] = Contact::getIdForURL($datarray['owner-link']);
568 $datarray['author-name'] = $author['name'];
569 $datarray['author-link'] = $author['url'];
570 $datarray['author-avatar'] = $author['thumb'];
571 $datarray['author-id'] = Contact::getIdForURL($datarray['author-link']);
572 $datarray['created'] = DateTimeFormat::utcNow();
573 $datarray['edited'] = DateTimeFormat::utcNow();
574 $datarray['commented'] = DateTimeFormat::utcNow();
575 $datarray['received'] = DateTimeFormat::utcNow();
576 $datarray['changed'] = DateTimeFormat::utcNow();
577 $datarray['extid'] = $extid;
578 $datarray['guid'] = $guid;
579 $datarray['uri'] = $uri;
580 $datarray['title'] = $title;
581 $datarray['body'] = $body;
582 $datarray['app'] = $app;
583 $datarray['location'] = $location;
584 $datarray['coord'] = $coord;
585 $datarray['file'] = $categories;
586 $datarray['inform'] = $inform;
587 $datarray['verb'] = $verb;
588 $datarray['post-type'] = $posttype;
589 $datarray['object-type'] = $objecttype;
590 $datarray['allow_cid'] = $str_contact_allow;
591 $datarray['allow_gid'] = $str_group_allow;
592 $datarray['deny_cid'] = $str_contact_deny;
593 $datarray['deny_gid'] = $str_group_deny;
594 $datarray['private'] = $private;
595 $datarray['pubmail'] = $pubmail_enabled;
596 $datarray['attach'] = $attachments;
598 // This is not a bug. The item store function changes 'parent-uri' to 'thr-parent' and fetches 'parent-uri' new. (We should change this)
599 $datarray['parent-uri'] = $thr_parent_uri;
601 $datarray['postopts'] = $postopts;
602 $datarray['origin'] = $origin;
603 $datarray['moderated'] = false;
604 $datarray['object'] = $object;
607 * These fields are for the convenience of addons...
608 * 'self' if true indicates the owner is posting on their own wall
609 * If parent is 0 it is a top-level post.
611 $datarray['parent'] = $toplevel_item_id;
612 $datarray['self'] = $self;
614 // This triggers posts via API and the mirror functions
615 $datarray['api_source'] = $api_source;
617 // This field is for storing the raw conversation data
618 $datarray['protocol'] = Conversation::PARCEL_DFRN;
620 $conversation = DBA::selectFirst('conversation', ['conversation-uri', 'conversation-href'], ['item-uri' => $datarray['parent-uri']]);
621 if (DBA::isResult($conversation)) {
622 if ($conversation['conversation-uri'] != '') {
623 $datarray['conversation-uri'] = $conversation['conversation-uri'];
625 if ($conversation['conversation-href'] != '') {
626 $datarray['conversation-href'] = $conversation['conversation-href'];
631 $datarray['edit'] = true;
633 // If this was a share, add missing data here
634 $datarray = Item::addShareDataFromOriginal($datarray);
636 $datarray['edit'] = false;
639 // Check for hashtags in the body and repair or add hashtag links
640 if ($preview || $orig_post) {
641 $datarray['body'] = Item::setHashtags($datarray['body']);
644 // preview mode - prepare the body for display and send it via json
646 // We set the datarray ID to -1 because in preview mode the dataray
647 // doesn't have an ID.
648 $datarray["id"] = -1;
649 $datarray["uri-id"] = -1;
650 $datarray["item_id"] = -1;
651 $datarray["author-network"] = Protocol::DFRN;
653 $o = conversation($a, [array_merge($contact_record, $datarray)], 'search', false, true);
655 System::jsonExit(['preview' => $o]);
658 Hook::callAll('post_local',$datarray);
660 if (!empty($datarray['cancel'])) {
661 Logger::info('mod_item: post cancelled by addon.');
663 DI::baseUrl()->redirect($return_path);
666 $json = ['cancel' => 1];
667 if (!empty($_REQUEST['jsreload'])) {
668 $json['reload'] = DI::baseUrl() . '/' . $_REQUEST['jsreload'];
671 System::jsonExit($json);
675 // Fill the cache field
676 // This could be done in Item::update as well - but we have to check for the existance of some fields.
677 Item::putInCache($datarray);
680 'title' => $datarray['title'],
681 'body' => $datarray['body'],
682 'attach' => $datarray['attach'],
683 'file' => $datarray['file'],
684 'rendered-html' => $datarray['rendered-html'],
685 'rendered-hash' => $datarray['rendered-hash'],
686 'edited' => DateTimeFormat::utcNow(),
687 'changed' => DateTimeFormat::utcNow()];
689 Item::update($fields, ['id' => $post_id]);
691 // update filetags in pconfig
692 FileTag::updatePconfig($uid, $categories_old, $categories_new, 'category');
694 info(DI::l10n()->t('Post updated.'));
696 DI::baseUrl()->redirect($return_path);
699 throw new HTTPException\OKException(DI::l10n()->t('Post updated.'));
702 unset($datarray['edit']);
703 unset($datarray['self']);
704 unset($datarray['api_source']);
707 $signed = Diaspora::createCommentSignature($uid, $datarray);
708 if (!empty($signed)) {
709 $datarray['diaspora_signed_text'] = json_encode($signed);
713 $post_id = Item::insert($datarray);
716 info(DI::l10n()->t('Item wasn\'t stored.'));
718 DI::baseUrl()->redirect($return_path);
721 throw new HTTPException\InternalServerErrorException(DI::l10n()->t('Item wasn\'t stored.'));
724 $datarray = Item::selectFirst(Item::ITEM_FIELDLIST, ['id' => $post_id]);
726 if (!DBA::isResult($datarray)) {
727 Logger::error('Item couldn\'t be fetched.', ['post_id' => $post_id]);
729 DI::baseUrl()->redirect($return_path);
732 throw new HTTPException\InternalServerErrorException(DI::l10n()->t('Item couldn\'t be fetched.'));
735 Tag::storeFromBody($datarray['uri-id'], $datarray['body']);
737 if (!\Friendica\Content\Feature::isEnabled($uid, 'explicit_mentions') && ($datarray['gravity'] == GRAVITY_COMMENT)) {
738 Tag::createImplicitMentions($datarray['uri-id'], $datarray['thr-parent-id']);
741 // update filetags in pconfig
742 FileTag::updatePconfig($uid, $categories_old, $categories_new, 'category');
744 // These notifications are sent if someone else is commenting other your wall
745 if ($toplevel_item_id) {
746 if ($contact_record != $author) {
748 'type' => Type::COMMENT,
749 'notify_flags' => $user['notify-flags'],
750 'language' => $user['language'],
751 'to_name' => $user['username'],
752 'to_email' => $user['email'],
753 'uid' => $user['uid'],
755 'link' => DI::baseUrl().'/display/'.urlencode($datarray['guid']),
756 'source_name' => $datarray['author-name'],
757 'source_link' => $datarray['author-link'],
758 'source_photo' => $datarray['author-avatar'],
759 'verb' => Activity::POST,
761 'parent' => $toplevel_item_id,
762 'parent_uri' => $toplevel_item['uri']
766 if (($contact_record != $author) && !count($forum_contact)) {
768 'type' => Type::WALL,
769 'notify_flags' => $user['notify-flags'],
770 'language' => $user['language'],
771 'to_name' => $user['username'],
772 'to_email' => $user['email'],
773 'uid' => $user['uid'],
775 'link' => DI::baseUrl().'/display/'.urlencode($datarray['guid']),
776 'source_name' => $datarray['author-name'],
777 'source_link' => $datarray['author-link'],
778 'source_photo' => $datarray['author-avatar'],
779 'verb' => Activity::POST,
785 Hook::callAll('post_local_end', $datarray);
787 if (strlen($emailcc) && $profile_uid == local_user()) {
788 $recipients = explode(',', $emailcc);
789 if (count($recipients)) {
790 foreach ($recipients as $recipient) {
791 $address = trim($recipient);
792 if (!strlen($address)) {
795 DI::emailer()->send(new ItemCCEMail(DI::app(), DI::l10n(), DI::baseUrl(),
796 $datarray, $address, $author['thumb'] ?? ''));
801 // Insert an item entry for UID=0 for global entries.
802 // We now do it in the background to save some time.
803 // This is important in interactive environments like the frontend or the API.
804 // We don't fork a new process since this is done anyway with the following command
805 Worker::add(['priority' => PRIORITY_HIGH, 'dont_fork' => true], "CreateShadowEntry", $post_id);
807 // When we are doing some forum posting via ! we have to start the notifier manually.
808 // These kind of posts don't initiate the notifier call in the item class.
809 if ($only_to_forum) {
810 Worker::add(['priority' => PRIORITY_HIGH, 'dont_fork' => false], "Notifier", Delivery::POST, $post_id);
813 Logger::info('post_complete');
819 info(DI::l10n()->t('Post published.'));
820 item_post_return(DI::baseUrl(), $api_source, $return_path);
824 function item_post_return($baseurl, $api_source, $return_path)
831 DI::baseUrl()->redirect($return_path);
834 $json = ['success' => 1];
835 if (!empty($_REQUEST['jsreload'])) {
836 $json['reload'] = $baseurl . '/' . $_REQUEST['jsreload'];
839 Logger::info('post_json', ['json' => $json]);
841 System::jsonExit($json);
844 function item_content(App $a)
846 if (!Session::isAuthenticated()) {
852 if (($a->argc >= 3) && ($a->argv[1] === 'drop') && intval($a->argv[2])) {
853 if (DI::mode()->isAjax()) {
854 $o = Item::deleteForUser(['id' => $a->argv[2]], local_user());
856 if (!empty($a->argv[3])) {
857 $o = drop_item($a->argv[2], $a->argv[3]);
860 $o = drop_item($a->argv[2]);
864 if (DI::mode()->isAjax()) {
865 // ajax return: [<item id>, 0 (no perm) | <owner id>]
866 System::jsonExit([intval($a->argv[2]), intval($o)]);