3 // This is the POST destination for most all locally posted
4 // text stuff. This function handles status, wall-to-wall status,
5 // local comments, and remote coments - that are posted on this site
6 // (as opposed to being delivered in a feed).
7 // All of these become an "item" which is our basic unit of
10 function item_post(&$a) {
12 if((! local_user()) && (! remote_user()))
15 require_once('include/security.php');
19 $parent = ((x($_POST,'parent')) ? intval($_POST['parent']) : 0);
24 $r = q("SELECT * FROM `item` WHERE `id` = %d LIMIT 1",
28 notice( t('Unable to locate original post.') . EOL);
29 goaway($a->get_baseurl() . "/" . $_POST['return'] );
34 $profile_uid = ((x($_POST,'profile_uid')) ? intval($_POST['profile_uid']) : 0);
37 if(! can_write_wall($a,$profile_uid)) {
38 notice( t('Permission denied.') . EOL) ;
44 $r = q("SELECT * FROM `user` WHERE `uid` = %d LIMIT 1",
51 $str_group_allow = perms2str($_POST['group_allow']);
52 $str_contact_allow = perms2str($_POST['contact_allow']);
53 $str_group_deny = perms2str($_POST['group_deny']);
54 $str_contact_deny = perms2str($_POST['contact_deny']);
56 $private = ((strlen($str_group_allow) || strlen($str_contact_allow) || strlen($str_group_deny) || strlen($str_contact_deny)) ? 1 : 0);
59 (($parent_item['private'])
60 || strlen($parent_item['allow_cid'])
61 || strlen($parent_item['allow_gid'])
62 || strlen($parent_item['deny_cid'])
63 || strlen($parent_item['deny_gid'])
69 $title = notags(trim($_POST['title']));
70 $body = escape_tags(trim($_POST['body']));
71 $location = notags(trim($_POST['location']));
72 $coord = notags(trim($_POST['coord']));
73 $verb = notags(trim($_POST['verb']));
76 notice( t('Empty post discarded.') . EOL );
77 goaway($a->get_baseurl() . "/" . $_POST['return'] );
81 // get contact info for poster
86 if(($_SESSION['uid']) && ($_SESSION['uid'] == $profile_uid)) {
88 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 1 LIMIT 1",
89 intval($_SESSION['uid'])
93 if((x($_SESSION,'visitor_id')) && (intval($_SESSION['visitor_id']))) {
94 $r = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
95 intval($_SESSION['visitor_id'])
102 $contact_id = $author['id'];
105 // get contact info for owner
107 if($profile_uid == $_SESSION['uid']) {
108 $contact_record = $author;
111 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 1 LIMIT 1",
115 $contact_record = $r[0];
118 $post_type = notags(trim($_POST['type']));
120 if($post_type === 'net-comment') {
121 if($parent_item !== null) {
122 if($parent_item['type'] === 'remote') {
123 $post_type = 'remote-comment';
126 $post_type = 'wall-comment';
134 * When a photo was uploaded into the message using the (profile wall) ajax
135 * uploader, The permissions are initially set to disallow anybody but the
136 * owner from seeing it. This is because the permissions may not yet have been
137 * set for the post. If it's private, the photo permissions should be set
138 * appropriately. But we didn't know the final permissions on the post until
139 * now. So now we'll look for links of uploaded messages that are in the
140 * post and set them to the same permissions as the post itself.
146 if(preg_match_all("/\[img\](.+?)\[\/img\]/",$body,$match)) {
149 foreach($images as $image) {
150 if(! stristr($image,$a->get_baseurl() . '/photo/'))
152 $image_uri = substr($image,strrpos($image,'/') + 1);
153 $image_uri = substr($image_uri,0, strpos($image_uri,'-'));
154 $r = q("UPDATE `photo` SET `allow_cid` = '%s', `allow_gid` = '%s', `deny_cid` = '%s', `deny_gid` = '%s'
155 WHERE `resource-id` = '%s' AND `album` = '%s' ",
156 dbesc($str_contact_allow),
157 dbesc($str_group_allow),
158 dbesc($str_contact_deny),
159 dbesc($str_group_deny),
161 dbesc( t('Wall Photos'))
169 * Fold multi-line [code] sequences
172 $body = preg_replace('/\[\/code\]\s*\[code\]/m',"\n",$body);
175 * Look for any tags and linkify them
181 $tags = get_tags($body);
185 foreach($tags as $tag) {
186 if(strpos($tag,'#') === 0) {
187 $basetag = str_replace('_',' ',substr($tag,1));
188 $body = str_replace($tag,'#[url=' . $a->get_baseurl() . '/search?search=' . rawurlencode($basetag) . ']' . $basetag . '[/url]',$body);
189 if(strlen($str_tags))
191 $str_tags .= '#[url=' . $a->get_baseurl() . '/search?search=' . rawurlencode($basetag) . ']' . $basetag . '[/url]';
194 if(strpos($tag,'@') === 0) {
195 $name = substr($tag,1);
196 if((strpos($name,'@')) || (strpos($name,'http://'))) {
198 $links = @lrdd($name);
200 foreach($links as $link) {
201 if($link['@attributes']['rel'] === 'http://webfinger.net/rel/profile-page')
202 $profile = $link['@attributes']['href'];
203 if($link['@attributes']['rel'] === 'salmon') {
206 $inform .= 'url:' . str_replace(',','%2c',$link['@attributes']['href']);
213 if(strstr($name,'_')) {
214 $newname = str_replace('_',' ',$name);
215 $r = q("SELECT * FROM `contact` WHERE `name` = '%s' AND `uid` = %d LIMIT 1",
221 $r = q("SELECT * FROM `contact` WHERE `nick` = '%s' AND `uid` = %d LIMIT 1",
227 $profile = $r[0]['url'];
228 $newname = $r[0]['name'];
231 $inform .= 'cid:' . $r[0]['id'];
235 $body = str_replace('@' . $name, '@' . '[url=' . $profile . ']' . $newname . '[/url]', $body);
236 $profile = str_replace(',','%2c',$profile);
237 if(strlen($str_tags))
239 $str_tags .= '@[url=' . $profile . ']' . $newname . '[/url]';
246 if($post_type === 'wall' || $post_type === 'wall-comment')
250 $verb = ACTIVITY_POST ;
252 $gravity = (($parent) ? 6 : 0 );
254 $notify_type = (($parent) ? 'comment-new' : 'wall-new' );
256 $uri = item_new_uri($a->get_hostname(),$profile_uid);
259 $datarray['uid'] = $profile_uid;
260 $datarray['type'] = $post_type;
261 $datarray['wall'] = $wall;
262 $datarray['gravity'] = $gravity;
263 $datarray['contact-id'] = $contact_id;
264 $datarray['owner-name'] = $contact_record['name'];
265 $datarray['owner-link'] = $contact_record['url'];
266 $datarray['owner-avatar'] = $contact_record['thumb'];
267 $datarray['author-name'] = $author['name'];
268 $datarray['author-link'] = $author['url'];
269 $datarray['author-avatar'] = $author['thumb'];
270 $datarray['created'] = datetime_convert();
271 $datarray['edited'] = datetime_convert();
272 $datarray['changed'] = datetime_convert();
273 $datarray['uri'] = $uri;
274 $datarray['title'] = $title;
275 $datarray['body'] = $body;
276 $datarray['location'] = $location;
277 $datarray['coord'] = $coord;
278 $datarray['tag'] = $str_tags;
279 $datarray['inform'] = $inform;
280 $datarray['verb'] = $verb;
281 $datarray['allow_cid'] = $str_contact_allow;
282 $datarray['allow_gid'] = $str_group_allow;
283 $datarray['deny_cid'] = $str_contact_deny;
284 $datarray['deny_gid'] = $str_group_deny;
285 $datarray['private'] = $private;
288 * These fields are for the convenience of plugins...
289 * 'self' if true indicates the owner is posting on their own wall
290 * If parent is 0 it is a top-level post.
293 $datarray['parent'] = $parent;
294 $datarray['self'] = $self;
297 call_hooks('post_local',$datarray);
299 $r = q("INSERT INTO `item` (`uid`,`type`,`wall`,`gravity`,`contact-id`,`owner-name`,`owner-link`,`owner-avatar`,
300 `author-name`, `author-link`, `author-avatar`, `created`, `edited`, `changed`, `uri`, `title`, `body`, `location`, `coord`,
301 `tag`, `inform`, `verb`, `allow_cid`, `allow_gid`, `deny_cid`, `deny_gid`, `private` )
302 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 )",
303 intval($datarray['uid']),
304 dbesc($datarray['type']),
305 intval($datarray['wall']),
306 intval($datarray['gravity']),
307 intval($datarray['contact-id']),
308 dbesc($datarray['owner-name']),
309 dbesc($datarray['owner-link']),
310 dbesc($datarray['owner-avatar']),
311 dbesc($datarray['author-name']),
312 dbesc($datarray['author-link']),
313 dbesc($datarray['author-avatar']),
314 dbesc($datarray['created']),
315 dbesc($datarray['edited']),
316 dbesc($datarray['changed']),
317 dbesc($datarray['uri']),
318 dbesc($datarray['title']),
319 dbesc($datarray['body']),
320 dbesc($datarray['location']),
321 dbesc($datarray['coord']),
322 dbesc($datarray['tag']),
323 dbesc($datarray['inform']),
324 dbesc($datarray['verb']),
325 dbesc($datarray['allow_cid']),
326 dbesc($datarray['allow_gid']),
327 dbesc($datarray['deny_cid']),
328 dbesc($datarray['deny_gid']),
329 intval($datarray['private'])
332 $r = q("SELECT `id` FROM `item` WHERE `uri` = '%s' LIMIT 1",
333 dbesc($datarray['uri']));
335 $post_id = $r[0]['id'];
336 logger('mod_item: saved item ' . $post_id);
340 // This item is the last leaf and gets the comment box, clear any ancestors
341 $r = q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent` = %d ",
342 dbesc(datetime_convert()),
346 // Inherit ACL's from the parent item.
348 $r = q("UPDATE `item` SET `allow_cid` = '%s', `allow_gid` = '%s', `deny_cid` = '%s', `deny_gid` = '%s', `private` = %d
349 WHERE `id` = %d LIMIT 1",
350 dbesc($parent_item['allow_cid']),
351 dbesc($parent_item['allow_gid']),
352 dbesc($parent_item['deny_cid']),
353 dbesc($parent_item['deny_gid']),
354 intval($parent_item['private']),
358 // Send a notification email to the conversation owner, unless the owner is me and I wrote this item
359 if(($user['notify-flags'] & NOTIFY_COMMENT) && ($contact_record != $author)) {
360 require_once('bbcode.php');
361 $from = $author['name'];
362 $tpl = load_view_file('view/cmnt_received_eml.tpl');
363 $email_tpl = replace_macros($tpl, array(
364 '$sitename' => $a->config['sitename'],
365 '$siteurl' => $a->get_baseurl(),
366 '$username' => $user['username'],
367 '$email' => $user['email'],
369 '$display' => $a->get_baseurl() . '/display/' . $user['nickname'] . '/' . $post_id,
370 '$body' => strip_tags(bbcode($datarray['body']))
373 $res = mail($user['email'], $from . t(" commented on your item at ") . $a->config['sitename'],
374 $email_tpl,t("From: Administrator@") . $a->get_hostname() );
380 // let me know if somebody did a wall-to-wall post on my profile
382 if(($user['notify-flags'] & NOTIFY_WALL) && ($contact_record != $author)) {
383 require_once('bbcode.php');
384 $from = $author['name'];
385 $tpl = load_view_file('view/wall_received_eml.tpl');
386 $email_tpl = replace_macros($tpl, array(
387 '$sitename' => $a->config['sitename'],
388 '$siteurl' => $a->get_baseurl(),
389 '$username' => $user['username'],
390 '$email' => $user['email'],
392 '$display' => $a->get_baseurl() . '/display/' . $user['nickname'] . '/' . $post_id,
393 '$body' => strip_tags(bbcode($datarray['body']))
396 $res = mail($user['email'], $from . t(" posted on your profile wall at ") . $a->config['sitename'],
397 $email_tpl,t("From: Administrator@") . $a->get_hostname() );
401 $r = q("UPDATE `item` SET `parent` = %d, `parent-uri` = '%s', `changed` = '%s', `last-child` = 1, `visible` = 1
402 WHERE `id` = %d LIMIT 1",
404 dbesc(($parent == $post_id) ? $uri : $parent_item['uri']),
405 dbesc(datetime_convert()),
409 // photo comments turn the corresponding item visible to the profile wall
410 // This way we don't see every picture in your new photo album posted to your wall at once.
411 // They will show up as people comment on them.
413 if(! $parent_item['visible']) {
414 $r = q("UPDATE `item` SET `visible` = 1 WHERE `id` = %d LIMIT 1",
415 intval($parent_item['id'])
420 $php_path = ((strlen($a->config['php_path'])) ? $a->config['php_path'] : 'php');
422 logger('mod_item: notifier invoked: ' . "\"$php_path\" \"include/notifier.php\" \"$notify_type\" \"$post_id\" &");
424 proc_close(proc_open("\"$php_path\" \"include/notifier.php\" \"$notify_type\" \"$post_id\" &",
427 $datarray['id'] = $post_id;
429 call_hooks('post_local_end', $datarray);
432 goaway($a->get_baseurl() . "/" . $_POST['return'] );
433 return; // NOTREACHED
440 function item_content(&$a) {
442 if((! local_user()) && (! remote_user()))
445 require_once('include/security.php');
447 $uid = $_SESSION['uid'];
449 if(($a->argc == 3) && ($a->argv[1] === 'drop') && intval($a->argv[2])) {
451 // locate item to be deleted
453 $r = q("SELECT * FROM `item` WHERE `id` = %d LIMIT 1",
458 notice( t('Item not found.') . EOL);
459 goaway($a->get_baseurl() . '/' . $_SESSION['return_url']);
463 // check if logged in user is either the author or owner of this item
465 if(($_SESSION['visitor_id'] == $item['contact-id']) || ($_SESSION['uid'] == $item['uid'])) {
469 $r = q("UPDATE `item` SET `deleted` = 1, `body` = '', `edited` = '%s', `changed` = '%s' WHERE `id` = %d LIMIT 1",
470 dbesc(datetime_convert()),
471 dbesc(datetime_convert()),
475 // If item is a link to a photo resource, nuke all the associated photos
476 // (visitors will not have photo resources)
477 // This only applies to photos uploaded from the photos page. Photos inserted into a post do not
478 // generate a resource-id and therefore aren't intimately linked to the item.
480 if(strlen($item['resource-id'])) {
481 $q("DELETE FROM `photo` WHERE `resource-id` = '%s' AND `uid` = %d ",
482 dbesc($item['resource-id']),
488 // If it's the parent of a comment thread, kill all the kids
490 if($item['uri'] == $item['parent-uri']) {
491 $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s', `body` = ''
492 WHERE `parent-uri` = '%s' AND `uid` = %d ",
493 dbesc(datetime_convert()),
494 dbesc(datetime_convert()),
495 dbesc($item['parent-uri']),
501 // ensure that last-child is set in case the comment that had it just got wiped.
502 q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d ",
503 dbesc(datetime_convert()),
504 dbesc($item['parent-uri']),
507 // who is the last child now?
508 $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",
509 dbesc($item['parent-uri']),
513 q("UPDATE `item` SET `last-child` = 1 WHERE `id` = %d LIMIT 1",
518 $drop_id = intval($item['id']);
519 $php_path = ((strlen($a->config['php_path'])) ? $a->config['php_path'] : 'php');
521 // send the notification upstream/downstream as the case may be
523 proc_close(proc_open("\"$php_path\" \"include/notifier.php\" \"drop\" \"$drop_id\" &",
526 goaway($a->get_baseurl() . '/' . $_SESSION['return_url']);
530 notice( t('Permission denied.') . EOL);
531 goaway($a->get_baseurl() . '/' . $_SESSION['return_url']);