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