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 $parent = ((x($_POST,'parent')) ? intval($_POST['parent']) : 0);
30 $r = q("SELECT * FROM `item` WHERE `id` = %d LIMIT 1",
34 notice( t('Unable to locate original post.') . EOL);
35 goaway($a->get_baseurl() . "/" . $_POST['return'] );
40 $profile_uid = ((x($_POST,'profile_uid')) ? intval($_POST['profile_uid']) : 0);
43 if(! can_write_wall($a,$profile_uid)) {
44 notice( t('Permission denied.') . EOL) ;
50 $r = q("SELECT * FROM `user` WHERE `uid` = %d LIMIT 1",
57 $str_group_allow = perms2str($_POST['group_allow']);
58 $str_contact_allow = perms2str($_POST['contact_allow']);
59 $str_group_deny = perms2str($_POST['group_deny']);
60 $str_contact_deny = perms2str($_POST['contact_deny']);
62 $private = ((strlen($str_group_allow) || strlen($str_contact_allow) || strlen($str_group_deny) || strlen($str_contact_deny)) ? 1 : 0);
65 (($parent_item['private'])
66 || strlen($parent_item['allow_cid'])
67 || strlen($parent_item['allow_gid'])
68 || strlen($parent_item['deny_cid'])
69 || strlen($parent_item['deny_gid'])
75 $title = notags(trim($_POST['title']));
76 $body = escape_tags(trim($_POST['body']));
77 $location = notags(trim($_POST['location']));
78 $coord = notags(trim($_POST['coord']));
79 $verb = notags(trim($_POST['verb']));
80 $emailcc = notags(trim($_POST['emailcc']));
83 notice( t('Empty post discarded.') . EOL );
84 goaway($a->get_baseurl() . "/" . $_POST['return'] );
88 // get contact info for poster
93 if(($_SESSION['uid']) && ($_SESSION['uid'] == $profile_uid)) {
95 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 1 LIMIT 1",
96 intval($_SESSION['uid'])
100 if((x($_SESSION,'visitor_id')) && (intval($_SESSION['visitor_id']))) {
101 $r = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
102 intval($_SESSION['visitor_id'])
109 $contact_id = $author['id'];
112 // get contact info for owner
114 if($profile_uid == $_SESSION['uid']) {
115 $contact_record = $author;
118 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 1 LIMIT 1",
122 $contact_record = $r[0];
125 $post_type = notags(trim($_POST['type']));
127 if($post_type === 'net-comment') {
128 if($parent_item !== null) {
129 if($parent_item['type'] === 'remote') {
130 $post_type = 'remote-comment';
133 $post_type = 'wall-comment';
141 * When a photo was uploaded into the message using the (profile wall) ajax
142 * uploader, The permissions are initially set to disallow anybody but the
143 * owner from seeing it. This is because the permissions may not yet have been
144 * set for the post. If it's private, the photo permissions should be set
145 * appropriately. But we didn't know the final permissions on the post until
146 * now. So now we'll look for links of uploaded messages that are in the
147 * post and set them to the same permissions as the post itself.
153 if(preg_match_all("/\[img\](.+?)\[\/img\]/",$body,$match)) {
156 foreach($images as $image) {
157 if(! stristr($image,$a->get_baseurl() . '/photo/'))
159 $image_uri = substr($image,strrpos($image,'/') + 1);
160 $image_uri = substr($image_uri,0, strpos($image_uri,'-'));
161 $r = q("UPDATE `photo` SET `allow_cid` = '%s', `allow_gid` = '%s', `deny_cid` = '%s', `deny_gid` = '%s'
162 WHERE `resource-id` = '%s' AND `album` = '%s' ",
163 dbesc($str_contact_allow),
164 dbesc($str_group_allow),
165 dbesc($str_contact_deny),
166 dbesc($str_group_deny),
168 dbesc( t('Wall Photos'))
176 * Fold multi-line [code] sequences
179 $body = preg_replace('/\[\/code\]\s*\[code\]/m',"\n",$body);
182 * Look for any tags and linkify them
188 $tags = get_tags($body);
192 foreach($tags as $tag) {
193 if(strpos($tag,'#') === 0) {
194 $basetag = str_replace('_',' ',substr($tag,1));
195 $body = str_replace($tag,'#[url=' . $a->get_baseurl() . '/search?search=' . rawurlencode($basetag) . ']' . $basetag . '[/url]',$body);
196 if(strlen($str_tags))
198 $str_tags .= '#[url=' . $a->get_baseurl() . '/search?search=' . rawurlencode($basetag) . ']' . $basetag . '[/url]';
201 if(strpos($tag,'@') === 0) {
202 $name = substr($tag,1);
203 if((strpos($name,'@')) || (strpos($name,'http://'))) {
205 $links = @lrdd($name);
207 foreach($links as $link) {
208 if($link['@attributes']['rel'] === 'http://webfinger.net/rel/profile-page')
209 $profile = $link['@attributes']['href'];
210 if($link['@attributes']['rel'] === 'salmon') {
213 $inform .= 'url:' . str_replace(',','%2c',$link['@attributes']['href']);
220 if(strstr($name,'_')) {
221 $newname = str_replace('_',' ',$name);
222 $r = q("SELECT * FROM `contact` WHERE `name` = '%s' AND `uid` = %d LIMIT 1",
228 $r = q("SELECT * FROM `contact` WHERE `nick` = '%s' AND `uid` = %d LIMIT 1",
234 $profile = $r[0]['url'];
235 $newname = $r[0]['name'];
238 $inform .= 'cid:' . $r[0]['id'];
242 $body = str_replace('@' . $name, '@' . '[url=' . $profile . ']' . $newname . '[/url]', $body);
243 $profile = str_replace(',','%2c',$profile);
244 if(strlen($str_tags))
246 $str_tags .= '@[url=' . $profile . ']' . $newname . '[/url]';
253 if($post_type === 'wall' || $post_type === 'wall-comment')
257 $verb = ACTIVITY_POST ;
259 $gravity = (($parent) ? 6 : 0 );
261 $notify_type = (($parent) ? 'comment-new' : 'wall-new' );
263 $uri = item_new_uri($a->get_hostname(),$profile_uid);
266 $datarray['uid'] = $profile_uid;
267 $datarray['type'] = $post_type;
268 $datarray['wall'] = $wall;
269 $datarray['gravity'] = $gravity;
270 $datarray['contact-id'] = $contact_id;
271 $datarray['owner-name'] = $contact_record['name'];
272 $datarray['owner-link'] = $contact_record['url'];
273 $datarray['owner-avatar'] = $contact_record['thumb'];
274 $datarray['author-name'] = $author['name'];
275 $datarray['author-link'] = $author['url'];
276 $datarray['author-avatar'] = $author['thumb'];
277 $datarray['created'] = datetime_convert();
278 $datarray['edited'] = datetime_convert();
279 $datarray['changed'] = datetime_convert();
280 $datarray['uri'] = $uri;
281 $datarray['title'] = $title;
282 $datarray['body'] = $body;
283 $datarray['location'] = $location;
284 $datarray['coord'] = $coord;
285 $datarray['tag'] = $str_tags;
286 $datarray['inform'] = $inform;
287 $datarray['verb'] = $verb;
288 $datarray['allow_cid'] = $str_contact_allow;
289 $datarray['allow_gid'] = $str_group_allow;
290 $datarray['deny_cid'] = $str_contact_deny;
291 $datarray['deny_gid'] = $str_group_deny;
292 $datarray['private'] = $private;
295 * These fields are for the convenience of plugins...
296 * 'self' if true indicates the owner is posting on their own wall
297 * If parent is 0 it is a top-level post.
300 $datarray['parent'] = $parent;
301 $datarray['self'] = $self;
304 call_hooks('post_local',$datarray);
306 $r = q("INSERT INTO `item` (`uid`,`type`,`wall`,`gravity`,`contact-id`,`owner-name`,`owner-link`,`owner-avatar`,
307 `author-name`, `author-link`, `author-avatar`, `created`, `edited`, `changed`, `uri`, `title`, `body`, `location`, `coord`,
308 `tag`, `inform`, `verb`, `allow_cid`, `allow_gid`, `deny_cid`, `deny_gid`, `private` )
309 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 )",
310 intval($datarray['uid']),
311 dbesc($datarray['type']),
312 intval($datarray['wall']),
313 intval($datarray['gravity']),
314 intval($datarray['contact-id']),
315 dbesc($datarray['owner-name']),
316 dbesc($datarray['owner-link']),
317 dbesc($datarray['owner-avatar']),
318 dbesc($datarray['author-name']),
319 dbesc($datarray['author-link']),
320 dbesc($datarray['author-avatar']),
321 dbesc($datarray['created']),
322 dbesc($datarray['edited']),
323 dbesc($datarray['changed']),
324 dbesc($datarray['uri']),
325 dbesc($datarray['title']),
326 dbesc($datarray['body']),
327 dbesc($datarray['location']),
328 dbesc($datarray['coord']),
329 dbesc($datarray['tag']),
330 dbesc($datarray['inform']),
331 dbesc($datarray['verb']),
332 dbesc($datarray['allow_cid']),
333 dbesc($datarray['allow_gid']),
334 dbesc($datarray['deny_cid']),
335 dbesc($datarray['deny_gid']),
336 intval($datarray['private'])
339 $r = q("SELECT `id` FROM `item` WHERE `uri` = '%s' LIMIT 1",
340 dbesc($datarray['uri']));
342 $post_id = $r[0]['id'];
343 logger('mod_item: saved item ' . $post_id);
347 // This item is the last leaf and gets the comment box, clear any ancestors
348 $r = q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent` = %d ",
349 dbesc(datetime_convert()),
353 // Inherit ACL's from the parent item.
355 $r = q("UPDATE `item` SET `allow_cid` = '%s', `allow_gid` = '%s', `deny_cid` = '%s', `deny_gid` = '%s', `private` = %d
356 WHERE `id` = %d LIMIT 1",
357 dbesc($parent_item['allow_cid']),
358 dbesc($parent_item['allow_gid']),
359 dbesc($parent_item['deny_cid']),
360 dbesc($parent_item['deny_gid']),
361 intval($parent_item['private']),
365 // Send a notification email to the conversation owner, unless the owner is me and I wrote this item
366 if(($user['notify-flags'] & NOTIFY_COMMENT) && ($contact_record != $author)) {
367 require_once('bbcode.php');
368 $from = $author['name'];
369 $tpl = load_view_file('view/cmnt_received_eml.tpl');
370 $email_tpl = replace_macros($tpl, array(
371 '$sitename' => $a->config['sitename'],
372 '$siteurl' => $a->get_baseurl(),
373 '$username' => $user['username'],
374 '$email' => $user['email'],
376 '$display' => $a->get_baseurl() . '/display/' . $user['nickname'] . '/' . $post_id,
377 '$body' => strip_tags(bbcode($datarray['body']))
380 $res = mail($user['email'], $from . t(" commented on your item at ") . $a->config['sitename'],
381 $email_tpl,t("From: Administrator@") . $a->get_hostname() );
387 // let me know if somebody did a wall-to-wall post on my profile
389 if(($user['notify-flags'] & NOTIFY_WALL) && ($contact_record != $author)) {
390 require_once('bbcode.php');
391 $from = $author['name'];
392 $tpl = load_view_file('view/wall_received_eml.tpl');
393 $email_tpl = replace_macros($tpl, array(
394 '$sitename' => $a->config['sitename'],
395 '$siteurl' => $a->get_baseurl(),
396 '$username' => $user['username'],
397 '$email' => $user['email'],
399 '$display' => $a->get_baseurl() . '/display/' . $user['nickname'] . '/' . $post_id,
400 '$body' => strip_tags(bbcode($datarray['body']))
403 $res = mail($user['email'], $from . t(" posted on your profile wall at ") . $a->config['sitename'],
404 $email_tpl,t("From: Administrator@") . $a->get_hostname() );
408 $r = q("UPDATE `item` SET `parent` = %d, `parent-uri` = '%s', `changed` = '%s', `last-child` = 1, `visible` = 1
409 WHERE `id` = %d LIMIT 1",
411 dbesc(($parent == $post_id) ? $uri : $parent_item['uri']),
412 dbesc(datetime_convert()),
416 // photo comments turn the corresponding item visible to the profile wall
417 // This way we don't see every picture in your new photo album posted to your wall at once.
418 // They will show up as people comment on them.
420 if(! $parent_item['visible']) {
421 $r = q("UPDATE `item` SET `visible` = 1 WHERE `id` = %d LIMIT 1",
422 intval($parent_item['id'])
427 logger('mod_item: unable to retrieve post that was just stored.');
428 notify( t('System error. Post not saved.'));
429 goaway($a->get_baseurl() . "/" . $_POST['return'] );
433 $php_path = ((strlen($a->config['php_path'])) ? $a->config['php_path'] : 'php');
435 logger('mod_item: notifier invoked: ' . "\"$php_path\" \"include/notifier.php\" \"$notify_type\" \"$post_id\" &");
437 proc_run($php_path, "include/notifier.php", $notify_type, "$post_id");
439 $datarray['id'] = $post_id;
441 call_hooks('post_local_end', $datarray);
443 if(strlen($emailcc) && $profile_uid == local_user()) {
444 $erecips = explode(',', $emailcc);
445 if(count($erecips)) {
446 foreach($erecips as $recip) {
447 $addr = trim($recip);
450 $disclaimer = '<hr />' . t('This message was sent to you by ') . $a->user['username']
451 . t(', a member of the Friendika social network.') . '<br />';
452 $disclaimer .= t('You may visit them online at') . ' '
453 . $a->get_baseurl() . '/profile/' . $a->user['nickname'] . '<br />';
454 $disclaimer .= t('Please contact the sender by replying to this post if you do not wish to receive these messages.') . '<br />';
456 $subject = '[Friendika]' . ' ' . $a->user['username'] . ' ' . t('posted an update.');
457 $headers = 'From: ' . $a->user['username'] . ' <' . $a->user['email'] . '>' . "\n";
458 $headers .= 'MIME-Version: 1.0' . "\n";
459 $headers .= 'Content-Type: text/html; charset=UTF-8' . "\n";
460 $headers .= 'Content-Transfer-Encoding: 8bit' . "\n\n";
461 $link = '<a href="' . $a->get_baseurl() . '/profile/' . $a->user['nickname'] . '"><img src="' . $author['thumb'] . '" alt="' . $a->user['username'] . '" /></a><br /><br />';
462 $html = prepare_body($datarray);
463 $message = '<html><body>' . $link . $html . $disclaimer . '</body></html>';
464 @mail($addr, $subject, $message, $headers);
469 goaway($a->get_baseurl() . "/" . $_POST['return'] );
477 function item_content(&$a) {
479 if((! local_user()) && (! remote_user()))
482 require_once('include/security.php');
484 $uid = $_SESSION['uid'];
486 if(($a->argc == 3) && ($a->argv[1] === 'drop') && intval($a->argv[2])) {
488 // locate item to be deleted
490 $r = q("SELECT * FROM `item` WHERE `id` = %d LIMIT 1",
495 notice( t('Item not found.') . EOL);
496 goaway($a->get_baseurl() . '/' . $_SESSION['return_url']);
500 // check if logged in user is either the author or owner of this item
502 if(($_SESSION['visitor_id'] == $item['contact-id']) || ($_SESSION['uid'] == $item['uid'])) {
506 $r = q("UPDATE `item` SET `deleted` = 1, `body` = '', `edited` = '%s', `changed` = '%s' WHERE `id` = %d LIMIT 1",
507 dbesc(datetime_convert()),
508 dbesc(datetime_convert()),
512 // If item is a link to a photo resource, nuke all the associated photos
513 // (visitors will not have photo resources)
514 // This only applies to photos uploaded from the photos page. Photos inserted into a post do not
515 // generate a resource-id and therefore aren't intimately linked to the item.
517 if(strlen($item['resource-id'])) {
518 $q("DELETE FROM `photo` WHERE `resource-id` = '%s' AND `uid` = %d ",
519 dbesc($item['resource-id']),
525 // If it's the parent of a comment thread, kill all the kids
527 if($item['uri'] == $item['parent-uri']) {
528 $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s', `body` = ''
529 WHERE `parent-uri` = '%s' AND `uid` = %d ",
530 dbesc(datetime_convert()),
531 dbesc(datetime_convert()),
532 dbesc($item['parent-uri']),
538 // ensure that last-child is set in case the comment that had it just got wiped.
539 q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d ",
540 dbesc(datetime_convert()),
541 dbesc($item['parent-uri']),
544 // who is the last child now?
545 $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",
546 dbesc($item['parent-uri']),
550 q("UPDATE `item` SET `last-child` = 1 WHERE `id` = %d LIMIT 1",
555 $drop_id = intval($item['id']);
556 $php_path = ((strlen($a->config['php_path'])) ? $a->config['php_path'] : 'php');
558 // send the notification upstream/downstream as the case may be
560 //proc_close(proc_open("\"$php_path\" \"include/notifier.php\" \"drop\" \"$drop_id\" &", array(), $foo));
561 proc_run($php_path,"include/notifier.php","drop","$drop_id");
563 goaway($a->get_baseurl() . '/' . $_SESSION['return_url']);
567 notice( t('Permission denied.') . EOL);
568 goaway($a->get_baseurl() . '/' . $_SESSION['return_url']);