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 call_hooks('post_local_start', $_POST);
27 $parent = ((x($_POST,'parent')) ? intval($_POST['parent']) : 0);
30 $parent_contact = null;
33 $r = q("SELECT * FROM `item` WHERE `id` = %d LIMIT 1",
37 notice( t('Unable to locate original post.') . EOL);
38 if(x($_POST,'return'))
39 goaway($a->get_baseurl() . "/" . $_POST['return'] );
43 if($parent_item['contact-id'] && $uid) {
44 $r = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
45 intval($parent_item['contact-id']),
49 $parent_contact = $r[0];
53 $profile_uid = ((x($_POST,'profile_uid')) ? intval($_POST['profile_uid']) : 0);
56 if(! can_write_wall($a,$profile_uid)) {
57 notice( t('Permission denied.') . EOL) ;
58 if(x($_POST,'return'))
59 goaway($a->get_baseurl() . "/" . $_POST['return'] );
65 $r = q("SELECT * FROM `user` WHERE `uid` = %d LIMIT 1",
72 $str_group_allow = perms2str($_POST['group_allow']);
73 $str_contact_allow = perms2str($_POST['contact_allow']);
74 $str_group_deny = perms2str($_POST['group_deny']);
75 $str_contact_deny = perms2str($_POST['contact_deny']);
77 $private = ((strlen($str_group_allow) || strlen($str_contact_allow) || strlen($str_group_deny) || strlen($str_contact_deny)) ? 1 : 0);
80 (($parent_item['private'])
81 || strlen($parent_item['allow_cid'])
82 || strlen($parent_item['allow_gid'])
83 || strlen($parent_item['deny_cid'])
84 || strlen($parent_item['deny_gid'])
90 $title = notags(trim($_POST['title']));
91 $body = escape_tags(trim($_POST['body']));
92 $location = notags(trim($_POST['location']));
93 $coord = notags(trim($_POST['coord']));
94 $verb = notags(trim($_POST['verb']));
95 $emailcc = notags(trim($_POST['emailcc']));
98 notice( t('Empty post discarded.') . EOL );
99 if(x($_POST,'return'))
100 goaway($a->get_baseurl() . "/" . $_POST['return'] );
104 // get contact info for poster
109 if(($_SESSION['uid']) && ($_SESSION['uid'] == $profile_uid)) {
111 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 1 LIMIT 1",
112 intval($_SESSION['uid'])
116 if((x($_SESSION,'visitor_id')) && (intval($_SESSION['visitor_id']))) {
117 $r = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
118 intval($_SESSION['visitor_id'])
125 $contact_id = $author['id'];
128 // get contact info for owner
130 if($profile_uid == $_SESSION['uid']) {
131 $contact_record = $author;
134 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 1 LIMIT 1",
138 $contact_record = $r[0];
141 $post_type = notags(trim($_POST['type']));
143 if($post_type === 'net-comment') {
144 if($parent_item !== null) {
145 if($parent_item['type'] === 'remote') {
146 $post_type = 'remote-comment';
149 $post_type = 'wall-comment';
157 * When a photo was uploaded into the message using the (profile wall) ajax
158 * uploader, The permissions are initially set to disallow anybody but the
159 * owner from seeing it. This is because the permissions may not yet have been
160 * set for the post. If it's private, the photo permissions should be set
161 * appropriately. But we didn't know the final permissions on the post until
162 * now. So now we'll look for links of uploaded messages that are in the
163 * post and set them to the same permissions as the post itself.
169 if(preg_match_all("/\[img\](.+?)\[\/img\]/",$body,$match)) {
172 foreach($images as $image) {
173 if(! stristr($image,$a->get_baseurl() . '/photo/'))
175 $image_uri = substr($image,strrpos($image,'/') + 1);
176 $image_uri = substr($image_uri,0, strpos($image_uri,'-'));
177 $r = q("UPDATE `photo` SET `allow_cid` = '%s', `allow_gid` = '%s', `deny_cid` = '%s', `deny_gid` = '%s'
178 WHERE `resource-id` = '%s' AND `album` = '%s' ",
179 dbesc($str_contact_allow),
180 dbesc($str_group_allow),
181 dbesc($str_contact_deny),
182 dbesc($str_group_deny),
184 dbesc( t('Wall Photos'))
192 * Fold multi-line [code] sequences
195 $body = preg_replace('/\[\/code\]\s*\[code\]/m',"\n",$body);
198 * Look for any tags and linkify them
205 $tags = get_tags($body);
207 if(($parent_contact) && ($parent_contact['network'] === 'stat') && ($parent_contact['nick']) && (! in_array('@' . $parent_contact['nick'],$tags))) {
208 $body = '@' . $parent_contact['nick'] . ' ' . $body;
209 $tags[] = '@' . $parent_contact['nick'];
213 foreach($tags as $tag) {
214 if(strpos($tag,'#') === 0) {
215 $basetag = str_replace('_',' ',substr($tag,1));
216 $body = str_replace($tag,'#[url=' . $a->get_baseurl() . '/search?search=' . rawurlencode($basetag) . ']' . $basetag . '[/url]',$body);
217 if(strlen($str_tags))
219 $str_tags .= '#[url=' . $a->get_baseurl() . '/search?search=' . rawurlencode($basetag) . ']' . $basetag . '[/url]';
222 if(strpos($tag,'@') === 0) {
224 $name = substr($tag,1);
225 if((strpos($name,'@')) || (strpos($name,'http://'))) {
227 $links = @lrdd($name);
229 foreach($links as $link) {
230 if($link['@attributes']['rel'] === 'http://webfinger.net/rel/profile-page')
231 $profile = $link['@attributes']['href'];
232 if($link['@attributes']['rel'] === 'salmon') {
235 $inform .= 'url:' . str_replace(',','%2c',$link['@attributes']['href']);
242 if(strstr($name,'_')) {
243 $newname = str_replace('_',' ',$name);
244 $r = q("SELECT * FROM `contact` WHERE `name` = '%s' AND `uid` = %d LIMIT 1",
250 $r = q("SELECT * FROM `contact` WHERE `nick` = '%s' AND `uid` = %d LIMIT 1",
256 $profile = $r[0]['url'];
257 if($r[0]['network'] === 'stat') {
258 $newname = $r[0]['nick'];
262 $newname = $r[0]['name'];
265 $inform .= 'cid:' . $r[0]['id'];
269 $body = str_replace('@' . $name, '@' . '[url=' . $profile . ']' . $newname . '[/url]', $body);
270 $profile = str_replace(',','%2c',$profile);
271 if(strlen($str_tags))
273 $str_tags .= '@[url=' . $profile . ']' . $newname . '[/url]';
282 if($post_type === 'wall' || $post_type === 'wall-comment')
286 $verb = ACTIVITY_POST ;
288 $gravity = (($parent) ? 6 : 0 );
290 $notify_type = (($parent) ? 'comment-new' : 'wall-new' );
292 $uri = item_new_uri($a->get_hostname(),$profile_uid);
295 $datarray['uid'] = $profile_uid;
296 $datarray['type'] = $post_type;
297 $datarray['wall'] = $wall;
298 $datarray['gravity'] = $gravity;
299 $datarray['contact-id'] = $contact_id;
300 $datarray['owner-name'] = $contact_record['name'];
301 $datarray['owner-link'] = $contact_record['url'];
302 $datarray['owner-avatar'] = $contact_record['thumb'];
303 $datarray['author-name'] = $author['name'];
304 $datarray['author-link'] = $author['url'];
305 $datarray['author-avatar'] = $author['thumb'];
306 $datarray['created'] = datetime_convert();
307 $datarray['edited'] = datetime_convert();
308 $datarray['changed'] = datetime_convert();
309 $datarray['uri'] = $uri;
310 $datarray['title'] = $title;
311 $datarray['body'] = $body;
312 $datarray['location'] = $location;
313 $datarray['coord'] = $coord;
314 $datarray['tag'] = $str_tags;
315 $datarray['inform'] = $inform;
316 $datarray['verb'] = $verb;
317 $datarray['allow_cid'] = $str_contact_allow;
318 $datarray['allow_gid'] = $str_group_allow;
319 $datarray['deny_cid'] = $str_contact_deny;
320 $datarray['deny_gid'] = $str_group_deny;
321 $datarray['private'] = $private;
324 * These fields are for the convenience of plugins...
325 * 'self' if true indicates the owner is posting on their own wall
326 * If parent is 0 it is a top-level post.
329 $datarray['parent'] = $parent;
330 $datarray['self'] = $self;
333 call_hooks('post_local',$datarray);
335 $r = q("INSERT INTO `item` (`uid`,`type`,`wall`,`gravity`,`contact-id`,`owner-name`,`owner-link`,`owner-avatar`,
336 `author-name`, `author-link`, `author-avatar`, `created`, `edited`, `changed`, `uri`, `title`, `body`, `location`, `coord`,
337 `tag`, `inform`, `verb`, `allow_cid`, `allow_gid`, `deny_cid`, `deny_gid`, `private` )
338 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 )",
339 intval($datarray['uid']),
340 dbesc($datarray['type']),
341 intval($datarray['wall']),
342 intval($datarray['gravity']),
343 intval($datarray['contact-id']),
344 dbesc($datarray['owner-name']),
345 dbesc($datarray['owner-link']),
346 dbesc($datarray['owner-avatar']),
347 dbesc($datarray['author-name']),
348 dbesc($datarray['author-link']),
349 dbesc($datarray['author-avatar']),
350 dbesc($datarray['created']),
351 dbesc($datarray['edited']),
352 dbesc($datarray['changed']),
353 dbesc($datarray['uri']),
354 dbesc($datarray['title']),
355 dbesc($datarray['body']),
356 dbesc($datarray['location']),
357 dbesc($datarray['coord']),
358 dbesc($datarray['tag']),
359 dbesc($datarray['inform']),
360 dbesc($datarray['verb']),
361 dbesc($datarray['allow_cid']),
362 dbesc($datarray['allow_gid']),
363 dbesc($datarray['deny_cid']),
364 dbesc($datarray['deny_gid']),
365 intval($datarray['private'])
368 $r = q("SELECT `id` FROM `item` WHERE `uri` = '%s' LIMIT 1",
369 dbesc($datarray['uri']));
371 $post_id = $r[0]['id'];
372 logger('mod_item: saved item ' . $post_id);
376 // This item is the last leaf and gets the comment box, clear any ancestors
377 $r = q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent` = %d ",
378 dbesc(datetime_convert()),
382 // Inherit ACL's from the parent item.
384 $r = q("UPDATE `item` SET `allow_cid` = '%s', `allow_gid` = '%s', `deny_cid` = '%s', `deny_gid` = '%s', `private` = %d
385 WHERE `id` = %d LIMIT 1",
386 dbesc($parent_item['allow_cid']),
387 dbesc($parent_item['allow_gid']),
388 dbesc($parent_item['deny_cid']),
389 dbesc($parent_item['deny_gid']),
390 intval($parent_item['private']),
394 // Send a notification email to the conversation owner, unless the owner is me and I wrote this item
395 if(($user['notify-flags'] & NOTIFY_COMMENT) && ($contact_record != $author)) {
396 require_once('bbcode.php');
397 $from = $author['name'];
398 $tpl = load_view_file('view/cmnt_received_eml.tpl');
399 $email_tpl = replace_macros($tpl, array(
400 '$sitename' => $a->config['sitename'],
401 '$siteurl' => $a->get_baseurl(),
402 '$username' => $user['username'],
403 '$email' => $user['email'],
405 '$display' => $a->get_baseurl() . '/display/' . $user['nickname'] . '/' . $post_id,
406 '$body' => strip_tags(bbcode($datarray['body']))
409 $res = mail($user['email'], $from . t(" commented on your item at ") . $a->config['sitename'],
410 $email_tpl,t("From: Administrator@") . $a->get_hostname() );
416 // let me know if somebody did a wall-to-wall post on my profile
418 if(($user['notify-flags'] & NOTIFY_WALL) && ($contact_record != $author)) {
419 require_once('bbcode.php');
420 $from = $author['name'];
421 $tpl = load_view_file('view/wall_received_eml.tpl');
422 $email_tpl = replace_macros($tpl, array(
423 '$sitename' => $a->config['sitename'],
424 '$siteurl' => $a->get_baseurl(),
425 '$username' => $user['username'],
426 '$email' => $user['email'],
428 '$display' => $a->get_baseurl() . '/display/' . $user['nickname'] . '/' . $post_id,
429 '$body' => strip_tags(bbcode($datarray['body']))
432 $res = mail($user['email'], $from . t(" posted on your profile wall at ") . $a->config['sitename'],
433 $email_tpl,t("From: Administrator@") . $a->get_hostname() );
437 $r = q("UPDATE `item` SET `parent` = %d, `parent-uri` = '%s', `plink` = '%s', `changed` = '%s', `last-child` = 1, `visible` = 1
438 WHERE `id` = %d LIMIT 1",
440 dbesc(($parent == $post_id) ? $uri : $parent_item['uri']),
441 dbesc($a->get_baseurl() . '/display/' . $user['nickname'] . '/' . $post_id),
442 dbesc(datetime_convert()),
446 // photo comments turn the corresponding item visible to the profile wall
447 // This way we don't see every picture in your new photo album posted to your wall at once.
448 // They will show up as people comment on them.
450 if(! $parent_item['visible']) {
451 $r = q("UPDATE `item` SET `visible` = 1 WHERE `id` = %d LIMIT 1",
452 intval($parent_item['id'])
457 logger('mod_item: unable to retrieve post that was just stored.');
458 notify( t('System error. Post not saved.'));
459 goaway($a->get_baseurl() . "/" . $_POST['return'] );
463 proc_run('php', "include/notifier.php", $notify_type, "$post_id");
465 $datarray['id'] = $post_id;
467 call_hooks('post_local_end', $datarray);
469 if(strlen($emailcc) && $profile_uid == local_user()) {
470 $erecips = explode(',', $emailcc);
471 if(count($erecips)) {
472 foreach($erecips as $recip) {
473 $addr = trim($recip);
476 $disclaimer = '<hr />' . t('This message was sent to you by ') . $a->user['username']
477 . t(', a member of the Friendika social network.') . '<br />';
478 $disclaimer .= t('You may visit them online at') . ' '
479 . $a->get_baseurl() . '/profile/' . $a->user['nickname'] . '<br />';
480 $disclaimer .= t('Please contact the sender by replying to this post if you do not wish to receive these messages.') . '<br />';
482 $subject = '[Friendika]' . ' ' . $a->user['username'] . ' ' . t('posted an update.');
483 $headers = 'From: ' . $a->user['username'] . ' <' . $a->user['email'] . '>' . "\n";
484 $headers .= 'MIME-Version: 1.0' . "\n";
485 $headers .= 'Content-Type: text/html; charset=UTF-8' . "\n";
486 $headers .= 'Content-Transfer-Encoding: 8bit' . "\n\n";
487 $link = '<a href="' . $a->get_baseurl() . '/profile/' . $a->user['nickname'] . '"><img src="' . $author['thumb'] . '" alt="' . $a->user['username'] . '" /></a><br /><br />';
488 $html = prepare_body($datarray);
489 $message = '<html><body>' . $link . $html . $disclaimer . '</body></html>';
490 @mail($addr, $subject, $message, $headers);
495 logger('post_complete');
496 if((x($_POST,'return')) && strlen($_POST['return'])) {
497 logger('return: ' . $_POST['return']);
498 goaway($a->get_baseurl() . "/" . $_POST['return'] );
500 $json = array('success' => 1);
501 if(x($_POST,'jsreload') && strlen($_POST['jsreload']))
502 $json['reload'] = $a->get_baseurl() . '/' . $_POST['jsreload'];
504 logger('post_json: ' . print_r($json,true), LOGGER_DEBUG);
506 echo json_encode($json);
515 function item_content(&$a) {
517 if((! local_user()) && (! remote_user()))
520 require_once('include/security.php');
522 $uid = $_SESSION['uid'];
524 if(($a->argc == 3) && ($a->argv[1] === 'drop') && intval($a->argv[2])) {
526 // locate item to be deleted
528 $r = q("SELECT * FROM `item` WHERE `id` = %d LIMIT 1",
533 notice( t('Item not found.') . EOL);
534 goaway($a->get_baseurl() . '/' . $_SESSION['return_url']);
538 // check if logged in user is either the author or owner of this item
540 if(($_SESSION['visitor_id'] == $item['contact-id']) || ($_SESSION['uid'] == $item['uid'])) {
544 $r = q("UPDATE `item` SET `deleted` = 1, `body` = '', `edited` = '%s', `changed` = '%s' WHERE `id` = %d LIMIT 1",
545 dbesc(datetime_convert()),
546 dbesc(datetime_convert()),
550 // If item is a link to a photo resource, nuke all the associated photos
551 // (visitors will not have photo resources)
552 // This only applies to photos uploaded from the photos page. Photos inserted into a post do not
553 // generate a resource-id and therefore aren't intimately linked to the item.
555 if(strlen($item['resource-id'])) {
556 q("DELETE FROM `photo` WHERE `resource-id` = '%s' AND `uid` = %d ",
557 dbesc($item['resource-id']),
563 // If it's the parent of a comment thread, kill all the kids
565 if($item['uri'] == $item['parent-uri']) {
566 $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s', `body` = ''
567 WHERE `parent-uri` = '%s' AND `uid` = %d ",
568 dbesc(datetime_convert()),
569 dbesc(datetime_convert()),
570 dbesc($item['parent-uri']),
576 // ensure that last-child is set in case the comment that had it just got wiped.
577 q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d ",
578 dbesc(datetime_convert()),
579 dbesc($item['parent-uri']),
582 // who is the last child now?
583 $r = q("SELECT `id` FROM `item` WHERE `parent-uri` = '%s' AND `type` != 'activity' AND `deleted` = 0 AND `uid` = %d ORDER BY `edited` DESC LIMIT 1",
584 dbesc($item['parent-uri']),
588 q("UPDATE `item` SET `last-child` = 1 WHERE `id` = %d LIMIT 1",
593 $drop_id = intval($item['id']);
595 // send the notification upstream/downstream as the case may be
597 proc_run('php',"include/notifier.php","drop","$drop_id");
599 goaway($a->get_baseurl() . '/' . $_SESSION['return_url']);
603 notice( t('Permission denied.') . EOL);
604 goaway($a->get_baseurl() . '/' . $_SESSION['return_url']);