]> git.mxchange.org Git - friendica.git/blob - mod/item.php
edit posting after submission
[friendica.git] / mod / item.php
1 <?php
2
3 /**
4  *
5  * This is the POST destination for most all locally posted
6  * text stuff. This function handles status, wall-to-wall status, 
7  * local comments, and remote coments - that are posted on this site 
8  * (as opposed to being delivered in a feed).
9  * All of these become an "item" which is our basic unit of 
10  * information.
11  * Posts that originate externally or do not fall into the above 
12  * posting categories go through item_store() instead of this function. 
13  *
14  */  
15
16 function item_post(&$a) {
17
18         if((! local_user()) && (! remote_user()))
19                 return;
20
21         require_once('include/security.php');
22
23         $uid = local_user();
24
25         call_hooks('post_local_start', $_POST);
26
27         $parent = ((x($_POST,'parent')) ? intval($_POST['parent']) : 0);
28
29         $parent_item = null;
30         $parent_contact = null;
31
32         if($parent) {
33                 $r = q("SELECT * FROM `item` WHERE `id` = %d LIMIT 1",
34                         intval($parent)
35                 );
36                 if(! count($r)) {
37                         notice( t('Unable to locate original post.') . EOL);
38                         if(x($_POST,'return')) 
39                                 goaway($a->get_baseurl() . "/" . $_POST['return'] );
40                         killme();
41                 }
42                 $parent_item = $r[0];
43                 if($parent_item['contact-id'] && $uid) {
44                         $r = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
45                                 intval($parent_item['contact-id']),
46                                 intval($uid)
47                         );
48                         if(count($r))
49                                 $parent_contact = $r[0];
50                 }
51         }
52
53         $profile_uid = ((x($_POST,'profile_uid')) ? intval($_POST['profile_uid']) : 0);
54         $post_id = ((x($_POST['post_id'])) ? intval($_POST['post_id']) : 0);
55
56         if(! can_write_wall($a,$profile_uid)) {
57                 notice( t('Permission denied.') . EOL) ;
58                 if(x($_POST,'return')) 
59                         goaway($a->get_baseurl() . "/" . $_POST['return'] );
60                 killme();
61         }
62
63         $user = null;
64
65         $r = q("SELECT * FROM `user` WHERE `uid` = %d LIMIT 1",
66                 intval($profile_uid)
67         );
68         if(count($r))
69                 $user = $r[0];
70         
71
72         $str_group_allow   = perms2str($_POST['group_allow']);
73         $str_contact_allow = perms2str($_POST['contact_allow']);
74         $str_group_deny    = perms2str($_POST['group_deny']);
75         $str_contact_deny  = perms2str($_POST['contact_deny']);
76
77         $private = ((strlen($str_group_allow) || strlen($str_contact_allow) || strlen($str_group_deny) || strlen($str_contact_deny)) ? 1 : 0);
78
79         if(($parent_item) && 
80                 (($parent_item['private']) 
81                         || strlen($parent_item['allow_cid']) 
82                         || strlen($parent_item['allow_gid']) 
83                         || strlen($parent_item['deny_cid']) 
84                         || strlen($parent_item['deny_gid'])
85                 )
86         ) {
87                 $private = 1;
88         }
89
90         $title             = notags(trim($_POST['title']));
91         $body              = escape_tags(trim($_POST['body']));
92         $location          = notags(trim($_POST['location']));
93         $coord             = notags(trim($_POST['coord']));
94         $verb              = notags(trim($_POST['verb']));
95         $emailcc           = notags(trim($_POST['emailcc']));
96
97         if(! strlen($body)) {
98                 notice( t('Empty post discarded.') . EOL );
99                 if(x($_POST,'return')) 
100                         goaway($a->get_baseurl() . "/" . $_POST['return'] );
101                 killme();
102         }
103
104         // get contact info for poster
105
106         $author = null;
107         $self   = false;
108
109         if(($_SESSION['uid']) && ($_SESSION['uid'] == $profile_uid)) {
110                 $self = true;
111                 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 1 LIMIT 1",
112                         intval($_SESSION['uid'])
113                 );
114         }
115         else {
116                 if((x($_SESSION,'visitor_id')) && (intval($_SESSION['visitor_id']))) {
117                         $r = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
118                                 intval($_SESSION['visitor_id'])
119                         );
120                 }
121         }
122
123         if(count($r)) {
124                 $author = $r[0];
125                 $contact_id = $author['id'];
126         }
127
128         // get contact info for owner
129         
130         if($profile_uid == $_SESSION['uid']) {
131                 $contact_record = $author;
132         }
133         else {
134                 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 1 LIMIT 1",
135                         intval($profile_uid)
136                 );
137                 if(count($r))
138                         $contact_record = $r[0];
139         }
140
141         $post_type = notags(trim($_POST['type']));
142
143         if($post_type === 'net-comment') {
144                 if($parent_item !== null) {
145                         if($parent_item['type'] === 'remote') {
146                                 $post_type = 'remote-comment';
147                         } 
148                         else {          
149                                 $post_type = 'wall-comment';
150                         }
151                 }
152         }
153
154         // is this an edited post?
155
156         $orig_post = null;
157
158         if($post_id) {
159                 $i = q("SELECT * FROM `item` WHERE `uid` = %d AND `id` = %d LIMIT 1",
160                         intval($profile_uid),
161                         intval($post_id)
162                 );
163                 if(! count($i))
164                         killme();
165                 $orig_post = $i[0];
166         }
167
168         if($orig_post) {
169                 $str_group_allow   = $orig_post['allow_gid'];
170                 $str_contact_allow = $orig_post['allow_cid'];
171                 $str_group_deny    = $orig_post['deny_gid'];
172                 $str_contact_deny  = $orig_post['deny_cid'];
173                 $private           = $orig_post['private'];
174                 $title             = $orig_post['title'];
175                 $location          = $orig_post['location'];
176                 $coord             = $orig_post['coord'];
177                 $verb              = $orig_post['verb'];
178                 $emailcc           = $orig_post['emailcc'];
179
180                 $body              = escape_tags(trim($_POST['body']));
181         }
182
183
184         /**
185          *
186          * When a photo was uploaded into the message using the (profile wall) ajax 
187          * uploader, The permissions are initially set to disallow anybody but the
188          * owner from seeing it. This is because the permissions may not yet have been
189          * set for the post. If it's private, the photo permissions should be set
190          * appropriately. But we didn't know the final permissions on the post until
191          * now. So now we'll look for links of uploaded messages that are in the
192          * post and set them to the same permissions as the post itself.
193          *
194          */
195
196         $match = null;
197
198         if(preg_match_all("/\[img\](.+?)\[\/img\]/",$body,$match)) {
199                 $images = $match[1];
200                 if(count($images)) {
201                         foreach($images as $image) {
202                                 if(! stristr($image,$a->get_baseurl() . '/photo/'))
203                                         continue;
204                                 $image_uri = substr($image,strrpos($image,'/') + 1);
205                                 $image_uri = substr($image_uri,0, strpos($image_uri,'-'));
206                                 $r = q("UPDATE `photo` SET `allow_cid` = '%s', `allow_gid` = '%s', `deny_cid` = '%s', `deny_gid` = '%s'
207                                         WHERE `resource-id` = '%s' AND `album` = '%s' ",
208                                         dbesc($str_contact_allow),
209                                         dbesc($str_group_allow),
210                                         dbesc($str_contact_deny),
211                                         dbesc($str_group_deny),
212                                         dbesc($image_uri),
213                                         dbesc( t('Wall Photos'))
214                                 );
215  
216                         }
217                 }
218         }
219
220         /**
221          * Fold multi-line [code] sequences
222          */
223
224         $body = preg_replace('/\[\/code\]\s*\[code\]/m',"\n",$body); 
225
226         /**
227          * Look for any tags and linkify them
228          */
229
230         $str_tags = '';
231         $inform   = '';
232
233
234         $tags = get_tags($body);
235
236         if(($parent_contact) && ($parent_contact['network'] === 'stat') && ($parent_contact['nick']) && (! in_array('@' . $parent_contact['nick'],$tags))) {
237                 $body = '@' . $parent_contact['nick'] . ' ' . $body;
238                 $tags[] = '@' . $parent_contact['nick'];
239         }               
240
241         if(count($tags)) {
242                 foreach($tags as $tag) {
243                         if(strpos($tag,'#') === 0) {
244                                 if(strpos($tag,'[url='))
245                                         continue;
246                                 $basetag = str_replace('_',' ',substr($tag,1));
247                                 $body = str_replace($tag,'#[url=' . $a->get_baseurl() . '/search?search=' . rawurlencode($basetag) . ']' . $basetag . '[/url]',$body);
248                                 if(strlen($str_tags))
249                                         $str_tags .= ',';
250                                 $str_tags .= '#[url=' . $a->get_baseurl() . '/search?search=' . rawurlencode($basetag) . ']' . $basetag . '[/url]';
251                                 continue;
252                         }
253                         if(strpos($tag,'@') === 0) {
254                                 if(strpos($tag,'[url='))
255                                         continue;
256                                 $stat = false;
257                                 $name = substr($tag,1);
258                                 if((strpos($name,'@')) || (strpos($name,'http://'))) {
259                                         $newname = $name;
260                                         $links = @lrdd($name);
261                                         if(count($links)) {
262                                                 foreach($links as $link) {
263                                                         if($link['@attributes']['rel'] === 'http://webfinger.net/rel/profile-page')
264                                         $profile = $link['@attributes']['href'];
265                                                         if($link['@attributes']['rel'] === 'salmon') {
266                                                                 if(strlen($inform))
267                                                                         $inform .= ',';
268                                         $inform .= 'url:' . str_replace(',','%2c',$link['@attributes']['href']);
269                                                         }
270                                                 }
271                                         }
272                                 }
273                                 else {
274                                         $newname = $name;
275                                         $alias = '';
276                                         if(strstr($name,'_')) {
277                                                 $newname = str_replace('_',' ',$name);
278                                                 $r = q("SELECT * FROM `contact` WHERE `name` = '%s' AND `uid` = %d LIMIT 1",
279                                                         dbesc($newname),
280                                                         intval($profile_uid)
281                                                 );
282                                         }
283                                         else {
284                                                 $r = q("SELECT * FROM `contact` WHERE `nick` = '%s' AND `uid` = %d LIMIT 1",
285                                                         dbesc($name),
286                                                         intval($profile_uid)
287                                                 );
288                                         }
289                                         if(count($r)) {
290                                                 $profile = $r[0]['url'];
291                                                 if($r[0]['network'] === 'stat') {
292                                                         $newname = $r[0]['nick'];
293                                                         $stat = true;
294                                                         if($r[0]['alias'])
295                                                                 $alias = $r[0]['alias'];
296                                                 }
297                                                 else
298                                                         $newname = $r[0]['name'];
299                                                 if(strlen($inform))
300                                                         $inform .= ',';
301                                                 $inform .= 'cid:' . $r[0]['id'];
302                                         }
303                                 }
304                                 if($profile) {
305                                         $body = str_replace('@' . $name, '@' . '[url=' . $profile . ']' . $newname      . '[/url]', $body);
306                                         $profile = str_replace(',','%2c',$profile);
307                                         if(strlen($str_tags))
308                                                 $str_tags .= ',';
309                                         $str_tags .= '@[url=' . $profile . ']' . $newname       . '[/url]';
310
311                                         // Status.Net seems to require the numeric ID URL in a mention if the person isn't 
312                                         // subscribed to you. But the nickname URL is OK if they are. Grrr. We'll tag both. 
313
314                                         if(strlen($alias)) {
315                                                 if(strlen($str_tags))
316                                                         $str_tags .= ',';
317                                                 $str_tags .= '@[url=' . $alias . ']' . $newname . '[/url]';
318                                         }
319
320                                 }
321                         }
322                 }
323         }
324
325
326         if($orig_post) {
327                 $r = q("UPDATE `item` SET `body` = '%s', `edited` = '%s' WHERE `id` = %d AND `uid` = %d LIMIT 1",
328                         dbesc($body),
329                         dbesc(datetime_convert()),
330                         intval($post_id),
331                         intval($profile_uid)
332                 );
333
334                 proc_run('php', "include/notifier.php", 'edit_post', "$post_id");
335                 if((x($_POST,'return')) && strlen($_POST['return'])) {
336                         logger('return: ' . $_POST['return']);
337                         goaway($a->get_baseurl() . "/" . $_POST['return'] );
338                 }
339                 killme();
340         }
341
342         $post_id = 0;
343         $wall = 0;
344
345         if($post_type === 'wall' || $post_type === 'wall-comment')
346                 $wall = 1;
347
348         if(! strlen($verb))
349                 $verb = ACTIVITY_POST ;
350
351         $gravity = (($parent) ? 6 : 0 );
352  
353         $notify_type = (($parent) ? 'comment-new' : 'wall-new' );
354
355         $uri = item_new_uri($a->get_hostname(),$profile_uid);
356
357         $datarray = array();
358         $datarray['uid']           = $profile_uid;
359         $datarray['type']          = $post_type;
360         $datarray['wall']          = $wall;
361         $datarray['gravity']       = $gravity;
362         $datarray['contact-id']    = $contact_id;
363         $datarray['owner-name']    = $contact_record['name'];
364         $datarray['owner-link']    = $contact_record['url'];
365         $datarray['owner-avatar']  = $contact_record['thumb'];
366         $datarray['author-name']   = $author['name'];
367         $datarray['author-link']   = $author['url'];
368         $datarray['author-avatar'] = $author['thumb'];
369         $datarray['created']       = datetime_convert();
370         $datarray['edited']        = datetime_convert();
371         $datarray['changed']       = datetime_convert();
372         $datarray['uri']           = $uri;
373         $datarray['title']         = $title;
374         $datarray['body']          = $body;
375         $datarray['location']      = $location;
376         $datarray['coord']         = $coord;
377         $datarray['tag']           = $str_tags;
378         $datarray['inform']        = $inform;
379         $datarray['verb']          = $verb;
380         $datarray['allow_cid']     = $str_contact_allow;
381         $datarray['allow_gid']     = $str_group_allow;
382         $datarray['deny_cid']      = $str_contact_deny;
383         $datarray['deny_gid']      = $str_group_deny;
384         $datarray['private']       = $private;
385
386         /**
387          * These fields are for the convenience of plugins...
388          * 'self' if true indicates the owner is posting on their own wall
389          * If parent is 0 it is a top-level post.
390          */
391
392         $datarray['parent']        = $parent;
393         $datarray['self']          = $self;
394
395
396         call_hooks('post_local',$datarray);
397
398         $r = q("INSERT INTO `item` (`uid`,`type`,`wall`,`gravity`,`contact-id`,`owner-name`,`owner-link`,`owner-avatar`, 
399                 `author-name`, `author-link`, `author-avatar`, `created`, `edited`, `changed`, `uri`, `title`, `body`, `location`, `coord`, 
400                 `tag`, `inform`, `verb`, `allow_cid`, `allow_gid`, `deny_cid`, `deny_gid`, `private` )
401                 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 )",
402                 intval($datarray['uid']),
403                 dbesc($datarray['type']),
404                 intval($datarray['wall']),
405                 intval($datarray['gravity']),
406                 intval($datarray['contact-id']),
407                 dbesc($datarray['owner-name']),
408                 dbesc($datarray['owner-link']),
409                 dbesc($datarray['owner-avatar']),
410                 dbesc($datarray['author-name']),
411                 dbesc($datarray['author-link']),
412                 dbesc($datarray['author-avatar']),
413                 dbesc($datarray['created']),
414                 dbesc($datarray['edited']),
415                 dbesc($datarray['changed']),
416                 dbesc($datarray['uri']),
417                 dbesc($datarray['title']),
418                 dbesc($datarray['body']),
419                 dbesc($datarray['location']),
420                 dbesc($datarray['coord']),
421                 dbesc($datarray['tag']),
422                 dbesc($datarray['inform']),
423                 dbesc($datarray['verb']),
424                 dbesc($datarray['allow_cid']),
425                 dbesc($datarray['allow_gid']),
426                 dbesc($datarray['deny_cid']),
427                 dbesc($datarray['deny_gid']),
428                 intval($datarray['private'])
429         );
430
431         $r = q("SELECT `id` FROM `item` WHERE `uri` = '%s' LIMIT 1",
432                 dbesc($datarray['uri']));
433         if(count($r)) {
434                 $post_id = $r[0]['id'];
435                 logger('mod_item: saved item ' . $post_id);
436
437                 if($parent) {
438
439                         // This item is the last leaf and gets the comment box, clear any ancestors
440                         $r = q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent` = %d ",
441                                 dbesc(datetime_convert()),
442                                 intval($parent)
443                         );
444
445                         // Inherit ACL's from the parent item.
446
447                         $r = q("UPDATE `item` SET `allow_cid` = '%s', `allow_gid` = '%s', `deny_cid` = '%s', `deny_gid` = '%s', `private` = %d
448                                 WHERE `id` = %d LIMIT 1",
449                                 dbesc($parent_item['allow_cid']),
450                                 dbesc($parent_item['allow_gid']),
451                                 dbesc($parent_item['deny_cid']),
452                                 dbesc($parent_item['deny_gid']),
453                                 intval($parent_item['private']),
454                                 intval($post_id)
455                         );
456
457                         // Send a notification email to the conversation owner, unless the owner is me and I wrote this item
458                         if(($user['notify-flags'] & NOTIFY_COMMENT) && ($contact_record != $author)) {
459                                 require_once('bbcode.php');
460                                 $from = $author['name'];
461                                 $tpl = load_view_file('view/cmnt_received_eml.tpl');                    
462                                 $email_tpl = replace_macros($tpl, array(
463                                         '$sitename' => $a->config['sitename'],
464                                         '$siteurl' =>  $a->get_baseurl(),
465                                         '$username' => $user['username'],
466                                         '$email' => $user['email'],
467                                         '$from' => $from,
468                                         '$display' => $a->get_baseurl() . '/display/' . $user['nickname'] . '/' . $post_id,
469                                         '$body' => strip_tags(bbcode($datarray['body']))
470                                 ));
471
472                                 $res = mail($user['email'], $from . t(" commented on your item at ") . $a->config['sitename'],
473                                         $email_tpl,t("From: Administrator@") . $a->get_hostname() );
474                         }
475                 }
476                 else {
477                         $parent = $post_id;
478
479                         // let me know if somebody did a wall-to-wall post on my profile
480
481                         if(($user['notify-flags'] & NOTIFY_WALL) && ($contact_record != $author)) {
482                                 require_once('bbcode.php');
483                                 $from = $author['name'];
484                                 $tpl = load_view_file('view/wall_received_eml.tpl');                    
485                                 $email_tpl = replace_macros($tpl, array(
486                                         '$sitename' => $a->config['sitename'],
487                                         '$siteurl' =>  $a->get_baseurl(),
488                                         '$username' => $user['username'],
489                                         '$email' => $user['email'],
490                                         '$from' => $from,
491                                         '$display' => $a->get_baseurl() . '/display/' . $user['nickname'] . '/' . $post_id,
492                                         '$body' => strip_tags(bbcode($datarray['body']))
493                                 ));
494
495                                 $res = mail($user['email'], $from . t(" posted on your profile wall at ") . $a->config['sitename'],
496                                         $email_tpl,t("From: Administrator@") . $a->get_hostname() );
497                         }
498                 }
499
500                 $r = q("UPDATE `item` SET `parent` = %d, `parent-uri` = '%s', `plink` = '%s', `changed` = '%s', `last-child` = 1, `visible` = 1
501                         WHERE `id` = %d LIMIT 1",
502                         intval($parent),
503                         dbesc(($parent == $post_id) ? $uri : $parent_item['uri']),
504                         dbesc($a->get_baseurl() . '/display/' . $user['nickname'] . '/' . $post_id),
505                         dbesc(datetime_convert()),
506                         intval($post_id)
507                 );
508
509                 // photo comments turn the corresponding item visible to the profile wall
510                 // This way we don't see every picture in your new photo album posted to your wall at once.
511                 // They will show up as people comment on them.
512
513                 if(! $parent_item['visible']) {
514                         $r = q("UPDATE `item` SET `visible` = 1 WHERE `id` = %d LIMIT 1",
515                                 intval($parent_item['id'])
516                         );
517                 }
518         }
519         else {
520                 logger('mod_item: unable to retrieve post that was just stored.');
521                 notify( t('System error. Post not saved.'));
522                 goaway($a->get_baseurl() . "/" . $_POST['return'] );
523                 // NOTREACHED
524         }
525
526         proc_run('php', "include/notifier.php", $notify_type, "$post_id");
527
528         $datarray['id'] = $post_id;
529
530         call_hooks('post_local_end', $datarray);
531
532         if(strlen($emailcc) && $profile_uid == local_user()) {
533                 $erecips = explode(',', $emailcc);
534                 if(count($erecips)) {
535                         foreach($erecips as $recip) {
536                                 $addr = trim($recip);
537                                 if(! strlen($addr))
538                                         continue;
539                                 $disclaimer = '<hr />' . t('This message was sent to you by ') . $a->user['username'] 
540                                         . t(', a member of the Friendika social network.') . '<br />';
541                                 $disclaimer .= t('You may visit them online at') . ' ' 
542                                         . $a->get_baseurl() . '/profile/' . $a->user['nickname'] . '<br />';
543                                 $disclaimer .= t('Please contact the sender by replying to this post if you do not wish to receive these messages.') . '<br />'; 
544
545                                 $subject  = '[Friendika]' . ' ' . $a->user['username'] . ' ' . t('posted an update.');
546                                 $headers  = 'From: ' . $a->user['username'] . ' <' . $a->user['email'] . '>' . "\n";
547                                 $headers .= 'MIME-Version: 1.0' . "\n";
548                                 $headers .= 'Content-Type: text/html; charset=UTF-8' . "\n";
549                                 $headers .= 'Content-Transfer-Encoding: 8bit' . "\n\n";
550                                 $link = '<a href="' . $a->get_baseurl() . '/profile/' . $a->user['nickname'] . '"><img src="' . $author['thumb'] . '" alt="' . $a->user['username'] . '" /></a><br /><br />';
551                                 $html    = prepare_body($datarray);
552                                 $message = '<html><body>' . $link . $html . $disclaimer . '</body></html>';
553                                 @mail($addr, $subject, $message, $headers);
554                         }
555                 }
556         }
557
558         logger('post_complete');
559         if((x($_POST,'return')) && strlen($_POST['return'])) {
560                 logger('return: ' . $_POST['return']);
561                 goaway($a->get_baseurl() . "/" . $_POST['return'] );
562         }
563         $json = array('success' => 1);
564         if(x($_POST,'jsreload') && strlen($_POST['jsreload']))
565                 $json['reload'] = $a->get_baseurl() . '/' . $_POST['jsreload'];
566
567         logger('post_json: ' . print_r($json,true), LOGGER_DEBUG);
568
569         echo json_encode($json);
570         killme();
571         // NOTREACHED
572 }
573
574
575
576
577
578 function item_content(&$a) {
579
580         if((! local_user()) && (! remote_user()))
581                 return;
582
583         require_once('include/security.php');
584
585         $uid = local_user();
586
587         if(($a->argc == 3) && ($a->argv[1] === 'drop') && intval($a->argv[2])) {
588
589                 // locate item to be deleted
590
591                 $r = q("SELECT * FROM `item` WHERE `id` = %d LIMIT 1",
592                         intval($a->argv[2])
593                 );
594
595                 if(! count($r)) {
596                         notice( t('Item not found.') . EOL);
597                         goaway($a->get_baseurl() . '/' . $_SESSION['return_url']);
598                 }
599                 $item = $r[0];
600
601                 // check if logged in user is either the author or owner of this item
602
603                 if(($_SESSION['visitor_id'] == $item['contact-id']) || ($_SESSION['uid'] == $item['uid'])) {
604
605                         // delete the item
606
607                         $r = q("UPDATE `item` SET `deleted` = 1, `body` = '', `edited` = '%s', `changed` = '%s' WHERE `id` = %d LIMIT 1",
608                                 dbesc(datetime_convert()),
609                                 dbesc(datetime_convert()),
610                                 intval($item['id'])
611                         );
612
613                         // If item is a link to a photo resource, nuke all the associated photos 
614                         // (visitors will not have photo resources)
615                         // This only applies to photos uploaded from the photos page. Photos inserted into a post do not
616                         // generate a resource-id and therefore aren't intimately linked to the item. 
617
618                         if(strlen($item['resource-id'])) {
619                                 q("DELETE FROM `photo` WHERE `resource-id` = '%s' AND `uid` = %d ",
620                                         dbesc($item['resource-id']),
621                                         intval($item['uid'])
622                                 );
623                                 // ignore the result
624                         }
625
626                         // If it's the parent of a comment thread, kill all the kids
627
628                         if($item['uri'] == $item['parent-uri']) {
629                                 $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s', `body` = '' 
630                                         WHERE `parent-uri` = '%s' AND `uid` = %d ",
631                                         dbesc(datetime_convert()),
632                                         dbesc(datetime_convert()),
633                                         dbesc($item['parent-uri']),
634                                         intval($item['uid'])
635                                 );
636                                 // ignore the result
637                         }
638                         else {
639                                 // ensure that last-child is set in case the comment that had it just got wiped.
640                                 q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d ",
641                                         dbesc(datetime_convert()),
642                                         dbesc($item['parent-uri']),
643                                         intval($item['uid'])
644                                 );
645                                 // who is the last child now? 
646                                 $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",
647                                         dbesc($item['parent-uri']),
648                                         intval($item['uid'])
649                                 );
650                                 if(count($r)) {
651                                         q("UPDATE `item` SET `last-child` = 1 WHERE `id` = %d LIMIT 1",
652                                                 intval($r[0]['id'])
653                                         );
654                                 }       
655                         }
656                         $drop_id = intval($item['id']);
657                         
658                         // send the notification upstream/downstream as the case may be
659
660                         proc_run('php',"include/notifier.php","drop","$drop_id");
661
662                         goaway($a->get_baseurl() . '/' . $_SESSION['return_url']);
663                         //NOTREACHED
664                 }
665                 else {
666                         notice( t('Permission denied.') . EOL);
667                         goaway($a->get_baseurl() . '/' . $_SESSION['return_url']);
668                         //NOTREACHED
669                 }
670         }
671 }