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