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
85 if(($_SESSION['uid']) && ($_SESSION['uid'] == $profile_uid)) {
86 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 1 LIMIT 1",
87 intval($_SESSION['uid'])
91 if((x($_SESSION,'visitor_id')) && (intval($_SESSION['visitor_id']))) {
92 $r = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
93 intval($_SESSION['visitor_id'])
100 $contact_id = $author['id'];
103 // get contact info for owner
105 if($profile_uid == $_SESSION['uid']) {
106 $contact_record = $author;
109 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 1 LIMIT 1",
113 $contact_record = $r[0];
116 $post_type = notags(trim($_POST['type']));
118 if($post_type === 'net-comment') {
119 if($parent_item !== null) {
120 if($parent_item['type'] === 'remote') {
121 $post_type = 'remote-comment';
124 $post_type = 'wall-comment';
132 $tags = get_tags($body);
136 foreach($tags as $tag) {
137 if(strpos($tag,'#') === 0) {
138 $basetag = str_replace('_',' ',substr($tag,1));
139 $body = str_replace($tag,'#[url=' . $a->get_baseurl() . '/search?search=' . rawurlencode($basetag) . ']' . $basetag . '[/url]',$body);
140 if(strlen($str_tags))
142 $str_tags .= '#[url=' . $a->get_baseurl() . '/search?search=' . rawurlencode($basetag) . ']' . $basetag . '[/url]';
145 if(strpos($tag,'@') === 0) {
146 $name = substr($tag,1);
147 if((strpos($name,'@')) || (strpos($name,'http://'))) {
149 $links = @lrdd($name);
151 foreach($links as $link) {
152 if($link['@attributes']['rel'] === 'http://webfinger.net/rel/profile-page')
153 $profile = $link['@attributes']['href'];
154 if($link['@attributes']['rel'] === 'salmon') {
157 $inform .= 'url:' . str_replace(',','%2c',$link['@attributes']['href']);
164 if(strstr($name,'_')) {
165 $newname = str_replace('_',' ',$name);
166 $r = q("SELECT * FROM `contact` WHERE `name` = '%s' AND `uid` = %d LIMIT 1",
172 $r = q("SELECT * FROM `contact` WHERE `nick` = '%s' AND `uid` = %d LIMIT 1",
178 $profile = $r[0]['url'];
179 $newname = $r[0]['name'];
182 $inform .= 'cid:' . $r[0]['id'];
186 $body = str_replace($name,'[url=' . $profile . ']' . $newname . '[/url]', $body);
187 $profile = str_replace(',','%2c',$profile);
188 if(strlen($str_tags))
190 $str_tags .= '@[url=' . $profile . ']' . $newname . '[/url]';
197 if($post_type === 'wall' || $post_type === 'wall-comment')
201 $verb = ACTIVITY_POST ;
203 $gravity = (($parent) ? 6 : 0 );
205 $notify_type = (($parent) ? 'comment-new' : 'wall-new' );
207 $uri = item_new_uri($a->get_hostname(),$profile_uid);
209 $r = q("INSERT INTO `item` (`uid`,`type`,`wall`,`gravity`,`contact-id`,`owner-name`,`owner-link`,`owner-avatar`,
210 `author-name`, `author-link`, `author-avatar`, `created`, `edited`, `changed`, `uri`, `title`, `body`, `location`, `coord`,
211 `tag`, `inform`, `verb`, `allow_cid`, `allow_gid`, `deny_cid`, `deny_gid`, `private` )
212 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 )",
213 intval($profile_uid),
218 dbesc($contact_record['name']),
219 dbesc($contact_record['url']),
220 dbesc($contact_record['thumb']),
221 dbesc($author['name']),
222 dbesc($author['url']),
223 dbesc($author['thumb']),
224 dbesc(datetime_convert()),
225 dbesc(datetime_convert()),
226 dbesc(datetime_convert()),
235 dbesc($str_contact_allow),
236 dbesc($str_group_allow),
237 dbesc($str_contact_deny),
238 dbesc($str_group_deny),
241 $r = q("SELECT `id` FROM `item` WHERE `uri` = '%s' LIMIT 1",
244 $post_id = $r[0]['id'];
245 logger('mod_item: saved item ' . $post_id);
249 // This item is the last leaf and gets the comment box, clear any ancestors
250 $r = q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent` = %d ",
251 dbesc(datetime_convert()),
255 // Inherit ACL's from the parent item.
257 $r = q("UPDATE `item` SET `allow_cid` = '%s', `allow_gid` = '%s', `deny_cid` = '%s', `deny_gid` = '%s', `private` = %d
258 WHERE `id` = %d LIMIT 1",
259 dbesc($parent_item['allow_cid']),
260 dbesc($parent_item['allow_gid']),
261 dbesc($parent_item['deny_cid']),
262 dbesc($parent_item['deny_gid']),
263 intval($parent_item['private']),
267 // Send a notification email to the conversation owner, unless the owner is me and I wrote this item
268 if(($user['notify-flags'] & NOTIFY_COMMENT) && ($contact_record != $author)) {
269 require_once('bbcode.php');
270 $from = $author['name'];
271 $tpl = load_view_file('view/cmnt_received_eml.tpl');
272 $email_tpl = replace_macros($tpl, array(
273 '$sitename' => $a->config['sitename'],
274 '$siteurl' => $a->get_baseurl(),
275 '$username' => $user['username'],
276 '$email' => $user['email'],
278 '$display' => $a->get_baseurl() . '/display/' . $user['nickname'] . '/' . $post_id,
279 '$body' => strip_tags(bbcode($body))
282 $res = mail($user['email'], $from . t(" commented on your item at ") . $a->config['sitename'],
283 $email_tpl,t("From: Administrator@") . $a->get_hostname() );
289 // let me know if somebody did a wall-to-wall post on my profile
291 if(($user['notify-flags'] & NOTIFY_WALL) && ($contact_record != $author)) {
292 require_once('bbcode.php');
293 $from = $author['name'];
294 $tpl = load_view_file('view/wall_received_eml.tpl');
295 $email_tpl = replace_macros($tpl, array(
296 '$sitename' => $a->config['sitename'],
297 '$siteurl' => $a->get_baseurl(),
298 '$username' => $user['username'],
299 '$email' => $user['email'],
301 '$display' => $a->get_baseurl() . '/display/' . $user['nickname'] . '/' . $post_id,
302 '$body' => strip_tags(bbcode($body))
305 $res = mail($user['email'], $from . t(" posted on your profile wall at ") . $a->config['sitename'],
306 $email_tpl,t("From: Administrator@") . $a->get_hostname() );
310 $r = q("UPDATE `item` SET `parent` = %d, `parent-uri` = '%s', `changed` = '%s', `last-child` = 1, `visible` = 1
311 WHERE `id` = %d LIMIT 1",
313 dbesc(($parent == $post_id) ? $uri : $parent_item['uri']),
314 dbesc(datetime_convert()),
318 // photo comments turn the corresponding item visible to the profile wall
319 // This way we don't see every picture in your new photo album posted to your wall at once.
320 // They will show up as people comment on them.
322 if(! $parent_item['visible']) {
323 $r = q("UPDATE `item` SET `visible` = 1 WHERE `id` = %d LIMIT 1",
324 intval($parent_item['id'])
329 $php_path = ((strlen($a->config['php_path'])) ? $a->config['php_path'] : 'php');
331 logger('mod_item: notifier invoked: ' . "\"$php_path\" \"include/notifier.php\" \"$notify_type\" \"$post_id\" &");
333 proc_close(proc_open("\"$php_path\" \"include/notifier.php\" \"$notify_type\" \"$post_id\" &",
337 * Post to Facebook stream
340 if((local_user()) && (local_user() == $profile_uid) && (! $private)) {
341 $appid = get_config('system', 'facebook_appid' );
342 $secret = get_config('system', 'facebook_secret' );
343 if($appid && $secret) {
344 $fb_post = get_pconfig($local_user(),'facebook','post');
346 require_once('library/facebook.php');
347 require_once('include/bbcode.php');
349 $facebook = new Facebook(array(
355 $statusUpdate = $facebook->api('/me/feed', 'post', array('message'=> bbcode($body), 'cb' => ''));
357 catch (FacebookApiException $e) {
358 notice( t('Facebook status update failed.') . EOL);
364 goaway($a->get_baseurl() . "/" . $_POST['return'] );
365 return; // NOTREACHED
372 function item_content(&$a) {
374 if((! local_user()) && (! remote_user()))
377 require_once('include/security.php');
379 $uid = $_SESSION['uid'];
381 if(($a->argc == 3) && ($a->argv[1] === 'drop') && intval($a->argv[2])) {
383 // locate item to be deleted
385 $r = q("SELECT * FROM `item` WHERE `id` = %d LIMIT 1",
390 notice( t('Item not found.') . EOL);
391 goaway($a->get_baseurl() . '/' . $_SESSION['return_url']);
395 // check if logged in user is either the author or owner of this item
397 if(($_SESSION['visitor_id'] == $item['contact-id']) || ($_SESSION['uid'] == $item['uid'])) {
401 $r = q("UPDATE `item` SET `deleted` = 1, `body` = '', `edited` = '%s', `changed` = '%s' WHERE `id` = %d LIMIT 1",
402 dbesc(datetime_convert()),
403 dbesc(datetime_convert()),
407 // If item is a link to a photo resource, nuke all the associated photos
408 // (visitors will not have photo resources)
409 // This only applies to photos uploaded from the photos page. Photos inserted into a post do not
410 // generate a resource-id and therefore aren't intimately linked to the item.
412 if(strlen($item['resource-id'])) {
413 $q("DELETE FROM `photo` WHERE `resource-id` = '%s' AND `uid` = %d ",
414 dbesc($item['resource-id']),
420 // If it's the parent of a comment thread, kill all the kids
422 if($item['uri'] == $item['parent-uri']) {
423 $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s', `body` = ''
424 WHERE `parent-uri` = '%s' AND `uid` = %d ",
425 dbesc(datetime_convert()),
426 dbesc(datetime_convert()),
427 dbesc($item['parent-uri']),
433 // ensure that last-child is set in case the comment that had it just got wiped.
434 q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d ",
435 dbesc(datetime_convert()),
436 dbesc($item['parent-uri']),
439 // who is the last child now?
440 $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",
441 dbesc($item['parent-uri']),
445 q("UPDATE `item` SET `last-child` = 1 WHERE `id` = %d LIMIT 1",
450 $drop_id = intval($item['id']);
451 $php_path = ((strlen($a->config['php_path'])) ? $a->config['php_path'] : 'php');
453 // send the notification upstream/downstream as the case may be
455 proc_close(proc_open("\"$php_path\" \"include/notifier.php\" \"drop\" \"$drop_id\" &",
458 goaway($a->get_baseurl() . '/' . $_SESSION['return_url']);
462 notice( t('Permission denied.') . EOL);
463 goaway($a->get_baseurl() . '/' . $_SESSION['return_url']);