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 $title = notags(trim($_POST['title']));
57 $body = escape_tags(trim($_POST['body']));
58 $location = notags(trim($_POST['location']));
59 $coord = notags(trim($_POST['coord']));
60 $verb = notags(trim($_POST['verb']));
63 notice( t('Empty post discarded.') . EOL );
64 goaway($a->get_baseurl() . "/" . $_POST['return'] );
68 // get contact info for poster
72 if(($_SESSION['uid']) && ($_SESSION['uid'] == $profile_uid)) {
73 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 1 LIMIT 1",
74 intval($_SESSION['uid'])
78 if((x($_SESSION,'visitor_id')) && (intval($_SESSION['visitor_id']))) {
79 $r = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
80 intval($_SESSION['visitor_id'])
87 $contact_id = $author['id'];
90 // get contact info for owner
92 if($profile_uid == $_SESSION['uid']) {
93 $contact_record = $author;
96 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 1 LIMIT 1",
100 $contact_record = $r[0];
103 $post_type = notags(trim($_POST['type']));
105 if($post_type === 'net-comment') {
106 if($parent_item !== null) {
107 if($parent_item['type'] === 'remote') {
108 $post_type = 'remote-comment';
111 $post_type = 'wall-comment';
119 $tags = get_tags($body);
123 foreach($tags as $tag) {
124 if(strpos($tag,'#') === 0) {
125 $basetag = substr($tag,1);
126 $body = str_replace($tag,'#[url=' . $a->get_baseurl() . '/search?search=' . urlencode($basetag) . ']' . $basetag . '[/url]',$body);
127 if(strlen($str_tags))
129 $str_tags .= '#[url=' . $a->get_baseurl() . '/search?search=' . urlencode($basetag) . ']' . $basetag . '[/url]';
132 if(strpos($tag,'@') === 0) {
133 $name = substr($tag,1);
134 if((strpos($name,'@')) || (strpos($name,'http://'))) {
136 $links = @lrdd($name);
138 foreach($links as $link) {
139 if($link['@attributes']['rel'] === 'http://webfinger.net/rel/profile-page')
140 $profile = $link['@attributes']['href'];
141 if($link['@attributes']['rel'] === 'salmon') {
144 $inform .= 'url:' . str_replace(',','%2c',$link['@attributes']['href']);
151 if(strstr($name,'_')) {
152 $newname = str_replace('_',' ',$name);
153 $r = q("SELECT * FROM `contact` WHERE `name` = '%s' AND `uid` = %d LIMIT 1",
159 $r = q("SELECT * FROM `contact` WHERE `nick` = '%s' AND `uid` = %d LIMIT 1",
165 $profile = $r[0]['url'];
166 $newname = $r[0]['name'];
169 $inform .= 'cid:' . $r[0]['id'];
173 $body = str_replace($name,'[url=' . $profile . ']' . $newname . '[/url]', $body);
174 $profile = str_replace(',','%2c',$profile);
175 if(strlen($str_tags))
177 $str_tags .= '@[url=' . $profile . ']' . $newname . '[/url]';
184 if($post_type === 'wall' || $post_type === 'wall-comment')
188 $verb = ACTIVITY_POST ;
190 $gravity = (($parent) ? 6 : 0 );
192 $notify_type = (($parent) ? 'comment-new' : 'wall-new' );
194 $uri = item_new_uri($a->get_hostname(),$profile_uid);
196 $r = q("INSERT INTO `item` (`uid`,`type`,`wall`,`gravity`,`contact-id`,`owner-name`,`owner-link`,`owner-avatar`,
197 `author-name`, `author-link`, `author-avatar`, `created`, `edited`, `changed`, `uri`, `title`, `body`, `location`, `coord`,
198 `tag`, `inform`, `verb`, `allow_cid`, `allow_gid`, `deny_cid`, `deny_gid`)
199 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' )",
200 intval($profile_uid),
205 dbesc($contact_record['name']),
206 dbesc($contact_record['url']),
207 dbesc($contact_record['thumb']),
208 dbesc($author['name']),
209 dbesc($author['url']),
210 dbesc($author['thumb']),
211 dbesc(datetime_convert()),
212 dbesc(datetime_convert()),
213 dbesc(datetime_convert()),
222 dbesc($str_contact_allow),
223 dbesc($str_group_allow),
224 dbesc($str_contact_deny),
225 dbesc($str_group_deny)
227 $r = q("SELECT `id` FROM `item` WHERE `uri` = '%s' LIMIT 1",
230 $post_id = $r[0]['id'];
234 // This item is the last leaf and gets the comment box, clear any ancestors
235 $r = q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent` = %d ",
236 dbesc(datetime_convert()),
240 // Inherit ACL's from the parent item.
241 // TODO merge with subsequent UPDATE operation and save a db write
243 $r = q("UPDATE `item` SET `allow_cid` = '%s', `allow_gid` = '%s', `deny_cid` = '%s', `deny_gid` = '%s'
244 WHERE `id` = %d LIMIT 1",
245 dbesc($parent_item['allow_cid']),
246 dbesc($parent_item['allow_gid']),
247 dbesc($parent_item['deny_cid']),
248 dbesc($parent_item['deny_gid']),
252 // Send a notification email to the conversation owner, unless the owner is me and I wrote this item
253 if(($user['notify-flags'] & NOTIFY_COMMENT) && ($contact_record != $author)) {
254 require_once('bbcode.php');
255 $from = $author['name'];
256 $tpl = load_view_file('view/cmnt_received_eml.tpl');
257 $email_tpl = replace_macros($tpl, array(
258 '$sitename' => $a->config['sitename'],
259 '$siteurl' => $a->get_baseurl(),
260 '$username' => $user['username'],
261 '$email' => $user['email'],
263 '$display' => $a->get_baseurl() . '/display/' . $post_id,
264 '$body' => strip_tags(bbcode($body))
267 $res = mail($user['email'], $from . t(" commented on your item at ") . $a->config['sitename'],
268 $email_tpl,t("From: Administrator@") . $a->get_hostname() );
274 // let me know if somebody did a wall-to-wall post on my profile
276 if(($user['notify-flags'] & NOTIFY_WALL) && ($contact_record != $author)) {
277 require_once('bbcode.php');
278 $from = $author['name'];
279 $tpl = load_view_file('view/wall_received_eml.tpl');
280 $email_tpl = replace_macros($tpl, array(
281 '$sitename' => $a->config['sitename'],
282 '$siteurl' => $a->get_baseurl(),
283 '$username' => $user['username'],
284 '$email' => $user['email'],
286 '$display' => $a->get_baseurl() . '/display/' . $post_id,
287 '$body' => strip_tags(bbcode($body))
290 $res = mail($user['email'], $from . t(" posted on your profile wall at ") . $a->config['sitename'],
291 $email_tpl,t("From: Administrator@") . $a->get_hostname() );
295 $r = q("UPDATE `item` SET `parent` = %d, `parent-uri` = '%s', `changed` = '%s', `last-child` = 1, `visible` = 1
296 WHERE `id` = %d LIMIT 1",
298 dbesc(($parent == $post_id) ? $uri : $parent_item['uri']),
299 dbesc(datetime_convert()),
303 // photo comments turn the corresponding item visible to the profile wall
304 // This way we don't see every picture in your new photo album posted to your wall at once.
305 // They will show up as people comment on them.
307 if(! $parent_item['visible']) {
308 $r = q("UPDATE `item` SET `visible` = 1 WHERE `id` = %d LIMIT 1",
309 intval($parent_item['id'])
314 $php_path = ((strlen($a->config['php_path'])) ? $a->config['php_path'] : 'php');
316 proc_close(proc_open("\"$php_path\" \"include/notifier.php\" \"$notify_type\" \"$post_id\" &",
319 goaway($a->get_baseurl() . "/" . $_POST['return'] );
320 return; // NOTREACHED
323 function item_content(&$a) {
325 if((! local_user()) && (! remote_user()))
328 require_once('include/security.php');
330 $uid = $_SESSION['uid'];
332 if(($a->argc == 3) && ($a->argv[1] === 'drop') && intval($a->argv[2])) {
334 // locate item to be deleted
336 $r = q("SELECT * FROM `item` WHERE `id` = %d LIMIT 1",
341 notice( t('Item not found.') . EOL);
342 goaway($a->get_baseurl() . '/' . $_SESSION['return_url']);
346 // check if logged in user is either the author or owner of this item
348 if(($_SESSION['visitor_id'] == $item['contact-id']) || ($_SESSION['uid'] == $item['uid'])) {
352 $r = q("UPDATE `item` SET `deleted` = 1, `body` = '', `edited` = '%s', `changed` = '%s' WHERE `id` = %d LIMIT 1",
353 dbesc(datetime_convert()),
354 dbesc(datetime_convert()),
358 // If item is a link to a photo resource, nuke all the associated photos
359 // (visitors will not have photo resources)
360 // This only applies to photos uploaded from the photos page. Photos inserted into a post do not
361 // generate a resource-id and therefore aren't intimately linked to the item.
363 if(strlen($item['resource-id'])) {
364 $q("DELETE FROM `photo` WHERE `resource-id` = '%s' AND `uid` = %d ",
365 dbesc($item['resource-id']),
371 // If it's the parent of a comment thread, kill all the kids
373 if($item['uri'] == $item['parent-uri']) {
374 $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s', `body` = ''
375 WHERE `parent-uri` = '%s' AND `uid` = %d ",
376 dbesc(datetime_convert()),
377 dbesc(datetime_convert()),
378 dbesc($item['parent-uri']),
384 // ensure that last-child is set in case the comment that had it just got wiped.
385 q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d ",
386 dbesc(datetime_convert()),
387 dbesc($item['parent-uri']),
390 // who is the last child now?
391 $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",
392 dbesc($item['parent-uri']),
396 q("UPDATE `item` SET `last-child` = 1 WHERE `id` = %d LIMIT 1",
401 $drop_id = intval($item['id']);
402 $php_path = ((strlen($a->config['php_path'])) ? $a->config['php_path'] : 'php');
404 // send the notification upstream/downstream as the case may be
406 proc_close(proc_open("\"$php_path\" \"include/notifier.php\" \"drop\" \"$drop_id\" &",
409 goaway($a->get_baseurl() . '/' . $_SESSION['return_url']);
413 notice( t('Permission denied.') . EOL);
414 goaway($a->get_baseurl() . '/' . $_SESSION['return_url']);