4 * This is the POST destination for most all locally posted
5 * text stuff. This function handles status, wall-to-wall status,
6 * local comments, and remote coments that are posted on this site
7 * (as opposed to being delivered in a feed).
8 * Also processed here are posts and comments coming through the
9 * statusnet/twitter API.
11 * All of these become an "item" which is our basic unit of
14 * Posts that originate externally or do not fall into the above
15 * posting categories go through item_store() instead of this function.
20 require_once 'include/crypto.php';
21 require_once 'include/enotify.php';
22 require_once 'include/email.php';
23 require_once 'include/tags.php';
24 require_once 'include/files.php';
25 require_once 'include/threads.php';
26 require_once 'include/text.php';
27 require_once 'include/items.php';
28 require_once 'include/probe.php';
29 require_once 'include/diaspora.php';
30 require_once 'include/Contact.php';
32 function item_post(App $a) {
34 if ((! local_user()) && (! remote_user()) && (! x($_REQUEST, 'commenter'))) {
38 require_once 'include/security.php';
42 if (x($_REQUEST, 'dropitems')) {
43 $arr_drop = explode(',', $_REQUEST['dropitems']);
44 drop_items($arr_drop);
45 $json = array('success' => 1);
46 echo json_encode($json);
50 call_hooks('post_local_start', $_REQUEST);
51 // logger('postinput ' . file_get_contents('php://input'));
52 logger('postvars ' . print_r($_REQUEST,true), LOGGER_DATA);
54 $api_source = ((x($_REQUEST, 'api_source') && $_REQUEST['api_source']) ? true : false);
56 $message_id = ((x($_REQUEST, 'message_id') && $api_source) ? strip_tags($_REQUEST['message_id']) : '');
58 $return_path = ((x($_REQUEST, 'return')) ? $_REQUEST['return'] : '');
59 $preview = ((x($_REQUEST, 'preview')) ? intval($_REQUEST['preview']) : 0);
62 * Check for doubly-submitted posts, and reject duplicates
63 * Note that we have to ignore previews, otherwise nothing will post
64 * after it's been previewed
66 if (!$preview && x($_REQUEST, 'post_id_random')) {
67 if (x($_SESSION, 'post-random') && $_SESSION['post-random'] == $_REQUEST['post_id_random']) {
68 logger("item post: duplicate post", LOGGER_DEBUG);
69 item_post_return(App::get_baseurl(), $api_source, $return_path);
71 $_SESSION['post-random'] = $_REQUEST['post_id_random'];
75 // Is this a reply to something?
76 $parent = ((x($_REQUEST, 'parent')) ? intval($_REQUEST['parent']) : 0);
77 $parent_uri = ((x($_REQUEST, 'parent_uri')) ? trim($_REQUEST['parent_uri']) : '');
80 $parent_contact = null;
86 if ($parent || $parent_uri) {
88 $objecttype = ACTIVITY_OBJ_COMMENT;
90 if (! x($_REQUEST, 'type')) {
91 $_REQUEST['type'] = 'net-comment';
95 $r = q("SELECT * FROM `item` WHERE `id` = %d LIMIT 1",
98 } elseif ($parent_uri && local_user()) {
99 // This is coming from an API source, and we are logged in
100 $r = q("SELECT * FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
106 // if this isn't the real parent of the conversation, find it
107 if (dbm::is_result($r)) {
108 $parid = $r[0]['parent'];
109 $parent_uri = $r[0]['uri'];
110 if ($r[0]['id'] != $r[0]['parent']) {
111 $r = q("SELECT * FROM `item` WHERE `id` = `parent` AND `parent` = %d LIMIT 1",
117 if (! dbm::is_result($r)) {
118 notice( t('Unable to locate original post.') . EOL);
119 if (x($_REQUEST, 'return')) {
120 goaway($return_path);
124 $parent_item = $r[0];
125 $parent = $r[0]['id'];
127 // multi-level threading - preserve the info but re-parent to our single level threading
128 //if(($parid) && ($parid != $parent))
129 $thr_parent = $parent_uri;
131 if ($parent_item['contact-id'] && $uid) {
132 $r = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
133 intval($parent_item['contact-id']),
136 if (dbm::is_result($r)) {
137 $parent_contact = $r[0];
140 // If the contact id doesn't fit with the contact, then set the contact to null
141 $thrparent = q("SELECT `author-link`, `network` FROM `item` WHERE `uri` = '%s' LIMIT 1", dbesc($thr_parent));
142 if (dbm::is_result($thrparent) AND ($thrparent[0]["network"] === NETWORK_OSTATUS)
143 AND (normalise_link($parent_contact["url"]) != normalise_link($thrparent[0]["author-link"]))) {
144 $parent_contact = get_contact_details_by_url($thrparent[0]["author-link"]);
146 if (!isset($parent_contact["nick"])) {
147 $probed_contact = probe_url($thrparent[0]["author-link"]);
148 if ($probed_contact["network"] != NETWORK_FEED) {
149 $parent_contact = $probed_contact;
150 $parent_contact["nurl"] = normalise_link($probed_contact["url"]);
151 $parent_contact["thumb"] = $probed_contact["photo"];
152 $parent_contact["micro"] = $probed_contact["photo"];
153 $parent_contact["addr"] = $probed_contact["addr"];
156 logger('no contact found: ' . print_r($thrparent, true), LOGGER_DEBUG);
158 logger('parent contact: ' . print_r($parent_contact, true), LOGGER_DEBUG);
161 if ($parent_contact["nick"] == "") {
162 $parent_contact["nick"] = $parent_contact["name"];
168 logger('mod_item: item_post parent=' . $parent);
171 $profile_uid = ((x($_REQUEST, 'profile_uid')) ? intval($_REQUEST['profile_uid']) : 0);
172 $post_id = ((x($_REQUEST, 'post_id')) ? intval($_REQUEST['post_id']) : 0);
173 $app = ((x($_REQUEST, 'source')) ? strip_tags($_REQUEST['source']) : '');
174 $extid = ((x($_REQUEST, 'extid')) ? strip_tags($_REQUEST['extid']) : '');
175 $object = ((x($_REQUEST, 'object')) ? $_REQUEST['object'] : '');
177 // Check for multiple posts with the same message id (when the post was created via API)
178 if (($message_id != '') AND ($profile_uid != 0)) {
179 $r = q("SELECT * FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
184 if (dbm::is_result($r)) {
185 logger("Message with URI ".$message_id." already exists for user ".$profile_uid, LOGGER_DEBUG);
190 $allow_moderated = false;
192 // here is where we are going to check for permission to post a moderated comment.
194 // First check that the parent exists and it is a wall item.
196 if ((x($_REQUEST, 'commenter')) && ((! $parent) || (! $parent_item['wall']))) {
197 notice(t('Permission denied.') . EOL) ;
198 if (x($_REQUEST, 'return')) {
199 goaway($return_path);
205 * Now check that it is a page_type of PAGE_BLOG, and that valid personal details
206 * have been provided, and run any anti-spam plugins
208 if ((! can_write_wall($a, $profile_uid)) && (! $allow_moderated)) {
209 notice(t('Permission denied.') . EOL) ;
210 if (x($_REQUEST, 'return')) {
211 goaway($return_path);
217 // is this an edited post?
222 $i = q("SELECT * FROM `item` WHERE `uid` = %d AND `id` = %d LIMIT 1",
223 intval($profile_uid),
226 if (! dbm::is_result($i)) {
234 $r = q("SELECT * FROM `user` WHERE `uid` = %d LIMIT 1",
237 if (dbm::is_result($r)) {
242 $str_group_allow = $orig_post['allow_gid'];
243 $str_contact_allow = $orig_post['allow_cid'];
244 $str_group_deny = $orig_post['deny_gid'];
245 $str_contact_deny = $orig_post['deny_cid'];
246 $location = $orig_post['location'];
247 $coord = $orig_post['coord'];
248 $verb = $orig_post['verb'];
249 $objecttype = $orig_post['object-type'];
250 $emailcc = $orig_post['emailcc'];
251 $app = $orig_post['app'];
252 $categories = $orig_post['file'];
253 $title = notags(trim($_REQUEST['title']));
254 $body = escape_tags(trim($_REQUEST['body']));
255 $private = $orig_post['private'];
256 $pubmail_enable = $orig_post['pubmail'];
257 $network = $orig_post['network'];
258 $guid = $orig_post['guid'];
259 $extid = $orig_post['extid'];
264 * if coming from the API and no privacy settings are set,
265 * use the user default permissions - as they won't have
266 * been supplied via a form.
268 /// @TODO use x($_REQUEST, 'foo') here
270 && (! array_key_exists('contact_allow', $_REQUEST))
271 && (! array_key_exists('group_allow', $_REQUEST))
272 && (! array_key_exists('contact_deny', $_REQUEST))
273 && (! array_key_exists('group_deny', $_REQUEST))) {
274 $str_group_allow = $user['allow_gid'];
275 $str_contact_allow = $user['allow_cid'];
276 $str_group_deny = $user['deny_gid'];
277 $str_contact_deny = $user['deny_cid'];
280 // use the posted permissions
282 $str_group_allow = perms2str($_REQUEST['group_allow']);
283 $str_contact_allow = perms2str($_REQUEST['contact_allow']);
284 $str_group_deny = perms2str($_REQUEST['group_deny']);
285 $str_contact_deny = perms2str($_REQUEST['contact_deny']);
288 $title = notags(trim($_REQUEST['title']));
289 $location = notags(trim($_REQUEST['location']));
290 $coord = notags(trim($_REQUEST['coord']));
291 $verb = notags(trim($_REQUEST['verb']));
292 $emailcc = notags(trim($_REQUEST['emailcc']));
293 $body = escape_tags(trim($_REQUEST['body']));
294 $network = notags(trim($_REQUEST['network']));
295 $guid = get_guid(32);
297 item_add_language_opt($_REQUEST);
298 $postopts = $_REQUEST['postopts'] ? $_REQUEST['postopts'] : "";
300 $private = ((strlen($str_group_allow) || strlen($str_contact_allow) || strlen($str_group_deny) || strlen($str_contact_deny)) ? 1 : 0);
302 if ($user['hidewall']) {
306 // If this is a comment, set the permissions from the parent.
310 // for non native networks use the network of the original post as network of the item
311 if (($parent_item['network'] != NETWORK_DIASPORA)
312 AND ($parent_item['network'] != NETWORK_OSTATUS)
313 AND ($network == "")) {
314 $network = $parent_item['network'];
317 $str_contact_allow = $parent_item['allow_cid'];
318 $str_group_allow = $parent_item['allow_gid'];
319 $str_contact_deny = $parent_item['deny_cid'];
320 $str_group_deny = $parent_item['deny_gid'];
321 $private = $parent_item['private'];
324 $pubmail_enable = ((x($_REQUEST, 'pubmail_enable') && intval($_REQUEST['pubmail_enable']) && (! $private)) ? 1 : 0);
326 // if using the API, we won't see pubmail_enable - figure out if it should be set
328 if ($api_source && $profile_uid && $profile_uid == local_user() && (! $private)) {
329 $mail_disabled = ((function_exists('imap_open') && (! get_config('system', 'imap_disabled'))) ? 0 : 1);
330 if (! $mail_disabled) {
331 /// @TODO Check if only pubmail is loaded, * loads all columns
332 $r = q("SELECT * FROM `mailacct` WHERE `uid` = %d AND `server` != '' LIMIT 1",
335 if (dbm::is_result($r) && intval($r[0]['pubmail'])) {
336 $pubmail_enabled = true;
341 if (! strlen($body)) {
345 info(t('Empty post discarded.') . EOL );
346 if (x($_REQUEST, 'return')) {
347 goaway($return_path);
353 if (strlen($categories)) {
354 // get the "fileas" tags for this post
355 $filedas = file_tag_file_to_list($categories, 'file');
357 // save old and new categories, so we can determine what needs to be deleted from pconfig
358 $categories_old = $categories;
359 $categories = file_tag_list_to_file(trim($_REQUEST['category']), 'category');
360 $categories_new = $categories;
361 if (strlen($filedas)) {
362 // append the fileas stuff to the new categories list
363 $categories .= file_tag_list_to_file($filedas, 'file');
366 // get contact info for poster
372 if ((local_user()) && (local_user() == $profile_uid)) {
374 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 1 LIMIT 1",
375 intval($_SESSION['uid']));
376 } elseif(remote_user()) {
377 if (x($_SESSION, 'remote') && is_array($_SESSION['remote'])) {
378 foreach ($_SESSION['remote'] as $v) {
379 if ($v['uid'] == $profile_uid) {
380 $contact_id = $v['cid'];
386 $r = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
392 if (dbm::is_result($r)) {
394 $contact_id = $author['id'];
397 // get contact info for owner
399 if ($profile_uid == local_user()) {
400 $contact_record = $author;
402 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 1 LIMIT 1",
405 if (dbm::is_result($r)) {
406 $contact_record = $r[0];
410 $post_type = notags(trim($_REQUEST['type']));
412 if ($post_type === 'net-comment' && $parent_item !== null) {
413 if ($parent_item['wall'] == 1) {
414 $post_type = 'wall-comment';
416 $post_type = 'remote-comment';
421 * When a photo was uploaded into the message using the (profile wall) ajax
422 * uploader, The permissions are initially set to disallow anybody but the
423 * owner from seeing it. This is because the permissions may not yet have been
424 * set for the post. If it's private, the photo permissions should be set
425 * appropriately. But we didn't know the final permissions on the post until
426 * now. So now we'll look for links of uploaded messages that are in the
427 * post and set them to the same permissions as the post itself.
432 if ((! $preview) && preg_match_all("/\[img([\=0-9x]*?)\](.*?)\[\/img\]/",$body,$match)) {
434 if (count($images)) {
436 $objecttype = ACTIVITY_OBJ_IMAGE;
438 foreach ($images as $image) {
439 if (! stristr($image,App::get_baseurl() . '/photo/')) {
442 $image_uri = substr($image,strrpos($image,'/') + 1);
443 $image_uri = substr($image_uri,0, strpos($image_uri,'-'));
444 if (! strlen($image_uri)) {
447 $srch = '<' . intval($contact_id) . '>';
449 $r = q("SELECT `id` FROM `photo` WHERE `allow_cid` = '%s' AND `allow_gid` = '' AND `deny_cid` = '' AND `deny_gid` = ''
450 AND `resource-id` = '%s' AND `uid` = %d LIMIT 1",
456 if (! dbm::is_result($r)) {
460 $r = q("UPDATE `photo` SET `allow_cid` = '%s', `allow_gid` = '%s', `deny_cid` = '%s', `deny_gid` = '%s'
461 WHERE `resource-id` = '%s' AND `uid` = %d AND `album` = '%s' ",
462 dbesc($str_contact_allow),
463 dbesc($str_group_allow),
464 dbesc($str_contact_deny),
465 dbesc($str_group_deny),
467 intval($profile_uid),
468 dbesc( t('Wall Photos'))
476 * Next link in any attachment references we find in the post.
480 if ((! $preview) && preg_match_all("/\[attachment\](.*?)\[\/attachment\]/", $body, $match)) {
481 $attaches = $match[1];
482 if (count($attaches)) {
483 foreach ($attaches as $attach) {
484 $r = q("SELECT * FROM `attach` WHERE `uid` = %d AND `id` = %d LIMIT 1",
485 intval($profile_uid),
488 if (dbm::is_result($r)) {
489 $r = q("UPDATE `attach` SET `allow_cid` = '%s', `allow_gid` = '%s', `deny_cid` = '%s', `deny_gid` = '%s'
490 WHERE `uid` = %d AND `id` = %d",
491 dbesc($str_contact_allow),
492 dbesc($str_group_allow),
493 dbesc($str_contact_deny),
494 dbesc($str_group_deny),
495 intval($profile_uid),
503 // embedded bookmark or attachment in post? set bookmark flag
506 $data = get_attachment_data($body);
507 if (preg_match_all("/\[bookmark\=([^\]]*)\](.*?)\[\/bookmark\]/ism", $body, $match, PREG_SET_ORDER) OR isset($data["type"])) {
508 $objecttype = ACTIVITY_OBJ_BOOKMARK;
512 $body = bb_translate_video($body);
515 // Fold multi-line [code] sequences
516 $body = preg_replace('/\[\/code\]\s*\[code\]/ism', "\n", $body);
518 $body = scale_external_images($body, false);
520 // Setting the object type if not defined before
522 $objecttype = ACTIVITY_OBJ_NOTE; // Default value
523 require_once 'include/plaintext.php';
524 $objectdata = get_attached_data($body);
526 if ($post["type"] == "link") {
527 $objecttype = ACTIVITY_OBJ_BOOKMARK;
528 } elseif ($post["type"] == "video") {
529 $objecttype = ACTIVITY_OBJ_VIDEO;
530 } elseif ($post["type"] == "photo") {
531 $objecttype = ACTIVITY_OBJ_IMAGE;
536 // Look for any tags and linkify them
540 $tags = get_tags($body);
543 * add a statusnet style reply tag if the original post was from there
544 * and we are replying, and there isn't one already
546 if ($parent AND ($parent_contact['network'] == NETWORK_OSTATUS)) {
547 $contact = '@[url=' . $parent_contact['url'] . ']' . $parent_contact['nick'] . '[/url]';
549 if (!in_array($contact, $tags)) {
550 $body = $contact . ' ' . $body;
554 $toplevel_contact = "";
555 $toplevel_parent = q("SELECT `contact`.* FROM `contact`
556 INNER JOIN `item` ON `item`.`contact-id` = `contact`.`id` AND `contact`.`url` = `item`.`author-link`
557 WHERE `item`.`id` = `item`.`parent` AND `item`.`parent` = %d", intval($parent));
558 if (dbm::is_result($toplevel_parent)) {
559 $toplevel_contact = '@' . $toplevel_parent[0]['nick'] . '+' . $toplevel_parent[0]['id'];
561 $toplevel_parent = q("SELECT `author-link`, `author-name` FROM `item` WHERE `id` = `parent` AND `parent` = %d", intval($parent));
562 $toplevel_contact = '@[url=' . $toplevel_parent[0]['author-link'] . ']' . $toplevel_parent[0]['author-name'] . '[/url]';
565 if (!in_array($toplevel_contact, $tags)) {
566 $tags[] = $toplevel_contact;
572 $private_forum = false;
575 foreach ($tags as $tag) {
577 if (strpos($tag, '#') === 0) {
582 * If we already tagged 'Robert Johnson', don't try and tag 'Robert'.
583 * Robert Johnson should be first in the $tags array
585 $fullnametagged = false;
586 /// @TODO $tagged is initialized above if() block and is not filled, maybe old-lost code?
587 foreach ($tagged as $nextTag) {
588 if (stristr($nextTag, $tag . ' ')) {
589 $fullnametagged = true;
593 if ($fullnametagged) {
597 $success = handle_tag($a, $body, $inform, $str_tags, (local_user()) ? local_user() : $profile_uid , $tag, $network);
598 if ($success['replaced']) {
601 if (is_array($success['contact']) && intval($success['contact']['prv'])) {
602 $private_forum = true;
603 $private_id = $success['contact']['id'];
608 if (($private_forum) && (! $parent) && (! $private)) {
609 // we tagged a private forum in a top level post and the message was public.
612 $str_contact_allow = '<' . $private_id . '>';
618 if (preg_match_all('/(\[attachment\]([0-9]+)\[\/attachment\])/',$body,$match)) {
619 foreach ($match[2] as $mtch) {
620 $r = q("SELECT `id`,`filename`,`filesize`,`filetype` FROM `attach` WHERE `uid` = %d AND `id` = %d LIMIT 1",
621 intval($profile_uid),
624 if (dbm::is_result($r)) {
625 if (strlen($attachments)) {
628 $attachments .= '[attach]href="' . App::get_baseurl() . '/attach/' . $r[0]['id'] . '" length="' . $r[0]['filesize'] . '" type="' . $r[0]['filetype'] . '" title="' . (($r[0]['filename']) ? $r[0]['filename'] : '') . '"[/attach]';
630 $body = str_replace($match[1],'',$body);
636 if ($post_type === 'wall' || $post_type === 'wall-comment') {
640 if (! strlen($verb)) {
641 $verb = ACTIVITY_POST ;
644 if ($network == "") {
645 $network = NETWORK_DFRN;
648 $gravity = (($parent) ? 6 : 0 );
650 // even if the post arrived via API we are considering that it
651 // originated on this site by default for determining relayability.
653 $origin = ((x($_REQUEST, 'origin')) ? intval($_REQUEST['origin']) : 1);
655 $notify_type = (($parent) ? 'comment-new' : 'wall-new' );
657 $uri = (($message_id) ? $message_id : item_new_uri($a->get_hostname(),$profile_uid, $guid));
659 // Fallback so that we alway have a thr-parent
665 $datarray['uid'] = $profile_uid;
666 $datarray['type'] = $post_type;
667 $datarray['wall'] = $wall;
668 $datarray['gravity'] = $gravity;
669 $datarray['network'] = $network;
670 $datarray['contact-id'] = $contact_id;
671 $datarray['owner-name'] = $contact_record['name'];
672 $datarray['owner-link'] = $contact_record['url'];
673 $datarray['owner-avatar'] = $contact_record['thumb'];
674 $datarray['owner-id'] = get_contact($datarray['owner-link'], 0);
675 $datarray['author-name'] = $author['name'];
676 $datarray['author-link'] = $author['url'];
677 $datarray['author-avatar'] = $author['thumb'];
678 $datarray['author-id'] = get_contact($datarray['author-link'], 0);
679 $datarray['created'] = datetime_convert();
680 $datarray['edited'] = datetime_convert();
681 $datarray['commented'] = datetime_convert();
682 $datarray['received'] = datetime_convert();
683 $datarray['changed'] = datetime_convert();
684 $datarray['extid'] = $extid;
685 $datarray['guid'] = $guid;
686 $datarray['uri'] = $uri;
687 $datarray['title'] = $title;
688 $datarray['body'] = $body;
689 $datarray['app'] = $app;
690 $datarray['location'] = $location;
691 $datarray['coord'] = $coord;
692 $datarray['tag'] = $str_tags;
693 $datarray['file'] = $categories;
694 $datarray['inform'] = $inform;
695 $datarray['verb'] = $verb;
696 $datarray['object-type'] = $objecttype;
697 $datarray['allow_cid'] = $str_contact_allow;
698 $datarray['allow_gid'] = $str_group_allow;
699 $datarray['deny_cid'] = $str_contact_deny;
700 $datarray['deny_gid'] = $str_group_deny;
701 $datarray['private'] = $private;
702 $datarray['pubmail'] = $pubmail_enable;
703 $datarray['attach'] = $attachments;
704 $datarray['bookmark'] = intval($bookmark);
705 $datarray['thr-parent'] = $thr_parent;
706 $datarray['postopts'] = $postopts;
707 $datarray['origin'] = $origin;
708 $datarray['moderated'] = $allow_moderated;
709 $datarray['gcontact-id'] = get_gcontact_id(array("url" => $datarray['author-link'], "network" => $datarray['network'],
710 "photo" => $datarray['author-avatar'], "name" => $datarray['author-name']));
711 $datarray['object'] = $object;
714 * These fields are for the convenience of plugins...
715 * 'self' if true indicates the owner is posting on their own wall
716 * If parent is 0 it is a top-level post.
718 $datarray['parent'] = $parent;
719 $datarray['self'] = $self;
720 // $datarray['prvnets'] = $user['prvnets'];
722 $datarray['parent-uri'] = ($parent == 0) ? $uri : $parent_item['uri'];
723 $datarray['plink'] = App::get_baseurl() . '/display/' . urlencode($datarray['guid']);
724 $datarray['last-child'] = 1;
725 $datarray['visible'] = 1;
727 $datarray['protocol'] = PROTOCOL_DFRN;
729 $r = dba::fetch_first("SELECT `conversation-uri`, `conversation-href` FROM `conversation` WHERE `item-uri` = ?", $datarray['parent-uri']);
730 if (dbm::is_result($r)) {
731 if ($r['conversation-uri'] != '') {
732 $datarray['conversation-uri'] = $r['conversation-uri'];
734 if ($r['conversation-href'] != '') {
735 $datarray['conversation-href'] = $r['conversation-href'];
740 $datarray['edit'] = true;
743 // Search for hashtags
744 item_body_set_hashtags($datarray);
746 // preview mode - prepare the body for display and send it via json
748 require_once 'include/conversation.php';
749 // We set the datarray ID to -1 because in preview mode the dataray
750 // doesn't have an ID.
751 $datarray["id"] = -1;
752 $o = conversation($a,array(array_merge($contact_record,$datarray)),'search', false, true);
753 logger('preview: ' . $o);
754 echo json_encode(array('preview' => $o));
758 call_hooks('post_local',$datarray);
760 if (x($datarray, 'cancel')) {
761 logger('mod_item: post cancelled by plugin.');
763 goaway($return_path);
766 $json = array('cancel' => 1);
767 if (x($_REQUEST, 'jsreload') && strlen($_REQUEST['jsreload'])) {
768 $json['reload'] = App::get_baseurl() . '/' . $_REQUEST['jsreload'];
771 echo json_encode($json);
775 // Fill the cache field
776 put_item_in_cache($datarray);
778 $datarray = store_conversation($datarray);
781 $r = q("UPDATE `item` SET `title` = '%s', `body` = '%s', `tag` = '%s', `attach` = '%s', `file` = '%s', `rendered-html` = '%s', `rendered-hash` = '%s', `edited` = '%s', `changed` = '%s' WHERE `id` = %d AND `uid` = %d",
782 dbesc($datarray['title']),
783 dbesc($datarray['body']),
784 dbesc($datarray['tag']),
785 dbesc($datarray['attach']),
786 dbesc($datarray['file']),
787 dbesc($datarray['rendered-html']),
788 dbesc($datarray['rendered-hash']),
789 dbesc(datetime_convert()),
790 dbesc(datetime_convert()),
795 create_tags_from_item($post_id);
796 create_files_from_item($post_id);
797 update_thread($post_id);
799 // update filetags in pconfig
800 file_tag_update_pconfig($uid,$categories_old,$categories_new,'category');
802 proc_run(PRIORITY_HIGH, "include/notifier.php", 'edit_post', $post_id);
803 if ((x($_REQUEST, 'return')) && strlen($return_path)) {
804 logger('return: ' . $return_path);
805 goaway($return_path);
814 $r = q("INSERT INTO `item` (`guid`, `extid`, `uid`,`type`,`wall`,`gravity`, `network`, `contact-id`,
815 `owner-name`,`owner-link`,`owner-avatar`, `owner-id`,
816 `author-name`, `author-link`, `author-avatar`, `author-id`,
817 `created`, `edited`, `commented`, `received`, `changed`,
818 `uri`, `thr-parent`, `title`, `body`, `app`, `location`, `coord`,
819 `tag`, `inform`, `verb`, `object-type`, `postopts`,
820 `allow_cid`, `allow_gid`, `deny_cid`, `deny_gid`, `private`,
821 `pubmail`, `attach`, `bookmark`,`origin`, `moderated`, `file`,
822 `rendered-html`, `rendered-hash`, `gcontact-id`, `object`,
823 `parent`, `parent-uri`, `plink`, `last-child`, `visible`)
824 VALUES('%s', '%s', %d, '%s', %d, %d, '%s', %d,
825 '%s', '%s', '%s', %d,
826 '%s', '%s', '%s', %d,
827 '%s', '%s', '%s', '%s', '%s',
828 '%s', '%s', '%s', '%s', '%s', '%s', '%s',
829 '%s', '%s', '%s', '%s', '%s',
830 '%s', '%s', '%s', '%s', %d,
831 %d, '%s', %d, %d, %d, '%s',
832 '%s', '%s', %d, '%s',
833 %d, '%s', '%s', %d, %d)",
834 dbesc($datarray['guid']),
835 dbesc($datarray['extid']),
836 intval($datarray['uid']),
837 dbesc($datarray['type']),
838 intval($datarray['wall']),
839 intval($datarray['gravity']),
840 dbesc($datarray['network']),
841 intval($datarray['contact-id']),
842 dbesc($datarray['owner-name']),
843 dbesc($datarray['owner-link']),
844 dbesc($datarray['owner-avatar']),
845 intval($datarray['owner-id']),
846 dbesc($datarray['author-name']),
847 dbesc($datarray['author-link']),
848 dbesc($datarray['author-avatar']),
849 intval($datarray['author-id']),
850 dbesc($datarray['created']),
851 dbesc($datarray['edited']),
852 dbesc($datarray['commented']),
853 dbesc($datarray['received']),
854 dbesc($datarray['changed']),
855 dbesc($datarray['uri']),
856 dbesc($datarray['thr-parent']),
857 dbesc($datarray['title']),
858 dbesc($datarray['body']),
859 dbesc($datarray['app']),
860 dbesc($datarray['location']),
861 dbesc($datarray['coord']),
862 dbesc($datarray['tag']),
863 dbesc($datarray['inform']),
864 dbesc($datarray['verb']),
865 dbesc($datarray['object-type']),
866 dbesc($datarray['postopts']),
867 dbesc($datarray['allow_cid']),
868 dbesc($datarray['allow_gid']),
869 dbesc($datarray['deny_cid']),
870 dbesc($datarray['deny_gid']),
871 intval($datarray['private']),
872 intval($datarray['pubmail']),
873 dbesc($datarray['attach']),
874 intval($datarray['bookmark']),
875 intval($datarray['origin']),
876 intval($datarray['moderated']),
877 dbesc($datarray['file']),
878 dbesc($datarray['rendered-html']),
879 dbesc($datarray['rendered-hash']),
880 intval($datarray['gcontact-id']),
881 dbesc($datarray['object']),
882 intval($datarray['parent']),
883 dbesc($datarray['parent-uri']),
884 dbesc($datarray['plink']),
885 intval($datarray['last-child']),
886 intval($datarray['visible'])
889 if (dbm::is_result($r)) {
890 $r = q("SELECT LAST_INSERT_ID() AS `item-id`");
891 if (dbm::is_result($r)) {
892 $post_id = $r[0]['item-id'];
897 logger('mod_item: unable to create post.');
903 logger('mod_item: unable to retrieve post that was just stored.');
904 notice(t('System error. Post not saved.') . EOL);
905 goaway($return_path);
909 logger('mod_item: saved item ' . $post_id);
911 $datarray["id"] = $post_id;
913 item_set_last_item($datarray);
915 // update filetags in pconfig
916 file_tag_update_pconfig($uid,$categories_old,$categories_new,'category');
920 // This item is the last leaf and gets the comment box, clear any ancestors
921 $r = q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent` = %d AND `last-child` AND `id` != %d",
922 dbesc(datetime_convert()),
927 // update the commented timestamp on the parent
928 q("UPDATE `item` SET `visible` = 1, `commented` = '%s', `changed` = '%s' WHERE `id` = %d",
929 dbesc(datetime_convert()),
930 dbesc(datetime_convert()),
934 if ($contact_record != $author) {
936 'type' => NOTIFY_COMMENT,
937 'notify_flags' => $user['notify-flags'],
938 'language' => $user['language'],
939 'to_name' => $user['username'],
940 'to_email' => $user['email'],
941 'uid' => $user['uid'],
943 'link' => App::get_baseurl().'/display/'.urlencode($datarray['guid']),
944 'source_name' => $datarray['author-name'],
945 'source_link' => $datarray['author-link'],
946 'source_photo' => $datarray['author-avatar'],
947 'verb' => ACTIVITY_POST,
950 'parent_uri' => $parent_item['uri']
956 // Store the comment signature information in case we need to relay to Diaspora
957 Diaspora::store_comment_signature($datarray, $author, ($self ? $user['prvkey'] : false), $post_id);
962 $r = q("UPDATE `item` SET `parent` = %d WHERE `id` = %d",
966 if ($contact_record != $author) {
968 'type' => NOTIFY_WALL,
969 'notify_flags' => $user['notify-flags'],
970 'language' => $user['language'],
971 'to_name' => $user['username'],
972 'to_email' => $user['email'],
973 'uid' => $user['uid'],
975 'link' => App::get_baseurl().'/display/'.urlencode($datarray['guid']),
976 'source_name' => $datarray['author-name'],
977 'source_link' => $datarray['author-link'],
978 'source_photo' => $datarray['author-avatar'],
979 'verb' => ACTIVITY_POST,
985 call_hooks('post_local_end', $datarray);
987 if (strlen($emailcc) && $profile_uid == local_user()) {
988 $erecips = explode(',', $emailcc);
989 if (count($erecips)) {
990 foreach ($erecips as $recip) {
991 $addr = trim($recip);
992 if (! strlen($addr)) {
995 $disclaimer = '<hr />' . sprintf( t('This message was sent to you by %s, a member of the Friendica social network.'), $a->user['username'])
997 $disclaimer .= sprintf( t('You may visit them online at %s'), App::get_baseurl() . '/profile/' . $a->user['nickname']) . EOL;
998 $disclaimer .= t('Please contact the sender by replying to this post if you do not wish to receive these messages.') . EOL;
999 if (!$datarray['title']=='') {
1000 $subject = email_header_encode($datarray['title'], 'UTF-8');
1002 $subject = email_header_encode('[Friendica]' . ' ' . sprintf( t('%s posted an update.'), $a->user['username']), 'UTF-8');
1004 $link = '<a href="' . App::get_baseurl() . '/profile/' . $a->user['nickname'] . '"><img src="' . $author['thumb'] . '" alt="' . $a->user['username'] . '" /></a><br /><br />';
1005 $html = prepare_body($datarray);
1006 $message = '<html><body>' . $link . $html . $disclaimer . '</body></html>';
1007 include_once 'include/html2plain.php';
1009 'fromName' => $a->user['username'],
1010 'fromEmail' => $a->user['email'],
1012 'replyTo' => $a->user['email'],
1013 'messageSubject' => $subject,
1014 'htmlVersion' => $message,
1015 'textVersion' => html2plain($html.$disclaimer),
1017 Emailer::send($params);
1022 if ($post_id == $parent) {
1023 add_thread($post_id);
1025 update_thread($parent, true);
1030 create_tags_from_item($post_id);
1031 create_files_from_item($post_id);
1033 // Insert an item entry for UID=0 for global entries.
1034 // We now do it in the background to save some time.
1035 // This is important in interactive environments like the frontend or the API.
1036 // We don't fork a new process since this is done anyway with the following command
1037 proc_run(array('priority' => PRIORITY_HIGH, 'dont_fork' => true), "include/create_shadowentry.php", $post_id);
1039 // Call the background process that is delivering the item to the receivers
1040 proc_run(PRIORITY_HIGH, "include/notifier.php", $notify_type, $post_id);
1042 logger('post_complete');
1044 item_post_return(App::get_baseurl(), $api_source, $return_path);
1048 function item_post_return($baseurl, $api_source, $return_path) {
1049 // figure out how to return, depending on from whence we came
1056 goaway($return_path);
1059 $json = array('success' => 1);
1060 if (x($_REQUEST, 'jsreload') && strlen($_REQUEST['jsreload'])) {
1061 $json['reload'] = $baseurl . '/' . $_REQUEST['jsreload'];
1064 logger('post_json: ' . print_r($json,true), LOGGER_DEBUG);
1066 echo json_encode($json);
1072 function item_content(App $a) {
1074 if ((! local_user()) && (! remote_user())) {
1078 require_once 'include/security.php';
1081 if (($a->argc == 3) && ($a->argv[1] === 'drop') && intval($a->argv[2])) {
1082 $o = drop_item($a->argv[2], !is_ajax());
1084 // ajax return: [<item id>, 0 (no perm) | <owner id>]
1085 echo json_encode(array(intval($a->argv[2]), intval($o)));
1093 * This function removes the tag $tag from the text $body and replaces it with
1094 * the appropiate link.
1096 * @param App $a Application instance @TODO is unused in this function's scope (excluding included files)
1097 * @param unknown_type $body the text to replace the tag in
1098 * @param string $inform a comma-seperated string containing everybody to inform
1099 * @param string $str_tags string to add the tag to
1100 * @param integer $profile_uid
1101 * @param string $tag the tag to replace
1102 * @param string $network The network of the post
1104 * @return boolean true if replaced, false if not replaced
1106 function handle_tag(App $a, &$body, &$inform, &$str_tags, $profile_uid, $tag, $network = "") {
1107 require_once 'include/socgraph.php';
1112 //is it a person tag?
1113 if (strpos($tag, '@') === 0) {
1114 //is it already replaced?
1115 if (strpos($tag, '[url=')) {
1116 //append tag to str_tags
1117 if (!stristr($str_tags, $tag)) {
1118 if (strlen($str_tags)) {
1124 // Checking for the alias that is used for OStatus
1125 $pattern = "/@\[url\=(.*?)\](.*?)\[\/url\]/ism";
1126 if (preg_match($pattern, $tag, $matches)) {
1128 $r = q("SELECT `alias`, `name` FROM `contact` WHERE `nurl` = '%s' AND `alias` != '' AND `uid` = 0",
1129 normalise_link($matches[1]));
1130 if (!dbm::is_result($r)) {
1131 $r = q("SELECT `alias`, `name` FROM `gcontact` WHERE `nurl` = '%s' AND `alias` != ''",
1132 normalise_link($matches[1]));
1134 if (dbm::is_result($r)) {
1137 $data = probe_url($matches[1]);
1140 if ($data["alias"] != "") {
1141 $newtag = '@[url=' . $data["alias"] . ']' . $data["name"] . '[/url]';
1142 if (!stristr($str_tags, $newtag)) {
1143 if (strlen($str_tags)) {
1146 $str_tags .= $newtag;
1154 //get the person's name
1155 $name = substr($tag, 1);
1157 // Sometimes the tag detection doesn't seem to work right
1158 // This is some workaround
1159 $nameparts = explode(" ", $name);
1160 $name = $nameparts[0];
1162 // Try to detect the contact in various ways
1163 if ((strpos($name, '@')) || (strpos($name, 'http://'))) {
1164 // Is it in format @user@domain.tld or @http://domain.tld/...?
1166 // First check the contact table for the address
1167 $r = q("SELECT `id`, `url`, `nick`, `name`, `alias`, `network`, `notify` FROM `contact`
1168 WHERE `addr` = '%s' AND `uid` = %d AND
1169 (`network` != '%s' OR (`notify` != '' AND `alias` != ''))
1172 intval($profile_uid),
1173 dbesc(NETWORK_OSTATUS)
1176 // Then check in the contact table for the url
1177 if (!dbm::is_result($r)) {
1178 $r = q("SELECT `id`, `url`, `nick`, `name`, `alias`, `network`, `notify` FROM `contact`
1179 WHERE `nurl` = '%s' AND `uid` = %d AND
1180 (`network` != '%s' OR (`notify` != '' AND `alias` != ''))
1182 dbesc(normalise_link($name)),
1183 intval($profile_uid),
1184 dbesc(NETWORK_OSTATUS)
1188 // Then check in the global contacts for the address
1189 if (!dbm::is_result($r)) {
1190 $r = q("SELECT `url`, `nick`, `name`, `alias`, `network`, `notify` FROM `gcontact`
1191 WHERE `addr` = '%s' AND (`network` != '%s' OR (`notify` != '' AND `alias` != ''))
1194 dbesc(NETWORK_OSTATUS)
1198 // Then check in the global contacts for the url
1199 if (!dbm::is_result($r)) {
1200 $r = q("SELECT `url`, `nick`, `name`, `alias`, `network`, `notify` FROM `gcontact`
1201 WHERE `nurl` = '%s' AND (`network` != '%s' OR (`notify` != '' AND `alias` != ''))
1203 dbesc(normalise_link($name)),
1204 dbesc(NETWORK_OSTATUS)
1208 if (!dbm::is_result($r)) {
1209 $probed = probe_url($name);
1210 if ($result['network'] != NETWORK_PHANTOM) {
1211 update_gcontact($probed);
1212 $r = q("SELECT `url`, `name`, `nick`, `network`, `alias`, `notify` FROM `gcontact` WHERE `nurl` = '%s' LIMIT 1",
1213 dbesc(normalise_link($probed["url"])));
1218 if (strrpos($name, '+')) {
1219 // Is it in format @nick+number?
1220 $tagcid = intval(substr($name, strrpos($name, '+') + 1));
1222 $r = q("SELECT `id`, `url`, `nick`, `name`, `alias`, `network` FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
1224 intval($profile_uid)
1228 // select someone by attag or nick and the name passed in the current network
1229 if(!dbm::is_result($r) AND ($network != ""))
1230 $r = q("SELECT `id`, `url`, `nick`, `name`, `alias`, `network` FROM `contact` WHERE `attag` = '%s' OR `nick` = '%s' AND `network` = '%s' AND `uid` = %d ORDER BY `attag` DESC LIMIT 1",
1234 intval($profile_uid)
1237 //select someone from this user's contacts by name in the current network
1238 if (!dbm::is_result($r) AND ($network != "")) {
1239 $r = q("SELECT `id`, `url`, `nick`, `name`, `alias`, `network` FROM `contact` WHERE `name` = '%s' AND `network` = '%s' AND `uid` = %d LIMIT 1",
1242 intval($profile_uid)
1246 // select someone by attag or nick and the name passed in
1247 if(!dbm::is_result($r)) {
1248 $r = q("SELECT `id`, `url`, `nick`, `name`, `alias`, `network` FROM `contact` WHERE `attag` = '%s' OR `nick` = '%s' AND `uid` = %d ORDER BY `attag` DESC LIMIT 1",
1251 intval($profile_uid)
1255 // select someone from this user's contacts by name
1256 if(!dbm::is_result($r)) {
1257 $r = q("SELECT `id`, `url`, `nick`, `name`, `alias`, `network` FROM `contact` WHERE `name` = '%s' AND `uid` = %d LIMIT 1",
1259 intval($profile_uid)
1264 if (dbm::is_result($r)) {
1265 if (strlen($inform) AND (isset($r[0]["notify"]) OR isset($r[0]["id"]))) {
1269 if (isset($r[0]["id"])) {
1270 $inform .= 'cid:' . $r[0]["id"];
1271 } elseif (isset($r[0]["notify"])) {
1272 $inform .= $r[0]["notify"];
1275 $profile = $r[0]["url"];
1276 $alias = $r[0]["alias"];
1277 $newname = $r[0]["nick"];
1278 if (($newname == "") OR (($r[0]["network"] != NETWORK_OSTATUS) AND ($r[0]["network"] != NETWORK_TWITTER)
1279 AND ($r[0]["network"] != NETWORK_STATUSNET) AND ($r[0]["network"] != NETWORK_APPNET))) {
1280 $newname = $r[0]["name"];
1284 //if there is an url for this persons profile
1285 if (isset($profile) AND ($newname != "")) {
1288 // create profile link
1289 $profile = str_replace(',', '%2c', $profile);
1290 $newtag = '@[url=' . $profile . ']' . $newname . '[/url]';
1291 $body = str_replace('@' . $name, $newtag, $body);
1292 // append tag to str_tags
1293 if (! stristr($str_tags, $newtag)) {
1294 if (strlen($str_tags)) {
1297 $str_tags .= $newtag;
1301 * Status.Net seems to require the numeric ID URL in a mention if the person isn't
1302 * subscribed to you. But the nickname URL is OK if they are. Grrr. We'll tag both.
1304 if (strlen($alias)) {
1305 $newtag = '@[url=' . $alias . ']' . $newname . '[/url]';
1306 if (! stristr($str_tags, $newtag)) {
1307 if (strlen($str_tags)) {
1310 $str_tags .= $newtag;
1316 return array('replaced' => $replaced, 'contact' => $r[0]);