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