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