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('library/langdet/Text/LanguageDetect.php');
22 require_once('include/tags.php');
24 function item_post(&$a) {
26 if((! local_user()) && (! remote_user()) && (! x($_REQUEST,'commenter')))
29 require_once('include/security.php');
33 if(x($_REQUEST,'dropitems')) {
34 require_once('include/items.php');
35 $arr_drop = explode(',',$_REQUEST['dropitems']);
36 drop_items($arr_drop);
37 $json = array('success' => 1);
38 echo json_encode($json);
42 call_hooks('post_local_start', $_REQUEST);
43 // logger('postinput ' . file_get_contents('php://input'));
44 logger('postvars ' . print_r($_REQUEST,true), LOGGER_DATA);
46 $api_source = ((x($_REQUEST,'api_source') && $_REQUEST['api_source']) ? true : false);
47 $return_path = ((x($_REQUEST,'return')) ? $_REQUEST['return'] : '');
48 $preview = ((x($_REQUEST,'preview')) ? intval($_REQUEST['preview']) : 0);
51 // Check for doubly-submitted posts, and reject duplicates
52 // Note that we have to ignore previews, otherwise nothing will post
53 // after it's been previewed
54 if(!$preview && x($_REQUEST['post_id_random'])) {
55 if(x($_SESSION['post-random']) && $_SESSION['post-random'] == $_REQUEST['post_id_random']) {
56 logger("item post: duplicate post", LOGGER_DEBUG);
57 item_post_return($a->get_baseurl(), $api_source, $return_path);
60 $_SESSION['post-random'] = $_REQUEST['post_id_random'];
64 * Is this a reply to something?
67 $parent = ((x($_REQUEST,'parent')) ? intval($_REQUEST['parent']) : 0);
68 $parent_uri = ((x($_REQUEST,'parent_uri')) ? trim($_REQUEST['parent_uri']) : '');
71 $parent_contact = null;
76 if($parent || $parent_uri) {
78 if(! x($_REQUEST,'type'))
79 $_REQUEST['type'] = 'net-comment';
82 $r = q("SELECT * FROM `item` WHERE `id` = %d LIMIT 1",
86 elseif($parent_uri && local_user()) {
87 // This is coming from an API source, and we are logged in
88 $r = q("SELECT * FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
93 // if this isn't the real parent of the conversation, find it
94 if($r !== false && count($r)) {
95 $parid = $r[0]['parent'];
96 $parent_uri = $r[0]['uri'];
97 if($r[0]['id'] != $r[0]['parent']) {
98 $r = q("SELECT * FROM `item` WHERE `id` = `parent` AND `parent` = %d LIMIT 1",
104 if(($r === false) || (! count($r))) {
105 notice( t('Unable to locate original post.') . EOL);
106 if(x($_REQUEST,'return'))
107 goaway($a->get_baseurl() . "/" . $return_path );
110 $parent_item = $r[0];
111 $parent = $r[0]['id'];
113 // multi-level threading - preserve the info but re-parent to our single level threading
114 //if(($parid) && ($parid != $parent))
115 $thr_parent = $parent_uri;
117 if($parent_item['contact-id'] && $uid) {
118 $r = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
119 intval($parent_item['contact-id']),
123 $parent_contact = $r[0];
127 if($parent) logger('mod_item: item_post parent=' . $parent);
129 $profile_uid = ((x($_REQUEST,'profile_uid')) ? intval($_REQUEST['profile_uid']) : 0);
130 $post_id = ((x($_REQUEST,'post_id')) ? intval($_REQUEST['post_id']) : 0);
131 $app = ((x($_REQUEST,'source')) ? strip_tags($_REQUEST['source']) : '');
133 $allow_moderated = false;
135 // here is where we are going to check for permission to post a moderated comment.
137 // First check that the parent exists and it is a wall item.
139 if((x($_REQUEST,'commenter')) && ((! $parent) || (! $parent_item['wall']))) {
140 notice( t('Permission denied.') . EOL) ;
141 if(x($_REQUEST,'return'))
142 goaway($a->get_baseurl() . "/" . $return_path );
146 // Now check that it is a page_type of PAGE_BLOG, and that valid personal details
147 // have been provided, and run any anti-spam plugins
155 if((! can_write_wall($a,$profile_uid)) && (! $allow_moderated)) {
156 notice( t('Permission denied.') . EOL) ;
157 if(x($_REQUEST,'return'))
158 goaway($a->get_baseurl() . "/" . $return_path );
163 // is this an edited post?
168 $i = q("SELECT * FROM `item` WHERE `uid` = %d AND `id` = %d LIMIT 1",
169 intval($profile_uid),
179 $r = q("SELECT * FROM `user` WHERE `uid` = %d LIMIT 1",
186 $str_group_allow = $orig_post['allow_gid'];
187 $str_contact_allow = $orig_post['allow_cid'];
188 $str_group_deny = $orig_post['deny_gid'];
189 $str_contact_deny = $orig_post['deny_cid'];
190 $location = $orig_post['location'];
191 $coord = $orig_post['coord'];
192 $verb = $orig_post['verb'];
193 $emailcc = $orig_post['emailcc'];
194 $app = $orig_post['app'];
195 $categories = $orig_post['file'];
196 $title = notags(trim($_REQUEST['title']));
197 $body = escape_tags(trim($_REQUEST['body']));
198 $private = $orig_post['private'];
199 $pubmail_enable = $orig_post['pubmail'];
204 // if coming from the API and no privacy settings are set,
205 // use the user default permissions - as they won't have
206 // been supplied via a form.
209 && (! array_key_exists('contact_allow',$_REQUEST))
210 && (! array_key_exists('group_allow',$_REQUEST))
211 && (! array_key_exists('contact_deny',$_REQUEST))
212 && (! array_key_exists('group_deny',$_REQUEST))) {
213 $str_group_allow = $user['allow_gid'];
214 $str_contact_allow = $user['allow_cid'];
215 $str_group_deny = $user['deny_gid'];
216 $str_contact_deny = $user['deny_cid'];
220 // use the posted permissions
222 $str_group_allow = perms2str($_REQUEST['group_allow']);
223 $str_contact_allow = perms2str($_REQUEST['contact_allow']);
224 $str_group_deny = perms2str($_REQUEST['group_deny']);
225 $str_contact_deny = perms2str($_REQUEST['contact_deny']);
228 $title = notags(trim($_REQUEST['title']));
229 $location = notags(trim($_REQUEST['location']));
230 $coord = notags(trim($_REQUEST['coord']));
231 $verb = notags(trim($_REQUEST['verb']));
232 $emailcc = notags(trim($_REQUEST['emailcc']));
233 $body = escape_tags(trim($_REQUEST['body']));
236 $naked_body = preg_replace('/\[(.+?)\]/','',$body);
238 if (version_compare(PHP_VERSION, '5.3.0', '>=')) {
239 $l = new Text_LanguageDetect;
240 //$lng = $l->detectConfidence($naked_body);
241 //$postopts = (($lng['language']) ? 'lang=' . $lng['language'] . ';' . $lng['confidence'] : '');
243 $lng = $l->detect($naked_body, 3);
245 if (sizeof($lng) > 0) {
248 foreach ($lng as $language => $score) {
254 $postopts .= $language.";".$score;
258 logger('mod_item: detect language' . print_r($lng,true) . $naked_body, LOGGER_DATA);
264 $private = ((strlen($str_group_allow) || strlen($str_contact_allow) || strlen($str_group_deny) || strlen($str_contact_deny)) ? 1 : 0);
266 // If this is a comment, set the permissions from the parent.
271 if(($parent_item['private'])
272 || strlen($parent_item['allow_cid'])
273 || strlen($parent_item['allow_gid'])
274 || strlen($parent_item['deny_cid'])
275 || strlen($parent_item['deny_gid'])) {
276 $private = (($parent_item['private']) ? $parent_item['private'] : 1);
279 $str_contact_allow = $parent_item['allow_cid'];
280 $str_group_allow = $parent_item['allow_gid'];
281 $str_contact_deny = $parent_item['deny_cid'];
282 $str_group_deny = $parent_item['deny_gid'];
285 $pubmail_enable = ((x($_REQUEST,'pubmail_enable') && intval($_REQUEST['pubmail_enable']) && (! $private)) ? 1 : 0);
287 // if using the API, we won't see pubmail_enable - figure out if it should be set
289 if($api_source && $profile_uid && $profile_uid == local_user() && (! $private)) {
290 $mail_disabled = ((function_exists('imap_open') && (! get_config('system','imap_disabled'))) ? 0 : 1);
291 if(! $mail_disabled) {
292 $r = q("SELECT * FROM `mailacct` WHERE `uid` = %d AND `server` != '' LIMIT 1",
295 if(count($r) && intval($r[0]['pubmail']))
296 $pubmail_enabled = true;
300 if(! strlen($body)) {
303 info( t('Empty post discarded.') . EOL );
304 if(x($_REQUEST,'return'))
305 goaway($a->get_baseurl() . "/" . $return_path );
310 if(strlen($categories)) {
311 // get the "fileas" tags for this post
312 $filedas = file_tag_file_to_list($categories, 'file');
314 // save old and new categories, so we can determine what needs to be deleted from pconfig
315 $categories_old = $categories;
316 $categories = file_tag_list_to_file(trim($_REQUEST['category']), 'category');
317 $categories_new = $categories;
318 if(strlen($filedas)) {
319 // append the fileas stuff to the new categories list
320 $categories .= file_tag_list_to_file($filedas, 'file');
323 // Work around doubled linefeeds in Tinymce 3.5b2
324 // First figure out if it's a status post that would've been
325 // created using tinymce. Otherwise leave it alone.
327 /* $plaintext = (local_user() ? intval(get_pconfig(local_user(),'system','plaintext')) || !feature_enabled($profile_uid,'richtext') : 0);
328 if((! $parent) && (! $api_source) && (! $plaintext)) {
329 $body = fix_mce_lf($body);
331 $plaintext = (local_user() ? !feature_enabled($profile_uid,'richtext') : 0);
332 if((! $parent) && (! $api_source) && (! $plaintext)) {
333 $body = fix_mce_lf($body);
337 // get contact info for poster
343 if((local_user()) && (local_user() == $profile_uid)) {
345 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 1 LIMIT 1",
346 intval($_SESSION['uid'])
349 elseif(remote_user()) {
350 if(is_array($_SESSION['remote'])) {
351 foreach($_SESSION['remote'] as $v) {
352 if($v['uid'] == $profile_uid) {
353 $contact_id = $v['cid'];
359 $r = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
367 $contact_id = $author['id'];
370 // get contact info for owner
372 if($profile_uid == local_user()) {
373 $contact_record = $author;
376 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 1 LIMIT 1",
380 $contact_record = $r[0];
383 $post_type = notags(trim($_REQUEST['type']));
385 if($post_type === 'net-comment') {
386 if($parent_item !== null) {
387 if($parent_item['wall'] == 1)
388 $post_type = 'wall-comment';
390 $post_type = 'remote-comment';
396 * When a photo was uploaded into the message using the (profile wall) ajax
397 * uploader, The permissions are initially set to disallow anybody but the
398 * owner from seeing it. This is because the permissions may not yet have been
399 * set for the post. If it's private, the photo permissions should be set
400 * appropriately. But we didn't know the final permissions on the post until
401 * now. So now we'll look for links of uploaded messages that are in the
402 * post and set them to the same permissions as the post itself.
408 if((! $preview) && preg_match_all("/\[img([\=0-9x]*?)\](.*?)\[\/img\]/",$body,$match)) {
411 foreach($images as $image) {
412 if(! stristr($image,$a->get_baseurl() . '/photo/'))
414 $image_uri = substr($image,strrpos($image,'/') + 1);
415 $image_uri = substr($image_uri,0, strpos($image_uri,'-'));
416 if(! strlen($image_uri))
418 $srch = '<' . intval($contact_id) . '>';
420 $r = q("SELECT `id` FROM `photo` WHERE `allow_cid` = '%s' AND `allow_gid` = '' AND `deny_cid` = '' AND `deny_gid` = ''
421 AND `resource-id` = '%s' AND `uid` = %d LIMIT 1",
431 $r = q("UPDATE `photo` SET `allow_cid` = '%s', `allow_gid` = '%s', `deny_cid` = '%s', `deny_gid` = '%s'
432 WHERE `resource-id` = '%s' AND `uid` = %d AND `album` = '%s' ",
433 dbesc($str_contact_allow),
434 dbesc($str_group_allow),
435 dbesc($str_contact_deny),
436 dbesc($str_group_deny),
438 intval($profile_uid),
439 dbesc( t('Wall Photos'))
448 * Next link in any attachment references we find in the post.
453 if((! $preview) && preg_match_all("/\[attachment\](.*?)\[\/attachment\]/",$body,$match)) {
454 $attaches = $match[1];
455 if(count($attaches)) {
456 foreach($attaches as $attach) {
457 $r = q("SELECT * FROM `attach` WHERE `uid` = %d AND `id` = %d LIMIT 1",
458 intval($profile_uid),
462 $r = q("UPDATE `attach` SET `allow_cid` = '%s', `allow_gid` = '%s', `deny_cid` = '%s', `deny_gid` = '%s'
463 WHERE `uid` = %d AND `id` = %d LIMIT 1",
464 dbesc($str_contact_allow),
465 dbesc($str_group_allow),
466 dbesc($str_contact_deny),
467 dbesc($str_group_deny),
468 intval($profile_uid),
476 // embedded bookmark in post? set bookmark flag
479 if(preg_match_all("/\[bookmark\=([^\]]*)\](.*?)\[\/bookmark\]/ism",$body,$match,PREG_SET_ORDER)) {
483 $body = bb_translate_video($body);
487 * Fold multi-line [code] sequences
490 $body = preg_replace('/\[\/code\]\s*\[code\]/ism',"\n",$body);
492 $body = scale_external_images($body,false);
497 * Look for any tags and linkify them
504 $tags = get_tags($body);
507 * add a statusnet style reply tag if the original post was from there
508 * and we are replying, and there isn't one already
511 if(($parent_contact) && ($parent_contact['network'] === NETWORK_OSTATUS)
512 && ($parent_contact['nick']) && (! in_array('@' . $parent_contact['nick'],$tags))) {
513 $body = '@' . $parent_contact['nick'] . ' ' . $body;
514 $tags[] = '@' . $parent_contact['nick'];
519 $private_forum = false;
522 foreach($tags as $tag) {
524 // If we already tagged 'Robert Johnson', don't try and tag 'Robert'.
525 // Robert Johnson should be first in the $tags array
527 $fullnametagged = false;
528 for($x = 0; $x < count($tagged); $x ++) {
529 if(stristr($tagged[$x],$tag . ' ')) {
530 $fullnametagged = true;
537 $success = handle_tag($a, $body, $inform, $str_tags, (local_user()) ? local_user() : $profile_uid , $tag);
538 if($success['replaced'])
540 if(is_array($success['contact']) && intval($success['contact']['prv'])) {
541 $private_forum = true;
542 $private_id = $success['contact']['id'];
547 if(($private_forum) && (! $parent) && (! $private)) {
548 // we tagged a private forum in a top level post and the message was public.
551 $str_contact_allow = '<' . $private_id . '>';
557 if(preg_match_all('/(\[attachment\]([0-9]+)\[\/attachment\])/',$body,$match)) {
558 foreach($match[2] as $mtch) {
559 $r = q("SELECT `id`,`filename`,`filesize`,`filetype` FROM `attach` WHERE `uid` = %d AND `id` = %d LIMIT 1",
560 intval($profile_uid),
564 if(strlen($attachments))
566 $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]';
568 $body = str_replace($match[1],'',$body);
574 if($post_type === 'wall' || $post_type === 'wall-comment')
578 $verb = ACTIVITY_POST ;
580 $gravity = (($parent) ? 6 : 0 );
582 // even if the post arrived via API we are considering that it
583 // originated on this site by default for determining relayability.
585 $origin = ((x($_REQUEST,'origin')) ? intval($_REQUEST['origin']) : 1);
587 $notify_type = (($parent) ? 'comment-new' : 'wall-new' );
589 $uri = item_new_uri($a->get_hostname(),$profile_uid);
591 // Fallback so that we alway have a thr-parent
596 $datarray['uid'] = $profile_uid;
597 $datarray['type'] = $post_type;
598 $datarray['wall'] = $wall;
599 $datarray['gravity'] = $gravity;
600 $datarray['contact-id'] = $contact_id;
601 $datarray['owner-name'] = $contact_record['name'];
602 $datarray['owner-link'] = $contact_record['url'];
603 $datarray['owner-avatar'] = $contact_record['thumb'];
604 $datarray['author-name'] = $author['name'];
605 $datarray['author-link'] = $author['url'];
606 $datarray['author-avatar'] = $author['thumb'];
607 $datarray['created'] = datetime_convert();
608 $datarray['edited'] = datetime_convert();
609 $datarray['commented'] = datetime_convert();
610 $datarray['received'] = datetime_convert();
611 $datarray['changed'] = datetime_convert();
612 $datarray['uri'] = $uri;
613 $datarray['title'] = $title;
614 $datarray['body'] = $body;
615 $datarray['app'] = $app;
616 $datarray['location'] = $location;
617 $datarray['coord'] = $coord;
618 $datarray['tag'] = $str_tags;
619 $datarray['file'] = $categories;
620 $datarray['inform'] = $inform;
621 $datarray['verb'] = $verb;
622 $datarray['allow_cid'] = $str_contact_allow;
623 $datarray['allow_gid'] = $str_group_allow;
624 $datarray['deny_cid'] = $str_contact_deny;
625 $datarray['deny_gid'] = $str_group_deny;
626 $datarray['private'] = $private;
627 $datarray['pubmail'] = $pubmail_enable;
628 $datarray['attach'] = $attachments;
629 $datarray['bookmark'] = intval($bookmark);
630 $datarray['thr-parent'] = $thr_parent;
631 $datarray['postopts'] = $postopts;
632 $datarray['origin'] = $origin;
633 $datarray['moderated'] = $allow_moderated;
636 * These fields are for the convenience of plugins...
637 * 'self' if true indicates the owner is posting on their own wall
638 * If parent is 0 it is a top-level post.
641 $datarray['parent'] = $parent;
642 $datarray['self'] = $self;
643 // $datarray['prvnets'] = $user['prvnets'];
646 $datarray['edit'] = true;
648 $datarray['guid'] = get_guid();
650 // preview mode - prepare the body for display and send it via json
653 require_once('include/conversation.php');
654 $o = conversation($a,array(array_merge($contact_record,$datarray)),'search', false, true);
655 logger('preview: ' . $o);
656 echo json_encode(array('preview' => $o));
661 call_hooks('post_local',$datarray);
663 if(x($datarray,'cancel')) {
664 logger('mod_item: post cancelled by plugin.');
666 goaway($a->get_baseurl() . "/" . $return_path);
669 $json = array('cancel' => 1);
670 if(x($_REQUEST,'jsreload') && strlen($_REQUEST['jsreload']))
671 $json['reload'] = $a->get_baseurl() . '/' . $_REQUEST['jsreload'];
673 echo json_encode($json);
679 $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",
680 dbesc($datarray['title']),
681 dbesc($datarray['body']),
682 dbesc($datarray['tag']),
683 dbesc($datarray['attach']),
684 dbesc($datarray['file']),
685 dbesc(datetime_convert()),
689 create_tags_from_itemuri($post_id, $profile_uid);
691 // update filetags in pconfig
692 file_tag_update_pconfig($uid,$categories_old,$categories_new,'category');
694 proc_run('php', "include/notifier.php", 'edit_post', "$post_id");
695 if((x($_REQUEST,'return')) && strlen($return_path)) {
696 logger('return: ' . $return_path);
697 goaway($a->get_baseurl() . "/" . $return_path );
705 $r = q("INSERT INTO `item` (`guid`, `uid`,`type`,`wall`,`gravity`,`contact-id`,`owner-name`,`owner-link`,`owner-avatar`,
706 `author-name`, `author-link`, `author-avatar`, `created`, `edited`, `commented`, `received`, `changed`, `uri`, `thr-parent`, `title`, `body`, `app`, `location`, `coord`,
707 `tag`, `inform`, `verb`, `postopts`, `allow_cid`, `allow_gid`, `deny_cid`, `deny_gid`, `private`, `pubmail`, `attach`, `bookmark`,`origin`, `moderated`, `file` )
708 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' )",
709 dbesc($datarray['guid']),
710 intval($datarray['uid']),
711 dbesc($datarray['type']),
712 intval($datarray['wall']),
713 intval($datarray['gravity']),
714 intval($datarray['contact-id']),
715 dbesc($datarray['owner-name']),
716 dbesc($datarray['owner-link']),
717 dbesc($datarray['owner-avatar']),
718 dbesc($datarray['author-name']),
719 dbesc($datarray['author-link']),
720 dbesc($datarray['author-avatar']),
721 dbesc($datarray['created']),
722 dbesc($datarray['edited']),
723 dbesc($datarray['commented']),
724 dbesc($datarray['received']),
725 dbesc($datarray['changed']),
726 dbesc($datarray['uri']),
727 dbesc($datarray['thr-parent']),
728 dbesc($datarray['title']),
729 dbesc($datarray['body']),
730 dbesc($datarray['app']),
731 dbesc($datarray['location']),
732 dbesc($datarray['coord']),
733 dbesc($datarray['tag']),
734 dbesc($datarray['inform']),
735 dbesc($datarray['verb']),
736 dbesc($datarray['postopts']),
737 dbesc($datarray['allow_cid']),
738 dbesc($datarray['allow_gid']),
739 dbesc($datarray['deny_cid']),
740 dbesc($datarray['deny_gid']),
741 intval($datarray['private']),
742 intval($datarray['pubmail']),
743 dbesc($datarray['attach']),
744 intval($datarray['bookmark']),
745 intval($datarray['origin']),
746 intval($datarray['moderated']),
747 dbesc($datarray['file'])
750 $r = q("SELECT `id` FROM `item` WHERE `uri` = '%s' LIMIT 1",
751 dbesc($datarray['uri']));
753 $post_id = $r[0]['id'];
754 logger('mod_item: saved item ' . $post_id);
755 create_tags_from_item($post_id);
757 // update filetags in pconfig
758 file_tag_update_pconfig($uid,$categories_old,$categories_new,'category');
760 // Store the fresh generated item into the cache
761 $cachefile = get_cachefile($datarray["guid"]."-".hash("md5", $datarray['body']));
763 if (($cachefile != '') AND !file_exists($cachefile)) {
764 $s = prepare_text($datarray['body']);
765 $stamp1 = microtime(true);
766 file_put_contents($cachefile, $s);
767 $a->save_timestamp($stamp1, "file");
768 logger('mod_item: put item '.$r[0]['id'].' into cachefile '.$cachefile);
773 // This item is the last leaf and gets the comment box, clear any ancestors
774 $r = q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent` = %d ",
775 dbesc(datetime_convert()),
779 // Inherit ACL's from the parent item.
781 $r = q("UPDATE `item` SET `allow_cid` = '%s', `allow_gid` = '%s', `deny_cid` = '%s', `deny_gid` = '%s', `private` = %d
782 WHERE `id` = %d LIMIT 1",
783 dbesc($parent_item['allow_cid']),
784 dbesc($parent_item['allow_gid']),
785 dbesc($parent_item['deny_cid']),
786 dbesc($parent_item['deny_gid']),
787 intval($parent_item['private']),
791 if($contact_record != $author) {
793 'type' => NOTIFY_COMMENT,
794 'notify_flags' => $user['notify-flags'],
795 'language' => $user['language'],
796 'to_name' => $user['username'],
797 'to_email' => $user['email'],
798 'uid' => $user['uid'],
800 'link' => $a->get_baseurl() . '/display/' . $user['nickname'] . '/' . $post_id,
801 'source_name' => $datarray['author-name'],
802 'source_link' => $datarray['author-link'],
803 'source_photo' => $datarray['author-avatar'],
804 'verb' => ACTIVITY_POST,
807 'parent_uri' => $parent_item['uri']
813 // Store the comment signature information in case we need to relay to Diaspora
814 store_diaspora_comment_sig($datarray, $author, ($self ? $a->user['prvkey'] : false), $parent_item, $post_id);
820 if($contact_record != $author) {
822 'type' => NOTIFY_WALL,
823 'notify_flags' => $user['notify-flags'],
824 'language' => $user['language'],
825 'to_name' => $user['username'],
826 'to_email' => $user['email'],
827 'uid' => $user['uid'],
829 'link' => $a->get_baseurl() . '/display/' . $user['nickname'] . '/' . $post_id,
830 'source_name' => $datarray['author-name'],
831 'source_link' => $datarray['author-link'],
832 'source_photo' => $datarray['author-avatar'],
833 'verb' => ACTIVITY_POST,
839 // fallback so that parent always gets set to non-zero.
844 $r = q("UPDATE `item` SET `parent` = %d, `parent-uri` = '%s', `plink` = '%s', `changed` = '%s', `last-child` = 1, `visible` = 1
845 WHERE `id` = %d LIMIT 1",
847 dbesc(($parent == $post_id) ? $uri : $parent_item['uri']),
848 dbesc($a->get_baseurl() . '/display/' . $user['nickname'] . '/' . $post_id),
849 dbesc(datetime_convert()),
853 // photo comments turn the corresponding item visible to the profile wall
854 // This way we don't see every picture in your new photo album posted to your wall at once.
855 // They will show up as people comment on them.
857 if(! $parent_item['visible']) {
858 $r = q("UPDATE `item` SET `visible` = 1 WHERE `id` = %d LIMIT 1",
859 intval($parent_item['id'])
864 logger('mod_item: unable to retrieve post that was just stored.');
865 notice( t('System error. Post not saved.') . EOL);
866 goaway($a->get_baseurl() . "/" . $return_path );
870 // update the commented timestamp on the parent
872 q("UPDATE `item` set `commented` = '%s', `changed` = '%s' WHERE `id` = %d LIMIT 1",
873 dbesc(datetime_convert()),
874 dbesc(datetime_convert()),
878 $datarray['id'] = $post_id;
879 $datarray['plink'] = $a->get_baseurl() . '/display/' . $user['nickname'] . '/' . $post_id;
881 call_hooks('post_local_end', $datarray);
883 if(strlen($emailcc) && $profile_uid == local_user()) {
884 $erecips = explode(',', $emailcc);
885 if(count($erecips)) {
886 foreach($erecips as $recip) {
887 $addr = trim($recip);
890 $disclaimer = '<hr />' . sprintf( t('This message was sent to you by %s, a member of the Friendica social network.'),$a->user['username'])
892 $disclaimer .= sprintf( t('You may visit them online at %s'), $a->get_baseurl() . '/profile/' . $a->user['nickname']) . EOL;
893 $disclaimer .= t('Please contact the sender by replying to this post if you do not wish to receive these messages.') . EOL;
895 $subject = email_header_encode('[Friendica]' . ' ' . sprintf( t('%s posted an update.'),$a->user['username']),'UTF-8');
896 $headers = 'From: ' . email_header_encode($a->user['username'],'UTF-8') . ' <' . $a->user['email'] . '>' . "\n";
897 $headers .= 'MIME-Version: 1.0' . "\n";
898 $headers .= 'Content-Type: text/html; charset=UTF-8' . "\n";
899 $headers .= 'Content-Transfer-Encoding: 8bit' . "\n\n";
900 $link = '<a href="' . $a->get_baseurl() . '/profile/' . $a->user['nickname'] . '"><img src="' . $author['thumb'] . '" alt="' . $a->user['username'] . '" /></a><br /><br />';
901 $html = prepare_body($datarray);
902 $message = '<html><body>' . $link . $html . $disclaimer . '</body></html>';
903 @mail($addr, $subject, $message, $headers);
908 // This is a real juggling act on shared hosting services which kill your processes
909 // e.g. dreamhost. We used to start delivery to our native delivery agents in the background
910 // and then run our plugin delivery from the foreground. We're now doing plugin delivery first,
911 // because as soon as you start loading up a bunch of remote delivey processes, *this* page is
912 // likely to get killed off. If you end up looking at an /item URL and a blank page,
913 // it's very likely the delivery got killed before all your friends could be notified.
914 // Currently the only realistic fixes are to use a reliable server - which precludes shared hosting,
915 // or cut back on plugins which do remote deliveries.
917 proc_run('php', "include/notifier.php", $notify_type, "$post_id");
919 logger('post_complete');
921 item_post_return($a->get_baseurl(), $api_source, $return_path);
925 function item_post_return($baseurl, $api_source, $return_path) {
926 // figure out how to return, depending on from whence we came
932 goaway($baseurl . "/" . $return_path);
935 $json = array('success' => 1);
936 if(x($_REQUEST,'jsreload') && strlen($_REQUEST['jsreload']))
937 $json['reload'] = $baseurl . '/' . $_REQUEST['jsreload'];
939 logger('post_json: ' . print_r($json,true), LOGGER_DEBUG);
941 echo json_encode($json);
947 function item_content(&$a) {
949 if((! local_user()) && (! remote_user()))
952 require_once('include/security.php');
954 if(($a->argc == 3) && ($a->argv[1] === 'drop') && intval($a->argv[2])) {
955 require_once('include/items.php');
956 drop_item($a->argv[2]);
961 * This function removes the tag $tag from the text $body and replaces it with
962 * the appropiate link.
964 * @param unknown_type $body the text to replace the tag in
965 * @param unknown_type $inform a comma-seperated string containing everybody to inform
966 * @param unknown_type $str_tags string to add the tag to
967 * @param unknown_type $profile_uid
968 * @param unknown_type $tag the tag to replace
970 * @return boolean true if replaced, false if not replaced
972 function handle_tag($a, &$body, &$inform, &$str_tags, $profile_uid, $tag) {
978 if(strpos($tag,'#') === 0) {
979 //if the tag is replaced...
980 if(strpos($tag,'[url='))
983 //base tag has the tags name only
984 $basetag = str_replace('_',' ',substr($tag,1));
985 //create text for link
986 $newtag = '#[url=' . $a->get_baseurl() . '/search?tag=' . rawurlencode($basetag) . ']' . $basetag . '[/url]';
987 //replace tag by the link
988 $body = str_replace($tag, $newtag, $body);
991 //is the link already in str_tags?
992 if(! stristr($str_tags,$newtag)) {
993 //append or set str_tags
994 if(strlen($str_tags))
996 $str_tags .= $newtag;
1000 //is it a person tag?
1001 if(strpos($tag,'@') === 0) {
1002 //is it already replaced?
1003 if(strpos($tag,'[url='))
1006 //get the person's name
1007 $name = substr($tag,1);
1008 //is it a link or a full dfrn address?
1009 if((strpos($name,'@')) || (strpos($name,'http://'))) {
1011 //get the profile links
1012 $links = @lrdd($name);
1014 //for all links, collect how is to inform and how's profile is to link
1015 foreach($links as $link) {
1016 if($link['@attributes']['rel'] === 'http://webfinger.net/rel/profile-page')
1017 $profile = $link['@attributes']['href'];
1018 if($link['@attributes']['rel'] === 'salmon') {
1021 $inform .= 'url:' . str_replace(',','%2c',$link['@attributes']['href']);
1025 } else { //if it is a name rather than an address
1029 //is it some generated name?
1030 if(strrpos($newname,'+')) {
1032 $tagcid = intval(substr($newname,strrpos($newname,'+') + 1));
1033 //remove the next word from tag's name
1034 if(strpos($name,' ')) {
1035 $name = substr($name,0,strpos($name,' '));
1038 if($tagcid) { //if there was an id
1039 //select contact with that id from the logged in user's contact list
1040 $r = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
1042 intval($profile_uid)
1046 $newname = str_replace('_',' ',$name);
1048 //select someone from this user's contacts by name
1049 $r = q("SELECT * FROM `contact` WHERE `name` = '%s' AND `uid` = %d LIMIT 1",
1051 intval($profile_uid)
1055 //select someone by attag or nick and the name passed in
1056 $r = q("SELECT * FROM `contact` WHERE `attag` = '%s' OR `nick` = '%s' AND `uid` = %d ORDER BY `attag` DESC LIMIT 1",
1059 intval($profile_uid)
1063 /* } elseif(strstr($name,'_') || strstr($name,' ')) { //no id
1065 $newname = str_replace('_',' ',$name);
1066 //select someone from this user's contacts by name
1067 $r = q("SELECT * FROM `contact` WHERE `name` = '%s' AND `uid` = %d LIMIT 1",
1069 intval($profile_uid)
1072 //select someone by attag or nick and the name passed in
1073 $r = q("SELECT * FROM `contact` WHERE `attag` = '%s' OR `nick` = '%s' AND `uid` = %d ORDER BY `attag` DESC LIMIT 1",
1076 intval($profile_uid)
1079 //$r is set, if someone could be selected
1081 $profile = $r[0]['url'];
1082 //set newname to nick, find alias
1083 if($r[0]['network'] === 'stat') {
1084 $newname = $r[0]['nick'];
1087 $alias = $r[0]['alias'];
1090 $newname = $r[0]['name'];
1091 //add person's id to $inform
1094 $inform .= 'cid:' . $r[0]['id'];
1097 //if there is an url for this persons profile
1098 if(isset($profile)) {
1100 //create profile link
1101 $profile = str_replace(',','%2c',$profile);
1102 $newtag = '@[url=' . $profile . ']' . $newname . '[/url]';
1103 $body = str_replace('@' . $name, $newtag, $body);
1104 //append tag to str_tags
1105 if(! stristr($str_tags,$newtag)) {
1106 if(strlen($str_tags))
1108 $str_tags .= $newtag;
1111 // Status.Net seems to require the numeric ID URL in a mention if the person isn't
1112 // subscribed to you. But the nickname URL is OK if they are. Grrr. We'll tag both.
1114 if(strlen($alias)) {
1115 $newtag = '@[url=' . $alias . ']' . $newname . '[/url]';
1116 if(! stristr($str_tags,$newtag)) {
1117 if(strlen($str_tags))
1119 $str_tags .= $newtag;
1125 return array('replaced' => $replaced, 'contact' => $r[0]);
1129 function store_diaspora_comment_sig($datarray, $author, $uprvkey, $parent_item, $post_id) {
1130 // We won't be able to sign Diaspora comments for authenticated visitors - we don't have their private key
1132 $enabled = intval(get_config('system','diaspora_enabled'));
1134 logger('mod_item: diaspora support disabled, not storing comment signature', LOGGER_DEBUG);
1139 logger('mod_item: storing diaspora comment signature');
1141 require_once('include/bb2diaspora.php');
1142 $signed_body = html_entity_decode(bb2diaspora($datarray['body']));
1144 // Only works for NETWORK_DFRN
1145 $contact_baseurl_start = strpos($author['url'],'://') + 3;
1146 $contact_baseurl_length = strpos($author['url'],'/profile') - $contact_baseurl_start;
1147 $contact_baseurl = substr($author['url'], $contact_baseurl_start, $contact_baseurl_length);
1148 $diaspora_handle = $author['nick'] . '@' . $contact_baseurl;
1150 $signed_text = $datarray['guid'] . ';' . $parent_item['guid'] . ';' . $signed_body . ';' . $diaspora_handle;
1152 if( $uprvkey !== false )
1153 $authorsig = base64_encode(rsa_sign($signed_text,$uprvkey,'sha256'));
1157 q("insert into sign (`iid`,`signed_text`,`signature`,`signer`) values (%d,'%s','%s','%s') ",
1159 dbesc($signed_text),
1160 dbesc(base64_encode($authorsig)),
1161 dbesc($diaspora_handle)