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