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