5 * This is the POST destination for most all locally posted
6 * text stuff. This function handles status, wall-to-wall status,
7 * local comments, and remote coments that are posted on this site
8 * (as opposed to being delivered in a feed).
9 * Also processed here are posts and comments coming through the
10 * statusnet/twitter API.
11 * All of these become an "item" which is our basic unit of
13 * Posts that originate externally or do not fall into the above
14 * posting categories go through item_store() instead of this function.
18 require_once('include/crypto.php');
19 require_once('include/enotify.php');
20 require_once('include/email.php');
21 require_once('Text/LanguageDetect.php');
23 function item_post(&$a) {
25 if((! local_user()) && (! remote_user()) && (! x($_REQUEST,'commenter')))
28 require_once('include/security.php');
32 if(x($_REQUEST,'dropitems')) {
33 require_once('include/items.php');
34 $arr_drop = explode(',',$_REQUEST['dropitems']);
35 drop_items($arr_drop);
36 $json = array('success' => 1);
37 echo json_encode($json);
41 call_hooks('post_local_start', $_REQUEST);
42 // logger('postinput ' . file_get_contents('php://input'));
43 logger('postvars ' . print_r($_REQUEST,true), LOGGER_DATA);
45 $api_source = ((x($_REQUEST,'api_source') && $_REQUEST['api_source']) ? true : false);
46 $return_path = ((x($_REQUEST,'return')) ? $_REQUEST['return'] : '');
47 $preview = ((x($_REQUEST,'preview')) ? intval($_REQUEST['preview']) : 0);
50 // Check for doubly-submitted posts, and reject duplicates
51 // Note that we have to ignore previews, otherwise nothing will post
52 // after it's been previewed
53 if(!$preview && x($_REQUEST['post_id_random'])) {
54 if(x($_SESSION['post-random']) && $_SESSION['post-random'] == $_REQUEST['post_id_random']) {
55 logger("item post: duplicate post", LOGGER_DEBUG);
56 item_post_return($a->get_baseurl(), $api_source, $return_path);
59 $_SESSION['post-random'] = $_REQUEST['post_id_random'];
63 * Is this a reply to something?
66 $parent = ((x($_REQUEST,'parent')) ? intval($_REQUEST['parent']) : 0);
67 $parent_uri = ((x($_REQUEST,'parent_uri')) ? trim($_REQUEST['parent_uri']) : '');
70 $parent_contact = null;
75 if($parent || $parent_uri) {
77 if(! x($_REQUEST,'type'))
78 $_REQUEST['type'] = 'net-comment';
81 $r = q("SELECT * FROM `item` WHERE `id` = %d LIMIT 1",
85 elseif($parent_uri && local_user()) {
86 // This is coming from an API source, and we are logged in
87 $r = q("SELECT * FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
92 // if this isn't the real parent of the conversation, find it
93 if($r !== false && count($r)) {
94 $parid = $r[0]['parent'];
95 $parent_uri = $r[0]['uri'];
96 if($r[0]['id'] != $r[0]['parent']) {
97 $r = q("SELECT * FROM `item` WHERE `id` = `parent` AND `parent` = %d LIMIT 1",
103 if(($r === false) || (! count($r))) {
104 notice( t('Unable to locate original post.') . EOL);
105 if(x($_REQUEST,'return'))
106 goaway($a->get_baseurl() . "/" . $return_path );
109 $parent_item = $r[0];
110 $parent = $r[0]['id'];
112 // multi-level threading - preserve the info but re-parent to our single level threading
113 //if(($parid) && ($parid != $parent))
114 $thr_parent = $parent_uri;
116 if($parent_item['contact-id'] && $uid) {
117 $r = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
118 intval($parent_item['contact-id']),
122 $parent_contact = $r[0];
126 if($parent) logger('mod_item: item_post parent=' . $parent);
128 $profile_uid = ((x($_REQUEST,'profile_uid')) ? intval($_REQUEST['profile_uid']) : 0);
129 $post_id = ((x($_REQUEST,'post_id')) ? intval($_REQUEST['post_id']) : 0);
130 $app = ((x($_REQUEST,'source')) ? strip_tags($_REQUEST['source']) : '');
132 $allow_moderated = false;
134 // here is where we are going to check for permission to post a moderated comment.
136 // First check that the parent exists and it is a wall item.
138 if((x($_REQUEST,'commenter')) && ((! $parent) || (! $parent_item['wall']))) {
139 notice( t('Permission denied.') . EOL) ;
140 if(x($_REQUEST,'return'))
141 goaway($a->get_baseurl() . "/" . $return_path );
145 // Now check that it is a page_type of PAGE_BLOG, and that valid personal details
146 // have been provided, and run any anti-spam plugins
154 if((! can_write_wall($a,$profile_uid)) && (! $allow_moderated)) {
155 notice( t('Permission denied.') . EOL) ;
156 if(x($_REQUEST,'return'))
157 goaway($a->get_baseurl() . "/" . $return_path );
162 // is this an edited post?
167 $i = q("SELECT * FROM `item` WHERE `uid` = %d AND `id` = %d LIMIT 1",
168 intval($profile_uid),
178 $r = q("SELECT * FROM `user` WHERE `uid` = %d LIMIT 1",
185 $str_group_allow = $orig_post['allow_gid'];
186 $str_contact_allow = $orig_post['allow_cid'];
187 $str_group_deny = $orig_post['deny_gid'];
188 $str_contact_deny = $orig_post['deny_cid'];
189 $location = $orig_post['location'];
190 $coord = $orig_post['coord'];
191 $verb = $orig_post['verb'];
192 $emailcc = $orig_post['emailcc'];
193 $app = $orig_post['app'];
194 $categories = $orig_post['file'];
195 $title = notags(trim($_REQUEST['title']));
196 $body = escape_tags(trim($_REQUEST['body']));
197 $private = $orig_post['private'];
198 $pubmail_enable = $orig_post['pubmail'];
203 // if coming from the API and no privacy settings are set,
204 // use the user default permissions - as they won't have
205 // been supplied via a form.
208 && (! array_key_exists('contact_allow',$_REQUEST))
209 && (! array_key_exists('group_allow',$_REQUEST))
210 && (! array_key_exists('contact_deny',$_REQUEST))
211 && (! array_key_exists('group_deny',$_REQUEST))) {
212 $str_group_allow = $user['allow_gid'];
213 $str_contact_allow = $user['allow_cid'];
214 $str_group_deny = $user['deny_gid'];
215 $str_contact_deny = $user['deny_cid'];
219 // 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']));
235 $naked_body = preg_replace('/\[(.+?)\]/','',$body);
237 if (version_compare(PHP_VERSION, '5.3.0', '>=')) {
238 $l = new Text_LanguageDetect;
239 $lng = $l->detectConfidence($naked_body);
241 $postopts = (($lng['language']) ? 'lang=' . $lng['language'] . ';' . $lng['confidence'] : '');
243 logger('mod_item: detect language' . print_r($lng,true) . $naked_body, LOGGER_DATA);
249 $private = ((strlen($str_group_allow) || strlen($str_contact_allow) || strlen($str_group_deny) || strlen($str_contact_deny)) ? 1 : 0);
251 // If this is a comment, set the permissions from the parent.
256 if(($parent_item['private'])
257 || strlen($parent_item['allow_cid'])
258 || strlen($parent_item['allow_gid'])
259 || strlen($parent_item['deny_cid'])
260 || strlen($parent_item['deny_gid'])) {
261 $private = (($parent_item['private']) ? $parent_item['private'] : 1);
264 $str_contact_allow = $parent_item['allow_cid'];
265 $str_group_allow = $parent_item['allow_gid'];
266 $str_contact_deny = $parent_item['deny_cid'];
267 $str_group_deny = $parent_item['deny_gid'];
270 $pubmail_enable = ((x($_REQUEST,'pubmail_enable') && intval($_REQUEST['pubmail_enable']) && (! $private)) ? 1 : 0);
272 // if using the API, we won't see pubmail_enable - figure out if it should be set
274 if($api_source && $profile_uid && $profile_uid == local_user() && (! $private)) {
275 $mail_disabled = ((function_exists('imap_open') && (! get_config('system','imap_disabled'))) ? 0 : 1);
276 if(! $mail_disabled) {
277 $r = q("SELECT * FROM `mailacct` WHERE `uid` = %d AND `server` != '' LIMIT 1",
280 if(count($r) && intval($r[0]['pubmail']))
281 $pubmail_enabled = true;
285 if(! strlen($body)) {
288 info( t('Empty post discarded.') . EOL );
289 if(x($_REQUEST,'return'))
290 goaway($a->get_baseurl() . "/" . $return_path );
295 if(strlen($categories)) {
296 // get the "fileas" tags for this post
297 $filedas = file_tag_file_to_list($categories, 'file');
299 // save old and new categories, so we can determine what needs to be deleted from pconfig
300 $categories_old = $categories;
301 $categories = file_tag_list_to_file(trim($_REQUEST['category']), 'category');
302 $categories_new = $categories;
303 if(strlen($filedas)) {
304 // append the fileas stuff to the new categories list
305 $categories .= file_tag_list_to_file($filedas, 'file');
308 // Work around doubled linefeeds in Tinymce 3.5b2
309 // First figure out if it's a status post that would've been
310 // created using tinymce. Otherwise leave it alone.
312 $plaintext = (local_user() ? intval(get_pconfig(local_user(),'system','plaintext')) : 0);
313 if((! $parent) && (! $api_source) && (! $plaintext)) {
314 $body = fix_mce_lf($body);
318 // get contact info for poster
324 if((local_user()) && (local_user() == $profile_uid)) {
326 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 1 LIMIT 1",
327 intval($_SESSION['uid'])
330 elseif(remote_user()) {
331 if(is_array($_SESSION['remote'])) {
332 foreach($_SESSION['remote'] as $v) {
333 if($v['uid'] == $profile_uid) {
334 $contact_id = $v['cid'];
340 $r = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
348 $contact_id = $author['id'];
351 // get contact info for owner
353 if($profile_uid == local_user()) {
354 $contact_record = $author;
357 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 1 LIMIT 1",
361 $contact_record = $r[0];
364 $post_type = notags(trim($_REQUEST['type']));
366 if($post_type === 'net-comment') {
367 if($parent_item !== null) {
368 if($parent_item['wall'] == 1)
369 $post_type = 'wall-comment';
371 $post_type = 'remote-comment';
377 * When a photo was uploaded into the message using the (profile wall) ajax
378 * uploader, The permissions are initially set to disallow anybody but the
379 * owner from seeing it. This is because the permissions may not yet have been
380 * set for the post. If it's private, the photo permissions should be set
381 * appropriately. But we didn't know the final permissions on the post until
382 * now. So now we'll look for links of uploaded messages that are in the
383 * post and set them to the same permissions as the post itself.
389 if((! $preview) && preg_match_all("/\[img([\=0-9x]*?)\](.*?)\[\/img\]/",$body,$match)) {
392 foreach($images as $image) {
393 if(! stristr($image,$a->get_baseurl() . '/photo/'))
395 $image_uri = substr($image,strrpos($image,'/') + 1);
396 $image_uri = substr($image_uri,0, strpos($image_uri,'-'));
397 if(! strlen($image_uri))
399 $srch = '<' . intval($contact_id) . '>';
401 $r = q("SELECT `id` FROM `photo` WHERE `allow_cid` = '%s' AND `allow_gid` = '' AND `deny_cid` = '' AND `deny_gid` = ''
402 AND `resource-id` = '%s' AND `uid` = %d LIMIT 1",
412 $r = q("UPDATE `photo` SET `allow_cid` = '%s', `allow_gid` = '%s', `deny_cid` = '%s', `deny_gid` = '%s'
413 WHERE `resource-id` = '%s' AND `uid` = %d AND `album` = '%s' ",
414 dbesc($str_contact_allow),
415 dbesc($str_group_allow),
416 dbesc($str_contact_deny),
417 dbesc($str_group_deny),
419 intval($profile_uid),
420 dbesc( t('Wall Photos'))
429 * Next link in any attachment references we find in the post.
434 if((! $preview) && preg_match_all("/\[attachment\](.*?)\[\/attachment\]/",$body,$match)) {
435 $attaches = $match[1];
436 if(count($attaches)) {
437 foreach($attaches as $attach) {
438 $r = q("SELECT * FROM `attach` WHERE `uid` = %d AND `id` = %d LIMIT 1",
439 intval($profile_uid),
443 $r = q("UPDATE `attach` SET `allow_cid` = '%s', `allow_gid` = '%s', `deny_cid` = '%s', `deny_gid` = '%s'
444 WHERE `uid` = %d AND `id` = %d LIMIT 1",
445 dbesc($str_contact_allow),
446 dbesc($str_group_allow),
447 dbesc($str_contact_deny),
448 dbesc($str_group_deny),
449 intval($profile_uid),
457 // embedded bookmark in post? set bookmark flag
460 if(preg_match_all("/\[bookmark\=([^\]]*)\](.*?)\[\/bookmark\]/ism",$body,$match,PREG_SET_ORDER)) {
464 $body = bb_translate_video($body);
468 * Fold multi-line [code] sequences
471 $body = preg_replace('/\[\/code\]\s*\[code\]/ism',"\n",$body);
473 $body = scale_external_images($body,false);
478 * Look for any tags and linkify them
485 $tags = get_tags($body);
488 * add a statusnet style reply tag if the original post was from there
489 * and we are replying, and there isn't one already
492 if(($parent_contact) && ($parent_contact['network'] === NETWORK_OSTATUS)
493 && ($parent_contact['nick']) && (! in_array('@' . $parent_contact['nick'],$tags))) {
494 $body = '@' . $parent_contact['nick'] . ' ' . $body;
495 $tags[] = '@' . $parent_contact['nick'];
500 $private_forum = false;
503 foreach($tags as $tag) {
505 // If we already tagged 'Robert Johnson', don't try and tag 'Robert'.
506 // Robert Johnson should be first in the $tags array
508 $fullnametagged = false;
509 for($x = 0; $x < count($tagged); $x ++) {
510 if(stristr($tagged[$x],$tag . ' ')) {
511 $fullnametagged = true;
518 $success = handle_tag($a, $body, $inform, $str_tags, (local_user()) ? local_user() : $profile_uid , $tag);
519 if($success['replaced'])
521 if(is_array($success['contact']) && intval($success['contact']['prv'])) {
522 $private_forum = true;
523 $private_id = $success['contact']['id'];
528 if(($private_forum) && (! $parent) && (! $private)) {
529 // we tagged a private forum in a top level post and the message was public.
532 $str_contact_allow = '<' . $private_id . '>';
538 if(preg_match_all('/(\[attachment\]([0-9]+)\[\/attachment\])/',$body,$match)) {
539 foreach($match[2] as $mtch) {
540 $r = q("SELECT `id`,`filename`,`filesize`,`filetype` FROM `attach` WHERE `uid` = %d AND `id` = %d LIMIT 1",
541 intval($profile_uid),
545 if(strlen($attachments))
547 $attachments .= '[attach]href="' . $a->get_baseurl() . '/attach/' . $r[0]['id'] . '" length="' . $r[0]['filesize'] . '" type="' . $r[0]['filetype'] . '" title="' . (($r[0]['filename']) ? $r[0]['filename'] : '') . '"[/attach]';
549 $body = str_replace($match[1],'',$body);
555 if($post_type === 'wall' || $post_type === 'wall-comment')
559 $verb = ACTIVITY_POST ;
561 $gravity = (($parent) ? 6 : 0 );
563 // even if the post arrived via API we are considering that it
564 // originated on this site by default for determining relayability.
566 $origin = ((x($_REQUEST,'origin')) ? intval($_REQUEST['origin']) : 1);
568 $notify_type = (($parent) ? 'comment-new' : 'wall-new' );
570 $uri = item_new_uri($a->get_hostname(),$profile_uid);
572 // Fallback so that we alway have a thr-parent
577 $datarray['uid'] = $profile_uid;
578 $datarray['type'] = $post_type;
579 $datarray['wall'] = $wall;
580 $datarray['gravity'] = $gravity;
581 $datarray['contact-id'] = $contact_id;
582 $datarray['owner-name'] = $contact_record['name'];
583 $datarray['owner-link'] = $contact_record['url'];
584 $datarray['owner-avatar'] = $contact_record['thumb'];
585 $datarray['author-name'] = $author['name'];
586 $datarray['author-link'] = $author['url'];
587 $datarray['author-avatar'] = $author['thumb'];
588 $datarray['created'] = datetime_convert();
589 $datarray['edited'] = datetime_convert();
590 $datarray['commented'] = datetime_convert();
591 $datarray['received'] = datetime_convert();
592 $datarray['changed'] = datetime_convert();
593 $datarray['uri'] = $uri;
594 $datarray['title'] = $title;
595 $datarray['body'] = $body;
596 $datarray['app'] = $app;
597 $datarray['location'] = $location;
598 $datarray['coord'] = $coord;
599 $datarray['tag'] = $str_tags;
600 $datarray['file'] = $categories;
601 $datarray['inform'] = $inform;
602 $datarray['verb'] = $verb;
603 $datarray['allow_cid'] = $str_contact_allow;
604 $datarray['allow_gid'] = $str_group_allow;
605 $datarray['deny_cid'] = $str_contact_deny;
606 $datarray['deny_gid'] = $str_group_deny;
607 $datarray['private'] = $private;
608 $datarray['pubmail'] = $pubmail_enable;
609 $datarray['attach'] = $attachments;
610 $datarray['bookmark'] = intval($bookmark);
611 $datarray['thr-parent'] = $thr_parent;
612 $datarray['postopts'] = $postopts;
613 $datarray['origin'] = $origin;
614 $datarray['moderated'] = $allow_moderated;
617 * These fields are for the convenience of plugins...
618 * 'self' if true indicates the owner is posting on their own wall
619 * If parent is 0 it is a top-level post.
622 $datarray['parent'] = $parent;
623 $datarray['self'] = $self;
624 // $datarray['prvnets'] = $user['prvnets'];
627 $datarray['edit'] = true;
629 $datarray['guid'] = get_guid();
631 // preview mode - prepare the body for display and send it via json
634 require_once('include/conversation.php');
635 $o = conversation($a,array(array_merge($contact_record,$datarray)),'search', false, true);
636 logger('preview: ' . $o);
637 echo json_encode(array('preview' => $o));
642 call_hooks('post_local',$datarray);
644 if(x($datarray,'cancel')) {
645 logger('mod_item: post cancelled by plugin.');
647 goaway($a->get_baseurl() . "/" . $return_path);
650 $json = array('cancel' => 1);
651 if(x($_REQUEST,'jsreload') && strlen($_REQUEST['jsreload']))
652 $json['reload'] = $a->get_baseurl() . '/' . $_REQUEST['jsreload'];
654 echo json_encode($json);
660 $r = q("UPDATE `item` SET `title` = '%s', `body` = '%s', `tag` = '%s', `attach` = '%s', `file` = '%s', `edited` = '%s' WHERE `id` = %d AND `uid` = %d LIMIT 1",
661 dbesc($datarray['title']),
662 dbesc($datarray['body']),
663 dbesc($datarray['tag']),
664 dbesc($datarray['attach']),
665 dbesc($datarray['file']),
666 dbesc(datetime_convert()),
671 // update filetags in pconfig
672 file_tag_update_pconfig($uid,$categories_old,$categories_new,'category');
674 proc_run('php', "include/notifier.php", 'edit_post', "$post_id");
675 if((x($_REQUEST,'return')) && strlen($return_path)) {
676 logger('return: ' . $return_path);
677 goaway($a->get_baseurl() . "/" . $return_path );
685 $r = q("INSERT INTO `item` (`guid`, `uid`,`type`,`wall`,`gravity`,`contact-id`,`owner-name`,`owner-link`,`owner-avatar`,
686 `author-name`, `author-link`, `author-avatar`, `created`, `edited`, `commented`, `received`, `changed`, `uri`, `thr-parent`, `title`, `body`, `app`, `location`, `coord`,
687 `tag`, `inform`, `verb`, `postopts`, `allow_cid`, `allow_gid`, `deny_cid`, `deny_gid`, `private`, `pubmail`, `attach`, `bookmark`,`origin`, `moderated`, `file` )
688 VALUES( '%s', %d, '%s', %d, %d, %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d, '%s', %d, %d, %d, '%s' )",
689 dbesc($datarray['guid']),
690 intval($datarray['uid']),
691 dbesc($datarray['type']),
692 intval($datarray['wall']),
693 intval($datarray['gravity']),
694 intval($datarray['contact-id']),
695 dbesc($datarray['owner-name']),
696 dbesc($datarray['owner-link']),
697 dbesc($datarray['owner-avatar']),
698 dbesc($datarray['author-name']),
699 dbesc($datarray['author-link']),
700 dbesc($datarray['author-avatar']),
701 dbesc($datarray['created']),
702 dbesc($datarray['edited']),
703 dbesc($datarray['commented']),
704 dbesc($datarray['received']),
705 dbesc($datarray['changed']),
706 dbesc($datarray['uri']),
707 dbesc($datarray['thr-parent']),
708 dbesc($datarray['title']),
709 dbesc($datarray['body']),
710 dbesc($datarray['app']),
711 dbesc($datarray['location']),
712 dbesc($datarray['coord']),
713 dbesc($datarray['tag']),
714 dbesc($datarray['inform']),
715 dbesc($datarray['verb']),
716 dbesc($datarray['postopts']),
717 dbesc($datarray['allow_cid']),
718 dbesc($datarray['allow_gid']),
719 dbesc($datarray['deny_cid']),
720 dbesc($datarray['deny_gid']),
721 intval($datarray['private']),
722 intval($datarray['pubmail']),
723 dbesc($datarray['attach']),
724 intval($datarray['bookmark']),
725 intval($datarray['origin']),
726 intval($datarray['moderated']),
727 dbesc($datarray['file'])
730 $r = q("SELECT `id` FROM `item` WHERE `uri` = '%s' LIMIT 1",
731 dbesc($datarray['uri']));
733 $post_id = $r[0]['id'];
734 logger('mod_item: saved item ' . $post_id);
736 // update filetags in pconfig
737 file_tag_update_pconfig($uid,$categories_old,$categories_new,'category');
741 // This item is the last leaf and gets the comment box, clear any ancestors
742 $r = q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent` = %d ",
743 dbesc(datetime_convert()),
747 // Inherit ACL's from the parent item.
749 $r = q("UPDATE `item` SET `allow_cid` = '%s', `allow_gid` = '%s', `deny_cid` = '%s', `deny_gid` = '%s', `private` = %d
750 WHERE `id` = %d LIMIT 1",
751 dbesc($parent_item['allow_cid']),
752 dbesc($parent_item['allow_gid']),
753 dbesc($parent_item['deny_cid']),
754 dbesc($parent_item['deny_gid']),
755 intval($parent_item['private']),
759 if($contact_record != $author) {
761 'type' => NOTIFY_COMMENT,
762 'notify_flags' => $user['notify-flags'],
763 'language' => $user['language'],
764 'to_name' => $user['username'],
765 'to_email' => $user['email'],
766 'uid' => $user['uid'],
768 'link' => $a->get_baseurl() . '/display/' . $user['nickname'] . '/' . $post_id,
769 'source_name' => $datarray['author-name'],
770 'source_link' => $datarray['author-link'],
771 'source_photo' => $datarray['author-avatar'],
772 'verb' => ACTIVITY_POST,
775 'parent_uri' => $parent_item['uri']
781 // Store the comment signature information in case we need to relay to Diaspora
782 store_diaspora_comment_sig($datarray, $author, ($self ? $a->user['prvkey'] : false), $parent_item, $post_id);
788 if($contact_record != $author) {
790 'type' => NOTIFY_WALL,
791 'notify_flags' => $user['notify-flags'],
792 'language' => $user['language'],
793 'to_name' => $user['username'],
794 'to_email' => $user['email'],
795 'uid' => $user['uid'],
797 'link' => $a->get_baseurl() . '/display/' . $user['nickname'] . '/' . $post_id,
798 'source_name' => $datarray['author-name'],
799 'source_link' => $datarray['author-link'],
800 'source_photo' => $datarray['author-avatar'],
801 'verb' => ACTIVITY_POST,
807 // fallback so that parent always gets set to non-zero.
812 $r = q("UPDATE `item` SET `parent` = %d, `parent-uri` = '%s', `plink` = '%s', `changed` = '%s', `last-child` = 1, `visible` = 1
813 WHERE `id` = %d LIMIT 1",
815 dbesc(($parent == $post_id) ? $uri : $parent_item['uri']),
816 dbesc($a->get_baseurl() . '/display/' . $user['nickname'] . '/' . $post_id),
817 dbesc(datetime_convert()),
821 // photo comments turn the corresponding item visible to the profile wall
822 // This way we don't see every picture in your new photo album posted to your wall at once.
823 // They will show up as people comment on them.
825 if(! $parent_item['visible']) {
826 $r = q("UPDATE `item` SET `visible` = 1 WHERE `id` = %d LIMIT 1",
827 intval($parent_item['id'])
832 logger('mod_item: unable to retrieve post that was just stored.');
833 notice( t('System error. Post not saved.') . EOL);
834 goaway($a->get_baseurl() . "/" . $return_path );
838 // update the commented timestamp on the parent
840 q("UPDATE `item` set `commented` = '%s', `changed` = '%s' WHERE `id` = %d LIMIT 1",
841 dbesc(datetime_convert()),
842 dbesc(datetime_convert()),
846 $datarray['id'] = $post_id;
847 $datarray['plink'] = $a->get_baseurl() . '/display/' . $user['nickname'] . '/' . $post_id;
849 call_hooks('post_local_end', $datarray);
851 if(strlen($emailcc) && $profile_uid == local_user()) {
852 $erecips = explode(',', $emailcc);
853 if(count($erecips)) {
854 foreach($erecips as $recip) {
855 $addr = trim($recip);
858 $disclaimer = '<hr />' . sprintf( t('This message was sent to you by %s, a member of the Friendica social network.'),$a->user['username'])
860 $disclaimer .= sprintf( t('You may visit them online at %s'), $a->get_baseurl() . '/profile/' . $a->user['nickname']) . EOL;
861 $disclaimer .= t('Please contact the sender by replying to this post if you do not wish to receive these messages.') . EOL;
863 $subject = email_header_encode('[Friendica]' . ' ' . sprintf( t('%s posted an update.'),$a->user['username']),'UTF-8');
864 $headers = 'From: ' . email_header_encode($a->user['username'],'UTF-8') . ' <' . $a->user['email'] . '>' . "\n";
865 $headers .= 'MIME-Version: 1.0' . "\n";
866 $headers .= 'Content-Type: text/html; charset=UTF-8' . "\n";
867 $headers .= 'Content-Transfer-Encoding: 8bit' . "\n\n";
868 $link = '<a href="' . $a->get_baseurl() . '/profile/' . $a->user['nickname'] . '"><img src="' . $author['thumb'] . '" alt="' . $a->user['username'] . '" /></a><br /><br />';
869 $html = prepare_body($datarray);
870 $message = '<html><body>' . $link . $html . $disclaimer . '</body></html>';
871 @mail($addr, $subject, $message, $headers);
876 // This is a real juggling act on shared hosting services which kill your processes
877 // e.g. dreamhost. We used to start delivery to our native delivery agents in the background
878 // and then run our plugin delivery from the foreground. We're now doing plugin delivery first,
879 // because as soon as you start loading up a bunch of remote delivey processes, *this* page is
880 // likely to get killed off. If you end up looking at an /item URL and a blank page,
881 // it's very likely the delivery got killed before all your friends could be notified.
882 // Currently the only realistic fixes are to use a reliable server - which precludes shared hosting,
883 // or cut back on plugins which do remote deliveries.
885 proc_run('php', "include/notifier.php", $notify_type, "$post_id");
887 logger('post_complete');
889 item_post_return($a->get_baseurl(), $api_source, $return_path);
893 function item_post_return($baseurl, $api_source, $return_path) {
894 // figure out how to return, depending on from whence we came
900 goaway($baseurl . "/" . $return_path);
903 $json = array('success' => 1);
904 if(x($_REQUEST,'jsreload') && strlen($_REQUEST['jsreload']))
905 $json['reload'] = $baseurl . '/' . $_REQUEST['jsreload'];
907 logger('post_json: ' . print_r($json,true), LOGGER_DEBUG);
909 echo json_encode($json);
915 function item_content(&$a) {
917 if((! local_user()) && (! remote_user()))
920 require_once('include/security.php');
922 if(($a->argc == 3) && ($a->argv[1] === 'drop') && intval($a->argv[2])) {
923 require_once('include/items.php');
924 drop_item($a->argv[2]);
929 * This function removes the tag $tag from the text $body and replaces it with
930 * the appropiate link.
932 * @param unknown_type $body the text to replace the tag in
933 * @param unknown_type $inform a comma-seperated string containing everybody to inform
934 * @param unknown_type $str_tags string to add the tag to
935 * @param unknown_type $profile_uid
936 * @param unknown_type $tag the tag to replace
938 * @return boolean true if replaced, false if not replaced
940 function handle_tag($a, &$body, &$inform, &$str_tags, $profile_uid, $tag) {
946 if(strpos($tag,'#') === 0) {
947 //if the tag is replaced...
948 if(strpos($tag,'[url='))
951 //base tag has the tags name only
952 $basetag = str_replace('_',' ',substr($tag,1));
953 //create text for link
954 $newtag = '#[url=' . $a->get_baseurl() . '/search?tag=' . rawurlencode($basetag) . ']' . $basetag . '[/url]';
955 //replace tag by the link
956 $body = str_replace($tag, $newtag, $body);
959 //is the link already in str_tags?
960 if(! stristr($str_tags,$newtag)) {
961 //append or set str_tags
962 if(strlen($str_tags))
964 $str_tags .= $newtag;
968 //is it a person tag?
969 if(strpos($tag,'@') === 0) {
970 //is it already replaced?
971 if(strpos($tag,'[url='))
974 //get the person's name
975 $name = substr($tag,1);
976 //is it a link or a full dfrn address?
977 if((strpos($name,'@')) || (strpos($name,'http://'))) {
979 //get the profile links
980 $links = @lrdd($name);
982 //for all links, collect how is to inform and how's profile is to link
983 foreach($links as $link) {
984 if($link['@attributes']['rel'] === 'http://webfinger.net/rel/profile-page')
985 $profile = $link['@attributes']['href'];
986 if($link['@attributes']['rel'] === 'salmon') {
989 $inform .= 'url:' . str_replace(',','%2c',$link['@attributes']['href']);
993 } else { //if it is a name rather than an address
997 //is it some generated name?
998 if(strrpos($newname,'+')) {
1000 $tagcid = intval(substr($newname,strrpos($newname,'+') + 1));
1001 //remove the next word from tag's name
1002 if(strpos($name,' ')) {
1003 $name = substr($name,0,strpos($name,' '));
1006 if($tagcid) { //if there was an id
1007 //select contact with that id from the logged in user's contact list
1008 $r = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
1010 intval($profile_uid)
1014 $newname = str_replace('_',' ',$name);
1016 //select someone from this user's contacts by name
1017 $r = q("SELECT * FROM `contact` WHERE `name` = '%s' AND `uid` = %d LIMIT 1",
1019 intval($profile_uid)
1023 //select someone by attag or nick and the name passed in
1024 $r = q("SELECT * FROM `contact` WHERE `attag` = '%s' OR `nick` = '%s' AND `uid` = %d ORDER BY `attag` DESC LIMIT 1",
1027 intval($profile_uid)
1031 /* } elseif(strstr($name,'_') || strstr($name,' ')) { //no id
1033 $newname = str_replace('_',' ',$name);
1034 //select someone from this user's contacts by name
1035 $r = q("SELECT * FROM `contact` WHERE `name` = '%s' AND `uid` = %d LIMIT 1",
1037 intval($profile_uid)
1040 //select someone by attag or nick and the name passed in
1041 $r = q("SELECT * FROM `contact` WHERE `attag` = '%s' OR `nick` = '%s' AND `uid` = %d ORDER BY `attag` DESC LIMIT 1",
1044 intval($profile_uid)
1047 //$r is set, if someone could be selected
1049 $profile = $r[0]['url'];
1050 //set newname to nick, find alias
1051 if($r[0]['network'] === 'stat') {
1052 $newname = $r[0]['nick'];
1055 $alias = $r[0]['alias'];
1058 $newname = $r[0]['name'];
1059 //add person's id to $inform
1062 $inform .= 'cid:' . $r[0]['id'];
1065 //if there is an url for this persons profile
1066 if(isset($profile)) {
1068 //create profile link
1069 $profile = str_replace(',','%2c',$profile);
1070 $newtag = '@[url=' . $profile . ']' . $newname . '[/url]';
1071 $body = str_replace('@' . $name, $newtag, $body);
1072 //append tag to str_tags
1073 if(! stristr($str_tags,$newtag)) {
1074 if(strlen($str_tags))
1076 $str_tags .= $newtag;
1079 // Status.Net seems to require the numeric ID URL in a mention if the person isn't
1080 // subscribed to you. But the nickname URL is OK if they are. Grrr. We'll tag both.
1082 if(strlen($alias)) {
1083 $newtag = '@[url=' . $alias . ']' . $newname . '[/url]';
1084 if(! stristr($str_tags,$newtag)) {
1085 if(strlen($str_tags))
1087 $str_tags .= $newtag;
1093 return array('replaced' => $replaced, 'contact' => $r[0]);
1097 function store_diaspora_comment_sig($datarray, $author, $uprvkey, $parent_item, $post_id) {
1098 // We won't be able to sign Diaspora comments for authenticated visitors - we don't have their private key
1100 $enabled = intval(get_config('system','diaspora_enabled'));
1102 logger('mod_item: diaspora support disabled, not storing comment signature', LOGGER_DEBUG);
1107 logger('mod_item: storing diaspora comment signature');
1109 require_once('include/bb2diaspora.php');
1110 $signed_body = html_entity_decode(bb2diaspora($datarray['body']));
1112 // Only works for NETWORK_DFRN
1113 $contact_baseurl_start = strpos($author['url'],'://') + 3;
1114 $contact_baseurl_length = strpos($author['url'],'/profile') - $contact_baseurl_start;
1115 $contact_baseurl = substr($author['url'], $contact_baseurl_start, $contact_baseurl_length);
1116 $diaspora_handle = $author['nick'] . '@' . $contact_baseurl;
1118 $signed_text = $datarray['guid'] . ';' . $parent_item['guid'] . ';' . $signed_body . ';' . $diaspora_handle;
1120 if( $uprvkey !== false )
1121 $authorsig = base64_encode(rsa_sign($signed_text,$uprvkey,'sha256'));
1125 q("insert into sign (`iid`,`signed_text`,`signature`,`signer`) values (%d,'%s','%s','%s') ",
1127 dbesc($signed_text),
1128 dbesc(base64_encode($authorsig)),
1129 dbesc($diaspora_handle)