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 * All of these become an "item" which is our basic unit of
11 * Posts that originate externally or do not fall into the above
12 * posting categories go through item_store() instead of this function.
16 function item_post(&$a) {
18 if((! local_user()) && (! remote_user()))
21 require_once('include/security.php');
25 if(x($_POST,'dropitems')) {
26 require_once('include/items.php');
27 $arr_drop = explode(',',$_POST['dropitems']);
28 drop_items($arr_drop);
32 call_hooks('post_local_start', $_POST);
34 $parent = ((x($_POST,'parent')) ? intval($_POST['parent']) : 0);
37 $parent_contact = null;
40 $r = q("SELECT * FROM `item` WHERE `id` = %d LIMIT 1",
44 notice( t('Unable to locate original post.') . EOL);
45 if(x($_POST,'return'))
46 goaway($a->get_baseurl() . "/" . $_POST['return'] );
50 if($parent_item['contact-id'] && $uid) {
51 $r = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
52 intval($parent_item['contact-id']),
56 $parent_contact = $r[0];
60 $profile_uid = ((x($_POST,'profile_uid')) ? intval($_POST['profile_uid']) : 0);
61 $post_id = ((x($_POST['post_id'])) ? intval($_POST['post_id']) : 0);
63 if(! can_write_wall($a,$profile_uid)) {
64 notice( t('Permission denied.') . EOL) ;
65 if(x($_POST,'return'))
66 goaway($a->get_baseurl() . "/" . $_POST['return'] );
71 // is this an edited post?
76 $i = q("SELECT * FROM `item` WHERE `uid` = %d AND `id` = %d LIMIT 1",
87 $r = q("SELECT * FROM `user` WHERE `uid` = %d LIMIT 1",
94 $str_group_allow = $orig_post['allow_gid'];
95 $str_contact_allow = $orig_post['allow_cid'];
96 $str_group_deny = $orig_post['deny_gid'];
97 $str_contact_deny = $orig_post['deny_cid'];
98 $title = $orig_post['title'];
99 $location = $orig_post['location'];
100 $coord = $orig_post['coord'];
101 $verb = $orig_post['verb'];
102 $emailcc = $orig_post['emailcc'];
104 $body = escape_tags(trim($_POST['body']));
105 $private = $orig_post['private'];
106 $pubmail_enable = $orig_post['pubmail'];
109 $str_group_allow = perms2str($_POST['group_allow']);
110 $str_contact_allow = perms2str($_POST['contact_allow']);
111 $str_group_deny = perms2str($_POST['group_deny']);
112 $str_contact_deny = perms2str($_POST['contact_deny']);
113 $title = notags(trim($_POST['title']));
114 $location = notags(trim($_POST['location']));
115 $coord = notags(trim($_POST['coord']));
116 $verb = notags(trim($_POST['verb']));
117 $emailcc = notags(trim($_POST['emailcc']));
119 $body = escape_tags(trim($_POST['body']));
120 $private = ((strlen($str_group_allow) || strlen($str_contact_allow) || strlen($str_group_deny) || strlen($str_contact_deny)) ? 1 : 0);
123 (($parent_item['private'])
124 || strlen($parent_item['allow_cid'])
125 || strlen($parent_item['allow_gid'])
126 || strlen($parent_item['deny_cid'])
127 || strlen($parent_item['deny_gid'])
132 $pubmail_enable = ((x($_POST,'pubmail_enable') && intval($_POST['pubmail_enable']) && (! $private)) ? 1 : 0);
134 if(! strlen($body)) {
135 info( t('Empty post discarded.') . EOL );
136 if(x($_POST,'return'))
137 goaway($a->get_baseurl() . "/" . $_POST['return'] );
142 // get contact info for poster
147 if(($_SESSION['uid']) && ($_SESSION['uid'] == $profile_uid)) {
149 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 1 LIMIT 1",
150 intval($_SESSION['uid'])
154 if((x($_SESSION,'visitor_id')) && (intval($_SESSION['visitor_id']))) {
155 $r = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
156 intval($_SESSION['visitor_id'])
163 $contact_id = $author['id'];
166 // get contact info for owner
168 if($profile_uid == $_SESSION['uid']) {
169 $contact_record = $author;
172 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 1 LIMIT 1",
176 $contact_record = $r[0];
179 $post_type = notags(trim($_POST['type']));
181 if($post_type === 'net-comment') {
182 if($parent_item !== null) {
183 if($parent_item['type'] === 'remote') {
184 $post_type = 'remote-comment';
187 $post_type = 'wall-comment';
194 * When a photo was uploaded into the message using the (profile wall) ajax
195 * uploader, The permissions are initially set to disallow anybody but the
196 * owner from seeing it. This is because the permissions may not yet have been
197 * set for the post. If it's private, the photo permissions should be set
198 * appropriately. But we didn't know the final permissions on the post until
199 * now. So now we'll look for links of uploaded messages that are in the
200 * post and set them to the same permissions as the post itself.
206 if(preg_match_all("/\[img\](.*?)\[\/img\]/",$body,$match)) {
209 foreach($images as $image) {
210 if(! stristr($image,$a->get_baseurl() . '/photo/'))
212 $image_uri = substr($image,strrpos($image,'/') + 1);
213 $image_uri = substr($image_uri,0, strpos($image_uri,'-'));
214 if(! strlen($image_uri))
216 $srch = '<' . intval($profile_uid) . '>';
217 $r = q("SELECT `id` FROM `photo` WHERE `allow_cid` = '%s' AND `allow_gid` = '' AND `deny_cid` = '' AND `deny_gid` = ''
218 AND `resource-id` = '%s' AND `uid` = %d LIMIT 1",
227 $r = q("UPDATE `photo` SET `allow_cid` = '%s', `allow_gid` = '%s', `deny_cid` = '%s', `deny_gid` = '%s'
228 WHERE `resource-id` = '%s' AND `uid` = %d AND `album` = '%s' ",
229 dbesc($str_contact_allow),
230 dbesc($str_group_allow),
231 dbesc($str_contact_deny),
232 dbesc($str_group_deny),
234 intval($profile_uid),
235 dbesc( t('Wall Photos'))
245 if(preg_match_all("/\[attachment\](.*?)\[\/attachment\]/",$body,$match)) {
246 $attaches = $match[1];
247 if(count($attaches)) {
248 foreach($attaches as $attach) {
249 $r = q("SELECT * FROM `attach` WHERE `uid` = %d AND `id` = %d LIMIT 1",
250 intval($profile_uid),
254 $r = q("UPDATE `attach` SET `allow_cid` = '%s', `allow_gid` = '%s', `deny_cid` = '%s', `deny_gid` = '%s'
255 WHERE `uid` = %d AND `id` = %d LIMIT 1",
256 intval($profile_uid),
269 * Fold multi-line [code] sequences
272 $body = preg_replace('/\[\/code\]\s*\[code\]/m',"\n",$body);
275 * Look for any tags and linkify them
282 $tags = get_tags($body);
284 if(($parent_contact) && ($parent_contact['network'] === 'stat') && ($parent_contact['nick']) && (! in_array('@' . $parent_contact['nick'],$tags))) {
285 $body = '@' . $parent_contact['nick'] . ' ' . $body;
286 $tags[] = '@' . $parent_contact['nick'];
290 foreach($tags as $tag) {
291 if(strpos($tag,'#') === 0) {
292 if(strpos($tag,'[url='))
294 $basetag = str_replace('_',' ',substr($tag,1));
295 $body = str_replace($tag,'#[url=' . $a->get_baseurl() . '/search?search=' . rawurlencode($basetag) . ']' . $basetag . '[/url]',$body);
296 if(strlen($str_tags))
298 $str_tags .= '#[url=' . $a->get_baseurl() . '/search?search=' . rawurlencode($basetag) . ']' . $basetag . '[/url]';
301 if(strpos($tag,'@') === 0) {
302 if(strpos($tag,'[url='))
305 $name = substr($tag,1);
306 if((strpos($name,'@')) || (strpos($name,'http://'))) {
308 $links = @lrdd($name);
310 foreach($links as $link) {
311 if($link['@attributes']['rel'] === 'http://webfinger.net/rel/profile-page')
312 $profile = $link['@attributes']['href'];
313 if($link['@attributes']['rel'] === 'salmon') {
316 $inform .= 'url:' . str_replace(',','%2c',$link['@attributes']['href']);
324 if(strstr($name,'_')) {
325 $newname = str_replace('_',' ',$name);
326 $r = q("SELECT * FROM `contact` WHERE `name` = '%s' AND `uid` = %d LIMIT 1",
332 $r = q("SELECT * FROM `contact` WHERE `nick` = '%s' AND `uid` = %d LIMIT 1",
338 $profile = $r[0]['url'];
339 if($r[0]['network'] === 'stat') {
340 $newname = $r[0]['nick'];
343 $alias = $r[0]['alias'];
346 $newname = $r[0]['name'];
349 $inform .= 'cid:' . $r[0]['id'];
353 $body = str_replace('@' . $name, '@' . '[url=' . $profile . ']' . $newname . '[/url]', $body);
354 $profile = str_replace(',','%2c',$profile);
355 if(strlen($str_tags))
357 $str_tags .= '@[url=' . $profile . ']' . $newname . '[/url]';
359 // Status.Net seems to require the numeric ID URL in a mention if the person isn't
360 // subscribed to you. But the nickname URL is OK if they are. Grrr. We'll tag both.
363 if(strlen($str_tags))
365 $str_tags .= '@[url=' . $alias . ']' . $newname . '[/url]';
375 if(preg_match_all('/(\[attachment\]([0-9]+)\[\/attachment\])/',$body,$match)) {
376 foreach($match[2] as $mtch) {
377 $r = q("SELECT `id`,`filename`,`filesize`,`filetype` FROM `attach` WHERE `uid` = %d AND `id` = %d LIMIT 1",
378 intval($profile_uid),
382 if(strlen($attachments))
384 $attachments .= '[attach]href="' . $a->get_baseurl() . '/attach/' . $r[0]['id'] . '" size="' . $r[0]['filesize'] . '" type="' . $r[0]['filetype'] . '" title="' . (($r[0]['filename']) ? $r[0]['filename'] : ' ') . '"[/attach]';
386 $body = str_replace($match[1],'',$body);
392 if($post_type === 'wall' || $post_type === 'wall-comment')
396 $verb = ACTIVITY_POST ;
398 $gravity = (($parent) ? 6 : 0 );
400 $notify_type = (($parent) ? 'comment-new' : 'wall-new' );
402 $uri = item_new_uri($a->get_hostname(),$profile_uid);
405 $datarray['uid'] = $profile_uid;
406 $datarray['type'] = $post_type;
407 $datarray['wall'] = $wall;
408 $datarray['gravity'] = $gravity;
409 $datarray['contact-id'] = $contact_id;
410 $datarray['owner-name'] = $contact_record['name'];
411 $datarray['owner-link'] = $contact_record['url'];
412 $datarray['owner-avatar'] = $contact_record['thumb'];
413 $datarray['author-name'] = $author['name'];
414 $datarray['author-link'] = $author['url'];
415 $datarray['author-avatar'] = $author['thumb'];
416 $datarray['created'] = datetime_convert();
417 $datarray['edited'] = datetime_convert();
418 $datarray['changed'] = datetime_convert();
419 $datarray['uri'] = $uri;
420 $datarray['title'] = $title;
421 $datarray['body'] = $body;
422 $datarray['location'] = $location;
423 $datarray['coord'] = $coord;
424 $datarray['tag'] = $str_tags;
425 $datarray['inform'] = $inform;
426 $datarray['verb'] = $verb;
427 $datarray['allow_cid'] = $str_contact_allow;
428 $datarray['allow_gid'] = $str_group_allow;
429 $datarray['deny_cid'] = $str_contact_deny;
430 $datarray['deny_gid'] = $str_group_deny;
431 $datarray['private'] = $private;
432 $datarray['pubmail'] = $pubmail_enable;
433 $datarray['attach'] = $attachments;
436 * These fields are for the convenience of plugins...
437 * 'self' if true indicates the owner is posting on their own wall
438 * If parent is 0 it is a top-level post.
441 $datarray['parent'] = $parent;
442 $datarray['self'] = $self;
445 $datarray['edit'] = true;
447 call_hooks('post_local',$datarray);
451 $r = q("UPDATE `item` SET `body` = '%s', `edited` = '%s' WHERE `id` = %d AND `uid` = %d LIMIT 1",
453 dbesc(datetime_convert()),
458 proc_run('php', "include/notifier.php", 'edit_post', "$post_id");
459 if((x($_POST,'return')) && strlen($_POST['return'])) {
460 logger('return: ' . $_POST['return']);
461 goaway($a->get_baseurl() . "/" . $_POST['return'] );
469 $r = q("INSERT INTO `item` (`uid`,`type`,`wall`,`gravity`,`contact-id`,`owner-name`,`owner-link`,`owner-avatar`,
470 `author-name`, `author-link`, `author-avatar`, `created`, `edited`, `changed`, `uri`, `title`, `body`, `location`, `coord`,
471 `tag`, `inform`, `verb`, `allow_cid`, `allow_gid`, `deny_cid`, `deny_gid`, `private`, `pubmail`, `attach` )
472 VALUES( %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', %d, %d, '%s' )",
473 intval($datarray['uid']),
474 dbesc($datarray['type']),
475 intval($datarray['wall']),
476 intval($datarray['gravity']),
477 intval($datarray['contact-id']),
478 dbesc($datarray['owner-name']),
479 dbesc($datarray['owner-link']),
480 dbesc($datarray['owner-avatar']),
481 dbesc($datarray['author-name']),
482 dbesc($datarray['author-link']),
483 dbesc($datarray['author-avatar']),
484 dbesc($datarray['created']),
485 dbesc($datarray['edited']),
486 dbesc($datarray['changed']),
487 dbesc($datarray['uri']),
488 dbesc($datarray['title']),
489 dbesc($datarray['body']),
490 dbesc($datarray['location']),
491 dbesc($datarray['coord']),
492 dbesc($datarray['tag']),
493 dbesc($datarray['inform']),
494 dbesc($datarray['verb']),
495 dbesc($datarray['allow_cid']),
496 dbesc($datarray['allow_gid']),
497 dbesc($datarray['deny_cid']),
498 dbesc($datarray['deny_gid']),
499 intval($datarray['private']),
500 intval($datarray['pubmail']),
501 dbesc($datarray['attach'])
504 $r = q("SELECT `id` FROM `item` WHERE `uri` = '%s' LIMIT 1",
505 dbesc($datarray['uri']));
507 $post_id = $r[0]['id'];
508 logger('mod_item: saved item ' . $post_id);
512 // This item is the last leaf and gets the comment box, clear any ancestors
513 $r = q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent` = %d ",
514 dbesc(datetime_convert()),
518 // Inherit ACL's from the parent item.
520 $r = q("UPDATE `item` SET `allow_cid` = '%s', `allow_gid` = '%s', `deny_cid` = '%s', `deny_gid` = '%s', `private` = %d
521 WHERE `id` = %d LIMIT 1",
522 dbesc($parent_item['allow_cid']),
523 dbesc($parent_item['allow_gid']),
524 dbesc($parent_item['deny_cid']),
525 dbesc($parent_item['deny_gid']),
526 intval($parent_item['private']),
530 // Send a notification email to the conversation owner, unless the owner is me and I wrote this item
531 if(($user['notify-flags'] & NOTIFY_COMMENT) && ($contact_record != $author)) {
532 push_lang($user['language']);
533 require_once('bbcode.php');
534 $from = $author['name'];
536 // name of the automated email sender
537 $msg['notificationfromname'] = stripslashes($datarray['author-name']);;
538 // noreply address to send from
539 $msg['notificationfromemail'] = t('noreply') . '@' . $a->get_hostname();
542 // process the message body to display properly in text mode
544 = html_entity_decode(strip_tags(bbcode(stripslashes($datarray['body']))), ENT_QUOTES, 'UTF-8');
547 // process the message body to display properly in text mode
549 = html_entity_decode(bbcode(stripslashes(str_replace(array("\\r\\n", "\\r","\\n\\n" ,"\\n"), "<br />\n",$datarray['body']))));
551 // load the template for private message notifications
552 $tpl = get_intltext_template('cmnt_received_html_body_eml.tpl');
553 $email_html_body_tpl = replace_macros($tpl,array(
554 '$username' => $user['username'],
555 '$sitename' => $a->config['sitename'], // name of this site
556 '$siteurl' => $a->get_baseurl(), // descriptive url of this site
557 '$thumb' => $author['thumb'], // thumbnail url for sender icon
558 '$email' => $importer['email'], // email address to send to
559 '$url' => $author['url'], // full url for the site
560 '$from' => $from, // name of the person sending the message
561 '$body' => $msg['htmlversion'], // html version of the message
562 '$display' => $a->get_baseurl() . '/display/' . $user['nickname'] . '/' . $post_id,
565 // load the template for private message notifications
566 $tpl = get_intltext_template('cmnt_received_text_body_eml.tpl');
567 $email_text_body_tpl = replace_macros($tpl,array(
568 '$username' => $user['username'],
569 '$sitename' => $a->config['sitename'], // name of this site
570 '$siteurl' => $a->get_baseurl(), // descriptive url of this site
571 '$thumb' => $author['thumb'], // thumbnail url for sender icon
572 '$email' => $importer['email'], // email address to send to
573 '$url' => $author['url'], // profile url for the author
574 '$from' => $from, // name of the person sending the message
575 '$body' => $msg['textversion'], // text version of the message
576 '$display' => $a->get_baseurl() . '/display/' . $user['nickname'] . '/' . $post_id,
579 // use the EmailNotification library to send the message
580 require_once("include/EmailNotification.php");
581 EmailNotification::sendTextHtmlEmail(
582 $msg['notificationfromname'],
583 t("Administrator@") . $a->get_hostname(),
584 t("noreply") . '@' . $a->get_hostname(),
586 sprintf( t('%s commented on an item at %s'), $from , $a->config['sitename']),
587 $email_html_body_tpl,
597 // let me know if somebody did a wall-to-wall post on my profile
599 if(($user['notify-flags'] & NOTIFY_WALL) && ($contact_record != $author)) {
600 push_lang($user['language']);
601 require_once('bbcode.php');
602 $from = $author['name'];
604 // name of the automated email sender
605 $msg['notificationfromname'] = $from;
606 // noreply address to send from
607 $msg['notificationfromemail'] = t('noreply') . '@' . $a->get_hostname();
610 // process the message body to display properly in text mode
612 = html_entity_decode(strip_tags(bbcode(stripslashes($datarray['body']))), ENT_QUOTES, 'UTF-8');
615 // process the message body to display properly in text mode
617 = html_entity_decode(bbcode(stripslashes(str_replace(array("\\r\\n", "\\r","\\n\\n" ,"\\n"), "<br />\n",$datarray['body']))));
619 // load the template for private message notifications
620 $tpl = load_view_file('view/wall_received_html_body_eml.tpl');
621 $email_html_body_tpl = replace_macros($tpl,array(
622 '$username' => $user['username'],
623 '$sitename' => $a->config['sitename'], // name of this site
624 '$siteurl' => $a->get_baseurl(), // descriptive url of this site
625 '$thumb' => $author['thumb'], // thumbnail url for sender icon
626 '$url' => $author['url'], // full url for the site
627 '$from' => $from, // name of the person sending the message
628 '$body' => $msg['htmlversion'], // html version of the message
629 '$display' => $a->get_baseurl() . '/display/' . $user['nickname'] . '/' . $post_id,
632 // load the template for private message notifications
633 $tpl = load_view_file('view/wall_received_text_body_eml.tpl');
634 $email_text_body_tpl = replace_macros($tpl,array(
635 '$username' => $user['username'],
636 '$sitename' => $a->config['sitename'], // name of this site
637 '$siteurl' => $a->get_baseurl(), // descriptive url of this site
638 '$thumb' => $author['thumb'], // thumbnail url for sender icon
639 '$url' => $author['url'], // full url for the site
640 '$from' => $from, // name of the person sending the message
641 '$body' => $msg['textversion'], // text version of the message
642 '$display' => $a->get_baseurl() . '/display/' . $user['nickname'] . '/' . $post_id,
645 // use the EmailNotification library to send the message
646 require_once("include/EmailNotification.php");
647 EmailNotification::sendTextHtmlEmail(
648 $msg['notificationfromname'],
649 t("Administrator@") . $a->get_hostname(),
650 t("noreply") . '@' . $a->get_hostname(),
652 sprintf( t('%s posted to your profile wall at %s') , $from , $a->config['sitename']),
653 $email_html_body_tpl,
660 $r = q("UPDATE `item` SET `parent` = %d, `parent-uri` = '%s', `plink` = '%s', `changed` = '%s', `last-child` = 1, `visible` = 1
661 WHERE `id` = %d LIMIT 1",
663 dbesc(($parent == $post_id) ? $uri : $parent_item['uri']),
664 dbesc($a->get_baseurl() . '/display/' . $user['nickname'] . '/' . $post_id),
665 dbesc(datetime_convert()),
669 // photo comments turn the corresponding item visible to the profile wall
670 // This way we don't see every picture in your new photo album posted to your wall at once.
671 // They will show up as people comment on them.
673 if(! $parent_item['visible']) {
674 $r = q("UPDATE `item` SET `visible` = 1 WHERE `id` = %d LIMIT 1",
675 intval($parent_item['id'])
680 logger('mod_item: unable to retrieve post that was just stored.');
681 notify( t('System error. Post not saved.'));
682 goaway($a->get_baseurl() . "/" . $_POST['return'] );
686 proc_run('php', "include/notifier.php", $notify_type, "$post_id");
688 $datarray['id'] = $post_id;
689 $datarray['plink'] = $a->get_baseurl() . '/display/' . $user['nickname'] . '/' . $post_id;
691 call_hooks('post_local_end', $datarray);
693 if(strlen($emailcc) && $profile_uid == local_user()) {
694 $erecips = explode(',', $emailcc);
695 if(count($erecips)) {
696 foreach($erecips as $recip) {
697 $addr = trim($recip);
700 $disclaimer = '<hr />' . sprintf( t('This message was sent to you by %s, a member of the Friendika social network.'),$a->user['username'])
702 $disclaimer .= sprintf( t('You may visit them online at %s'), $a->get_baseurl() . '/profile/' . $a->user['nickname']) . EOL;
703 $disclaimer .= t('Please contact the sender by replying to this post if you do not wish to receive these messages.') . EOL;
705 $subject = '[Friendika]' . ' ' . sprintf( t('%s posted an update.'),$a->user['username']);
706 $headers = 'From: ' . $a->user['username'] . ' <' . $a->user['email'] . '>' . "\n";
707 $headers .= 'MIME-Version: 1.0' . "\n";
708 $headers .= 'Content-Type: text/html; charset=UTF-8' . "\n";
709 $headers .= 'Content-Transfer-Encoding: 8bit' . "\n\n";
710 $link = '<a href="' . $a->get_baseurl() . '/profile/' . $a->user['nickname'] . '"><img src="' . $author['thumb'] . '" alt="' . $a->user['username'] . '" /></a><br /><br />';
711 $html = prepare_body($datarray);
712 $message = '<html><body>' . $link . $html . $disclaimer . '</body></html>';
713 @mail($addr, $subject, $message, $headers);
718 logger('post_complete');
719 if((x($_POST,'return')) && strlen($_POST['return'])) {
720 logger('return: ' . $_POST['return']);
721 goaway($a->get_baseurl() . "/" . $_POST['return'] );
723 $json = array('success' => 1);
724 if(x($_POST,'jsreload') && strlen($_POST['jsreload']))
725 $json['reload'] = $a->get_baseurl() . '/' . $_POST['jsreload'];
727 logger('post_json: ' . print_r($json,true), LOGGER_DEBUG);
729 echo json_encode($json);
738 function item_content(&$a) {
740 if((! local_user()) && (! remote_user()))
743 require_once('include/security.php');
745 if(($a->argc == 3) && ($a->argv[1] === 'drop') && intval($a->argv[2])) {
746 require_once('include/items.php');
747 drop_item($a->argv[2]);