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