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);
129 if(strpos($tag,'@') === 0) {
130 $name = substr($tag,1);
131 if((strpos($name,'@')) || (strpos($name,'http://'))) {
133 $links = @lrdd($name);
135 foreach($links as $link) {
136 if($link['@attributes']['rel'] === 'http://webfinger.net/rel/profile-page')
137 $profile = $link['@attributes']['href'];
138 if($link['@attributes']['rel'] === 'salmon') {
141 $inform .= 'url:' . str_replace(',','%2c',$link['@attributes']['href']);
148 if(strstr($name,'_')) {
149 $newname = str_replace('_',' ',$name);
150 $r = q("SELECT * FROM `contact` WHERE `name` = '%s' AND `uid` = %d LIMIT 1",
156 $r = q("SELECT * FROM `contact` WHERE `nick` = '%s' AND `uid` = %d LIMIT 1",
162 $profile = $r[0]['url'];
163 $newname = $r[0]['name'];
166 $inform .= 'cid:' . $r[0]['id'];
170 $body = str_replace($name,'[url=' . $profile . ']' . $newname . '[/url]', $body);
171 if(strlen($str_tags))
173 $profile = str_replace(',','%2c',$profile);
174 $str_tags .= '@[url=' . $profile . ']' . $newname . '[/url]';
181 if($post_type === 'wall' || $post_type === 'wall-comment')
185 $verb = ACTIVITY_POST ;
187 $gravity = (($parent) ? 6 : 0 );
189 $notify_type = (($parent) ? 'comment-new' : 'wall-new' );
191 $uri = item_new_uri($a->get_hostname(),$profile_uid);
193 $r = q("INSERT INTO `item` (`uid`,`type`,`wall`,`gravity`,`contact-id`,`owner-name`,`owner-link`,`owner-avatar`,
194 `author-name`, `author-link`, `author-avatar`, `created`, `edited`, `changed`, `uri`, `title`, `body`, `location`, `coord`,
195 `tag`, `inform`, `verb`, `allow_cid`, `allow_gid`, `deny_cid`, `deny_gid`)
196 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' )",
197 intval($profile_uid),
202 dbesc($contact_record['name']),
203 dbesc($contact_record['url']),
204 dbesc($contact_record['thumb']),
205 dbesc($author['name']),
206 dbesc($author['url']),
207 dbesc($author['thumb']),
208 dbesc(datetime_convert()),
209 dbesc(datetime_convert()),
210 dbesc(datetime_convert()),
219 dbesc($str_contact_allow),
220 dbesc($str_group_allow),
221 dbesc($str_contact_deny),
222 dbesc($str_group_deny)
224 $r = q("SELECT `id` FROM `item` WHERE `uri` = '%s' LIMIT 1",
227 $post_id = $r[0]['id'];
231 // This item is the last leaf and gets the comment box, clear any ancestors
232 $r = q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent` = %d ",
233 dbesc(datetime_convert()),
237 // Inherit ACL's from the parent item.
238 // TODO merge with subsequent UPDATE operation and save a db write
240 $r = q("UPDATE `item` SET `allow_cid` = '%s', `allow_gid` = '%s', `deny_cid` = '%s', `deny_gid` = '%s'
241 WHERE `id` = %d LIMIT 1",
242 dbesc($parent_item['allow_cid']),
243 dbesc($parent_item['allow_gid']),
244 dbesc($parent_item['deny_cid']),
245 dbesc($parent_item['deny_gid']),
249 // Send a notification email to the conversation owner, unless the owner is me and I wrote this item
250 if(($user['notify-flags'] & NOTIFY_COMMENT) && ($contact_record != $author)) {
251 require_once('bbcode.php');
252 $from = $author['name'];
253 $tpl = load_view_file('view/cmnt_received_eml.tpl');
254 $email_tpl = replace_macros($tpl, array(
255 '$sitename' => $a->config['sitename'],
256 '$siteurl' => $a->get_baseurl(),
257 '$username' => $user['username'],
258 '$email' => $user['email'],
260 '$display' => $a->get_baseurl() . '/display/' . $post_id,
261 '$body' => strip_tags(bbcode($body))
264 $res = mail($user['email'], $from . t(" commented on your item at ") . $a->config['sitename'],
265 $email_tpl,t("From: Administrator@") . $a->get_hostname() );
271 // let me know if somebody did a wall-to-wall post on my profile
273 if(($user['notify-flags'] & NOTIFY_WALL) && ($contact_record != $author)) {
274 require_once('bbcode.php');
275 $from = $author['name'];
276 $tpl = load_view_file('view/wall_received_eml.tpl');
277 $email_tpl = replace_macros($tpl, array(
278 '$sitename' => $a->config['sitename'],
279 '$siteurl' => $a->get_baseurl(),
280 '$username' => $user['username'],
281 '$email' => $user['email'],
283 '$display' => $a->get_baseurl() . '/display/' . $post_id,
284 '$body' => strip_tags(bbcode($body))
287 $res = mail($user['email'], $from . t(" posted on your profile wall at ") . $a->config['sitename'],
288 $email_tpl,t("From: Administrator@") . $a->get_hostname() );
292 $r = q("UPDATE `item` SET `parent` = %d, `parent-uri` = '%s', `changed` = '%s', `last-child` = 1, `visible` = 1
293 WHERE `id` = %d LIMIT 1",
295 dbesc(($parent == $post_id) ? $uri : $parent_item['uri']),
296 dbesc(datetime_convert()),
300 // photo comments turn the corresponding item visible to the profile wall
301 // This way we don't see every picture in your new photo album posted to your wall at once.
302 // They will show up as people comment on them.
304 if(! $parent_item['visible']) {
305 $r = q("UPDATE `item` SET `visible` = 1 WHERE `id` = %d LIMIT 1",
306 intval($parent_item['id'])
311 $php_path = ((strlen($a->config['php_path'])) ? $a->config['php_path'] : 'php');
313 proc_close(proc_open("\"$php_path\" \"include/notifier.php\" \"$notify_type\" \"$post_id\" &",
316 goaway($a->get_baseurl() . "/" . $_POST['return'] );
317 return; // NOTREACHED
320 function item_content(&$a) {
322 if((! local_user()) && (! remote_user()))
325 require_once('include/security.php');
327 $uid = $_SESSION['uid'];
329 if(($a->argc == 3) && ($a->argv[1] === 'drop') && intval($a->argv[2])) {
331 // locate item to be deleted
333 $r = q("SELECT * FROM `item` WHERE `id` = %d LIMIT 1",
338 notice( t('Item not found.') . EOL);
339 goaway($a->get_baseurl() . '/' . $_SESSION['return_url']);
343 // check if logged in user is either the author or owner of this item
345 if(($_SESSION['visitor_id'] == $item['contact-id']) || ($_SESSION['uid'] == $item['uid'])) {
349 $r = q("UPDATE `item` SET `deleted` = 1, `body` = '', `edited` = '%s', `changed` = '%s' WHERE `id` = %d LIMIT 1",
350 dbesc(datetime_convert()),
351 dbesc(datetime_convert()),
355 // If item is a link to a photo resource, nuke all the associated photos
356 // (visitors will not have photo resources)
357 // This only applies to photos uploaded from the photos page. Photos inserted into a post do not
358 // generate a resource-id and therefore aren't intimately linked to the item.
360 if(strlen($item['resource-id'])) {
361 $q("DELETE FROM `photo` WHERE `resource-id` = '%s' AND `uid` = %d ",
362 dbesc($item['resource-id']),
368 // If it's the parent of a comment thread, kill all the kids
370 if($item['uri'] == $item['parent-uri']) {
371 $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s', `body` = ''
372 WHERE `parent-uri` = '%s' AND `uid` = %d ",
373 dbesc(datetime_convert()),
374 dbesc(datetime_convert()),
375 dbesc($item['parent-uri']),
381 // ensure that last-child is set in case the comment that had it just got wiped.
382 q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d ",
383 dbesc(datetime_convert()),
384 dbesc($item['parent-uri']),
387 // who is the last child now?
388 $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",
389 dbesc($item['parent-uri']),
393 q("UPDATE `item` SET `last-child` = 1 WHERE `id` = %d LIMIT 1",
398 $drop_id = intval($item['id']);
399 $php_path = ((strlen($a->config['php_path'])) ? $a->config['php_path'] : 'php');
401 // send the notification upstream/downstream as the case may be
403 proc_close(proc_open("\"$php_path\" \"include/notifier.php\" \"drop\" \"$drop_id\" &",
406 goaway($a->get_baseurl() . '/' . $_SESSION['return_url']);
410 notice( t('Permission denied.') . EOL);
411 goaway($a->get_baseurl() . '/' . $_SESSION['return_url']);