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