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