7 * This is the POST destination for most all locally posted
8 * text stuff. This function handles status, wall-to-wall status,
9 * local comments, and remote coments that are posted on this site
10 * (as opposed to being delivered in a feed).
11 * Also processed here are posts and comments coming through the
12 * statusnet/twitter API.
14 * All of these become an "item" which is our basic unit of
19 use Friendica\Content\Text\BBCode;
20 use Friendica\Core\Addon;
21 use Friendica\Core\Config;
22 use Friendica\Core\L10n;
23 use Friendica\Core\System;
24 use Friendica\Core\Worker;
25 use Friendica\Database\DBM;
26 use Friendica\Model\Contact;
27 use Friendica\Model\GContact;
28 use Friendica\Model\Item;
29 use Friendica\Protocol\Diaspora;
30 use Friendica\Protocol\Email;
31 use Friendica\Util\DateTimeFormat;
32 use Friendica\Util\Emailer;
34 require_once 'include/enotify.php';
35 require_once 'include/text.php';
36 require_once 'include/items.php';
38 function item_post(App $a) {
39 if (!local_user() && !remote_user()) {
43 require_once 'include/security.php';
47 if (x($_REQUEST, 'dropitems')) {
48 $arr_drop = explode(',', $_REQUEST['dropitems']);
49 drop_items($arr_drop);
50 $json = ['success' => 1];
51 echo json_encode($json);
55 Addon::callHooks('post_local_start', $_REQUEST);
57 logger('postvars ' . print_r($_REQUEST,true), LOGGER_DATA);
59 $api_source = defaults($_REQUEST, 'api_source', false);
61 $message_id = ((x($_REQUEST, 'message_id') && $api_source) ? strip_tags($_REQUEST['message_id']) : '');
63 $return_path = defaults($_REQUEST, 'return', '');
64 $preview = intval(defaults($_REQUEST, 'preview', 0));
67 * Check for doubly-submitted posts, and reject duplicates
68 * Note that we have to ignore previews, otherwise nothing will post
69 * after it's been previewed
71 if (!$preview && x($_REQUEST, 'post_id_random')) {
72 if (x($_SESSION, 'post-random') && $_SESSION['post-random'] == $_REQUEST['post_id_random']) {
73 logger("item post: duplicate post", LOGGER_DEBUG);
74 item_post_return(System::baseUrl(), $api_source, $return_path);
76 $_SESSION['post-random'] = $_REQUEST['post_id_random'];
80 // Is this a reply to something?
81 $thr_parent = intval(defaults($_REQUEST, 'parent', 0));
82 $thr_parent_uri = trim(defaults($_REQUEST, 'parent_uri', ''));
84 $thr_parent_contact = null;
90 $parent_contact = null;
93 $profile_uid = defaults($_REQUEST, 'profile_uid', local_user());
95 if ($thr_parent || $thr_parent_uri) {
97 $parent_item = dba::selectFirst('item', [], ['id' => $thr_parent]);
98 } elseif ($thr_parent_uri) {
99 $parent_item = dba::selectFirst('item', [], ['uri' => $thr_parent_uri, 'uid' => $profile_uid]);
102 // if this isn't the real parent of the conversation, find it
103 if (DBM::is_result($parent_item)) {
105 // The URI and the contact is taken from the direct parent which needn't to be the top parent
106 $thr_parent_uri = $parent_item['uri'];
107 $thr_parent_contact = Contact::getDetailsByURL($parent_item["author-link"]);
109 if ($parent_item['id'] != $parent_item['parent']) {
110 $parent_item = dba::selectFirst('item', [], ['id' => $parent_item['parent']]);
114 if (!DBM::is_result($parent_item)) {
115 notice(L10n::t('Unable to locate original post.') . EOL);
116 if (x($_REQUEST, 'return')) {
117 goaway($return_path);
122 $parent = $parent_item['id'];
123 $parent_user = $parent_item['uid'];
125 $parent_contact = Contact::getDetailsByURL($parent_item["author-link"]);
127 $objecttype = ACTIVITY_OBJ_COMMENT;
129 if (!x($_REQUEST, 'type')) {
130 $_REQUEST['type'] = 'net-comment';
135 logger('mod_item: item_post parent=' . $parent);
138 $post_id = intval(defaults($_REQUEST, 'post_id', 0));
139 $app = strip_tags(defaults($_REQUEST, 'source', ''));
140 $extid = strip_tags(defaults($_REQUEST, 'extid', ''));
141 $object = defaults($_REQUEST, 'object', '');
143 // Ensure that the user id in a thread always stay the same
144 if (!is_null($parent_user) && in_array($parent_user, [local_user(), 0])) {
145 $profile_uid = $parent_user;
148 // Check for multiple posts with the same message id (when the post was created via API)
149 if (($message_id != '') && ($profile_uid != 0)) {
150 if (dba::exists('item', ['uri' => $message_id, 'uid' => $profile_uid])) {
151 logger("Message with URI ".$message_id." already exists for user ".$profile_uid, LOGGER_DEBUG);
156 // Allow commenting if it is an answer to a public post
157 $allow_comment = local_user() && ($profile_uid == 0) && $parent && in_array($parent_item['network'], [NETWORK_OSTATUS, NETWORK_DIASPORA, NETWORK_DFRN]);
159 // Now check that valid personal details have been provided
160 if (!can_write_wall($profile_uid) && !$allow_comment) {
161 notice(L10n::t('Permission denied.') . EOL) ;
162 if (x($_REQUEST, 'return')) {
163 goaway($return_path);
169 // is this an edited post?
174 $orig_post = dba::selectFirst('item', [], ['id' => $post_id]);
177 $user = dba::selectFirst('user', [], ['uid' => $profile_uid]);
178 if (!DBM::is_result($user) && !$parent) {
183 $str_group_allow = $orig_post['allow_gid'];
184 $str_contact_allow = $orig_post['allow_cid'];
185 $str_group_deny = $orig_post['deny_gid'];
186 $str_contact_deny = $orig_post['deny_cid'];
187 $location = $orig_post['location'];
188 $coord = $orig_post['coord'];
189 $verb = $orig_post['verb'];
190 $objecttype = $orig_post['object-type'];
191 $emailcc = $orig_post['emailcc'];
192 $app = $orig_post['app'];
193 $categories = $orig_post['file'];
194 $title = notags(trim($_REQUEST['title']));
195 $body = escape_tags(trim($_REQUEST['body']));
196 $private = $orig_post['private'];
197 $pubmail_enabled = $orig_post['pubmail'];
198 $network = $orig_post['network'];
199 $guid = $orig_post['guid'];
200 $extid = $orig_post['extid'];
205 * if coming from the API and no privacy settings are set,
206 * use the user default permissions - as they won't have
207 * been supplied via a form.
209 /// @TODO use x($_REQUEST, 'foo') here
211 && !array_key_exists('contact_allow', $_REQUEST)
212 && !array_key_exists('group_allow', $_REQUEST)
213 && !array_key_exists('contact_deny', $_REQUEST)
214 && !array_key_exists('group_deny', $_REQUEST)) {
215 $str_group_allow = $user['allow_gid'];
216 $str_contact_allow = $user['allow_cid'];
217 $str_group_deny = $user['deny_gid'];
218 $str_contact_deny = $user['deny_cid'];
220 // use the posted permissions
221 $str_group_allow = perms2str($_REQUEST['group_allow']);
222 $str_contact_allow = perms2str($_REQUEST['contact_allow']);
223 $str_group_deny = perms2str($_REQUEST['group_deny']);
224 $str_contact_deny = perms2str($_REQUEST['contact_deny']);
227 $title = notags(trim($_REQUEST['title']));
228 $location = notags(trim($_REQUEST['location']));
229 $coord = notags(trim($_REQUEST['coord']));
230 $verb = notags(trim($_REQUEST['verb']));
231 $emailcc = notags(trim($_REQUEST['emailcc']));
232 $body = escape_tags(trim($_REQUEST['body']));
233 $network = notags(trim($_REQUEST['network']));
234 $guid = get_guid(32);
236 $postopts = defaults($_REQUEST, 'postopts', '');
238 $private = ((strlen($str_group_allow) || strlen($str_contact_allow) || strlen($str_group_deny) || strlen($str_contact_deny)) ? 1 : 0);
240 if ($user['hidewall']) {
244 // If this is a comment, set the permissions from the parent.
248 // for non native networks use the network of the original post as network of the item
249 if (($parent_item['network'] != NETWORK_DIASPORA)
250 && ($parent_item['network'] != NETWORK_OSTATUS)
251 && ($network == "")) {
252 $network = $parent_item['network'];
255 $str_contact_allow = $parent_item['allow_cid'];
256 $str_group_allow = $parent_item['allow_gid'];
257 $str_contact_deny = $parent_item['deny_cid'];
258 $str_group_deny = $parent_item['deny_gid'];
259 $private = $parent_item['private'];
262 $pubmail_enabled = defaults($_REQUEST, 'pubmail_enable', false) && !$private;
264 // if using the API, we won't see pubmail_enable - figure out if it should be set
265 if ($api_source && $profile_uid && $profile_uid == local_user() && !$private) {
266 if (function_exists('imap_open') && !Config::get('system', 'imap_disabled')) {
267 $pubmail_enabled = dba::exists('mailacct', ["`uid` = ? AND `server` != ? AND `pubmail`", local_user(), '']);
271 if (!strlen($body)) {
275 info(L10n::t('Empty post discarded.') . EOL);
276 if (x($_REQUEST, 'return')) {
277 goaway($return_path);
283 if (strlen($categories)) {
284 // get the "fileas" tags for this post
285 $filedas = file_tag_file_to_list($categories, 'file');
287 // save old and new categories, so we can determine what needs to be deleted from pconfig
288 $categories_old = $categories;
289 $categories = file_tag_list_to_file(trim($_REQUEST['category']), 'category');
290 $categories_new = $categories;
291 if (strlen($filedas)) {
292 // append the fileas stuff to the new categories list
293 $categories .= file_tag_list_to_file($filedas, 'file');
296 // get contact info for poster
302 if (local_user() && ((local_user() == $profile_uid) || $allow_comment)) {
304 $author = dba::selectFirst('contact', [], ['uid' => local_user(), 'self' => true]);
305 } elseif (remote_user()) {
306 if (x($_SESSION, 'remote') && is_array($_SESSION['remote'])) {
307 foreach ($_SESSION['remote'] as $v) {
308 if ($v['uid'] == $profile_uid) {
309 $contact_id = $v['cid'];
315 $author = dba::selectFirst('contact', [], ['id' => $contact_id]);
319 if (DBM::is_result($author)) {
320 $contact_id = $author['id'];
323 // get contact info for owner
324 if ($profile_uid == local_user() || $allow_comment) {
325 $contact_record = $author;
327 $contact_record = dba::selectFirst('contact', [], ['uid' => $profile_uid, 'self' => true]);
330 $post_type = notags(trim($_REQUEST['type']));
332 if ($post_type === 'net-comment' && $parent_item !== null) {
333 if ($parent_item['wall'] == 1) {
334 $post_type = 'wall-comment';
336 $post_type = 'remote-comment';
340 // Look for any tags and linkify them
344 $tags = get_tags($body);
346 // Add a tag if the parent contact is from OStatus (This will notify them during delivery)
348 if ($thr_parent_contact['network'] == NETWORK_OSTATUS) {
349 $contact = '@[url=' . $thr_parent_contact['url'] . ']' . $thr_parent_contact['nick'] . '[/url]';
350 if (!stripos(implode($tags), '[url=' . $thr_parent_contact['url'] . ']')) {
355 if ($parent_contact['network'] == NETWORK_OSTATUS) {
356 $contact = '@[url=' . $parent_contact['url'] . ']' . $parent_contact['nick'] . '[/url]';
357 if (!stripos(implode($tags), '[url=' . $parent_contact['url'] . ']')) {
365 $private_forum = false;
366 $only_to_forum = false;
370 foreach ($tags as $tag) {
371 $tag_type = substr($tag, 0, 1);
373 if ($tag_type == '#') {
378 * If we already tagged 'Robert Johnson', don't try and tag 'Robert'.
379 * Robert Johnson should be first in the $tags array
381 $fullnametagged = false;
382 /// @TODO $tagged is initialized above if () block and is not filled, maybe old-lost code?
383 foreach ($tagged as $nextTag) {
384 if (stristr($nextTag, $tag . ' ')) {
385 $fullnametagged = true;
389 if ($fullnametagged) {
393 $success = handle_tag($a, $body, $inform, $str_tags, local_user() ? local_user() : $profile_uid, $tag, $network);
394 if ($success['replaced']) {
397 // When the forum is private or the forum is addressed with a "!" make the post private
398 if (is_array($success['contact']) && ($success['contact']['prv'] || ($tag_type == '!'))) {
399 $private_forum = $success['contact']['prv'];
400 $only_to_forum = ($tag_type == '!');
401 $private_id = $success['contact']['id'];
402 $forum_contact = $success['contact'];
403 } elseif (is_array($success['contact']) && $success['contact']['forum'] &&
404 ($str_contact_allow == '<' . $success['contact']['id'] . '>')) {
405 $private_forum = false;
406 $only_to_forum = true;
407 $private_id = $success['contact']['id'];
408 $forum_contact = $success['contact'];
413 $original_contact_id = $contact_id;
415 if (!$parent && count($forum_contact) && ($private_forum || $only_to_forum)) {
416 // we tagged a forum in a top level post. Now we change the post
417 $private = $private_forum;
419 $str_group_allow = '';
420 $str_contact_deny = '';
421 $str_group_deny = '';
422 if ($private_forum) {
423 $str_contact_allow = '<' . $private_id . '>';
425 $str_contact_allow = '';
427 $contact_id = $private_id;
428 $contact_record = $forum_contact;
429 $_REQUEST['origin'] = false;
433 * When a photo was uploaded into the message using the (profile wall) ajax
434 * uploader, The permissions are initially set to disallow anybody but the
435 * owner from seeing it. This is because the permissions may not yet have been
436 * set for the post. If it's private, the photo permissions should be set
437 * appropriately. But we didn't know the final permissions on the post until
438 * now. So now we'll look for links of uploaded messages that are in the
439 * post and set them to the same permissions as the post itself.
444 /// @todo these lines should be moved to Model/Photo
445 if (!$preview && preg_match_all("/\[img([\=0-9x]*?)\](.*?)\[\/img\]/",$body,$match)) {
447 if (count($images)) {
449 $objecttype = ACTIVITY_OBJ_IMAGE;
451 foreach ($images as $image) {
452 if (!stristr($image, System::baseUrl() . '/photo/')) {
455 $image_uri = substr($image,strrpos($image,'/') + 1);
456 $image_uri = substr($image_uri,0, strpos($image_uri,'-'));
457 if (!strlen($image_uri)) {
461 // Ensure to only modify photos that you own
462 $srch = '<' . intval($original_contact_id) . '>';
464 $condition = ['allow_cid' => $srch, 'allow_gid' => '', 'deny_cid' => '', 'deny_gid' => '',
465 'resource-id' => $image_uri, 'uid' => $profile_uid];
466 if (!dba::exists('photo', $condition)) {
470 $fields = ['allow_cid' => $str_contact_allow, 'allow_gid' => $str_group_allow,
471 'deny_cid' => $str_contact_deny, 'deny_gid' => $str_group_deny];
472 $condition = ['resource-id' => $image_uri, 'uid' => $profile_uid, 'album' => L10n::t('Wall Photos')];
473 dba::update('photo', $fields, $condition);
480 * Next link in any attachment references we find in the post.
484 /// @todo these lines should be moved to Model/Attach (Once it exists)
485 if (!$preview && preg_match_all("/\[attachment\](.*?)\[\/attachment\]/", $body, $match)) {
486 $attaches = $match[1];
487 if (count($attaches)) {
488 foreach ($attaches as $attach) {
489 // Ensure to only modify attachments that you own
490 $srch = '<' . intval($original_contact_id) . '>';
492 $condition = ['allow_cid' => $srch, 'allow_gid' => '', 'deny_cid' => '', 'deny_gid' => '',
494 if (!dba::exists('attach', $condition)) {
498 $fields = ['allow_cid' => $str_contact_allow, 'allow_gid' => $str_group_allow,
499 'deny_cid' => $str_contact_deny, 'deny_gid' => $str_group_deny];
500 $condition = ['id' => $attach];
501 dba::update('attach', $fields, $condition);
506 // embedded bookmark or attachment in post? set bookmark flag
509 $data = BBCode::getAttachmentData($body);
510 if (preg_match_all("/\[bookmark\=([^\]]*)\](.*?)\[\/bookmark\]/ism", $body, $match, PREG_SET_ORDER) || isset($data["type"])) {
511 $objecttype = ACTIVITY_OBJ_BOOKMARK;
515 $body = bb_translate_video($body);
518 // Fold multi-line [code] sequences
519 $body = preg_replace('/\[\/code\]\s*\[code\]/ism', "\n", $body);
521 $body = BBCode::scaleExternalImages($body, false);
523 // Setting the object type if not defined before
525 $objecttype = ACTIVITY_OBJ_NOTE; // Default value
526 $objectdata = BBCode::getAttachedData($body);
528 if ($objectdata["type"] == "link") {
529 $objecttype = ACTIVITY_OBJ_BOOKMARK;
530 } elseif ($objectdata["type"] == "video") {
531 $objecttype = ACTIVITY_OBJ_VIDEO;
532 } elseif ($objectdata["type"] == "photo") {
533 $objecttype = ACTIVITY_OBJ_IMAGE;
541 if (preg_match_all('/(\[attachment\]([0-9]+)\[\/attachment\])/',$body,$match)) {
542 foreach ($match[2] as $mtch) {
543 $fields = ['id', 'filename', 'filesize', 'filetype'];
544 $attachment = dba::selectFirst('attach', $fields, ['id' => $mtch]);
545 if (DBM::is_result($attachment)) {
546 if (strlen($attachments)) {
549 $attachments .= '[attach]href="' . System::baseUrl() . '/attach/' . $attachment['id'] .
550 '" length="' . $attachment['filesize'] . '" type="' . $attachment['filetype'] .
551 '" title="' . ($attachment['filename'] ? $attachment['filename'] : '') . '"[/attach]';
553 $body = str_replace($match[1],'',$body);
559 if (($post_type === 'wall' || $post_type === 'wall-comment') && !count($forum_contact)) {
563 if (!strlen($verb)) {
564 $verb = ACTIVITY_POST;
567 if ($network == "") {
568 $network = NETWORK_DFRN;
571 $gravity = ($parent ? 6 : 0);
573 // even if the post arrived via API we are considering that it
574 // originated on this site by default for determining relayability.
576 $origin = intval(defaults($_REQUEST, 'origin', 1));
578 $notify_type = ($parent ? 'comment-new' : 'wall-new');
580 $uri = ($message_id ? $message_id : item_new_uri($a->get_hostname(), $profile_uid, $guid));
582 // Fallback so that we alway have a parent uri
583 if (!$thr_parent_uri || !$parent) {
584 $thr_parent_uri = $uri;
588 $datarray['uid'] = $profile_uid;
589 $datarray['type'] = $post_type;
590 $datarray['wall'] = $wall;
591 $datarray['gravity'] = $gravity;
592 $datarray['network'] = $network;
593 $datarray['contact-id'] = $contact_id;
594 $datarray['owner-name'] = $contact_record['name'];
595 $datarray['owner-link'] = $contact_record['url'];
596 $datarray['owner-avatar'] = $contact_record['thumb'];
597 $datarray['owner-id'] = Contact::getIdForURL($datarray['owner-link'], 0);
598 $datarray['author-name'] = $author['name'];
599 $datarray['author-link'] = $author['url'];
600 $datarray['author-avatar'] = $author['thumb'];
601 $datarray['author-id'] = Contact::getIdForURL($datarray['author-link'], 0);
602 $datarray['created'] = DateTimeFormat::utcNow();
603 $datarray['edited'] = DateTimeFormat::utcNow();
604 $datarray['commented'] = DateTimeFormat::utcNow();
605 $datarray['received'] = DateTimeFormat::utcNow();
606 $datarray['changed'] = DateTimeFormat::utcNow();
607 $datarray['extid'] = $extid;
608 $datarray['guid'] = $guid;
609 $datarray['uri'] = $uri;
610 $datarray['title'] = $title;
611 $datarray['body'] = $body;
612 $datarray['app'] = $app;
613 $datarray['location'] = $location;
614 $datarray['coord'] = $coord;
615 $datarray['tag'] = $str_tags;
616 $datarray['file'] = $categories;
617 $datarray['inform'] = $inform;
618 $datarray['verb'] = $verb;
619 $datarray['object-type'] = $objecttype;
620 $datarray['allow_cid'] = $str_contact_allow;
621 $datarray['allow_gid'] = $str_group_allow;
622 $datarray['deny_cid'] = $str_contact_deny;
623 $datarray['deny_gid'] = $str_group_deny;
624 $datarray['private'] = $private;
625 $datarray['pubmail'] = $pubmail_enabled;
626 $datarray['attach'] = $attachments;
627 $datarray['bookmark'] = intval($bookmark);
629 // This is not a bug. The item store function changes 'parent-uri' to 'thr-parent' and fetches 'parent-uri' new. (We should change this)
630 $datarray['parent-uri'] = $thr_parent_uri;
632 $datarray['postopts'] = $postopts;
633 $datarray['origin'] = $origin;
634 $datarray['moderated'] = false;
635 $datarray['gcontact-id'] = GContact::getId(["url" => $datarray['author-link'], "network" => $datarray['network'],
636 "photo" => $datarray['author-avatar'], "name" => $datarray['author-name']]);
637 $datarray['object'] = $object;
640 * These fields are for the convenience of addons...
641 * 'self' if true indicates the owner is posting on their own wall
642 * If parent is 0 it is a top-level post.
644 $datarray['parent'] = $parent;
645 $datarray['self'] = $self;
647 // This triggers posts via API and the mirror functions
648 $datarray['api_source'] = $api_source;
650 // This field is for storing the raw conversation data
651 $datarray['protocol'] = PROTOCOL_DFRN;
653 $r = dba::fetch_first("SELECT `conversation-uri`, `conversation-href` FROM `conversation` WHERE `item-uri` = ?", $datarray['parent-uri']);
654 if (DBM::is_result($r)) {
655 if ($r['conversation-uri'] != '') {
656 $datarray['conversation-uri'] = $r['conversation-uri'];
658 if ($r['conversation-href'] != '') {
659 $datarray['conversation-href'] = $r['conversation-href'];
664 $datarray['edit'] = true;
667 // preview mode - prepare the body for display and send it via json
669 require_once 'include/conversation.php';
670 // We set the datarray ID to -1 because in preview mode the dataray
671 // doesn't have an ID.
672 $datarray["id"] = -1;
673 $o = conversation($a,[array_merge($contact_record,$datarray)],'search', false, true);
674 logger('preview: ' . $o);
675 echo json_encode(['preview' => $o]);
679 Addon::callHooks('post_local',$datarray);
681 if (x($datarray, 'cancel')) {
682 logger('mod_item: post cancelled by addon.');
684 goaway($return_path);
687 $json = ['cancel' => 1];
688 if (x($_REQUEST, 'jsreload') && strlen($_REQUEST['jsreload'])) {
689 $json['reload'] = System::baseUrl() . '/' . $_REQUEST['jsreload'];
692 echo json_encode($json);
698 // Fill the cache field
699 // This could be done in Item::update as well - but we have to check for the existance of some fields.
700 put_item_in_cache($datarray);
703 'title' => $datarray['title'],
704 'body' => $datarray['body'],
705 'tag' => $datarray['tag'],
706 'attach' => $datarray['attach'],
707 'file' => $datarray['file'],
708 'rendered-html' => $datarray['rendered-html'],
709 'rendered-hash' => $datarray['rendered-hash'],
710 'edited' => DateTimeFormat::utcNow(),
711 'changed' => DateTimeFormat::utcNow()];
713 Item::update($fields, ['id' => $post_id]);
715 // update filetags in pconfig
716 file_tag_update_pconfig($uid,$categories_old,$categories_new,'category');
718 if (x($_REQUEST, 'return') && strlen($return_path)) {
719 logger('return: ' . $return_path);
720 goaway($return_path);
727 unset($datarray['edit']);
728 unset($datarray['self']);
729 unset($datarray['api_source']);
731 $post_id = Item::insert($datarray);
734 logger("Item wasn't stored.");
735 goaway($return_path);
738 $datarray = dba::selectFirst('item', [], ['id' => $post_id]);
740 if (!DBM::is_result($datarray)) {
741 logger("Item with id ".$post_id." couldn't be fetched.");
742 goaway($return_path);
745 // update filetags in pconfig
746 file_tag_update_pconfig($uid, $categories_old, $categories_new, 'category');
748 // These notifications are sent if someone else is commenting other your wall
750 if ($contact_record != $author) {
752 'type' => NOTIFY_COMMENT,
753 'notify_flags' => $user['notify-flags'],
754 'language' => $user['language'],
755 'to_name' => $user['username'],
756 'to_email' => $user['email'],
757 'uid' => $user['uid'],
759 'link' => System::baseUrl().'/display/'.urlencode($datarray['guid']),
760 'source_name' => $datarray['author-name'],
761 'source_link' => $datarray['author-link'],
762 'source_photo' => $datarray['author-avatar'],
763 'verb' => ACTIVITY_POST,
766 'parent_uri' => $parent_item['uri']
770 // Store the comment signature information in case we need to relay to Diaspora
771 Diaspora::storeCommentSignature($datarray, $author, ($self ? $user['prvkey'] : false), $post_id);
773 if (($contact_record != $author) && !count($forum_contact)) {
775 'type' => NOTIFY_WALL,
776 'notify_flags' => $user['notify-flags'],
777 'language' => $user['language'],
778 'to_name' => $user['username'],
779 'to_email' => $user['email'],
780 'uid' => $user['uid'],
782 'link' => System::baseUrl().'/display/'.urlencode($datarray['guid']),
783 'source_name' => $datarray['author-name'],
784 'source_link' => $datarray['author-link'],
785 'source_photo' => $datarray['author-avatar'],
786 'verb' => ACTIVITY_POST,
792 Addon::callHooks('post_local_end', $datarray);
794 if (strlen($emailcc) && $profile_uid == local_user()) {
795 $erecips = explode(',', $emailcc);
796 if (count($erecips)) {
797 foreach ($erecips as $recip) {
798 $addr = trim($recip);
799 if (!strlen($addr)) {
802 $disclaimer = '<hr />' . L10n::t('This message was sent to you by %s, a member of the Friendica social network.', $a->user['username'])
804 $disclaimer .= L10n::t('You may visit them online at %s', System::baseUrl() . '/profile/' . $a->user['nickname']) . EOL;
805 $disclaimer .= L10n::t('Please contact the sender by replying to this post if you do not wish to receive these messages.') . EOL;
806 if (!$datarray['title']=='') {
807 $subject = Email::encodeHeader($datarray['title'], 'UTF-8');
809 $subject = Email::encodeHeader('[Friendica]' . ' ' . L10n::t('%s posted an update.', $a->user['username']), 'UTF-8');
811 $link = '<a href="' . System::baseUrl() . '/profile/' . $a->user['nickname'] . '"><img src="' . $author['thumb'] . '" alt="' . $a->user['username'] . '" /></a><br /><br />';
812 $html = prepare_body($datarray);
813 $message = '<html><body>' . $link . $html . $disclaimer . '</body></html>';
814 include_once 'include/html2plain.php';
816 'fromName' => $a->user['username'],
817 'fromEmail' => $a->user['email'],
819 'replyTo' => $a->user['email'],
820 'messageSubject' => $subject,
821 'htmlVersion' => $message,
822 'textVersion' => html2plain($html.$disclaimer)
824 Emailer::send($params);
829 // Insert an item entry for UID=0 for global entries.
830 // We now do it in the background to save some time.
831 // This is important in interactive environments like the frontend or the API.
832 // We don't fork a new process since this is done anyway with the following command
833 Worker::add(['priority' => PRIORITY_HIGH, 'dont_fork' => true], "CreateShadowEntry", $post_id);
835 // Call the background process that is delivering the item to the receivers
836 Worker::add(PRIORITY_HIGH, "Notifier", $notify_type, $post_id);
838 logger('post_complete');
840 item_post_return(System::baseUrl(), $api_source, $return_path);
844 function item_post_return($baseurl, $api_source, $return_path) {
845 // figure out how to return, depending on from whence we came
852 goaway($return_path);
855 $json = ['success' => 1];
856 if (x($_REQUEST, 'jsreload') && strlen($_REQUEST['jsreload'])) {
857 $json['reload'] = $baseurl . '/' . $_REQUEST['jsreload'];
860 logger('post_json: ' . print_r($json,true), LOGGER_DEBUG);
862 echo json_encode($json);
868 function item_content(App $a) {
870 if (!local_user() && !remote_user()) {
874 require_once 'include/security.php';
877 if (($a->argc == 3) && ($a->argv[1] === 'drop') && intval($a->argv[2])) {
879 $o = Item::deleteById($a->argv[2]);
881 $o = drop_item($a->argv[2]);
884 // ajax return: [<item id>, 0 (no perm) | <owner id>]
885 echo json_encode([intval($a->argv[2]), intval($o)]);
893 * This function removes the tag $tag from the text $body and replaces it with
894 * the appropiate link.
896 * @param App $a Application instance @TODO is unused in this function's scope (excluding included files)
897 * @param unknown_type $body the text to replace the tag in
898 * @param string $inform a comma-seperated string containing everybody to inform
899 * @param string $str_tags string to add the tag to
900 * @param integer $profile_uid
901 * @param string $tag the tag to replace
902 * @param string $network The network of the post
904 * @return boolean true if replaced, false if not replaced
906 function handle_tag(App $a, &$body, &$inform, &$str_tags, $profile_uid, $tag, $network = "")
912 //is it a person tag?
913 if ((strpos($tag, '@') === 0) || (strpos($tag, '!') === 0)) {
914 $tag_type = substr($tag, 0, 1);
915 //is it already replaced?
916 if (strpos($tag, '[url=')) {
917 //append tag to str_tags
918 if (!stristr($str_tags, $tag)) {
919 if (strlen($str_tags)) {
925 // Checking for the alias that is used for OStatus
926 $pattern = "/[@!]\[url\=(.*?)\](.*?)\[\/url\]/ism";
927 if (preg_match($pattern, $tag, $matches)) {
928 $data = Contact::getDetailsByURL($matches[1]);
929 if ($data["alias"] != "") {
930 $newtag = '@[url=' . $data["alias"] . ']' . $data["nick"] . '[/url]';
931 if (!stripos($str_tags, '[url=' . $data["alias"] . ']')) {
932 if (strlen($str_tags)) {
935 $str_tags .= $newtag;
943 //get the person's name
944 $name = substr($tag, 1);
946 // Sometimes the tag detection doesn't seem to work right
947 // This is some workaround
948 $nameparts = explode(" ", $name);
949 $name = $nameparts[0];
951 // Try to detect the contact in various ways
952 if (strpos($name, 'http://')) {
953 // At first we have to ensure that the contact exists
954 Contact::getIdForURL($name);
956 // Now we should have something
957 $contact = Contact::getDetailsByURL($name);
958 } elseif (strpos($name, '@')) {
959 // This function automatically probes when no entry was found
960 $contact = Contact::getDetailsByAddr($name);
963 $fields = ['id', 'url', 'nick', 'name', 'alias', 'network'];
965 if (strrpos($name, '+')) {
966 // Is it in format @nick+number?
967 $tagcid = intval(substr($name, strrpos($name, '+') + 1));
968 $contact = dba::selectFirst('contact', $fields, ['id' => $tagcid, 'uid' => $profile_uid]);
971 // select someone by nick or attag in the current network
972 if (!DBM::is_result($contact) && ($network != "")) {
973 $condition = ["(`nick` = ? OR `attag` = ?) AND `network` = ? AND `uid` = ?",
974 $name, $name, $network, $profile_uid];
975 $contact = dba::selectFirst('contact', $fields, $condition);
978 //select someone by name in the current network
979 if (!DBM::is_result($contact) && ($network != "")) {
980 $condition = ['name' => $name, 'network' => $network, 'uid' => $profile_uid];
981 $contact = dba::selectFirst('contact', $fields, $condition);
984 // select someone by nick or attag in any network
985 if (!DBM::is_result($contact)) {
986 $condition = ["(`nick` = ? OR `attag` = ?) AND `uid` = ?", $name, $name, $profile_uid];
987 $contact = dba::selectFirst('contact', $fields, $condition);
990 // select someone by name in any network
991 if (!DBM::is_result($contact)) {
992 $condition = ['name' => $name, 'uid' => $profile_uid];
993 $contact = dba::selectFirst('contact', $fields, $condition);
998 if (strlen($inform) && (isset($contact["notify"]) || isset($contact["id"]))) {
1002 if (isset($contact["id"])) {
1003 $inform .= 'cid:' . $contact["id"];
1004 } elseif (isset($contact["notify"])) {
1005 $inform .= $contact["notify"];
1008 $profile = $contact["url"];
1009 $alias = $contact["alias"];
1010 $newname = $contact["nick"];
1011 if (($newname == "") || (($contact["network"] != NETWORK_OSTATUS) && ($contact["network"] != NETWORK_TWITTER)
1012 && ($contact["network"] != NETWORK_STATUSNET) && ($contact["network"] != NETWORK_APPNET))) {
1013 $newname = $contact["name"];
1017 //if there is an url for this persons profile
1018 if (isset($profile) && ($newname != "")) {
1020 // create profile link
1021 $profile = str_replace(',', '%2c', $profile);
1022 $newtag = $tag_type.'[url=' . $profile . ']' . $newname . '[/url]';
1023 $body = str_replace($tag_type . $name, $newtag, $body);
1024 // append tag to str_tags
1025 if (!stristr($str_tags, $newtag)) {
1026 if (strlen($str_tags)) {
1029 $str_tags .= $newtag;
1033 * Status.Net seems to require the numeric ID URL in a mention if the person isn't
1034 * subscribed to you. But the nickname URL is OK if they are. Grrr. We'll tag both.
1036 if (strlen($alias)) {
1037 $newtag = '@[url=' . $alias . ']' . $newname . '[/url]';
1038 if (!stripos($str_tags, '[url=' . $alias . ']')) {
1039 if (strlen($str_tags)) {
1042 $str_tags .= $newtag;
1048 return ['replaced' => $replaced, 'contact' => $contact];