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