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');
17 $uid = $_SESSION['uid'];
20 $parent = ((x($_POST,'parent')) ? intval($_POST['parent']) : 0);
25 $r = q("SELECT * FROM `item` WHERE `id` = %d LIMIT 1",
29 notice( t('Unable to locate original post.') . EOL);
30 goaway($a->get_baseurl() . "/" . $_POST['return'] );
35 $profile_uid = ((x($_POST,'profile_uid')) ? intval($_POST['profile_uid']) : 0);
38 if(! can_write_wall($a,$profile_uid)) {
39 notice( t('Permission denied.') . EOL) ;
45 $r = q("SELECT * FROM `user` WHERE `uid` = %d LIMIT 1",
52 $str_group_allow = perms2str($_POST['group_allow']);
53 $str_contact_allow = perms2str($_POST['contact_allow']);
54 $str_group_deny = perms2str($_POST['group_deny']);
55 $str_contact_deny = perms2str($_POST['contact_deny']);
57 $title = notags(trim($_POST['title']));
58 $body = escape_tags(trim($_POST['body']));
59 $location = notags(trim($_POST['location']));
60 $coord = notags(trim($_POST['coord']));
61 $verb = notags(trim($_POST['verb']));
64 notice( t('Empty post discarded.') . EOL );
65 goaway($a->get_baseurl() . "/" . $_POST['return'] );
69 // get contact info for poster
73 if(($_SESSION['uid']) && ($_SESSION['uid'] == $profile_uid)) {
74 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 1 LIMIT 1",
75 intval($_SESSION['uid'])
79 if((x($_SESSION,'visitor_id')) && (intval($_SESSION['visitor_id']))) {
80 $r = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
81 intval($_SESSION['visitor_id'])
88 $contact_id = $author['id'];
91 // get contact info for owner
93 if($profile_uid == $_SESSION['uid']) {
94 $contact_record = $author;
97 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 1 LIMIT 1",
101 $contact_record = $r[0];
104 $post_type = notags(trim($_POST['type']));
106 if($post_type === 'net-comment') {
107 if($parent_item !== null) {
108 if($parent_item['type'] === 'remote') {
109 $post_type = 'remote-comment';
112 $post_type = 'wall-comment';
118 if($post_type === 'wall' || $post_type === 'wall-comment')
122 $verb = ACTIVITY_POST ;
124 $gravity = (($parent) ? 6 : 0 );
126 $notify_type = (($parent) ? 'comment-new' : 'wall-new' );
128 $uri = item_new_uri($a->get_hostname(),$profile_uid);
130 $r = q("INSERT INTO `item` (`uid`,`type`,`wall`,`gravity`,`contact-id`,`owner-name`,`owner-link`,`owner-avatar`,
131 `author-name`, `author-link`, `author-avatar`, `created`,
132 `edited`, `changed`, `uri`, `title`, `body`, `location`, `coord`, `verb`, `allow_cid`, `allow_gid`, `deny_cid`, `deny_gid`)
133 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' )",
134 intval($profile_uid),
139 dbesc($contact_record['name']),
140 dbesc($contact_record['url']),
141 dbesc($contact_record['thumb']),
142 dbesc($author['name']),
143 dbesc($author['url']),
144 dbesc($author['thumb']),
145 dbesc(datetime_convert()),
146 dbesc(datetime_convert()),
147 dbesc(datetime_convert()),
154 dbesc($str_contact_allow),
155 dbesc($str_group_allow),
156 dbesc($str_contact_deny),
157 dbesc($str_group_deny)
159 $r = q("SELECT `id` FROM `item` WHERE `uri` = '%s' LIMIT 1",
162 $post_id = $r[0]['id'];
166 // This item is the last leaf and gets the comment box, clear any ancestors
167 $r = q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent` = %d ",
168 dbesc(datetime_convert()),
172 // Inherit ACL's from the parent item.
173 // TODO merge with subsequent UPDATE operation and save a db write
175 $r = q("UPDATE `item` SET `allow_cid` = '%s', `allow_gid` = '%s', `deny_cid` = '%s', `deny_gid` = '%s'
176 WHERE `id` = %d LIMIT 1",
177 dbesc($parent_item['allow_cid']),
178 dbesc($parent_item['allow_gid']),
179 dbesc($parent_item['deny_cid']),
180 dbesc($parent_item['deny_gid']),
184 // Send a notification email to the conversation owner, unless the owner is me and I wrote this item
185 if(($user['notify-flags'] & NOTIFY_COMMENT) && ($contact_record != $author)) {
186 require_once('bbcode.php');
187 $from = $author['name'];
188 $tpl = load_view_file('view/cmnt_received_eml.tpl');
189 $email_tpl = replace_macros($tpl, array(
190 '$sitename' => $a->config['sitename'],
191 '$siteurl' => $a->get_baseurl(),
192 '$username' => $user['username'],
193 '$email' => $user['email'],
195 '$body' => strip_tags(bbcode($body))
198 $res = mail($user['email'], $from . t(" commented on your item at ") . $a->config['sitename'],
199 $email_tpl,t("From: Administrator@") . $a->get_hostname() );
205 // let me know if somebody did a wall-to-wall post on my profile
207 if(($user['notify-flags'] & NOTIFY_WALL) && ($contact_record != $author)) {
208 require_once('bbcode.php');
209 $from = $author['name'];
210 $tpl = load_view_file('view/wall_received_eml.tpl');
211 $email_tpl = replace_macros($tpl, array(
212 '$sitename' => $a->config['sitename'],
213 '$siteurl' => $a->get_baseurl(),
214 '$username' => $user['username'],
215 '$email' => $user['email'],
217 '$body' => strip_tags(bbcode($body))
220 $res = mail($user['email'], $from . t(" posted on your profile wall at ") . $a->config['sitename'],
221 $email_tpl,t("From: Administrator@") . $a->get_hostname() );
225 $r = q("UPDATE `item` SET `parent` = %d, `parent-uri` = '%s', `changed` = '%s', `last-child` = 1, `visible` = 1
226 WHERE `id` = %d LIMIT 1",
228 dbesc(($parent == $post_id) ? $uri : $parent_item['uri']),
229 dbesc(datetime_convert()),
233 // photo comments turn the corresponding item visible to the profile wall
234 // This way we don't see every picture in your new photo album posted to your wall at once.
235 // They will show up as people comment on them.
237 if(! $parent_item['visible']) {
238 $r = q("UPDATE `item` SET `visible` = 1 WHERE `id` = %d LIMIT 1",
239 intval($parent_item['id'])
244 $php_path = ((strlen($a->config['php_path'])) ? $a->config['php_path'] : 'php');
245 $proc_debug = get_config('system','proc_debug');
247 proc_close(proc_open("\"$php_path\" \"include/notifier.php\" \"$notify_type\" \"$post_id\" $proc_debug &",
250 goaway($a->get_baseurl() . "/" . $_POST['return'] );
251 return; // NOTREACHED
254 function item_content(&$a) {
256 if((! local_user()) && (! remote_user()))
259 require_once('include/security.php');
261 $uid = $_SESSION['uid'];
263 if(($a->argc == 3) && ($a->argv[1] === 'drop') && intval($a->argv[2])) {
265 // locate item to be deleted
267 $r = q("SELECT * FROM `item` WHERE `id` = %d LIMIT 1",
272 notice( t('Item not found.') . EOL);
273 goaway($a->get_baseurl() . '/' . $_SESSION['return_url']);
277 // check if logged in user is either the author or owner of this item
279 if(($_SESSION['visitor_id'] == $item['contact-id']) || ($_SESSION['uid'] == $item['uid'])) {
283 $r = q("UPDATE `item` SET `deleted` = 1, `body` = '', `edited` = '%s', `changed` = '%s' WHERE `id` = %d LIMIT 1",
284 dbesc(datetime_convert()),
285 dbesc(datetime_convert()),
289 // If item is a link to a photo resource, nuke all the associated photos
290 // (visitors will not have photo resources)
291 // This only applies to photos uploaded from the photos page. Photos inserted into a post do not
292 // generate a resource-id and therefore aren't intimately linked to the item.
294 if(strlen($item['resource-id'])) {
295 $q("DELETE FROM `photo` WHERE `resource-id` = '%s' AND `uid` = %d ",
296 dbesc($item['resource-id']),
302 // If it's the parent of a comment thread, kill all the kids
304 if($item['uri'] == $item['parent-uri']) {
305 $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s', `body` = ''
306 WHERE `parent-uri` = '%s' AND `uid` = %d ",
307 dbesc(datetime_convert()),
308 dbesc(datetime_convert()),
309 dbesc($item['parent-uri']),
315 // ensure that last-child is set in case the comment that had it just got wiped.
316 q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d ",
317 dbesc(datetime_convert()),
318 dbesc($item['parent-uri']),
321 // who is the last child now?
322 $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",
323 dbesc($item['parent-uri']),
327 q("UPDATE `item` SET `last-child` = 1 WHERE `id` = %d LIMIT 1",
332 $drop_id = intval($item['id']);
333 $php_path = ((strlen($a->config['php_path'])) ? $a->config['php_path'] : 'php');
334 $proc_debug = get_config('system','proc_debug');
337 // send the notification upstream/downstream as the case may be
339 proc_close(proc_open("\"$php_path\" \"include/notifier.php\" \"drop\" \"$drop_id\" $proc_debug &",
342 goaway($a->get_baseurl() . '/' . $_SESSION['return_url']);
346 notice( t('Permission denied.') . EOL);
347 goaway($a->get_baseurl() . '/' . $_SESSION['return_url']);