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);
58 $title = notags(trim($_POST['title']));
59 $body = escape_tags(trim($_POST['body']));
60 $location = notags(trim($_POST['location']));
61 $coord = notags(trim($_POST['coord']));
62 $verb = notags(trim($_POST['verb']));
65 notice( t('Empty post discarded.') . EOL );
66 goaway($a->get_baseurl() . "/" . $_POST['return'] );
70 // get contact info for poster
74 if(($_SESSION['uid']) && ($_SESSION['uid'] == $profile_uid)) {
75 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 1 LIMIT 1",
76 intval($_SESSION['uid'])
80 if((x($_SESSION,'visitor_id')) && (intval($_SESSION['visitor_id']))) {
81 $r = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
82 intval($_SESSION['visitor_id'])
89 $contact_id = $author['id'];
92 // get contact info for owner
94 if($profile_uid == $_SESSION['uid']) {
95 $contact_record = $author;
98 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 1 LIMIT 1",
102 $contact_record = $r[0];
105 $post_type = notags(trim($_POST['type']));
107 if($post_type === 'net-comment') {
108 if($parent_item !== null) {
109 if($parent_item['type'] === 'remote') {
110 $post_type = 'remote-comment';
113 $post_type = 'wall-comment';
121 $tags = get_tags($body);
125 foreach($tags as $tag) {
126 if(strpos($tag,'#') === 0) {
127 $basetag = str_replace('_',' ',substr($tag,1));
128 $body = str_replace($tag,'#[url=' . $a->get_baseurl() . '/search?search=' . rawurlencode($basetag) . ']' . $basetag . '[/url]',$body);
129 if(strlen($str_tags))
131 $str_tags .= '#[url=' . $a->get_baseurl() . '/search?search=' . rawurlencode($basetag) . ']' . $basetag . '[/url]';
134 if(strpos($tag,'@') === 0) {
135 $name = substr($tag,1);
136 if((strpos($name,'@')) || (strpos($name,'http://'))) {
138 $links = @lrdd($name);
140 foreach($links as $link) {
141 if($link['@attributes']['rel'] === 'http://webfinger.net/rel/profile-page')
142 $profile = $link['@attributes']['href'];
143 if($link['@attributes']['rel'] === 'salmon') {
146 $inform .= 'url:' . str_replace(',','%2c',$link['@attributes']['href']);
153 if(strstr($name,'_')) {
154 $newname = str_replace('_',' ',$name);
155 $r = q("SELECT * FROM `contact` WHERE `name` = '%s' AND `uid` = %d LIMIT 1",
161 $r = q("SELECT * FROM `contact` WHERE `nick` = '%s' AND `uid` = %d LIMIT 1",
167 $profile = $r[0]['url'];
168 $newname = $r[0]['name'];
171 $inform .= 'cid:' . $r[0]['id'];
175 $body = str_replace($name,'[url=' . $profile . ']' . $newname . '[/url]', $body);
176 $profile = str_replace(',','%2c',$profile);
177 if(strlen($str_tags))
179 $str_tags .= '@[url=' . $profile . ']' . $newname . '[/url]';
186 if($post_type === 'wall' || $post_type === 'wall-comment')
190 $verb = ACTIVITY_POST ;
192 $gravity = (($parent) ? 6 : 0 );
194 $notify_type = (($parent) ? 'comment-new' : 'wall-new' );
196 $uri = item_new_uri($a->get_hostname(),$profile_uid);
198 $r = q("INSERT INTO `item` (`uid`,`type`,`wall`,`gravity`,`contact-id`,`owner-name`,`owner-link`,`owner-avatar`,
199 `author-name`, `author-link`, `author-avatar`, `created`, `edited`, `changed`, `uri`, `title`, `body`, `location`, `coord`,
200 `tag`, `inform`, `verb`, `allow_cid`, `allow_gid`, `deny_cid`, `deny_gid`, `private` )
201 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 )",
202 intval($profile_uid),
207 dbesc($contact_record['name']),
208 dbesc($contact_record['url']),
209 dbesc($contact_record['thumb']),
210 dbesc($author['name']),
211 dbesc($author['url']),
212 dbesc($author['thumb']),
213 dbesc(datetime_convert()),
214 dbesc(datetime_convert()),
215 dbesc(datetime_convert()),
224 dbesc($str_contact_allow),
225 dbesc($str_group_allow),
226 dbesc($str_contact_deny),
227 dbesc($str_group_deny),
230 $r = q("SELECT `id` FROM `item` WHERE `uri` = '%s' LIMIT 1",
233 $post_id = $r[0]['id'];
234 logger('mod_item: saved item ' . $post_id);
238 // This item is the last leaf and gets the comment box, clear any ancestors
239 $r = q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent` = %d ",
240 dbesc(datetime_convert()),
244 // Inherit ACL's from the parent item.
245 // TODO merge with subsequent UPDATE operation and save a db write
247 $r = q("UPDATE `item` SET `allow_cid` = '%s', `allow_gid` = '%s', `deny_cid` = '%s', `deny_gid` = '%s', `private` = %d
248 WHERE `id` = %d LIMIT 1",
249 dbesc($parent_item['allow_cid']),
250 dbesc($parent_item['allow_gid']),
251 dbesc($parent_item['deny_cid']),
252 dbesc($parent_item['deny_gid']),
253 intval($parent_item['private']),
257 // Send a notification email to the conversation owner, unless the owner is me and I wrote this item
258 if(($user['notify-flags'] & NOTIFY_COMMENT) && ($contact_record != $author)) {
259 require_once('bbcode.php');
260 $from = $author['name'];
261 $tpl = load_view_file('view/cmnt_received_eml.tpl');
262 $email_tpl = replace_macros($tpl, array(
263 '$sitename' => $a->config['sitename'],
264 '$siteurl' => $a->get_baseurl(),
265 '$username' => $user['username'],
266 '$email' => $user['email'],
268 '$display' => $a->get_baseurl() . '/display/' . $user['nickname'] . '/' . $post_id,
269 '$body' => strip_tags(bbcode($body))
272 $res = mail($user['email'], $from . t(" commented on your item at ") . $a->config['sitename'],
273 $email_tpl,t("From: Administrator@") . $a->get_hostname() );
279 // let me know if somebody did a wall-to-wall post on my profile
281 if(($user['notify-flags'] & NOTIFY_WALL) && ($contact_record != $author)) {
282 require_once('bbcode.php');
283 $from = $author['name'];
284 $tpl = load_view_file('view/wall_received_eml.tpl');
285 $email_tpl = replace_macros($tpl, array(
286 '$sitename' => $a->config['sitename'],
287 '$siteurl' => $a->get_baseurl(),
288 '$username' => $user['username'],
289 '$email' => $user['email'],
291 '$display' => $a->get_baseurl() . '/display/' . $user['nickname'] . '/' . $post_id,
292 '$body' => strip_tags(bbcode($body))
295 $res = mail($user['email'], $from . t(" posted on your profile wall at ") . $a->config['sitename'],
296 $email_tpl,t("From: Administrator@") . $a->get_hostname() );
300 $r = q("UPDATE `item` SET `parent` = %d, `parent-uri` = '%s', `changed` = '%s', `last-child` = 1, `visible` = 1
301 WHERE `id` = %d LIMIT 1",
303 dbesc(($parent == $post_id) ? $uri : $parent_item['uri']),
304 dbesc(datetime_convert()),
308 // photo comments turn the corresponding item visible to the profile wall
309 // This way we don't see every picture in your new photo album posted to your wall at once.
310 // They will show up as people comment on them.
312 if(! $parent_item['visible']) {
313 $r = q("UPDATE `item` SET `visible` = 1 WHERE `id` = %d LIMIT 1",
314 intval($parent_item['id'])
319 $php_path = ((strlen($a->config['php_path'])) ? $a->config['php_path'] : 'php');
321 logger('mod_item: notifier invoked: ' . "\"$php_path\" \"include/notifier.php\" \"$notify_type\" \"$post_id\" &");
323 proc_close(proc_open("\"$php_path\" \"include/notifier.php\" \"$notify_type\" \"$post_id\" &",
326 goaway($a->get_baseurl() . "/" . $_POST['return'] );
327 return; // NOTREACHED
330 function item_content(&$a) {
332 if((! local_user()) && (! remote_user()))
335 require_once('include/security.php');
337 $uid = $_SESSION['uid'];
339 if(($a->argc == 3) && ($a->argv[1] === 'drop') && intval($a->argv[2])) {
341 // locate item to be deleted
343 $r = q("SELECT * FROM `item` WHERE `id` = %d LIMIT 1",
348 notice( t('Item not found.') . EOL);
349 goaway($a->get_baseurl() . '/' . $_SESSION['return_url']);
353 // check if logged in user is either the author or owner of this item
355 if(($_SESSION['visitor_id'] == $item['contact-id']) || ($_SESSION['uid'] == $item['uid'])) {
359 $r = q("UPDATE `item` SET `deleted` = 1, `body` = '', `edited` = '%s', `changed` = '%s' WHERE `id` = %d LIMIT 1",
360 dbesc(datetime_convert()),
361 dbesc(datetime_convert()),
365 // If item is a link to a photo resource, nuke all the associated photos
366 // (visitors will not have photo resources)
367 // This only applies to photos uploaded from the photos page. Photos inserted into a post do not
368 // generate a resource-id and therefore aren't intimately linked to the item.
370 if(strlen($item['resource-id'])) {
371 $q("DELETE FROM `photo` WHERE `resource-id` = '%s' AND `uid` = %d ",
372 dbesc($item['resource-id']),
378 // If it's the parent of a comment thread, kill all the kids
380 if($item['uri'] == $item['parent-uri']) {
381 $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s', `body` = ''
382 WHERE `parent-uri` = '%s' AND `uid` = %d ",
383 dbesc(datetime_convert()),
384 dbesc(datetime_convert()),
385 dbesc($item['parent-uri']),
391 // ensure that last-child is set in case the comment that had it just got wiped.
392 q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d ",
393 dbesc(datetime_convert()),
394 dbesc($item['parent-uri']),
397 // who is the last child now?
398 $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",
399 dbesc($item['parent-uri']),
403 q("UPDATE `item` SET `last-child` = 1 WHERE `id` = %d LIMIT 1",
408 $drop_id = intval($item['id']);
409 $php_path = ((strlen($a->config['php_path'])) ? $a->config['php_path'] : 'php');
411 // send the notification upstream/downstream as the case may be
413 proc_close(proc_open("\"$php_path\" \"include/notifier.php\" \"drop\" \"$drop_id\" &",
416 goaway($a->get_baseurl() . '/' . $_SESSION['return_url']);
420 notice( t('Permission denied.') . EOL);
421 goaway($a->get_baseurl() . '/' . $_SESSION['return_url']);