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 $tags = get_tags($body);
138 foreach($tags as $tag) {
139 if(strpos($tag,'#') === 0) {
140 $basetag = str_replace('_',' ',substr($tag,1));
141 $body = str_replace($tag,'#[url=' . $a->get_baseurl() . '/search?search=' . rawurlencode($basetag) . ']' . $basetag . '[/url]',$body);
142 if(strlen($str_tags))
144 $str_tags .= '#[url=' . $a->get_baseurl() . '/search?search=' . rawurlencode($basetag) . ']' . $basetag . '[/url]';
147 if(strpos($tag,'@') === 0) {
148 $name = substr($tag,1);
149 if((strpos($name,'@')) || (strpos($name,'http://'))) {
151 $links = @lrdd($name);
153 foreach($links as $link) {
154 if($link['@attributes']['rel'] === 'http://webfinger.net/rel/profile-page')
155 $profile = $link['@attributes']['href'];
156 if($link['@attributes']['rel'] === 'salmon') {
159 $inform .= 'url:' . str_replace(',','%2c',$link['@attributes']['href']);
166 if(strstr($name,'_')) {
167 $newname = str_replace('_',' ',$name);
168 $r = q("SELECT * FROM `contact` WHERE `name` = '%s' AND `uid` = %d LIMIT 1",
174 $r = q("SELECT * FROM `contact` WHERE `nick` = '%s' AND `uid` = %d LIMIT 1",
180 $profile = $r[0]['url'];
181 $newname = $r[0]['name'];
184 $inform .= 'cid:' . $r[0]['id'];
188 $body = str_replace($name,'[url=' . $profile . ']' . $newname . '[/url]', $body);
189 $profile = str_replace(',','%2c',$profile);
190 if(strlen($str_tags))
192 $str_tags .= '@[url=' . $profile . ']' . $newname . '[/url]';
199 if($post_type === 'wall' || $post_type === 'wall-comment')
203 $verb = ACTIVITY_POST ;
205 $gravity = (($parent) ? 6 : 0 );
207 $notify_type = (($parent) ? 'comment-new' : 'wall-new' );
209 $uri = item_new_uri($a->get_hostname(),$profile_uid);
212 $datarray['uid'] = $profile_uid;
213 $datarray['type'] = $post_type;
214 $datarray['wall'] = $wall;
215 $datarray['gravity'] = $gravity;
216 $datarray['contact-id'] = $contact_id;
217 $datarray['owner-name'] = $contact_record['name'];
218 $datarray['owner-link'] = $contact_record['url'];
219 $datarray['owner-avatar'] = $contact_record['thumb'];
220 $datarray['author-name'] = $author['name'];
221 $datarray['author-link'] = $author['url'];
222 $datarray['author-avatar'] = $author['thumb'];
223 $datarray['created'] = datetime_convert();
224 $datarray['edited'] = datetime_convert();
225 $datarray['changed'] = datetime_convert();
226 $datarray['uri'] = $uri;
227 $datarray['title'] = $title;
228 $datarray['body'] = $body;
229 $datarray['location'] = $location;
230 $datarray['coord'] = $coord;
231 $datarray['tag'] = $str_tags;
232 $datarray['inform'] = $inform;
233 $datarray['verb'] = $verb;
234 $datarray['allow_cid'] = $str_contact_allow;
235 $datarray['allow_gid'] = $str_group_allow;
236 $datarray['deny_cid'] = $str_contact_deny;
237 $datarray['deny_gid'] = $str_group_deny;
238 $datarray['private'] = $private;
241 * These fields are for the convenience of plugins...
242 * 'self' if true indicates the owner is posting on their own wall
243 * If parent is 0 it is a top-level post.
246 $datarray['parent'] = $parent;
247 $datarray['self'] = $self;
250 call_hooks('post_local',$datarray);
252 $r = q("INSERT INTO `item` (`uid`,`type`,`wall`,`gravity`,`contact-id`,`owner-name`,`owner-link`,`owner-avatar`,
253 `author-name`, `author-link`, `author-avatar`, `created`, `edited`, `changed`, `uri`, `title`, `body`, `location`, `coord`,
254 `tag`, `inform`, `verb`, `allow_cid`, `allow_gid`, `deny_cid`, `deny_gid`, `private` )
255 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 )",
256 intval($datarray['uid']),
257 dbesc($datarray['type']),
258 intval($datarray['wall']),
259 intval($datarray['gravity']),
260 intval($datarray['contact-id']),
261 dbesc($datarray['owner-name']),
262 dbesc($datarray['owner-link']),
263 dbesc($datarray['owner-avatar']),
264 dbesc($datarray['author-name']),
265 dbesc($datarray['author-link']),
266 dbesc($datarray['author-avatar']),
267 dbesc($datarray['created']),
268 dbesc($datarray['edited']),
269 dbesc($datarray['changed']),
270 dbesc($datarray['uri']),
271 dbesc($datarray['title']),
272 dbesc($datarray['body']),
273 dbesc($datarray['location']),
274 dbesc($datarray['coord']),
275 dbesc($datarray['tag']),
276 dbesc($datarray['inform']),
277 dbesc($datarray['verb']),
278 dbesc($datarray['allow_cid']),
279 dbesc($datarray['allow_gid']),
280 dbesc($datarray['deny_cid']),
281 dbesc($datarray['deny_gid']),
282 intval($datarray['private'])
285 $r = q("SELECT `id` FROM `item` WHERE `uri` = '%s' LIMIT 1",
286 dbesc($datarray['uri']));
288 $post_id = $r[0]['id'];
289 logger('mod_item: saved item ' . $post_id);
293 // This item is the last leaf and gets the comment box, clear any ancestors
294 $r = q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent` = %d ",
295 dbesc(datetime_convert()),
299 // Inherit ACL's from the parent item.
301 $r = q("UPDATE `item` SET `allow_cid` = '%s', `allow_gid` = '%s', `deny_cid` = '%s', `deny_gid` = '%s', `private` = %d
302 WHERE `id` = %d LIMIT 1",
303 dbesc($parent_item['allow_cid']),
304 dbesc($parent_item['allow_gid']),
305 dbesc($parent_item['deny_cid']),
306 dbesc($parent_item['deny_gid']),
307 intval($parent_item['private']),
311 // Send a notification email to the conversation owner, unless the owner is me and I wrote this item
312 if(($user['notify-flags'] & NOTIFY_COMMENT) && ($contact_record != $author)) {
313 require_once('bbcode.php');
314 $from = $author['name'];
315 $tpl = load_view_file('view/cmnt_received_eml.tpl');
316 $email_tpl = replace_macros($tpl, array(
317 '$sitename' => $a->config['sitename'],
318 '$siteurl' => $a->get_baseurl(),
319 '$username' => $user['username'],
320 '$email' => $user['email'],
322 '$display' => $a->get_baseurl() . '/display/' . $user['nickname'] . '/' . $post_id,
323 '$body' => strip_tags(bbcode($datarray['body']))
326 $res = mail($user['email'], $from . t(" commented on your item at ") . $a->config['sitename'],
327 $email_tpl,t("From: Administrator@") . $a->get_hostname() );
333 // let me know if somebody did a wall-to-wall post on my profile
335 if(($user['notify-flags'] & NOTIFY_WALL) && ($contact_record != $author)) {
336 require_once('bbcode.php');
337 $from = $author['name'];
338 $tpl = load_view_file('view/wall_received_eml.tpl');
339 $email_tpl = replace_macros($tpl, array(
340 '$sitename' => $a->config['sitename'],
341 '$siteurl' => $a->get_baseurl(),
342 '$username' => $user['username'],
343 '$email' => $user['email'],
345 '$display' => $a->get_baseurl() . '/display/' . $user['nickname'] . '/' . $post_id,
346 '$body' => strip_tags(bbcode($datarray['body']))
349 $res = mail($user['email'], $from . t(" posted on your profile wall at ") . $a->config['sitename'],
350 $email_tpl,t("From: Administrator@") . $a->get_hostname() );
354 $r = q("UPDATE `item` SET `parent` = %d, `parent-uri` = '%s', `changed` = '%s', `last-child` = 1, `visible` = 1
355 WHERE `id` = %d LIMIT 1",
357 dbesc(($parent == $post_id) ? $uri : $parent_item['uri']),
358 dbesc(datetime_convert()),
362 // photo comments turn the corresponding item visible to the profile wall
363 // This way we don't see every picture in your new photo album posted to your wall at once.
364 // They will show up as people comment on them.
366 if(! $parent_item['visible']) {
367 $r = q("UPDATE `item` SET `visible` = 1 WHERE `id` = %d LIMIT 1",
368 intval($parent_item['id'])
373 $php_path = ((strlen($a->config['php_path'])) ? $a->config['php_path'] : 'php');
375 logger('mod_item: notifier invoked: ' . "\"$php_path\" \"include/notifier.php\" \"$notify_type\" \"$post_id\" &");
377 proc_close(proc_open("\"$php_path\" \"include/notifier.php\" \"$notify_type\" \"$post_id\" &",
380 $datarray['id'] = $post_id;
382 call_hooks('post_local_end', $datarray);
385 goaway($a->get_baseurl() . "/" . $_POST['return'] );
386 return; // NOTREACHED
393 function item_content(&$a) {
395 if((! local_user()) && (! remote_user()))
398 require_once('include/security.php');
400 $uid = $_SESSION['uid'];
402 if(($a->argc == 3) && ($a->argv[1] === 'drop') && intval($a->argv[2])) {
404 // locate item to be deleted
406 $r = q("SELECT * FROM `item` WHERE `id` = %d LIMIT 1",
411 notice( t('Item not found.') . EOL);
412 goaway($a->get_baseurl() . '/' . $_SESSION['return_url']);
416 // check if logged in user is either the author or owner of this item
418 if(($_SESSION['visitor_id'] == $item['contact-id']) || ($_SESSION['uid'] == $item['uid'])) {
422 $r = q("UPDATE `item` SET `deleted` = 1, `body` = '', `edited` = '%s', `changed` = '%s' WHERE `id` = %d LIMIT 1",
423 dbesc(datetime_convert()),
424 dbesc(datetime_convert()),
428 // If item is a link to a photo resource, nuke all the associated photos
429 // (visitors will not have photo resources)
430 // This only applies to photos uploaded from the photos page. Photos inserted into a post do not
431 // generate a resource-id and therefore aren't intimately linked to the item.
433 if(strlen($item['resource-id'])) {
434 $q("DELETE FROM `photo` WHERE `resource-id` = '%s' AND `uid` = %d ",
435 dbesc($item['resource-id']),
441 // If it's the parent of a comment thread, kill all the kids
443 if($item['uri'] == $item['parent-uri']) {
444 $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s', `body` = ''
445 WHERE `parent-uri` = '%s' AND `uid` = %d ",
446 dbesc(datetime_convert()),
447 dbesc(datetime_convert()),
448 dbesc($item['parent-uri']),
454 // ensure that last-child is set in case the comment that had it just got wiped.
455 q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d ",
456 dbesc(datetime_convert()),
457 dbesc($item['parent-uri']),
460 // who is the last child now?
461 $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",
462 dbesc($item['parent-uri']),
466 q("UPDATE `item` SET `last-child` = 1 WHERE `id` = %d LIMIT 1",
471 $drop_id = intval($item['id']);
472 $php_path = ((strlen($a->config['php_path'])) ? $a->config['php_path'] : 'php');
474 // send the notification upstream/downstream as the case may be
476 proc_close(proc_open("\"$php_path\" \"include/notifier.php\" \"drop\" \"$drop_id\" &",
479 goaway($a->get_baseurl() . '/' . $_SESSION['return_url']);
483 notice( t('Permission denied.') . EOL);
484 goaway($a->get_baseurl() . '/' . $_SESSION['return_url']);