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