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\Item;
28 use Friendica\Protocol\Diaspora;
29 use Friendica\Protocol\Email;
30 use Friendica\Util\DateTimeFormat;
31 use Friendica\Util\Emailer;
33 require_once 'include/enotify.php';
34 require_once 'include/text.php';
35 require_once 'include/items.php';
37 function item_post(App $a) {
38 if (!local_user() && !remote_user()) {
42 require_once 'include/security.php';
46 if (x($_REQUEST, 'dropitems')) {
47 $arr_drop = explode(',', $_REQUEST['dropitems']);
48 drop_items($arr_drop);
49 $json = ['success' => 1];
50 echo json_encode($json);
54 Addon::callHooks('post_local_start', $_REQUEST);
56 logger('postvars ' . print_r($_REQUEST,true), LOGGER_DATA);
58 $api_source = defaults($_REQUEST, 'api_source', false);
60 $message_id = ((x($_REQUEST, 'message_id') && $api_source) ? strip_tags($_REQUEST['message_id']) : '');
62 $return_path = defaults($_REQUEST, 'return', '');
63 $preview = intval(defaults($_REQUEST, 'preview', 0));
66 * Check for doubly-submitted posts, and reject duplicates
67 * Note that we have to ignore previews, otherwise nothing will post
68 * after it's been previewed
70 if (!$preview && x($_REQUEST, 'post_id_random')) {
71 if (x($_SESSION, 'post-random') && $_SESSION['post-random'] == $_REQUEST['post_id_random']) {
72 logger("item post: duplicate post", LOGGER_DEBUG);
73 item_post_return(System::baseUrl(), $api_source, $return_path);
75 $_SESSION['post-random'] = $_REQUEST['post_id_random'];
79 // Is this a reply to something?
80 $thr_parent = intval(defaults($_REQUEST, 'parent', 0));
81 $thr_parent_uri = trim(defaults($_REQUEST, 'parent_uri', ''));
83 $thr_parent_contact = null;
89 $parent_contact = null;
92 $profile_uid = defaults($_REQUEST, 'profile_uid', local_user());
94 if ($thr_parent || $thr_parent_uri) {
96 $parent_item = dba::selectFirst('item', [], ['id' => $thr_parent]);
97 } elseif ($thr_parent_uri) {
98 $parent_item = dba::selectFirst('item', [], ['uri' => $thr_parent_uri, 'uid' => $profile_uid]);
101 // if this isn't the real parent of the conversation, find it
102 if (DBM::is_result($parent_item)) {
104 // The URI and the contact is taken from the direct parent which needn't to be the top parent
105 $thr_parent_uri = $parent_item['uri'];
106 $thr_parent_contact = Contact::getDetailsByURL($parent_item["author-link"]);
108 if ($parent_item['id'] != $parent_item['parent']) {
109 $parent_item = dba::selectFirst('item', [], ['id' => $parent_item['parent']]);
113 if (!DBM::is_result($parent_item)) {
114 notice(L10n::t('Unable to locate original post.') . EOL);
115 if (x($_REQUEST, 'return')) {
116 goaway($return_path);
121 $parent = $parent_item['id'];
122 $parent_user = $parent_item['uid'];
124 $parent_contact = Contact::getDetailsByURL($parent_item["author-link"]);
126 $objecttype = ACTIVITY_OBJ_COMMENT;
128 if (!x($_REQUEST, 'type')) {
129 $_REQUEST['type'] = 'net-comment';
134 logger('mod_item: item_post parent=' . $parent);
137 $post_id = intval(defaults($_REQUEST, 'post_id', 0));
138 $app = strip_tags(defaults($_REQUEST, 'source', ''));
139 $extid = strip_tags(defaults($_REQUEST, 'extid', ''));
140 $object = defaults($_REQUEST, 'object', '');
142 // Ensure that the user id in a thread always stay the same
143 if (!is_null($parent_user) && in_array($parent_user, [local_user(), 0])) {
144 $profile_uid = $parent_user;
147 // Check for multiple posts with the same message id (when the post was created via API)
148 if (($message_id != '') && ($profile_uid != 0)) {
149 if (dba::exists('item', ['uri' => $message_id, 'uid' => $profile_uid])) {
150 logger("Message with URI ".$message_id." already exists for user ".$profile_uid, LOGGER_DEBUG);
155 // Allow commenting if it is an answer to a public post
156 $allow_comment = local_user() && ($profile_uid == 0) && $parent && in_array($parent_item['network'], [NETWORK_OSTATUS, NETWORK_DIASPORA, NETWORK_DFRN]);
158 // Now check that valid personal details have been provided
159 if (!can_write_wall($profile_uid) && !$allow_comment) {
160 notice(L10n::t('Permission denied.') . EOL) ;
161 if (x($_REQUEST, 'return')) {
162 goaway($return_path);
168 // is this an edited post?
173 $orig_post = dba::selectFirst('item', [], ['id' => $post_id]);
176 $user = dba::selectFirst('user', [], ['uid' => $profile_uid]);
177 if (!DBM::is_result($user) && !$parent) {
182 $str_group_allow = $orig_post['allow_gid'];
183 $str_contact_allow = $orig_post['allow_cid'];
184 $str_group_deny = $orig_post['deny_gid'];
185 $str_contact_deny = $orig_post['deny_cid'];
186 $location = $orig_post['location'];
187 $coord = $orig_post['coord'];
188 $verb = $orig_post['verb'];
189 $objecttype = $orig_post['object-type'];
190 $emailcc = $orig_post['emailcc'];
191 $app = $orig_post['app'];
192 $categories = $orig_post['file'];
193 $title = notags(trim($_REQUEST['title']));
194 $body = escape_tags(trim($_REQUEST['body']));
195 $private = $orig_post['private'];
196 $pubmail_enabled = $orig_post['pubmail'];
197 $network = $orig_post['network'];
198 $guid = $orig_post['guid'];
199 $extid = $orig_post['extid'];
204 * if coming from the API and no privacy settings are set,
205 * use the user default permissions - as they won't have
206 * been supplied via a form.
208 /// @TODO use x($_REQUEST, 'foo') here
210 && !array_key_exists('contact_allow', $_REQUEST)
211 && !array_key_exists('group_allow', $_REQUEST)
212 && !array_key_exists('contact_deny', $_REQUEST)
213 && !array_key_exists('group_deny', $_REQUEST)) {
214 $str_group_allow = $user['allow_gid'];
215 $str_contact_allow = $user['allow_cid'];
216 $str_group_deny = $user['deny_gid'];
217 $str_contact_deny = $user['deny_cid'];
219 // use the posted permissions
220 $str_group_allow = perms2str($_REQUEST['group_allow']);
221 $str_contact_allow = perms2str($_REQUEST['contact_allow']);
222 $str_group_deny = perms2str($_REQUEST['group_deny']);
223 $str_contact_deny = perms2str($_REQUEST['contact_deny']);
226 $title = notags(trim($_REQUEST['title']));
227 $location = notags(trim($_REQUEST['location']));
228 $coord = notags(trim($_REQUEST['coord']));
229 $verb = notags(trim($_REQUEST['verb']));
230 $emailcc = notags(trim($_REQUEST['emailcc']));
231 $body = escape_tags(trim($_REQUEST['body']));
232 $network = notags(trim(defaults($_REQUEST, 'network', NETWORK_DFRN)));
233 $guid = get_guid(32);
235 $postopts = defaults($_REQUEST, 'postopts', '');
237 $private = ((strlen($str_group_allow) || strlen($str_contact_allow) || strlen($str_group_deny) || strlen($str_contact_deny)) ? 1 : 0);
239 if ($user['hidewall']) {
243 // If this is a comment, set the permissions from the parent.
247 // for non native networks use the network of the original post as network of the item
248 if (($parent_item['network'] != NETWORK_DIASPORA)
249 && ($parent_item['network'] != NETWORK_OSTATUS)
250 && ($network == "")) {
251 $network = $parent_item['network'];
254 $str_contact_allow = $parent_item['allow_cid'];
255 $str_group_allow = $parent_item['allow_gid'];
256 $str_contact_deny = $parent_item['deny_cid'];
257 $str_group_deny = $parent_item['deny_gid'];
258 $private = $parent_item['private'];
261 $pubmail_enabled = defaults($_REQUEST, 'pubmail_enable', false) && !$private;
263 // if using the API, we won't see pubmail_enable - figure out if it should be set
264 if ($api_source && $profile_uid && $profile_uid == local_user() && !$private) {
265 if (function_exists('imap_open') && !Config::get('system', 'imap_disabled')) {
266 $pubmail_enabled = dba::exists('mailacct', ["`uid` = ? AND `server` != ? AND `pubmail`", local_user(), '']);
270 if (!strlen($body)) {
274 info(L10n::t('Empty post discarded.') . EOL);
275 if (x($_REQUEST, 'return')) {
276 goaway($return_path);
282 if (strlen($categories)) {
283 // get the "fileas" tags for this post
284 $filedas = file_tag_file_to_list($categories, 'file');
286 // save old and new categories, so we can determine what needs to be deleted from pconfig
287 $categories_old = $categories;
288 $categories = file_tag_list_to_file(trim($_REQUEST['category']), 'category');
289 $categories_new = $categories;
290 if (strlen($filedas)) {
291 // append the fileas stuff to the new categories list
292 $categories .= file_tag_list_to_file($filedas, 'file');
295 // get contact info for poster
301 if (local_user() && ((local_user() == $profile_uid) || $allow_comment)) {
303 $author = dba::selectFirst('contact', [], ['uid' => local_user(), 'self' => true]);
304 } elseif (remote_user()) {
305 if (x($_SESSION, 'remote') && is_array($_SESSION['remote'])) {
306 foreach ($_SESSION['remote'] as $v) {
307 if ($v['uid'] == $profile_uid) {
308 $contact_id = $v['cid'];
314 $author = dba::selectFirst('contact', [], ['id' => $contact_id]);
318 if (DBM::is_result($author)) {
319 $contact_id = $author['id'];
322 // get contact info for owner
323 if ($profile_uid == local_user() || $allow_comment) {
324 $contact_record = $author;
326 $contact_record = dba::selectFirst('contact', [], ['uid' => $profile_uid, 'self' => true]);
329 $post_type = notags(trim($_REQUEST['type']));
331 if ($post_type === 'net-comment' && $parent_item !== null) {
332 if ($parent_item['wall'] == 1) {
333 $post_type = 'wall-comment';
335 $post_type = 'remote-comment';
339 // Look for any tags and linkify them
343 $tags = get_tags($body);
345 // Add a tag if the parent contact is from OStatus (This will notify them during delivery)
347 if ($thr_parent_contact['network'] == NETWORK_OSTATUS) {
348 $contact = '@[url=' . $thr_parent_contact['url'] . ']' . $thr_parent_contact['nick'] . '[/url]';
349 if (!stripos(implode($tags), '[url=' . $thr_parent_contact['url'] . ']')) {
354 if ($parent_contact['network'] == NETWORK_OSTATUS) {
355 $contact = '@[url=' . $parent_contact['url'] . ']' . $parent_contact['nick'] . '[/url]';
356 if (!stripos(implode($tags), '[url=' . $parent_contact['url'] . ']')) {
364 $private_forum = false;
365 $only_to_forum = false;
369 foreach ($tags as $tag) {
370 $tag_type = substr($tag, 0, 1);
372 if ($tag_type == '#') {
377 * If we already tagged 'Robert Johnson', don't try and tag 'Robert'.
378 * Robert Johnson should be first in the $tags array
380 $fullnametagged = false;
381 /// @TODO $tagged is initialized above if () block and is not filled, maybe old-lost code?
382 foreach ($tagged as $nextTag) {
383 if (stristr($nextTag, $tag . ' ')) {
384 $fullnametagged = true;
388 if ($fullnametagged) {
392 $success = handle_tag($a, $body, $inform, $str_tags, local_user() ? local_user() : $profile_uid, $tag, $network);
393 if ($success['replaced']) {
396 // When the forum is private or the forum is addressed with a "!" make the post private
397 if (is_array($success['contact']) && ($success['contact']['prv'] || ($tag_type == '!'))) {
398 $private_forum = $success['contact']['prv'];
399 $only_to_forum = ($tag_type == '!');
400 $private_id = $success['contact']['id'];
401 $forum_contact = $success['contact'];
402 } elseif (is_array($success['contact']) && $success['contact']['forum'] &&
403 ($str_contact_allow == '<' . $success['contact']['id'] . '>')) {
404 $private_forum = false;
405 $only_to_forum = true;
406 $private_id = $success['contact']['id'];
407 $forum_contact = $success['contact'];
412 $original_contact_id = $contact_id;
414 if (!$parent && count($forum_contact) && ($private_forum || $only_to_forum)) {
415 // we tagged a forum in a top level post. Now we change the post
416 $private = $private_forum;
418 $str_group_allow = '';
419 $str_contact_deny = '';
420 $str_group_deny = '';
421 if ($private_forum) {
422 $str_contact_allow = '<' . $private_id . '>';
424 $str_contact_allow = '';
426 $contact_id = $private_id;
427 $contact_record = $forum_contact;
428 $_REQUEST['origin'] = false;
432 * When a photo was uploaded into the message using the (profile wall) ajax
433 * uploader, The permissions are initially set to disallow anybody but the
434 * owner from seeing it. This is because the permissions may not yet have been
435 * set for the post. If it's private, the photo permissions should be set
436 * appropriately. But we didn't know the final permissions on the post until
437 * now. So now we'll look for links of uploaded messages that are in the
438 * post and set them to the same permissions as the post itself.
443 /// @todo these lines should be moved to Model/Photo
444 if (!$preview && preg_match_all("/\[img([\=0-9x]*?)\](.*?)\[\/img\]/",$body,$match)) {
446 if (count($images)) {
448 $objecttype = ACTIVITY_OBJ_IMAGE;
450 foreach ($images as $image) {
451 if (!stristr($image, System::baseUrl() . '/photo/')) {
454 $image_uri = substr($image,strrpos($image,'/') + 1);
455 $image_uri = substr($image_uri,0, strpos($image_uri,'-'));
456 if (!strlen($image_uri)) {
460 // Ensure to only modify photos that you own
461 $srch = '<' . intval($original_contact_id) . '>';
463 $condition = ['allow_cid' => $srch, 'allow_gid' => '', 'deny_cid' => '', 'deny_gid' => '',
464 'resource-id' => $image_uri, 'uid' => $profile_uid];
465 if (!dba::exists('photo', $condition)) {
469 $fields = ['allow_cid' => $str_contact_allow, 'allow_gid' => $str_group_allow,
470 'deny_cid' => $str_contact_deny, 'deny_gid' => $str_group_deny];
471 $condition = ['resource-id' => $image_uri, 'uid' => $profile_uid, 'album' => L10n::t('Wall Photos')];
472 dba::update('photo', $fields, $condition);
479 * Next link in any attachment references we find in the post.
483 /// @todo these lines should be moved to Model/Attach (Once it exists)
484 if (!$preview && preg_match_all("/\[attachment\](.*?)\[\/attachment\]/", $body, $match)) {
485 $attaches = $match[1];
486 if (count($attaches)) {
487 foreach ($attaches as $attach) {
488 // Ensure to only modify attachments that you own
489 $srch = '<' . intval($original_contact_id) . '>';
491 $condition = ['allow_cid' => $srch, 'allow_gid' => '', 'deny_cid' => '', 'deny_gid' => '',
493 if (!dba::exists('attach', $condition)) {
497 $fields = ['allow_cid' => $str_contact_allow, 'allow_gid' => $str_group_allow,
498 'deny_cid' => $str_contact_deny, 'deny_gid' => $str_group_deny];
499 $condition = ['id' => $attach];
500 dba::update('attach', $fields, $condition);
505 // embedded bookmark or attachment in post? set bookmark flag
508 $data = BBCode::getAttachmentData($body);
509 if (preg_match_all("/\[bookmark\=([^\]]*)\](.*?)\[\/bookmark\]/ism", $body, $match, PREG_SET_ORDER) || isset($data["type"])) {
510 $objecttype = ACTIVITY_OBJ_BOOKMARK;
514 $body = bb_translate_video($body);
517 // Fold multi-line [code] sequences
518 $body = preg_replace('/\[\/code\]\s*\[code\]/ism', "\n", $body);
520 $body = BBCode::scaleExternalImages($body, false);
522 // Setting the object type if not defined before
524 $objecttype = ACTIVITY_OBJ_NOTE; // Default value
525 $objectdata = BBCode::getAttachedData($body);
527 if ($objectdata["type"] == "link") {
528 $objecttype = ACTIVITY_OBJ_BOOKMARK;
529 } elseif ($objectdata["type"] == "video") {
530 $objecttype = ACTIVITY_OBJ_VIDEO;
531 } elseif ($objectdata["type"] == "photo") {
532 $objecttype = ACTIVITY_OBJ_IMAGE;
540 if (preg_match_all('/(\[attachment\]([0-9]+)\[\/attachment\])/',$body,$match)) {
541 foreach ($match[2] as $mtch) {
542 $fields = ['id', 'filename', 'filesize', 'filetype'];
543 $attachment = dba::selectFirst('attach', $fields, ['id' => $mtch]);
544 if (DBM::is_result($attachment)) {
545 if (strlen($attachments)) {
548 $attachments .= '[attach]href="' . System::baseUrl() . '/attach/' . $attachment['id'] .
549 '" length="' . $attachment['filesize'] . '" type="' . $attachment['filetype'] .
550 '" title="' . ($attachment['filename'] ? $attachment['filename'] : '') . '"[/attach]';
552 $body = str_replace($match[1],'',$body);
558 if (($post_type === 'wall' || $post_type === 'wall-comment') && !count($forum_contact)) {
562 if (!strlen($verb)) {
563 $verb = ACTIVITY_POST;
566 if ($network == "") {
567 $network = NETWORK_DFRN;
570 $gravity = ($parent ? 6 : 0);
572 // even if the post arrived via API we are considering that it
573 // originated on this site by default for determining relayability.
575 $origin = intval(defaults($_REQUEST, 'origin', 1));
577 $notify_type = ($parent ? 'comment-new' : 'wall-new');
579 $uri = ($message_id ? $message_id : item_new_uri($a->get_hostname(), $profile_uid, $guid));
581 // Fallback so that we alway have a parent uri
582 if (!$thr_parent_uri || !$parent) {
583 $thr_parent_uri = $uri;
587 $datarray['uid'] = $profile_uid;
588 $datarray['type'] = $post_type;
589 $datarray['wall'] = $wall;
590 $datarray['gravity'] = $gravity;
591 $datarray['network'] = $network;
592 $datarray['contact-id'] = $contact_id;
593 $datarray['owner-name'] = $contact_record['name'];
594 $datarray['owner-link'] = $contact_record['url'];
595 $datarray['owner-avatar'] = $contact_record['thumb'];
596 $datarray['owner-id'] = Contact::getIdForURL($datarray['owner-link']);
597 $datarray['author-name'] = $author['name'];
598 $datarray['author-link'] = $author['url'];
599 $datarray['author-avatar'] = $author['thumb'];
600 $datarray['author-id'] = Contact::getIdForURL($datarray['author-link']);
601 $datarray['created'] = DateTimeFormat::utcNow();
602 $datarray['edited'] = DateTimeFormat::utcNow();
603 $datarray['commented'] = DateTimeFormat::utcNow();
604 $datarray['received'] = DateTimeFormat::utcNow();
605 $datarray['changed'] = DateTimeFormat::utcNow();
606 $datarray['extid'] = $extid;
607 $datarray['guid'] = $guid;
608 $datarray['uri'] = $uri;
609 $datarray['title'] = $title;
610 $datarray['body'] = $body;
611 $datarray['app'] = $app;
612 $datarray['location'] = $location;
613 $datarray['coord'] = $coord;
614 $datarray['tag'] = $str_tags;
615 $datarray['file'] = $categories;
616 $datarray['inform'] = $inform;
617 $datarray['verb'] = $verb;
618 $datarray['object-type'] = $objecttype;
619 $datarray['allow_cid'] = $str_contact_allow;
620 $datarray['allow_gid'] = $str_group_allow;
621 $datarray['deny_cid'] = $str_contact_deny;
622 $datarray['deny_gid'] = $str_group_deny;
623 $datarray['private'] = $private;
624 $datarray['pubmail'] = $pubmail_enabled;
625 $datarray['attach'] = $attachments;
626 $datarray['bookmark'] = intval($bookmark);
628 // This is not a bug. The item store function changes 'parent-uri' to 'thr-parent' and fetches 'parent-uri' new. (We should change this)
629 $datarray['parent-uri'] = $thr_parent_uri;
631 $datarray['postopts'] = $postopts;
632 $datarray['origin'] = $origin;
633 $datarray['moderated'] = false;
634 $datarray['object'] = $object;
637 * These fields are for the convenience of addons...
638 * 'self' if true indicates the owner is posting on their own wall
639 * If parent is 0 it is a top-level post.
641 $datarray['parent'] = $parent;
642 $datarray['self'] = $self;
644 // This triggers posts via API and the mirror functions
645 $datarray['api_source'] = $api_source;
647 // This field is for storing the raw conversation data
648 $datarray['protocol'] = PROTOCOL_DFRN;
650 $r = dba::fetch_first("SELECT `conversation-uri`, `conversation-href` FROM `conversation` WHERE `item-uri` = ?", $datarray['parent-uri']);
651 if (DBM::is_result($r)) {
652 if ($r['conversation-uri'] != '') {
653 $datarray['conversation-uri'] = $r['conversation-uri'];
655 if ($r['conversation-href'] != '') {
656 $datarray['conversation-href'] = $r['conversation-href'];
661 $datarray['edit'] = true;
664 // preview mode - prepare the body for display and send it via json
666 require_once 'include/conversation.php';
667 // We set the datarray ID to -1 because in preview mode the dataray
668 // doesn't have an ID.
669 $datarray["id"] = -1;
670 $o = conversation($a,[array_merge($contact_record,$datarray)],'search', false, true);
671 logger('preview: ' . $o);
672 echo json_encode(['preview' => $o]);
676 Addon::callHooks('post_local',$datarray);
678 if (x($datarray, 'cancel')) {
679 logger('mod_item: post cancelled by addon.');
681 goaway($return_path);
684 $json = ['cancel' => 1];
685 if (x($_REQUEST, 'jsreload') && strlen($_REQUEST['jsreload'])) {
686 $json['reload'] = System::baseUrl() . '/' . $_REQUEST['jsreload'];
689 echo json_encode($json);
695 // Fill the cache field
696 // This could be done in Item::update as well - but we have to check for the existance of some fields.
697 put_item_in_cache($datarray);
700 'title' => $datarray['title'],
701 'body' => $datarray['body'],
702 'tag' => $datarray['tag'],
703 'attach' => $datarray['attach'],
704 'file' => $datarray['file'],
705 'rendered-html' => $datarray['rendered-html'],
706 'rendered-hash' => $datarray['rendered-hash'],
707 'edited' => DateTimeFormat::utcNow(),
708 'changed' => DateTimeFormat::utcNow()];
710 Item::update($fields, ['id' => $post_id]);
712 // update filetags in pconfig
713 file_tag_update_pconfig($uid,$categories_old,$categories_new,'category');
715 if (x($_REQUEST, 'return') && strlen($return_path)) {
716 logger('return: ' . $return_path);
717 goaway($return_path);
724 unset($datarray['edit']);
725 unset($datarray['self']);
726 unset($datarray['api_source']);
728 $post_id = Item::insert($datarray);
731 logger("Item wasn't stored.");
732 goaway($return_path);
735 $datarray = dba::selectFirst('item', [], ['id' => $post_id]);
737 if (!DBM::is_result($datarray)) {
738 logger("Item with id ".$post_id." couldn't be fetched.");
739 goaway($return_path);
742 // update filetags in pconfig
743 file_tag_update_pconfig($uid, $categories_old, $categories_new, 'category');
745 // These notifications are sent if someone else is commenting other your wall
747 if ($contact_record != $author) {
749 'type' => NOTIFY_COMMENT,
750 'notify_flags' => $user['notify-flags'],
751 'language' => $user['language'],
752 'to_name' => $user['username'],
753 'to_email' => $user['email'],
754 'uid' => $user['uid'],
756 'link' => System::baseUrl().'/display/'.urlencode($datarray['guid']),
757 'source_name' => $datarray['author-name'],
758 'source_link' => $datarray['author-link'],
759 'source_photo' => $datarray['author-avatar'],
760 'verb' => ACTIVITY_POST,
763 'parent_uri' => $parent_item['uri']
767 // Store the comment signature information in case we need to relay to Diaspora
768 Diaspora::storeCommentSignature($datarray, $author, ($self ? $user['prvkey'] : false), $post_id);
770 if (($contact_record != $author) && !count($forum_contact)) {
772 'type' => NOTIFY_WALL,
773 'notify_flags' => $user['notify-flags'],
774 'language' => $user['language'],
775 'to_name' => $user['username'],
776 'to_email' => $user['email'],
777 'uid' => $user['uid'],
779 'link' => System::baseUrl().'/display/'.urlencode($datarray['guid']),
780 'source_name' => $datarray['author-name'],
781 'source_link' => $datarray['author-link'],
782 'source_photo' => $datarray['author-avatar'],
783 'verb' => ACTIVITY_POST,
789 Addon::callHooks('post_local_end', $datarray);
791 if (strlen($emailcc) && $profile_uid == local_user()) {
792 $erecips = explode(',', $emailcc);
793 if (count($erecips)) {
794 foreach ($erecips as $recip) {
795 $addr = trim($recip);
796 if (!strlen($addr)) {
799 $disclaimer = '<hr />' . L10n::t('This message was sent to you by %s, a member of the Friendica social network.', $a->user['username'])
801 $disclaimer .= L10n::t('You may visit them online at %s', System::baseUrl() . '/profile/' . $a->user['nickname']) . EOL;
802 $disclaimer .= L10n::t('Please contact the sender by replying to this post if you do not wish to receive these messages.') . EOL;
803 if (!$datarray['title']=='') {
804 $subject = Email::encodeHeader($datarray['title'], 'UTF-8');
806 $subject = Email::encodeHeader('[Friendica]' . ' ' . L10n::t('%s posted an update.', $a->user['username']), 'UTF-8');
808 $link = '<a href="' . System::baseUrl() . '/profile/' . $a->user['nickname'] . '"><img src="' . $author['thumb'] . '" alt="' . $a->user['username'] . '" /></a><br /><br />';
809 $html = prepare_body($datarray);
810 $message = '<html><body>' . $link . $html . $disclaimer . '</body></html>';
811 include_once 'include/html2plain.php';
813 'fromName' => $a->user['username'],
814 'fromEmail' => $a->user['email'],
816 'replyTo' => $a->user['email'],
817 'messageSubject' => $subject,
818 'htmlVersion' => $message,
819 'textVersion' => html2plain($html.$disclaimer)
821 Emailer::send($params);
826 // Insert an item entry for UID=0 for global entries.
827 // We now do it in the background to save some time.
828 // This is important in interactive environments like the frontend or the API.
829 // We don't fork a new process since this is done anyway with the following command
830 Worker::add(['priority' => PRIORITY_HIGH, 'dont_fork' => true], "CreateShadowEntry", $post_id);
832 // Call the background process that is delivering the item to the receivers
833 Worker::add(PRIORITY_HIGH, "Notifier", $notify_type, $post_id);
835 logger('post_complete');
837 item_post_return(System::baseUrl(), $api_source, $return_path);
841 function item_post_return($baseurl, $api_source, $return_path) {
842 // figure out how to return, depending on from whence we came
849 goaway($return_path);
852 $json = ['success' => 1];
853 if (x($_REQUEST, 'jsreload') && strlen($_REQUEST['jsreload'])) {
854 $json['reload'] = $baseurl . '/' . $_REQUEST['jsreload'];
857 logger('post_json: ' . print_r($json,true), LOGGER_DEBUG);
859 echo json_encode($json);
865 function item_content(App $a) {
867 if (!local_user() && !remote_user()) {
871 require_once 'include/security.php';
874 if (($a->argc == 3) && ($a->argv[1] === 'drop') && intval($a->argv[2])) {
876 $o = Item::deleteById($a->argv[2]);
878 $o = drop_item($a->argv[2]);
881 // ajax return: [<item id>, 0 (no perm) | <owner id>]
882 echo json_encode([intval($a->argv[2]), intval($o)]);
890 * This function removes the tag $tag from the text $body and replaces it with
891 * the appropiate link.
893 * @param App $a Application instance @TODO is unused in this function's scope (excluding included files)
894 * @param unknown_type $body the text to replace the tag in
895 * @param string $inform a comma-seperated string containing everybody to inform
896 * @param string $str_tags string to add the tag to
897 * @param integer $profile_uid
898 * @param string $tag the tag to replace
899 * @param string $network The network of the post
901 * @return boolean true if replaced, false if not replaced
903 function handle_tag(App $a, &$body, &$inform, &$str_tags, $profile_uid, $tag, $network = "")
909 //is it a person tag?
910 if ((strpos($tag, '@') === 0) || (strpos($tag, '!') === 0)) {
911 $tag_type = substr($tag, 0, 1);
912 //is it already replaced?
913 if (strpos($tag, '[url=')) {
914 //append tag to str_tags
915 if (!stristr($str_tags, $tag)) {
916 if (strlen($str_tags)) {
922 // Checking for the alias that is used for OStatus
923 $pattern = "/[@!]\[url\=(.*?)\](.*?)\[\/url\]/ism";
924 if (preg_match($pattern, $tag, $matches)) {
925 $data = Contact::getDetailsByURL($matches[1]);
926 if ($data["alias"] != "") {
927 $newtag = '@[url=' . $data["alias"] . ']' . $data["nick"] . '[/url]';
928 if (!stripos($str_tags, '[url=' . $data["alias"] . ']')) {
929 if (strlen($str_tags)) {
932 $str_tags .= $newtag;
940 //get the person's name
941 $name = substr($tag, 1);
943 // Sometimes the tag detection doesn't seem to work right
944 // This is some workaround
945 $nameparts = explode(" ", $name);
946 $name = $nameparts[0];
948 // Try to detect the contact in various ways
949 if (strpos($name, 'http://')) {
950 // At first we have to ensure that the contact exists
951 Contact::getIdForURL($name);
953 // Now we should have something
954 $contact = Contact::getDetailsByURL($name);
955 } elseif (strpos($name, '@')) {
956 // This function automatically probes when no entry was found
957 $contact = Contact::getDetailsByAddr($name);
960 $fields = ['id', 'url', 'nick', 'name', 'alias', 'network'];
962 if (strrpos($name, '+')) {
963 // Is it in format @nick+number?
964 $tagcid = intval(substr($name, strrpos($name, '+') + 1));
965 $contact = dba::selectFirst('contact', $fields, ['id' => $tagcid, 'uid' => $profile_uid]);
968 // select someone by nick or attag in the current network
969 if (!DBM::is_result($contact) && ($network != "")) {
970 $condition = ["(`nick` = ? OR `attag` = ?) AND `network` = ? AND `uid` = ?",
971 $name, $name, $network, $profile_uid];
972 $contact = dba::selectFirst('contact', $fields, $condition);
975 //select someone by name in the current network
976 if (!DBM::is_result($contact) && ($network != "")) {
977 $condition = ['name' => $name, 'network' => $network, 'uid' => $profile_uid];
978 $contact = dba::selectFirst('contact', $fields, $condition);
981 // select someone by nick or attag in any network
982 if (!DBM::is_result($contact)) {
983 $condition = ["(`nick` = ? OR `attag` = ?) AND `uid` = ?", $name, $name, $profile_uid];
984 $contact = dba::selectFirst('contact', $fields, $condition);
987 // select someone by name in any network
988 if (!DBM::is_result($contact)) {
989 $condition = ['name' => $name, 'uid' => $profile_uid];
990 $contact = dba::selectFirst('contact', $fields, $condition);
995 if (strlen($inform) && (isset($contact["notify"]) || isset($contact["id"]))) {
999 if (isset($contact["id"])) {
1000 $inform .= 'cid:' . $contact["id"];
1001 } elseif (isset($contact["notify"])) {
1002 $inform .= $contact["notify"];
1005 $profile = $contact["url"];
1006 $alias = $contact["alias"];
1007 $newname = $contact["nick"];
1008 if (($newname == "") || (($contact["network"] != NETWORK_OSTATUS) && ($contact["network"] != NETWORK_TWITTER)
1009 && ($contact["network"] != NETWORK_STATUSNET) && ($contact["network"] != NETWORK_APPNET))) {
1010 $newname = $contact["name"];
1014 //if there is an url for this persons profile
1015 if (isset($profile) && ($newname != "")) {
1017 // create profile link
1018 $profile = str_replace(',', '%2c', $profile);
1019 $newtag = $tag_type.'[url=' . $profile . ']' . $newname . '[/url]';
1020 $body = str_replace($tag_type . $name, $newtag, $body);
1021 // append tag to str_tags
1022 if (!stristr($str_tags, $newtag)) {
1023 if (strlen($str_tags)) {
1026 $str_tags .= $newtag;
1030 * Status.Net seems to require the numeric ID URL in a mention if the person isn't
1031 * subscribed to you. But the nickname URL is OK if they are. Grrr. We'll tag both.
1033 if (strlen($alias)) {
1034 $newtag = '@[url=' . $alias . ']' . $newname . '[/url]';
1035 if (!stripos($str_tags, '[url=' . $alias . ']')) {
1036 if (strlen($str_tags)) {
1039 $str_tags .= $newtag;
1045 return ['replaced' => $replaced, 'contact' => $contact];