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