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 * Is this a reply to something?
53 $parent = ((x($_REQUEST,'parent')) ? intval($_REQUEST['parent']) : 0);
54 $parent_uri = ((x($_REQUEST,'parent_uri')) ? trim($_REQUEST['parent_uri']) : '');
57 $parent_contact = null;
62 if($parent || $parent_uri) {
64 if(! x($_REQUEST,'type'))
65 $_REQUEST['type'] = 'net-comment';
68 $r = q("SELECT * FROM `item` WHERE `id` = %d LIMIT 1",
72 elseif($parent_uri && local_user()) {
73 // This is coming from an API source, and we are logged in
74 $r = q("SELECT * FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
79 // if this isn't the real parent of the conversation, find it
80 if($r !== false && count($r)) {
81 $parid = $r[0]['parent'];
82 $parent_uri = $r[0]['uri'];
83 if($r[0]['id'] != $r[0]['parent']) {
84 $r = q("SELECT * FROM `item` WHERE `id` = `parent` AND `parent` = %d LIMIT 1",
90 if(($r === false) || (! count($r))) {
91 notice( t('Unable to locate original post.') . EOL);
92 if(x($_REQUEST,'return'))
93 goaway($a->get_baseurl() . "/" . $return_path );
97 $parent = $r[0]['id'];
99 // multi-level threading - preserve the info but re-parent to our single level threading
100 //if(($parid) && ($parid != $parent))
101 $thr_parent = $parent_uri;
103 if($parent_item['contact-id'] && $uid) {
104 $r = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
105 intval($parent_item['contact-id']),
109 $parent_contact = $r[0];
113 if($parent) logger('mod_item: item_post parent=' . $parent);
115 $profile_uid = ((x($_REQUEST,'profile_uid')) ? intval($_REQUEST['profile_uid']) : 0);
116 $post_id = ((x($_REQUEST,'post_id')) ? intval($_REQUEST['post_id']) : 0);
117 $app = ((x($_REQUEST,'source')) ? strip_tags($_REQUEST['source']) : '');
119 $allow_moderated = false;
121 // here is where we are going to check for permission to post a moderated comment.
123 // First check that the parent exists and it is a wall item.
125 if((x($_REQUEST,'commenter')) && ((! $parent) || (! $parent_item['wall']))) {
126 notice( t('Permission denied.') . EOL) ;
127 if(x($_REQUEST,'return'))
128 goaway($a->get_baseurl() . "/" . $return_path );
132 // Now check that it is a page_type of PAGE_BLOG, and that valid personal details
133 // have been provided, and run any anti-spam plugins
141 if((! can_write_wall($a,$profile_uid)) && (! $allow_moderated)) {
142 notice( t('Permission denied.') . EOL) ;
143 if(x($_REQUEST,'return'))
144 goaway($a->get_baseurl() . "/" . $return_path );
149 // is this an edited post?
154 $i = q("SELECT * FROM `item` WHERE `uid` = %d AND `id` = %d LIMIT 1",
155 intval($profile_uid),
165 $r = q("SELECT * FROM `user` WHERE `uid` = %d LIMIT 1",
172 $str_group_allow = $orig_post['allow_gid'];
173 $str_contact_allow = $orig_post['allow_cid'];
174 $str_group_deny = $orig_post['deny_gid'];
175 $str_contact_deny = $orig_post['deny_cid'];
176 $location = $orig_post['location'];
177 $coord = $orig_post['coord'];
178 $verb = $orig_post['verb'];
179 $emailcc = $orig_post['emailcc'];
180 $app = $orig_post['app'];
181 $categories = $orig_post['file'];
182 $title = notags(trim($_REQUEST['title']));
183 $body = escape_tags(trim($_REQUEST['body']));
184 $private = $orig_post['private'];
185 $pubmail_enable = $orig_post['pubmail'];
190 // if coming from the API and no privacy settings are set,
191 // use the user default permissions - as they won't have
192 // been supplied via a form.
195 && (! array_key_exists('contact_allow',$_REQUEST))
196 && (! array_key_exists('group_allow',$_REQUEST))
197 && (! array_key_exists('contact_deny',$_REQUEST))
198 && (! array_key_exists('group_deny',$_REQUEST))) {
199 $str_group_allow = $user['allow_gid'];
200 $str_contact_allow = $user['allow_cid'];
201 $str_group_deny = $user['deny_gid'];
202 $str_contact_deny = $user['deny_cid'];
206 // use the posted permissions
208 $str_group_allow = perms2str($_REQUEST['group_allow']);
209 $str_contact_allow = perms2str($_REQUEST['contact_allow']);
210 $str_group_deny = perms2str($_REQUEST['group_deny']);
211 $str_contact_deny = perms2str($_REQUEST['contact_deny']);
214 $title = notags(trim($_REQUEST['title']));
215 $location = notags(trim($_REQUEST['location']));
216 $coord = notags(trim($_REQUEST['coord']));
217 $verb = notags(trim($_REQUEST['verb']));
218 $emailcc = notags(trim($_REQUEST['emailcc']));
219 $body = escape_tags(trim($_REQUEST['body']));
222 $naked_body = preg_replace('/\[(.+?)\]/','',$body);
224 if (version_compare(PHP_VERSION, '5.3.0', '>=')) {
225 $l = new Text_LanguageDetect;
226 $lng = $l->detectConfidence($naked_body);
228 $postopts = (($lng['language']) ? 'lang=' . $lng['language'] . ';' . $lng['confidence'] : '');
230 logger('mod_item: detect language' . print_r($lng,true) . $naked_body, LOGGER_DATA);
236 $private = ((strlen($str_group_allow) || strlen($str_contact_allow) || strlen($str_group_deny) || strlen($str_contact_deny)) ? 1 : 0);
238 // If this is a comment, set the permissions from the parent.
243 if(($parent_item['private'])
244 || strlen($parent_item['allow_cid'])
245 || strlen($parent_item['allow_gid'])
246 || strlen($parent_item['deny_cid'])
247 || strlen($parent_item['deny_gid'])) {
248 $private = (($parent_item['private']) ? $parent_item['private'] : 1);
251 $str_contact_allow = $parent_item['allow_cid'];
252 $str_group_allow = $parent_item['allow_gid'];
253 $str_contact_deny = $parent_item['deny_cid'];
254 $str_group_deny = $parent_item['deny_gid'];
257 $pubmail_enable = ((x($_REQUEST,'pubmail_enable') && intval($_REQUEST['pubmail_enable']) && (! $private)) ? 1 : 0);
259 // if using the API, we won't see pubmail_enable - figure out if it should be set
261 if($api_source && $profile_uid && $profile_uid == local_user() && (! $private)) {
262 $mail_disabled = ((function_exists('imap_open') && (! get_config('system','imap_disabled'))) ? 0 : 1);
263 if(! $mail_disabled) {
264 $r = q("SELECT * FROM `mailacct` WHERE `uid` = %d AND `server` != '' LIMIT 1",
267 if(count($r) && intval($r[0]['pubmail']))
268 $pubmail_enabled = true;
272 if(! strlen($body)) {
275 info( t('Empty post discarded.') . EOL );
276 if(x($_REQUEST,'return'))
277 goaway($a->get_baseurl() . "/" . $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 // Work around doubled linefeeds in Tinymce 3.5b2
296 // First figure out if it's a status post that would've been
297 // created using tinymce. Otherwise leave it alone.
299 $plaintext = (local_user() ? intval(get_pconfig(local_user(),'system','plaintext')) : 0);
300 if((! $parent) && (! $api_source) && (! $plaintext)) {
301 $body = fix_mce_lf($body);
305 // get contact info for poster
310 if((local_user()) && (local_user() == $profile_uid)) {
312 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 1 LIMIT 1",
313 intval($_SESSION['uid'])
316 elseif(remote_user()) {
317 $r = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
318 intval(remote_user())
324 $contact_id = $author['id'];
327 // get contact info for owner
329 if($profile_uid == local_user()) {
330 $contact_record = $author;
333 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 1 LIMIT 1",
337 $contact_record = $r[0];
340 $post_type = notags(trim($_REQUEST['type']));
342 if($post_type === 'net-comment') {
343 if($parent_item !== null) {
344 if($parent_item['wall'] == 1)
345 $post_type = 'wall-comment';
347 $post_type = 'remote-comment';
353 * When a photo was uploaded into the message using the (profile wall) ajax
354 * uploader, The permissions are initially set to disallow anybody but the
355 * owner from seeing it. This is because the permissions may not yet have been
356 * set for the post. If it's private, the photo permissions should be set
357 * appropriately. But we didn't know the final permissions on the post until
358 * now. So now we'll look for links of uploaded messages that are in the
359 * post and set them to the same permissions as the post itself.
365 if((! $preview) && preg_match_all("/\[img\](.*?)\[\/img\]/",$body,$match)) {
368 foreach($images as $image) {
369 if(! stristr($image,$a->get_baseurl() . '/photo/'))
371 $image_uri = substr($image,strrpos($image,'/') + 1);
372 $image_uri = substr($image_uri,0, strpos($image_uri,'-'));
373 if(! strlen($image_uri))
375 $srch = '<' . intval($contact_id) . '>';
377 $r = q("SELECT `id` FROM `photo` WHERE `allow_cid` = '%s' AND `allow_gid` = '' AND `deny_cid` = '' AND `deny_gid` = ''
378 AND `resource-id` = '%s' AND `uid` = %d LIMIT 1",
388 $r = q("UPDATE `photo` SET `allow_cid` = '%s', `allow_gid` = '%s', `deny_cid` = '%s', `deny_gid` = '%s'
389 WHERE `resource-id` = '%s' AND `uid` = %d AND `album` = '%s' ",
390 dbesc($str_contact_allow),
391 dbesc($str_group_allow),
392 dbesc($str_contact_deny),
393 dbesc($str_group_deny),
395 intval($profile_uid),
396 dbesc( t('Wall Photos'))
405 * Next link in any attachment references we find in the post.
410 if((! $preview) && preg_match_all("/\[attachment\](.*?)\[\/attachment\]/",$body,$match)) {
411 $attaches = $match[1];
412 if(count($attaches)) {
413 foreach($attaches as $attach) {
414 $r = q("SELECT * FROM `attach` WHERE `uid` = %d AND `id` = %d LIMIT 1",
415 intval($profile_uid),
419 $r = q("UPDATE `attach` SET `allow_cid` = '%s', `allow_gid` = '%s', `deny_cid` = '%s', `deny_gid` = '%s'
420 WHERE `uid` = %d AND `id` = %d LIMIT 1",
421 dbesc($str_contact_allow),
422 dbesc($str_group_allow),
423 dbesc($str_contact_deny),
424 dbesc($str_group_deny),
425 intval($profile_uid),
433 // embedded bookmark in post? set bookmark flag
436 if(preg_match_all("/\[bookmark\=([^\]]*)\](.*?)\[\/bookmark\]/ism",$body,$match,PREG_SET_ORDER)) {
440 $body = bb_translate_video($body);
444 * Fold multi-line [code] sequences
447 $body = preg_replace('/\[\/code\]\s*\[code\]/ism',"\n",$body);
449 $body = scale_external_images($body,false);
454 * Look for any tags and linkify them
461 $tags = get_tags($body);
464 * add a statusnet style reply tag if the original post was from there
465 * and we are replying, and there isn't one already
468 if(($parent_contact) && ($parent_contact['network'] === NETWORK_OSTATUS)
469 && ($parent_contact['nick']) && (! in_array('@' . $parent_contact['nick'],$tags))) {
470 $body = '@' . $parent_contact['nick'] . ' ' . $body;
471 $tags[] = '@' . $parent_contact['nick'];
476 $private_forum = false;
479 foreach($tags as $tag) {
481 // If we already tagged 'Robert Johnson', don't try and tag 'Robert'.
482 // Robert Johnson should be first in the $tags array
484 $fullnametagged = false;
485 for($x = 0; $x < count($tagged); $x ++) {
486 if(stristr($tagged[$x],$tag . ' ')) {
487 $fullnametagged = true;
494 $success = handle_tag($a, $body, $inform, $str_tags, (local_user()) ? local_user() : $profile_uid , $tag);
495 if($success['replaced'])
497 if(is_array($success['contact']) && intval($success['contact']['prv'])) {
498 $private_forum = true;
499 $private_id = $success['contact']['id'];
504 if(($private_forum) && (! $parent) && (! $private)) {
505 // we tagged a private forum in a top level post and the message was public.
508 $str_contact_allow = '<' . $private_id . '>';
514 if(preg_match_all('/(\[attachment\]([0-9]+)\[\/attachment\])/',$body,$match)) {
515 foreach($match[2] as $mtch) {
516 $r = q("SELECT `id`,`filename`,`filesize`,`filetype` FROM `attach` WHERE `uid` = %d AND `id` = %d LIMIT 1",
517 intval($profile_uid),
521 if(strlen($attachments))
523 $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]';
525 $body = str_replace($match[1],'',$body);
531 if($post_type === 'wall' || $post_type === 'wall-comment')
535 $verb = ACTIVITY_POST ;
537 $gravity = (($parent) ? 6 : 0 );
539 // even if the post arrived via API we are considering that it
540 // originated on this site by default for determining relayability.
542 $origin = ((x($_REQUEST,'origin')) ? intval($_REQUEST['origin']) : 1);
544 $notify_type = (($parent) ? 'comment-new' : 'wall-new' );
546 $uri = item_new_uri($a->get_hostname(),$profile_uid);
548 // Fallback so that we alway have a thr-parent
553 $datarray['uid'] = $profile_uid;
554 $datarray['type'] = $post_type;
555 $datarray['wall'] = $wall;
556 $datarray['gravity'] = $gravity;
557 $datarray['contact-id'] = $contact_id;
558 $datarray['owner-name'] = $contact_record['name'];
559 $datarray['owner-link'] = $contact_record['url'];
560 $datarray['owner-avatar'] = $contact_record['thumb'];
561 $datarray['author-name'] = $author['name'];
562 $datarray['author-link'] = $author['url'];
563 $datarray['author-avatar'] = $author['thumb'];
564 $datarray['created'] = datetime_convert();
565 $datarray['edited'] = datetime_convert();
566 $datarray['commented'] = datetime_convert();
567 $datarray['received'] = datetime_convert();
568 $datarray['changed'] = datetime_convert();
569 $datarray['uri'] = $uri;
570 $datarray['title'] = $title;
571 $datarray['body'] = $body;
572 $datarray['app'] = $app;
573 $datarray['location'] = $location;
574 $datarray['coord'] = $coord;
575 $datarray['tag'] = $str_tags;
576 $datarray['file'] = $categories;
577 $datarray['inform'] = $inform;
578 $datarray['verb'] = $verb;
579 $datarray['allow_cid'] = $str_contact_allow;
580 $datarray['allow_gid'] = $str_group_allow;
581 $datarray['deny_cid'] = $str_contact_deny;
582 $datarray['deny_gid'] = $str_group_deny;
583 $datarray['private'] = $private;
584 $datarray['pubmail'] = $pubmail_enable;
585 $datarray['attach'] = $attachments;
586 $datarray['bookmark'] = intval($bookmark);
587 $datarray['thr-parent'] = $thr_parent;
588 $datarray['postopts'] = $postopts;
589 $datarray['origin'] = $origin;
590 $datarray['moderated'] = $allow_moderated;
593 * These fields are for the convenience of plugins...
594 * 'self' if true indicates the owner is posting on their own wall
595 * If parent is 0 it is a top-level post.
598 $datarray['parent'] = $parent;
599 $datarray['self'] = $self;
600 // $datarray['prvnets'] = $user['prvnets'];
603 $datarray['edit'] = true;
605 $datarray['guid'] = get_guid();
607 // preview mode - prepare the body for display and send it via json
610 require_once('include/conversation.php');
611 $o = conversation($a,array(array_merge($contact_record,$datarray)),'search', false);
612 logger('preview: ' . $o);
613 echo json_encode(array('preview' => $o));
618 call_hooks('post_local',$datarray);
620 if(x($datarray,'cancel')) {
621 logger('mod_item: post cancelled by plugin.');
623 goaway($a->get_baseurl() . "/" . $return_path);
626 $json = array('cancel' => 1);
627 if(x($_REQUEST,'jsreload') && strlen($_REQUEST['jsreload']))
628 $json['reload'] = $a->get_baseurl() . '/' . $_REQUEST['jsreload'];
630 echo json_encode($json);
636 $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",
637 dbesc($datarray['title']),
638 dbesc($datarray['body']),
639 dbesc($datarray['tag']),
640 dbesc($datarray['attach']),
641 dbesc($datarray['file']),
642 dbesc(datetime_convert()),
647 // update filetags in pconfig
648 file_tag_update_pconfig($uid,$categories_old,$categories_new,'category');
650 proc_run('php', "include/notifier.php", 'edit_post', "$post_id");
651 if((x($_REQUEST,'return')) && strlen($return_path)) {
652 logger('return: ' . $return_path);
653 goaway($a->get_baseurl() . "/" . $return_path );
661 $r = q("INSERT INTO `item` (`guid`, `uid`,`type`,`wall`,`gravity`,`contact-id`,`owner-name`,`owner-link`,`owner-avatar`,
662 `author-name`, `author-link`, `author-avatar`, `created`, `edited`, `commented`, `received`, `changed`, `uri`, `thr-parent`, `title`, `body`, `app`, `location`, `coord`,
663 `tag`, `inform`, `verb`, `postopts`, `allow_cid`, `allow_gid`, `deny_cid`, `deny_gid`, `private`, `pubmail`, `attach`, `bookmark`,`origin`, `moderated`, `file` )
664 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' )",
665 dbesc($datarray['guid']),
666 intval($datarray['uid']),
667 dbesc($datarray['type']),
668 intval($datarray['wall']),
669 intval($datarray['gravity']),
670 intval($datarray['contact-id']),
671 dbesc($datarray['owner-name']),
672 dbesc($datarray['owner-link']),
673 dbesc($datarray['owner-avatar']),
674 dbesc($datarray['author-name']),
675 dbesc($datarray['author-link']),
676 dbesc($datarray['author-avatar']),
677 dbesc($datarray['created']),
678 dbesc($datarray['edited']),
679 dbesc($datarray['commented']),
680 dbesc($datarray['received']),
681 dbesc($datarray['changed']),
682 dbesc($datarray['uri']),
683 dbesc($datarray['thr-parent']),
684 dbesc($datarray['title']),
685 dbesc($datarray['body']),
686 dbesc($datarray['app']),
687 dbesc($datarray['location']),
688 dbesc($datarray['coord']),
689 dbesc($datarray['tag']),
690 dbesc($datarray['inform']),
691 dbesc($datarray['verb']),
692 dbesc($datarray['postopts']),
693 dbesc($datarray['allow_cid']),
694 dbesc($datarray['allow_gid']),
695 dbesc($datarray['deny_cid']),
696 dbesc($datarray['deny_gid']),
697 intval($datarray['private']),
698 intval($datarray['pubmail']),
699 dbesc($datarray['attach']),
700 intval($datarray['bookmark']),
701 intval($datarray['origin']),
702 intval($datarray['moderated']),
703 dbesc($datarray['file'])
706 $r = q("SELECT `id` FROM `item` WHERE `uri` = '%s' LIMIT 1",
707 dbesc($datarray['uri']));
709 $post_id = $r[0]['id'];
710 logger('mod_item: saved item ' . $post_id);
712 // update filetags in pconfig
713 file_tag_update_pconfig($uid,$categories_old,$categories_new,'category');
717 // This item is the last leaf and gets the comment box, clear any ancestors
718 $r = q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent` = %d ",
719 dbesc(datetime_convert()),
723 // Inherit ACL's from the parent item.
725 $r = q("UPDATE `item` SET `allow_cid` = '%s', `allow_gid` = '%s', `deny_cid` = '%s', `deny_gid` = '%s', `private` = %d
726 WHERE `id` = %d LIMIT 1",
727 dbesc($parent_item['allow_cid']),
728 dbesc($parent_item['allow_gid']),
729 dbesc($parent_item['deny_cid']),
730 dbesc($parent_item['deny_gid']),
731 intval($parent_item['private']),
735 if($contact_record != $author) {
737 'type' => NOTIFY_COMMENT,
738 'notify_flags' => $user['notify-flags'],
739 'language' => $user['language'],
740 'to_name' => $user['username'],
741 'to_email' => $user['email'],
742 'uid' => $user['uid'],
744 'link' => $a->get_baseurl() . '/display/' . $user['nickname'] . '/' . $post_id,
745 'source_name' => $datarray['author-name'],
746 'source_link' => $datarray['author-link'],
747 'source_photo' => $datarray['author-avatar'],
748 'verb' => ACTIVITY_POST,
756 // Store the comment signature information in case we need to relay to Diaspora
757 store_diaspora_comment_sig($datarray, $author, ($self ? $a->user['prvkey'] : false), $parent_item, $post_id);
763 if($contact_record != $author) {
765 'type' => NOTIFY_WALL,
766 'notify_flags' => $user['notify-flags'],
767 'language' => $user['language'],
768 'to_name' => $user['username'],
769 'to_email' => $user['email'],
770 'uid' => $user['uid'],
772 'link' => $a->get_baseurl() . '/display/' . $user['nickname'] . '/' . $post_id,
773 'source_name' => $datarray['author-name'],
774 'source_link' => $datarray['author-link'],
775 'source_photo' => $datarray['author-avatar'],
776 'verb' => ACTIVITY_POST,
782 // fallback so that parent always gets set to non-zero.
787 $r = q("UPDATE `item` SET `parent` = %d, `parent-uri` = '%s', `plink` = '%s', `changed` = '%s', `last-child` = 1, `visible` = 1
788 WHERE `id` = %d LIMIT 1",
790 dbesc(($parent == $post_id) ? $uri : $parent_item['uri']),
791 dbesc($a->get_baseurl() . '/display/' . $user['nickname'] . '/' . $post_id),
792 dbesc(datetime_convert()),
796 // photo comments turn the corresponding item visible to the profile wall
797 // This way we don't see every picture in your new photo album posted to your wall at once.
798 // They will show up as people comment on them.
800 if(! $parent_item['visible']) {
801 $r = q("UPDATE `item` SET `visible` = 1 WHERE `id` = %d LIMIT 1",
802 intval($parent_item['id'])
807 logger('mod_item: unable to retrieve post that was just stored.');
808 notice( t('System error. Post not saved.') . EOL);
809 goaway($a->get_baseurl() . "/" . $return_path );
813 // update the commented timestamp on the parent
815 q("UPDATE `item` set `commented` = '%s', `changed` = '%s' WHERE `id` = %d LIMIT 1",
816 dbesc(datetime_convert()),
817 dbesc(datetime_convert()),
821 $datarray['id'] = $post_id;
822 $datarray['plink'] = $a->get_baseurl() . '/display/' . $user['nickname'] . '/' . $post_id;
824 call_hooks('post_local_end', $datarray);
826 if(strlen($emailcc) && $profile_uid == local_user()) {
827 $erecips = explode(',', $emailcc);
828 if(count($erecips)) {
829 foreach($erecips as $recip) {
830 $addr = trim($recip);
833 $disclaimer = '<hr />' . sprintf( t('This message was sent to you by %s, a member of the Friendica social network.'),$a->user['username'])
835 $disclaimer .= sprintf( t('You may visit them online at %s'), $a->get_baseurl() . '/profile/' . $a->user['nickname']) . EOL;
836 $disclaimer .= t('Please contact the sender by replying to this post if you do not wish to receive these messages.') . EOL;
838 $subject = email_header_encode('[Friendica]' . ' ' . sprintf( t('%s posted an update.'),$a->user['username']),'UTF-8');
839 $headers = 'From: ' . email_header_encode($a->user['username'],'UTF-8') . ' <' . $a->user['email'] . '>' . "\n";
840 $headers .= 'MIME-Version: 1.0' . "\n";
841 $headers .= 'Content-Type: text/html; charset=UTF-8' . "\n";
842 $headers .= 'Content-Transfer-Encoding: 8bit' . "\n\n";
843 $link = '<a href="' . $a->get_baseurl() . '/profile/' . $a->user['nickname'] . '"><img src="' . $author['thumb'] . '" alt="' . $a->user['username'] . '" /></a><br /><br />';
844 $html = prepare_body($datarray);
845 $message = '<html><body>' . $link . $html . $disclaimer . '</body></html>';
846 @mail($addr, $subject, $message, $headers);
851 // This is a real juggling act on shared hosting services which kill your processes
852 // e.g. dreamhost. We used to start delivery to our native delivery agents in the background
853 // and then run our plugin delivery from the foreground. We're now doing plugin delivery first,
854 // because as soon as you start loading up a bunch of remote delivey processes, *this* page is
855 // likely to get killed off. If you end up looking at an /item URL and a blank page,
856 // it's very likely the delivery got killed before all your friends could be notified.
857 // Currently the only realistic fixes are to use a reliable server - which precludes shared hosting,
858 // or cut back on plugins which do remote deliveries.
860 proc_run('php', "include/notifier.php", $notify_type, "$post_id");
862 logger('post_complete');
864 // figure out how to return, depending on from whence we came
870 goaway($a->get_baseurl() . "/" . $return_path);
873 $json = array('success' => 1);
874 if(x($_REQUEST,'jsreload') && strlen($_REQUEST['jsreload']))
875 $json['reload'] = $a->get_baseurl() . '/' . $_REQUEST['jsreload'];
877 logger('post_json: ' . print_r($json,true), LOGGER_DEBUG);
879 echo json_encode($json);
888 function item_content(&$a) {
890 if((! local_user()) && (! remote_user()))
893 require_once('include/security.php');
895 if(($a->argc == 3) && ($a->argv[1] === 'drop') && intval($a->argv[2])) {
896 require_once('include/items.php');
897 drop_item($a->argv[2]);
902 * This function removes the tag $tag from the text $body and replaces it with
903 * the appropiate link.
905 * @param unknown_type $body the text to replace the tag in
906 * @param unknown_type $inform a comma-seperated string containing everybody to inform
907 * @param unknown_type $str_tags string to add the tag to
908 * @param unknown_type $profile_uid
909 * @param unknown_type $tag the tag to replace
911 * @return boolean true if replaced, false if not replaced
913 function handle_tag($a, &$body, &$inform, &$str_tags, $profile_uid, $tag) {
919 if(strpos($tag,'#') === 0) {
920 //if the tag is replaced...
921 if(strpos($tag,'[url='))
924 //base tag has the tags name only
925 $basetag = str_replace('_',' ',substr($tag,1));
926 //create text for link
927 $newtag = '#[url=' . $a->get_baseurl() . '/search?tag=' . rawurlencode($basetag) . ']' . $basetag . '[/url]';
928 //replace tag by the link
929 $body = str_replace($tag, $newtag, $body);
932 //is the link already in str_tags?
933 if(! stristr($str_tags,$newtag)) {
934 //append or set str_tags
935 if(strlen($str_tags))
937 $str_tags .= $newtag;
941 //is it a person tag?
942 if(strpos($tag,'@') === 0) {
943 //is it already replaced?
944 if(strpos($tag,'[url='))
947 //get the person's name
948 $name = substr($tag,1);
949 //is it a link or a full dfrn address?
950 if((strpos($name,'@')) || (strpos($name,'http://'))) {
952 //get the profile links
953 $links = @lrdd($name);
955 //for all links, collect how is to inform and how's profile is to link
956 foreach($links as $link) {
957 if($link['@attributes']['rel'] === 'http://webfinger.net/rel/profile-page')
958 $profile = $link['@attributes']['href'];
959 if($link['@attributes']['rel'] === 'salmon') {
962 $inform .= 'url:' . str_replace(',','%2c',$link['@attributes']['href']);
966 } else { //if it is a name rather than an address
970 //is it some generated name?
971 if(strrpos($newname,'+')) {
973 $tagcid = intval(substr($newname,strrpos($newname,'+') + 1));
974 //remove the next word from tag's name
975 if(strpos($name,' ')) {
976 $name = substr($name,0,strpos($name,' '));
979 if($tagcid) { //if there was an id
980 //select contact with that id from the logged in user's contact list
981 $r = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
985 } elseif(strstr($name,'_') || strstr($name,' ')) { //no id
987 $newname = str_replace('_',' ',$name);
988 //select someone from this user's contacts by name
989 $r = q("SELECT * FROM `contact` WHERE `name` = '%s' AND `uid` = %d LIMIT 1",
994 //select someone by attag or nick and the name passed in
995 $r = q("SELECT * FROM `contact` WHERE `attag` = '%s' OR `nick` = '%s' AND `uid` = %d ORDER BY `attag` DESC LIMIT 1",
1001 //$r is set, if someone could be selected
1003 $profile = $r[0]['url'];
1004 //set newname to nick, find alias
1005 if($r[0]['network'] === 'stat') {
1006 $newname = $r[0]['nick'];
1009 $alias = $r[0]['alias'];
1012 $newname = $r[0]['name'];
1013 //add person's id to $inform
1016 $inform .= 'cid:' . $r[0]['id'];
1019 //if there is an url for this persons profile
1020 if(isset($profile)) {
1022 //create profile link
1023 $profile = str_replace(',','%2c',$profile);
1024 $newtag = '@[url=' . $profile . ']' . $newname . '[/url]';
1025 $body = str_replace('@' . $name, $newtag, $body);
1026 //append tag to str_tags
1027 if(! stristr($str_tags,$newtag)) {
1028 if(strlen($str_tags))
1030 $str_tags .= $newtag;
1033 // Status.Net seems to require the numeric ID URL in a mention if the person isn't
1034 // subscribed to you. But the nickname URL is OK if they are. Grrr. We'll tag both.
1036 if(strlen($alias)) {
1037 $newtag = '@[url=' . $alias . ']' . $newname . '[/url]';
1038 if(! stristr($str_tags,$newtag)) {
1039 if(strlen($str_tags))
1041 $str_tags .= $newtag;
1047 return array('replaced' => $replaced, 'contact' => $r[0]);
1051 function store_diaspora_comment_sig($datarray, $author, $uprvkey, $parent_item, $post_id) {
1052 // We won't be able to sign Diaspora comments for authenticated visitors - we don't have their private key
1054 $enabled = intval(get_config('system','diaspora_enabled'));
1056 logger('mod_item: diaspora support disabled, not storing comment signature', LOGGER_DEBUG);
1061 logger('mod_item: storing diaspora comment signature');
1063 require_once('include/bb2diaspora.php');
1064 $signed_body = html_entity_decode(bb2diaspora($datarray['body']));
1066 // Only works for NETWORK_DFRN
1067 $contact_baseurl_start = strpos($author['url'],'://') + 3;
1068 $contact_baseurl_length = strpos($author['url'],'/profile') - $contact_baseurl_start;
1069 $contact_baseurl = substr($author['url'], $contact_baseurl_start, $contact_baseurl_length);
1070 $diaspora_handle = $author['nick'] . '@' . $contact_baseurl;
1072 $signed_text = $datarray['guid'] . ';' . $parent_item['guid'] . ';' . $signed_body . ';' . $diaspora_handle;
1074 if( $uprvkey !== false )
1075 $authorsig = base64_encode(rsa_sign($signed_text,$uprvkey,'sha256'));
1079 q("insert into sign (`iid`,`signed_text`,`signature`,`signer`) values (%d,'%s','%s','%s') ",
1081 dbesc($signed_text),
1082 dbesc(base64_encode($authorsig)),
1083 dbesc($diaspora_handle)