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 * If a photo was uploaded into the message using the ajax uploader,
135 * it can be seen by anybody. Set the permissions to match the message.
136 * Ideally this should be done when the photo was uploaded, but the permissions
137 * may not have been set at that time, and passing the permission arrays via
138 * javascript to the ajax upload is going to be a challenge.
139 * This is a compromise. Granted there is a window of time when the photo
140 * is public. You are welcome to suggest other ways to fix this.
147 if(preg_match_all("/\[img\](.+?)\[\/img\]/",$body,$match)) {
150 foreach($images as $image) {
151 if(! stristr($image,$a->get_baseurl() . '/photo/'))
153 $image_uri = substr($image,strrpos($image,'/') + 1);
154 $image_uri = substr($image_uri,0, strpos($image_uri,'-'));
155 $r = q("UPDATE `photo` SET `allow_cid` = '%s', `allow_gid` = '%s', `deny_cid` = '%s', `deny_gid` = '%s'
156 WHERE `resource-id` = '%s' AND `album` = '%s' ",
157 dbesc($str_contact_allow),
158 dbesc($str_group_allow),
159 dbesc($str_contact_deny),
160 dbesc($str_group_deny),
162 dbesc( t('Wall Photos'))
172 * Look for any tags and linkify them
178 $tags = get_tags($body);
182 foreach($tags as $tag) {
183 if(strpos($tag,'#') === 0) {
184 $basetag = str_replace('_',' ',substr($tag,1));
185 $body = str_replace($tag,'#[url=' . $a->get_baseurl() . '/search?search=' . rawurlencode($basetag) . ']' . $basetag . '[/url]',$body);
186 if(strlen($str_tags))
188 $str_tags .= '#[url=' . $a->get_baseurl() . '/search?search=' . rawurlencode($basetag) . ']' . $basetag . '[/url]';
191 if(strpos($tag,'@') === 0) {
192 $name = substr($tag,1);
193 if((strpos($name,'@')) || (strpos($name,'http://'))) {
195 $links = @lrdd($name);
197 foreach($links as $link) {
198 if($link['@attributes']['rel'] === 'http://webfinger.net/rel/profile-page')
199 $profile = $link['@attributes']['href'];
200 if($link['@attributes']['rel'] === 'salmon') {
203 $inform .= 'url:' . str_replace(',','%2c',$link['@attributes']['href']);
210 if(strstr($name,'_')) {
211 $newname = str_replace('_',' ',$name);
212 $r = q("SELECT * FROM `contact` WHERE `name` = '%s' AND `uid` = %d LIMIT 1",
218 $r = q("SELECT * FROM `contact` WHERE `nick` = '%s' AND `uid` = %d LIMIT 1",
224 $profile = $r[0]['url'];
225 $newname = $r[0]['name'];
228 $inform .= 'cid:' . $r[0]['id'];
232 $body = str_replace($name,'[url=' . $profile . ']' . $newname . '[/url]', $body);
233 $profile = str_replace(',','%2c',$profile);
234 if(strlen($str_tags))
236 $str_tags .= '@[url=' . $profile . ']' . $newname . '[/url]';
243 if($post_type === 'wall' || $post_type === 'wall-comment')
247 $verb = ACTIVITY_POST ;
249 $gravity = (($parent) ? 6 : 0 );
251 $notify_type = (($parent) ? 'comment-new' : 'wall-new' );
253 $uri = item_new_uri($a->get_hostname(),$profile_uid);
256 $datarray['uid'] = $profile_uid;
257 $datarray['type'] = $post_type;
258 $datarray['wall'] = $wall;
259 $datarray['gravity'] = $gravity;
260 $datarray['contact-id'] = $contact_id;
261 $datarray['owner-name'] = $contact_record['name'];
262 $datarray['owner-link'] = $contact_record['url'];
263 $datarray['owner-avatar'] = $contact_record['thumb'];
264 $datarray['author-name'] = $author['name'];
265 $datarray['author-link'] = $author['url'];
266 $datarray['author-avatar'] = $author['thumb'];
267 $datarray['created'] = datetime_convert();
268 $datarray['edited'] = datetime_convert();
269 $datarray['changed'] = datetime_convert();
270 $datarray['uri'] = $uri;
271 $datarray['title'] = $title;
272 $datarray['body'] = $body;
273 $datarray['location'] = $location;
274 $datarray['coord'] = $coord;
275 $datarray['tag'] = $str_tags;
276 $datarray['inform'] = $inform;
277 $datarray['verb'] = $verb;
278 $datarray['allow_cid'] = $str_contact_allow;
279 $datarray['allow_gid'] = $str_group_allow;
280 $datarray['deny_cid'] = $str_contact_deny;
281 $datarray['deny_gid'] = $str_group_deny;
282 $datarray['private'] = $private;
285 * These fields are for the convenience of plugins...
286 * 'self' if true indicates the owner is posting on their own wall
287 * If parent is 0 it is a top-level post.
290 $datarray['parent'] = $parent;
291 $datarray['self'] = $self;
294 call_hooks('post_local',$datarray);
296 $r = q("INSERT INTO `item` (`uid`,`type`,`wall`,`gravity`,`contact-id`,`owner-name`,`owner-link`,`owner-avatar`,
297 `author-name`, `author-link`, `author-avatar`, `created`, `edited`, `changed`, `uri`, `title`, `body`, `location`, `coord`,
298 `tag`, `inform`, `verb`, `allow_cid`, `allow_gid`, `deny_cid`, `deny_gid`, `private` )
299 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 )",
300 intval($datarray['uid']),
301 dbesc($datarray['type']),
302 intval($datarray['wall']),
303 intval($datarray['gravity']),
304 intval($datarray['contact-id']),
305 dbesc($datarray['owner-name']),
306 dbesc($datarray['owner-link']),
307 dbesc($datarray['owner-avatar']),
308 dbesc($datarray['author-name']),
309 dbesc($datarray['author-link']),
310 dbesc($datarray['author-avatar']),
311 dbesc($datarray['created']),
312 dbesc($datarray['edited']),
313 dbesc($datarray['changed']),
314 dbesc($datarray['uri']),
315 dbesc($datarray['title']),
316 dbesc($datarray['body']),
317 dbesc($datarray['location']),
318 dbesc($datarray['coord']),
319 dbesc($datarray['tag']),
320 dbesc($datarray['inform']),
321 dbesc($datarray['verb']),
322 dbesc($datarray['allow_cid']),
323 dbesc($datarray['allow_gid']),
324 dbesc($datarray['deny_cid']),
325 dbesc($datarray['deny_gid']),
326 intval($datarray['private'])
329 $r = q("SELECT `id` FROM `item` WHERE `uri` = '%s' LIMIT 1",
330 dbesc($datarray['uri']));
332 $post_id = $r[0]['id'];
333 logger('mod_item: saved item ' . $post_id);
337 // This item is the last leaf and gets the comment box, clear any ancestors
338 $r = q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent` = %d ",
339 dbesc(datetime_convert()),
343 // Inherit ACL's from the parent item.
345 $r = q("UPDATE `item` SET `allow_cid` = '%s', `allow_gid` = '%s', `deny_cid` = '%s', `deny_gid` = '%s', `private` = %d
346 WHERE `id` = %d LIMIT 1",
347 dbesc($parent_item['allow_cid']),
348 dbesc($parent_item['allow_gid']),
349 dbesc($parent_item['deny_cid']),
350 dbesc($parent_item['deny_gid']),
351 intval($parent_item['private']),
355 // Send a notification email to the conversation owner, unless the owner is me and I wrote this item
356 if(($user['notify-flags'] & NOTIFY_COMMENT) && ($contact_record != $author)) {
357 require_once('bbcode.php');
358 $from = $author['name'];
359 $tpl = load_view_file('view/cmnt_received_eml.tpl');
360 $email_tpl = replace_macros($tpl, array(
361 '$sitename' => $a->config['sitename'],
362 '$siteurl' => $a->get_baseurl(),
363 '$username' => $user['username'],
364 '$email' => $user['email'],
366 '$display' => $a->get_baseurl() . '/display/' . $user['nickname'] . '/' . $post_id,
367 '$body' => strip_tags(bbcode($datarray['body']))
370 $res = mail($user['email'], $from . t(" commented on your item at ") . $a->config['sitename'],
371 $email_tpl,t("From: Administrator@") . $a->get_hostname() );
377 // let me know if somebody did a wall-to-wall post on my profile
379 if(($user['notify-flags'] & NOTIFY_WALL) && ($contact_record != $author)) {
380 require_once('bbcode.php');
381 $from = $author['name'];
382 $tpl = load_view_file('view/wall_received_eml.tpl');
383 $email_tpl = replace_macros($tpl, array(
384 '$sitename' => $a->config['sitename'],
385 '$siteurl' => $a->get_baseurl(),
386 '$username' => $user['username'],
387 '$email' => $user['email'],
389 '$display' => $a->get_baseurl() . '/display/' . $user['nickname'] . '/' . $post_id,
390 '$body' => strip_tags(bbcode($datarray['body']))
393 $res = mail($user['email'], $from . t(" posted on your profile wall at ") . $a->config['sitename'],
394 $email_tpl,t("From: Administrator@") . $a->get_hostname() );
398 $r = q("UPDATE `item` SET `parent` = %d, `parent-uri` = '%s', `changed` = '%s', `last-child` = 1, `visible` = 1
399 WHERE `id` = %d LIMIT 1",
401 dbesc(($parent == $post_id) ? $uri : $parent_item['uri']),
402 dbesc(datetime_convert()),
406 // photo comments turn the corresponding item visible to the profile wall
407 // This way we don't see every picture in your new photo album posted to your wall at once.
408 // They will show up as people comment on them.
410 if(! $parent_item['visible']) {
411 $r = q("UPDATE `item` SET `visible` = 1 WHERE `id` = %d LIMIT 1",
412 intval($parent_item['id'])
417 $php_path = ((strlen($a->config['php_path'])) ? $a->config['php_path'] : 'php');
419 logger('mod_item: notifier invoked: ' . "\"$php_path\" \"include/notifier.php\" \"$notify_type\" \"$post_id\" &");
421 proc_close(proc_open("\"$php_path\" \"include/notifier.php\" \"$notify_type\" \"$post_id\" &",
424 $datarray['id'] = $post_id;
426 call_hooks('post_local_end', $datarray);
429 goaway($a->get_baseurl() . "/" . $_POST['return'] );
430 return; // NOTREACHED
437 function item_content(&$a) {
439 if((! local_user()) && (! remote_user()))
442 require_once('include/security.php');
444 $uid = $_SESSION['uid'];
446 if(($a->argc == 3) && ($a->argv[1] === 'drop') && intval($a->argv[2])) {
448 // locate item to be deleted
450 $r = q("SELECT * FROM `item` WHERE `id` = %d LIMIT 1",
455 notice( t('Item not found.') . EOL);
456 goaway($a->get_baseurl() . '/' . $_SESSION['return_url']);
460 // check if logged in user is either the author or owner of this item
462 if(($_SESSION['visitor_id'] == $item['contact-id']) || ($_SESSION['uid'] == $item['uid'])) {
466 $r = q("UPDATE `item` SET `deleted` = 1, `body` = '', `edited` = '%s', `changed` = '%s' WHERE `id` = %d LIMIT 1",
467 dbesc(datetime_convert()),
468 dbesc(datetime_convert()),
472 // If item is a link to a photo resource, nuke all the associated photos
473 // (visitors will not have photo resources)
474 // This only applies to photos uploaded from the photos page. Photos inserted into a post do not
475 // generate a resource-id and therefore aren't intimately linked to the item.
477 if(strlen($item['resource-id'])) {
478 $q("DELETE FROM `photo` WHERE `resource-id` = '%s' AND `uid` = %d ",
479 dbesc($item['resource-id']),
485 // If it's the parent of a comment thread, kill all the kids
487 if($item['uri'] == $item['parent-uri']) {
488 $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s', `body` = ''
489 WHERE `parent-uri` = '%s' AND `uid` = %d ",
490 dbesc(datetime_convert()),
491 dbesc(datetime_convert()),
492 dbesc($item['parent-uri']),
498 // ensure that last-child is set in case the comment that had it just got wiped.
499 q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d ",
500 dbesc(datetime_convert()),
501 dbesc($item['parent-uri']),
504 // who is the last child now?
505 $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",
506 dbesc($item['parent-uri']),
510 q("UPDATE `item` SET `last-child` = 1 WHERE `id` = %d LIMIT 1",
515 $drop_id = intval($item['id']);
516 $php_path = ((strlen($a->config['php_path'])) ? $a->config['php_path'] : 'php');
518 // send the notification upstream/downstream as the case may be
520 proc_close(proc_open("\"$php_path\" \"include/notifier.php\" \"drop\" \"$drop_id\" &",
523 goaway($a->get_baseurl() . '/' . $_SESSION['return_url']);
527 notice( t('Permission denied.') . EOL);
528 goaway($a->get_baseurl() . '/' . $_SESSION['return_url']);