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