]> git.mxchange.org Git - friendica.git/blob - mod/content.php
Bugfix: The user id wasn't fetched correctly.
[friendica.git] / mod / content.php
1 <?php
2
3 // This is a purely experimental module and is not yet generally useful.
4
5 // The eventual goal is to provide a json backend to fetch content and fill the current page.
6 // The page will be filled in on the frontend using javascript.
7 // At the present time this page is based on "network", but the hope is to extend to serving
8 // any content (wall, community, search, etc.).
9 // All search parameters, etc. will be managed in javascript and sent as request params.
10 // Security will be managed on the backend.
11 // There is no "pagination query", but we will manage the "current page" on the client
12 // and provide a link to fetch the next page - until there are no pages left to fetch.
13
14 // With the exception of complex tag and text searches, this prototype is incredibly 
15 // fast - e.g. one or two milliseconds to fetch parent items for the current content, 
16 // and 10-20 milliseconds to fetch all the child items.
17
18
19 function content_content(&$a, $update = 0) {
20
21         require_once('include/conversation.php');
22
23
24         // Currently security is based on the logged in user
25
26         if(! local_user()) {
27                 return;
28         }
29
30         $arr = array('query' => $a->query_string);
31
32         call_hooks('content_content_init', $arr);
33
34
35         $datequery = $datequery2 = '';
36
37         $group = 0;
38
39         $nouveau = false;
40
41         if($a->argc > 1) {
42                 for($x = 1; $x < $a->argc; $x ++) {
43                         if(is_a_date_arg($a->argv[$x])) {
44                                 if($datequery)
45                                         $datequery2 = escape_tags($a->argv[$x]);
46                                 else {
47                                         $datequery = escape_tags($a->argv[$x]);
48                                         $_GET['order'] = 'post';
49                                 }
50                         }
51                         elseif($a->argv[$x] === 'new') {
52                                 $nouveau = true;
53                         }
54                         elseif(intval($a->argv[$x])) {
55                                 $group = intval($a->argv[$x]);
56                                 $def_acl = array('allow_gid' => '<' . $group . '>');
57                         }
58                 }
59         }
60
61
62         $o = '';
63
64         
65
66         $contact_id = $a->cid;
67
68         require_once('include/acl_selectors.php');
69
70         $cid = ((x($_GET,'cid')) ? intval($_GET['cid']) : 0);
71         $star = ((x($_GET,'star')) ? intval($_GET['star']) : 0);
72         $bmark = ((x($_GET,'bmark')) ? intval($_GET['bmark']) : 0);
73         $order = ((x($_GET,'order')) ? notags($_GET['order']) : 'comment');
74         $liked = ((x($_GET,'liked')) ? intval($_GET['liked']) : 0);
75         $conv = ((x($_GET,'conv')) ? intval($_GET['conv']) : 0);
76         $spam = ((x($_GET,'spam')) ? intval($_GET['spam']) : 0);
77         $nets = ((x($_GET,'nets')) ? $_GET['nets'] : '');
78         $cmin = ((x($_GET,'cmin')) ? intval($_GET['cmin']) : 0);
79         $cmax = ((x($_GET,'cmax')) ? intval($_GET['cmax']) : 99);
80         $file = ((x($_GET,'file')) ? $_GET['file'] : '');
81
82
83
84         if(x($_GET,'search') || x($_GET,'file'))
85                 $nouveau = true;
86         if($cid)
87                 $def_acl = array('allow_cid' => '<' . intval($cid) . '>');
88
89         if($nets) {
90                 $r = q("select id from contact where uid = %d and network = '%s' and self = 0",
91                         intval(local_user()),
92                         dbesc($nets)
93                 );
94
95                 $str = '';
96                 if(count($r))
97                         foreach($r as $rr)
98                                 $str .= '<' . $rr['id'] . '>';
99                 if(strlen($str))
100                         $def_acl = array('allow_cid' => $str);
101         }
102
103         
104         $sql_options  = (($star) ? " and starred = 1 " : '');
105         $sql_options .= (($bmark) ? " and bookmark = 1 " : '');
106
107         $sql_nets = (($nets) ? sprintf(" and `contact`.`network` = '%s' ", dbesc($nets)) : '');
108
109         $sql_extra = " AND `item`.`parent` IN ( SELECT `parent` FROM `item` WHERE `id` = `parent` $sql_options ) ";
110
111         if($group) {
112                 $r = q("SELECT `name`, `id` FROM `group` WHERE `id` = %d AND `uid` = %d LIMIT 1",
113                         intval($group),
114                         intval($_SESSION['uid'])
115                 );
116                 if(! count($r)) {
117                         if($update)
118                                 killme();
119                         notice( t('No such group') . EOL );
120                         goaway($a->get_baseurl(true) . '/network');
121                         // NOTREACHED
122                 }
123
124                 $contacts = expand_groups(array($group));
125                 if((is_array($contacts)) && count($contacts)) {
126                         $contact_str = implode(',',$contacts);
127                 }
128                 else {
129                                 $contact_str = ' 0 ';
130                                 info( t('Group is empty'));
131                 }
132
133                 $sql_extra = " AND `item`.`parent` IN ( SELECT DISTINCT(`parent`) FROM `item` WHERE 1 $sql_options AND ( `contact-id` IN ( $contact_str ) OR `allow_gid` like '" . protect_sprintf('%<' . intval($group) . '>%') . "' ) and deleted = 0 ) ";
134                 $o = replace_macros(get_markup_template("section_title.tpl"),array(
135                         '$title' => sprintf( t('Group: %s'), $r[0]['name'])
136                 )) . $o;
137         }
138         elseif($cid) {
139
140                 $r = q("SELECT `id`,`name`,`network`,`writable`,`nurl` FROM `contact` WHERE `id` = %d 
141                                 AND `blocked` = 0 AND `pending` = 0 LIMIT 1",
142                         intval($cid)
143                 );
144                 if(count($r)) {
145                         $sql_extra = " AND `item`.`parent` IN ( SELECT DISTINCT(`parent`) FROM `item` WHERE 1 $sql_options AND `contact-id` = " . intval($cid) . " and deleted = 0 ) ";
146
147                 }
148                 else {
149                         killme();
150                 }
151         }
152
153
154         $sql_extra3 = '';
155
156         if($datequery) {
157                 $sql_extra3 .= protect_sprintf(sprintf(" AND item.created <= '%s' ", dbesc(datetime_convert(date_default_timezone_get(),'',$datequery))));
158         }
159         if($datequery2) {
160                 $sql_extra3 .= protect_sprintf(sprintf(" AND item.created >= '%s' ", dbesc(datetime_convert(date_default_timezone_get(),'',$datequery2))));
161         }
162
163         $sql_extra2 = (($nouveau) ? '' : " AND `item`.`parent` = `item`.`id` ");
164         $sql_extra3 = (($nouveau) ? '' : $sql_extra3);
165         $sql_table = "`item`";
166
167         if(x($_GET,'search')) {
168                 $search = escape_tags($_GET['search']);
169
170                 if(strpos($search,'#') === 0) {
171                         $tag = true;
172                         $search = substr($search,1);
173                 }
174
175                 if (get_config('system','only_tag_search'))
176                         $tag = true;
177
178                 if($tag) {
179                         //$sql_extra = sprintf(" AND `term`.`term` = '%s' AND `term`.`otype` = %d AND `term`.`type` = %d ",
180                         //      dbesc(protect_sprintf($search)), intval(TERM_OBJ_POST), intval(TERM_HASHTAG));
181                         //$sql_table = "`term` INNER JOIN `item` ON `item`.`id` = `term`.`oid` AND `item`.`uid` = `term`.`uid` ";
182
183                         $sql_extra = "";
184                         $sql_table = sprintf("`item` INNER JOIN (SELECT `oid` FROM `term` WHERE `term` = '%s' AND `otype` = %d AND `type` = %d AND `uid` = %d ORDER BY `tid` DESC) AS `term` ON `item`.`id` = `term`.`oid` ",
185                                 dbesc(protect_sprintf($search)), intval(TERM_OBJ_POST), intval(TERM_HASHTAG), intval(local_user()));
186
187                 } else {
188                         if (get_config('system','use_fulltext_engine'))
189                                 $sql_extra = sprintf(" AND MATCH (`item`.`body`, `item`.`title`) AGAINST ('%s' in boolean mode) ", dbesc(protect_sprintf($search)));
190                         else
191                                 $sql_extra = sprintf(" AND `item`.`body` REGEXP '%s' ", dbesc(protect_sprintf(preg_quote($search))));
192                 }
193
194         }
195         if(strlen($file)) {
196                 $sql_extra .= file_tag_file_query('item',unxmlify($file));
197         }
198
199         if($conv) {
200                 $myurl = $a->get_baseurl() . '/profile/'. $a->user['nickname'];
201                 $myurl = substr($myurl,strpos($myurl,'://')+3);
202                 $myurl = str_replace('www.','',$myurl);
203                 $diasp_url = str_replace('/profile/','/u/',$myurl);
204
205                 $sql_extra .= sprintf(" AND `item`.`parent` IN (SELECT distinct(`parent`) from item where `author-link` IN ('https://%s', 'http://%s') OR `mention`)",
206                         dbesc(protect_sprintf($myurl)),
207                         dbesc(protect_sprintf($myurl))
208                 );
209         }
210
211         $pager_sql = sprintf(" LIMIT %d, %d ",intval($a->pager['start']), intval($a->pager['itemspage']));
212
213
214         if($nouveau) {
215                 // "New Item View" - show all items unthreaded in reverse created date order
216
217                 $items = q("SELECT `item`.*, `item`.`id` AS `item_id`,
218                         `contact`.`name`, `contact`.`photo`, `contact`.`url`, `contact`.`rel`, `contact`.`writable`,
219                         `contact`.`network`, `contact`.`thumb`, `contact`.`dfrn-id`, `contact`.`self`,
220                         `contact`.`id` AS `cid`, `contact`.`uid` AS `contact-uid`
221                         FROM $sql_table INNER JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
222                         WHERE `item`.`uid` = %d AND `item`.`visible` = 1
223                         AND `item`.`deleted` = 0 and `item`.`moderated` = 0
224                         $simple_update
225                         AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
226                         $sql_extra $sql_nets
227                         ORDER BY `item`.`received` DESC $pager_sql ",
228                         intval($_SESSION['uid'])
229                 );
230
231         }
232         else {
233
234                 // Normal conversation view
235
236
237                 if($order === 'post')
238                                 $ordering = "`created`";
239                 else
240                                 $ordering = "`commented`";
241
242                 $start = dba_timer();
243
244                 $r = q("SELECT `item`.`id` AS `item_id`, `contact`.`uid` AS `contact_uid`
245                         FROM $sql_table INNER JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
246                         WHERE `item`.`uid` = %d AND `item`.`visible` = 1 AND `item`.`deleted` = 0
247                         AND `item`.`moderated` = 0 AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
248                         AND `item`.`parent` = `item`.`id`
249                         $sql_extra3 $sql_extra $sql_nets
250                         ORDER BY `item`.$ordering DESC $pager_sql ",
251                         intval(local_user())
252                 );
253
254                 $first = dba_timer();
255
256
257                 // Then fetch all the children of the parents that are on this page
258
259                 $parents_arr = array();
260                 $parents_str = '';
261
262                 if(count($r)) {
263                         foreach($r as $rr)
264                                 if(! in_array($rr['item_id'],$parents_arr))
265                                         $parents_arr[] = $rr['item_id'];
266                         $parents_str = implode(', ', $parents_arr);
267
268                         $items = q("SELECT `item`.*, `item`.`id` AS `item_id`,
269                                 `contact`.`name`, `contact`.`photo`, `contact`.`url`, `contact`.`alias`, `contact`.`rel`, `contact`.`writable`,
270                                 `contact`.`network`, `contact`.`thumb`, `contact`.`dfrn-id`, `contact`.`self`,
271                                 `contact`.`id` AS `cid`, `contact`.`uid` AS `contact-uid`
272                                 FROM $sql_table INNER JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
273                                 WHERE `item`.`uid` = %d AND `item`.`visible` = 1 AND `item`.`deleted` = 0
274                                 AND `item`.`moderated` = 0
275                                 AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
276                                 AND `item`.`parent` IN ( %s )
277                                 $sql_extra ",
278                                 intval(local_user()),
279                                 dbesc($parents_str)
280                         );
281
282                         $second = dba_timer();
283
284                         $items = conv_sort($items,$ordering);
285
286                 } else {
287                         $items = array();
288                 }
289         }
290
291
292         logger('parent dba_timer: ' . sprintf('%01.4f',$first - $start));
293         logger('child  dba_timer: ' . sprintf('%01.4f',$second - $first));
294
295         // Set this so that the conversation function can find out contact info for our wall-wall items
296         $a->page_contact = $a->contact;
297
298         $mode = (($nouveau) ? 'network-new' : 'network');
299
300         $o = render_content($a,$items,$mode,false);
301
302
303         header('Content-type: application/json');
304         echo json_encode($o);
305         killme();
306 }
307
308
309
310 function render_content(&$a, $items, $mode, $update, $preview = false) {
311
312         require_once('include/bbcode.php');
313         require_once('mod/proxy.php');
314
315         $ssl_state = ((local_user()) ? true : false);
316
317         $profile_owner = 0;
318         $page_writeable      = false;
319
320         $previewing = (($preview) ? ' preview ' : '');
321
322         if($mode === 'network') {
323                 $profile_owner = local_user();
324                 $page_writeable = true;
325         }
326
327         if($mode === 'profile') {
328                 $profile_owner = $a->profile['profile_uid'];
329                 $page_writeable = can_write_wall($a,$profile_owner);
330         }
331
332         if($mode === 'notes') {
333                 $profile_owner = local_user();
334                 $page_writeable = true;
335         }
336
337         if($mode === 'display') {
338                 $profile_owner = $a->profile['uid'];
339                 $page_writeable = can_write_wall($a,$profile_owner);
340         }
341
342         if($mode === 'community') {
343                 $profile_owner = 0;
344                 $page_writeable = false;
345         }
346
347         if($update)
348                 $return_url = $_SESSION['return_url'];
349         else
350                 $return_url = $_SESSION['return_url'] = $a->query_string;
351
352         load_contact_links(local_user());
353
354         $cb = array('items' => $items, 'mode' => $mode, 'update' => $update, 'preview' => $preview);
355         call_hooks('conversation_start',$cb);
356
357         $items = $cb['items'];
358
359         $cmnt_tpl    = get_markup_template('comment_item.tpl');
360         $tpl         = 'wall_item.tpl';
361         $wallwall    = 'wallwall_item.tpl';
362         $hide_comments_tpl = get_markup_template('hide_comments.tpl');
363
364         $alike = array();
365         $dlike = array();
366
367
368         // array with html for each thread (parent+comments)
369         $threads = array();
370         $threadsid = -1;
371
372         if($items && count($items)) {
373
374                 if($mode === 'network-new' || $mode === 'search' || $mode === 'community') {
375
376                         // "New Item View" on network page or search page results 
377                         // - just loop through the items and format them minimally for display
378
379                         //$tpl = get_markup_template('search_item.tpl');
380                         $tpl = 'search_item.tpl';
381
382                         foreach($items as $item) {
383                                 $threadsid++;
384
385                                 $comment     = '';
386                                 $owner_url   = '';
387                                 $owner_photo = '';
388                                 $owner_name  = '';
389                                 $sparkle     = '';
390
391                                 if($mode === 'search' || $mode === 'community') {
392                                         if(((activity_match($item['verb'],ACTIVITY_LIKE)) || (activity_match($item['verb'],ACTIVITY_DISLIKE))) 
393                                                 && ($item['id'] != $item['parent']))
394                                                 continue;
395                                         $nickname = $item['nickname'];
396                                 }
397                                 else
398                                         $nickname = $a->user['nickname'];
399
400                                 // prevent private email from leaking.
401                                 if($item['network'] === NETWORK_MAIL && local_user() != $item['uid'])
402                                                 continue;
403
404                                 $profile_name   = ((strlen($item['author-name']))   ? $item['author-name']   : $item['name']);
405                                 if($item['author-link'] && (! $item['author-name']))
406                                         $profile_name = $item['author-link'];
407
408
409
410                                 $sp = false;
411                                 $profile_link = best_link_url($item,$sp);
412                                 if($profile_link === 'mailbox')
413                                         $profile_link = '';
414                                 if($sp)
415                                         $sparkle = ' sparkle';
416                                 else
417                                         $profile_link = zrl($profile_link);
418
419                                 $normalised = normalise_link((strlen($item['author-link'])) ? $item['author-link'] : $item['url']);
420                                 if(($normalised != 'mailbox') && (x($a->contacts[$normalised])))
421                                         $profile_avatar = $a->contacts[$normalised]['thumb'];
422                                 else
423                                         $profile_avatar = ((strlen($item['author-avatar'])) ? $a->get_cached_avatar_image($item['author-avatar']) : $item['thumb']);
424
425                                 $locate = array('location' => $item['location'], 'coord' => $item['coord'], 'html' => '');
426                                 call_hooks('render_location',$locate);
427
428                                 $location = ((strlen($locate['html'])) ? $locate['html'] : render_location_google($locate));
429
430                                 localize_item($item);
431                                 if($mode === 'network-new')
432                                         $dropping = true;
433                                 else
434                                         $dropping = false;
435
436
437                                 $drop = array(
438                                         'dropping' => $dropping,
439                                         'select' => t('Select'), 
440                                         'delete' => t('Delete'),
441                                 );
442
443                                 $star = false;
444                                 $isstarred = "unstarred";
445
446                                 $lock = false;
447                                 $likebuttons = false;
448                                 $shareable = false;
449
450                                 $body = prepare_body($item,true);
451
452                                 if($a->theme['template_engine'] === 'internal') {
453                                         $name_e = template_escape($profile_name);
454                                         $title_e = template_escape($item['title']);
455                                         $body_e = template_escape($body);
456                                         $text_e = strip_tags(template_escape($body));
457                                         $location_e = template_escape($location);
458                                         $owner_name_e = template_escape($owner_name);
459                                 }
460                                 else {
461                                         $name_e = $profile_name;
462                                         $title_e = $item['title'];
463                                         $body_e = $body;
464                                         $text_e = strip_tags($body);
465                                         $location_e = $location;
466                                         $owner_name_e = $owner_name;
467                                 }
468
469                                 //$tmp_item = replace_macros($tpl,array(
470                                 $tmp_item = array(
471                                         'template' => $tpl,
472                                         'id' => (($preview) ? 'P0' : $item['item_id']),
473                                         'linktitle' => sprintf( t('View %s\'s profile @ %s'), $profile_name, ((strlen($item['author-link'])) ? $item['author-link'] : $item['url'])),
474                                         'profile_url' => $profile_link,
475                                         'item_photo_menu' => item_photo_menu($item),
476                                         'name' => $name_e,
477                                         'sparkle' => $sparkle,
478                                         'lock' => $lock,
479                                         'thumb' => proxy_url($profile_avatar),
480                                         'title' => $title_e,
481                                         'body' => $body_e,
482                                         'text' => $text_e,
483                                         'ago' => (($item['app']) ? sprintf( t('%s from %s'),relative_date($item['created']),$item['app']) : relative_date($item['created'])),
484                                         'location' => $location_e,
485                                         'indent' => '',
486                                         'owner_name' => $owner_name_e,
487                                         'owner_url' => $owner_url,
488                                         'owner_photo' => proxy_url($owner_photo),
489                                         'plink' => get_plink($item),
490                                         'edpost' => false,
491                                         'isstarred' => $isstarred,
492                                         'star' => $star,
493                                         'drop' => $drop,
494                                         'vote' => $likebuttons,
495                                         'like' => '',
496                                         'dislike' => '',
497                                         'comment' => '',
498                                         //'conv' => (($preview) ? '' : array('href'=> $a->get_baseurl($ssl_state) . '/display/' . $nickname . '/' . $item['id'], 'title'=> t('View in context'))),
499                                         'conv' => (($preview) ? '' : array('href'=> $a->get_baseurl($ssl_state).'/display/'.$item['guid'], 'title'=> t('View in context'))),
500                                         'previewing' => $previewing,
501                                         'wait' => t('Please wait'),
502                                 );
503
504                                 $arr = array('item' => $item, 'output' => $tmp_item);
505                                 call_hooks('display_item', $arr);
506
507                                 $threads[$threadsid]['id'] = $item['item_id'];
508                                 $threads[$threadsid]['items'] = array($arr['output']);
509
510                         }
511
512                 }
513                 else
514                 {
515                         // Normal View
516
517
518                         // Figure out how many comments each parent has
519                         // (Comments all have gravity of 6)
520                         // Store the result in the $comments array
521
522                         $comments = array();
523                         foreach($items as $item) {
524                                 if((intval($item['gravity']) == 6) && ($item['id'] != $item['parent'])) {
525                                         if(! x($comments,$item['parent']))
526                                                 $comments[$item['parent']] = 1;
527                                         else
528                                                 $comments[$item['parent']] += 1;
529                                 } elseif(! x($comments,$item['parent'])) 
530                                         $comments[$item['parent']] = 0; // avoid notices later on
531                         }
532
533                         // map all the like/dislike activities for each parent item 
534                         // Store these in the $alike and $dlike arrays
535
536                         foreach($items as $item) {
537                                 like_puller($a,$item,$alike,'like');
538                                 like_puller($a,$item,$dlike,'dislike');
539                         }
540
541                         $comments_collapsed = false;
542                         $comments_seen = 0;
543                         $comment_lastcollapsed = false;
544                         $comment_firstcollapsed = false;
545                         $blowhard = 0;
546                         $blowhard_count = 0;
547
548
549                         foreach($items as $item) {
550
551                                 $comment = '';
552                                 $template = $tpl;
553                                 $commentww = '';
554                                 $sparkle = '';
555                                 $owner_url = $owner_photo = $owner_name = '';
556
557                                 // We've already parsed out like/dislike for special treatment. We can ignore them now
558
559                                 if(((activity_match($item['verb'],ACTIVITY_LIKE))
560                                         || (activity_match($item['verb'],ACTIVITY_DISLIKE)))
561                                         && ($item['id'] != $item['parent']))
562                                         continue;
563
564                                 $toplevelpost = (($item['id'] == $item['parent']) ? true : false);
565
566
567                                 // Take care of author collapsing and comment collapsing
568                                 // (author collapsing is currently disabled)
569                                 // If a single author has more than 3 consecutive top-level posts, squash the remaining ones.
570                                 // If there are more than two comments, squash all but the last 2.
571
572                                 if($toplevelpost) {
573
574                                         $item_writeable = (($item['writable'] || $item['self']) ? true : false);
575
576                                         $comments_seen = 0;
577                                         $comments_collapsed = false;
578                                         $comment_lastcollapsed  = false;
579                                         $comment_firstcollapsed = false;
580
581                                         $threadsid++;
582                                         $threads[$threadsid]['id'] = $item['item_id'];
583                                         $threads[$threadsid]['private'] = $item['private'];
584                                         $threads[$threadsid]['items'] = array();
585
586                                 }
587                                 else {
588
589                                         // prevent private email reply to public conversation from leaking.
590                                         if($item['network'] === NETWORK_MAIL && local_user() != $item['uid'])
591                                                         continue;
592
593                                         $comments_seen ++;
594                                         $comment_lastcollapsed  = false;
595                                         $comment_firstcollapsed = false;
596                                 }
597
598                                 $override_comment_box = ((($page_writeable) && ($item_writeable)) ? true : false);
599                                 $show_comment_box = ((($page_writeable) && ($item_writeable) && ($comments_seen == $comments[$item['parent']])) ? true : false);
600
601
602                                 if(($comments[$item['parent']] > 2) && ($comments_seen <= ($comments[$item['parent']] - 2)) && ($item['gravity'] == 6)) {
603
604                                         if (!$comments_collapsed){
605                                                 $threads[$threadsid]['num_comments'] = sprintf( tt('%d comment','%d comments',$comments[$item['parent']]),$comments[$item['parent']] );
606                                                 $threads[$threadsid]['hidden_comments_num'] = $comments[$item['parent']];
607                                                 $threads[$threadsid]['hidden_comments_text'] = tt('comment', 'comments', $comments[$item['parent']]);
608                                                 $threads[$threadsid]['hide_text'] = t('show more');
609                                                 $comments_collapsed = true;
610                                                 $comment_firstcollapsed = true;
611                                         }
612                                 }
613                                 if(($comments[$item['parent']] > 2) && ($comments_seen == ($comments[$item['parent']] - 1))) {
614
615                                         $comment_lastcollapsed = true;
616                                 }
617
618                                 $redirect_url = $a->get_baseurl($ssl_state) . '/redir/' . $item['cid'] ;
619
620                                 $lock = ((($item['private'] == 1) || (($item['uid'] == local_user()) && (strlen($item['allow_cid']) || strlen($item['allow_gid']) 
621                                         || strlen($item['deny_cid']) || strlen($item['deny_gid']))))
622                                         ? t('Private Message')
623                                         : false);
624
625
626                                 // Top-level wall post not written by the wall owner (wall-to-wall)
627                                 // First figure out who owns it. 
628
629                                 $osparkle = '';
630
631                                 if(($toplevelpost) && (! $item['self']) && ($mode !== 'profile')) {
632
633                                         if($item['wall']) {
634
635                                                 // On the network page, I am the owner. On the display page it will be the profile owner.
636                                                 // This will have been stored in $a->page_contact by our calling page.
637                                                 // Put this person as the wall owner of the wall-to-wall notice.
638
639                                                 $owner_url = zrl($a->page_contact['url']);
640                                                 $owner_photo = $a->page_contact['thumb'];
641                                                 $owner_name = $a->page_contact['name'];
642                                                 $template = $wallwall;
643                                                 $commentww = 'ww';
644                                         }
645
646                                         if((! $item['wall']) && $item['owner-link']) {
647
648                                                 $owner_linkmatch = (($item['owner-link']) && link_compare($item['owner-link'],$item['author-link']));
649                                                 $alias_linkmatch = (($item['alias']) && link_compare($item['alias'],$item['author-link']));
650                                                 $owner_namematch = (($item['owner-name']) && $item['owner-name'] == $item['author-name']);
651                                                 if((! $owner_linkmatch) && (! $alias_linkmatch) && (! $owner_namematch)) {
652
653                                                         // The author url doesn't match the owner (typically the contact)
654                                                         // and also doesn't match the contact alias. 
655                                                         // The name match is a hack to catch several weird cases where URLs are 
656                                                         // all over the park. It can be tricked, but this prevents you from
657                                                         // seeing "Bob Smith to Bob Smith via Wall-to-wall" and you know darn
658                                                         // well that it's the same Bob Smith. 
659
660                                                         // But it could be somebody else with the same name. It just isn't highly likely. 
661
662
663                                                         $owner_url = $item['owner-link'];
664                                                         $owner_photo = $item['owner-avatar'];
665                                                         $owner_name = $item['owner-name'];
666                                                         $template = $wallwall;
667                                                         $commentww = 'ww';
668                                                         // If it is our contact, use a friendly redirect link
669                                                         if((link_compare($item['owner-link'],$item['url'])) 
670                                                                 && ($item['network'] === NETWORK_DFRN)) {
671                                                                 $owner_url = $redirect_url;
672                                                                 $osparkle = ' sparkle';
673                                                         }
674                                                         else
675                                                                 $owner_url = zrl($owner_url);
676                                                 }
677                                         }
678                                 }
679
680                                 $likebuttons = '';
681                                 $shareable = ((($profile_owner == local_user()) && ($item['private'] != 1)) ? true : false); 
682
683                                 if($page_writeable) {
684 /*                                      if($toplevelpost) {  */
685                                                 $likebuttons = array(
686                                                         'like' => array( t("I like this \x28toggle\x29"), t("like")),
687                                                         'dislike' => array( t("I don't like this \x28toggle\x29"), t("dislike")),
688                                                 );
689                                                 if ($shareable) $likebuttons['share'] = array( t('Share this'), t('share'));
690 /*                                      } */
691
692                                         $qc = $qcomment =  null;
693
694                                         if(in_array('qcomment',$a->plugins)) {
695                                                 $qc = ((local_user()) ? get_pconfig(local_user(),'qcomment','words') : null);
696                                                 $qcomment = (($qc) ? explode("\n",$qc) : null);
697                                         }
698
699                                         if(($show_comment_box) || (($show_comment_box == false) && ($override_comment_box == false) && ($item['last-child']))) {
700                                                 $comment = replace_macros($cmnt_tpl,array(
701                                                         '$return_path' => '', 
702                                                         '$jsreload' => (($mode === 'display') ? $_SESSION['return_url'] : ''),
703                                                         '$type' => (($mode === 'profile') ? 'wall-comment' : 'net-comment'),
704                                                         '$id' => $item['item_id'],
705                                                         '$parent' => $item['parent'],
706                                                         '$qcomment' => $qcomment,
707                                                         '$profile_uid' =>  $profile_owner,
708                                                         '$mylink' => $a->contact['url'],
709                                                         '$mytitle' => t('This is you'),
710                                                         '$myphoto' => $a->contact['thumb'],
711                                                         '$comment' => t('Comment'),
712                                                         '$submit' => t('Submit'),
713                                                         '$edbold' => t('Bold'),
714                                                         '$editalic' => t('Italic'),
715                                                         '$eduline' => t('Underline'),
716                                                         '$edquote' => t('Quote'),
717                                                         '$edcode' => t('Code'),
718                                                         '$edimg' => t('Image'),
719                                                         '$edurl' => t('Link'),
720                                                         '$edvideo' => t('Video'),
721                                                         '$preview' => t('Preview'),
722                                                         '$sourceapp' => t($a->sourcename),
723                                                         '$ww' => (($mode === 'network') ? $commentww : ''),
724                                                         '$rand_num' => random_digits(12)
725                                                 ));
726                                         }
727                                 }
728
729                                 if(local_user() && link_compare($a->contact['url'],$item['author-link']))
730                                         $edpost = array($a->get_baseurl($ssl_state)."/editpost/".$item['id'], t("Edit"));
731                                 else
732                                         $edpost = false;
733
734                                 $drop = '';
735                                 $dropping = false;
736
737                                 if((intval($item['contact-id']) && $item['contact-id'] == remote_user()) || ($item['uid'] == local_user()))
738                                         $dropping = true;
739
740                                 $drop = array(
741                                         'dropping' => $dropping,
742                                         'select' => t('Select'), 
743                                         'delete' => t('Delete'),
744                                 );
745
746                                 $star = false;
747                                 $filer = false;
748
749                                 $isstarred = "unstarred";
750                                 if ($profile_owner == local_user()) {
751                                         if($toplevelpost) {
752                                                 $isstarred = (($item['starred']) ? "starred" : "unstarred");
753
754                                                 $star = array(
755                                                         'do' => t("add star"),
756                                                         'undo' => t("remove star"),
757                                                         'toggle' => t("toggle star status"),
758                                                         'classdo' => (($item['starred']) ? "hidden" : ""),
759                                                         'classundo' => (($item['starred']) ? "" : "hidden"),
760                                                         'starred' =>  t('starred'),
761                                                         'tagger' => t("add tag"),
762                                                         'classtagger' => "",
763                                                 );
764                                         }
765                                         $filer = t("save to folder");
766                                 }
767
768
769                                 $photo = $item['photo'];
770                                 $thumb = $item['thumb'];
771
772                                 // Post was remotely authored.
773
774                                 $diff_author    = ((link_compare($item['url'],$item['author-link'])) ? false : true);
775
776                                 $profile_name   = (((strlen($item['author-name']))   && $diff_author) ? $item['author-name']   : $item['name']);
777
778                                 if($item['author-link'] && (! $item['author-name']))
779                                         $profile_name = $item['author-link'];
780
781                                 $sp = false;
782                                 $profile_link = best_link_url($item,$sp);
783                                 if($profile_link === 'mailbox')
784                                         $profile_link = '';
785                                 if($sp)
786                                         $sparkle = ' sparkle';
787                                 else
788                                         $profile_link = zrl($profile_link);
789
790                                 $normalised = normalise_link((strlen($item['author-link'])) ? $item['author-link'] : $item['url']);
791                                 if(($normalised != 'mailbox') && (x($a->contacts,$normalised)))
792                                         $profile_avatar = $a->contacts[$normalised]['thumb'];
793                                 else
794                                         $profile_avatar = (((strlen($item['author-avatar'])) && $diff_author) ? $item['author-avatar'] : $a->get_cached_avatar_image($thumb));
795
796                                 $like    = ((x($alike,$item['uri'])) ? format_like($alike[$item['uri']],$alike[$item['uri'] . '-l'],'like',$item['uri']) : '');
797                                 $dislike = ((x($dlike,$item['uri'])) ? format_like($dlike[$item['uri']],$dlike[$item['uri'] . '-l'],'dislike',$item['uri']) : '');
798
799                                 $locate = array('location' => $item['location'], 'coord' => $item['coord'], 'html' => '');
800                                 call_hooks('render_location',$locate);
801
802                                 $location = ((strlen($locate['html'])) ? $locate['html'] : render_location_google($locate));
803
804                                 $indent = (($toplevelpost) ? '' : ' comment');
805
806                                 $shiny = "";
807                                 if(strcmp(datetime_convert('UTC','UTC',$item['created']),datetime_convert('UTC','UTC','now - 12 hours')) > 0)
808                                         $shiny = 'shiny'; 
809
810                                 // 
811                                 localize_item($item);
812
813
814                                 $tags=array();
815                                 foreach(explode(',',$item['tag']) as $tag){
816                                         $tag = trim($tag);
817                                         if ($tag!="") $tags[] = bbcode($tag);
818                                 }
819
820                                 // Build the HTML
821
822                                 $body = prepare_body($item,true);
823                                 //$tmp_item = replace_macros($template,
824
825                                 if($a->theme['template_engine'] === 'internal') {
826                                         $body_e = template_escape($body);
827                                         $text_e = strip_tags(template_escape($body));
828                                         $name_e = template_escape($profile_name);
829                                         $title_e = template_escape($item['title']);
830                                         $location_e = template_escape($location);
831                                         $owner_name_e = template_escape($owner_name);
832                                 }
833                                 else {
834                                         $body_e = $body;
835                                         $text_e = strip_tags($body);
836                                         $name_e = $profile_name;
837                                         $title_e = $item['title'];
838                                         $location_e = $location;
839                                         $owner_name_e = $owner_name;
840                                 }
841
842                                 $tmp_item = array(
843                                         // collapse comments in template. I don't like this much...
844                                         'comment_firstcollapsed' => $comment_firstcollapsed,
845                                         'comment_lastcollapsed' => $comment_lastcollapsed,
846                                         // template to use to render item (wall, walltowall, search)
847                                         'template' => $template,
848
849                                         'type' => implode("",array_slice(explode("/",$item['verb']),-1)),
850                                         'tags' => $tags,
851                                         'body' => $body_e,
852                                         'text' => $text_e,
853                                         'id' => $item['item_id'],
854                                         'linktitle' => sprintf( t('View %s\'s profile @ %s'), $profile_name, ((strlen($item['author-link'])) ? $item['author-link'] : $item['url'])),
855                                         'olinktitle' => sprintf( t('View %s\'s profile @ %s'), $profile_name, ((strlen($item['owner-link'])) ? $item['owner-link'] : $item['url'])),
856                                         'to' => t('to'),
857                                         'wall' => t('Wall-to-Wall'),
858                                         'vwall' => t('via Wall-To-Wall:'),
859                                         'profile_url' => $profile_link,
860                                         'item_photo_menu' => item_photo_menu($item),
861                                         'name' => $name_e,
862                                         'thumb' => proxy_url($profile_avatar),
863                                         'osparkle' => $osparkle,
864                                         'sparkle' => $sparkle,
865                                         'title' => $title_e,
866                                         'ago' => (($item['app']) ? sprintf( t('%s from %s'),relative_date($item['created']),$item['app']) : relative_date($item['created'])),
867                                         'lock' => $lock,
868                                         'location' => $location_e,
869                                         'indent' => $indent,
870                                         'shiny' => $shiny,
871                                         'owner_url' => $owner_url,
872                                         'owner_photo' => proxy_url($owner_photo),
873                                         'owner_name' => $owner_name_e,
874                                         'plink' => get_plink($item),
875                                         'edpost' => $edpost,
876                                         'isstarred' => $isstarred,
877                                         'star' => $star,
878                                         'filer' => $filer,
879                                         'drop' => $drop,
880                                         'vote' => $likebuttons,
881                                         'like' => $like,
882                                         'dislike' => $dislike,
883                                         'comment' => $comment,
884                                         'previewing' => $previewing,
885                                         'wait' => t('Please wait'),
886
887                                 );
888
889
890                                 $arr = array('item' => $item, 'output' => $tmp_item);
891                                 call_hooks('display_item', $arr);
892
893                                 $threads[$threadsid]['items'][] = $arr['output'];
894                         }
895                 }
896         }
897
898
899         return $threads;
900
901 }