]> git.mxchange.org Git - friendica.git/blob - mod/content.php
Merge pull request #3112 from Quix0r/rewrites/coding-convention
[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(App $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                         } elseif ($a->argv[$x] === 'new') {
51                                 $nouveau = true;
52                         } elseif (intval($a->argv[$x])) {
53                                 $group = intval($a->argv[$x]);
54                                 $def_acl = array('allow_gid' => '<' . $group . '>');
55                         }
56                 }
57         }
58
59
60         $o = '';
61
62
63
64         $contact_id = $a->cid;
65
66         require_once('include/acl_selectors.php');
67
68         $cid = ((x($_GET,'cid')) ? intval($_GET['cid']) : 0);
69         $star = ((x($_GET,'star')) ? intval($_GET['star']) : 0);
70         $bmark = ((x($_GET,'bmark')) ? intval($_GET['bmark']) : 0);
71         $order = ((x($_GET,'order')) ? notags($_GET['order']) : 'comment');
72         $liked = ((x($_GET,'liked')) ? intval($_GET['liked']) : 0);
73         $conv = ((x($_GET,'conv')) ? intval($_GET['conv']) : 0);
74         $spam = ((x($_GET,'spam')) ? intval($_GET['spam']) : 0);
75         $nets = ((x($_GET,'nets')) ? $_GET['nets'] : '');
76         $cmin = ((x($_GET,'cmin')) ? intval($_GET['cmin']) : 0);
77         $cmax = ((x($_GET,'cmax')) ? intval($_GET['cmax']) : 99);
78         $file = ((x($_GET,'file')) ? $_GET['file'] : '');
79
80
81
82         if (x($_GET,'search') || x($_GET,'file'))
83                 $nouveau = true;
84         if ($cid)
85                 $def_acl = array('allow_cid' => '<' . intval($cid) . '>');
86
87         if ($nets) {
88                 $r = q("select id from contact where uid = %d and network = '%s' and self = 0",
89                         intval(local_user()),
90                         dbesc($nets)
91                 );
92
93                 $str = '';
94                 if (dbm::is_result($r))
95                         foreach ($r as $rr)
96                                 $str .= '<' . $rr['id'] . '>';
97                 if (strlen($str))
98                         $def_acl = array('allow_cid' => $str);
99         }
100
101
102         $sql_options  = (($star) ? " and starred = 1 " : '');
103         $sql_options .= (($bmark) ? " and bookmark = 1 " : '');
104
105         $sql_nets = (($nets) ? sprintf(" and `contact`.`network` = '%s' ", dbesc($nets)) : '');
106
107         $sql_extra = " AND `item`.`parent` IN ( SELECT `parent` FROM `item` WHERE `id` = `parent` $sql_options ) ";
108
109         if ($group) {
110                 $r = q("SELECT `name`, `id` FROM `group` WHERE `id` = %d AND `uid` = %d LIMIT 1",
111                         intval($group),
112                         intval($_SESSION['uid'])
113                 );
114                 if (! dbm::is_result($r)) {
115                         if ($update)
116                                 killme();
117                         notice( t('No such group') . EOL );
118                         goaway(App::get_baseurl(true) . '/network');
119                         // NOTREACHED
120                 }
121
122                 $contacts = expand_groups(array($group));
123                 if ((is_array($contacts)) && count($contacts)) {
124                         $contact_str = implode(',',$contacts);
125                 }
126                 else {
127                                 $contact_str = ' 0 ';
128                                 info( t('Group is empty'));
129                 }
130
131                 $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 ) ";
132                 $o = replace_macros(get_markup_template("section_title.tpl"),array(
133                         '$title' => sprintf( t('Group: %s'), $r[0]['name'])
134                 )) . $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 (dbm::is_result($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 = App::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`
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`.`id` 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 (dbm::is_result($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`
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(App $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         $edited = false;
321         if (strcmp($item['created'], $item['edited'])<>0) {
322                 $edited = array(
323                         'label' => t('This entry was edited'),
324                         'date' => datetime_convert('UTC', date_default_timezone_get(), $item['edited'], 'r'),
325                         'relative' => relative_date($item['edited'])
326                 );
327         }
328
329         if ($mode === 'network') {
330                 $profile_owner = local_user();
331                 $page_writeable = true;
332         }
333
334         if ($mode === 'profile') {
335                 $profile_owner = $a->profile['profile_uid'];
336                 $page_writeable = can_write_wall($a,$profile_owner);
337         }
338
339         if ($mode === 'notes') {
340                 $profile_owner = local_user();
341                 $page_writeable = true;
342         }
343
344         if ($mode === 'display') {
345                 $profile_owner = $a->profile['uid'];
346                 $page_writeable = can_write_wall($a,$profile_owner);
347         }
348
349         if ($mode === 'community') {
350                 $profile_owner = 0;
351                 $page_writeable = false;
352         }
353
354         if ($update)
355                 $return_url = $_SESSION['return_url'];
356         else
357                 $return_url = $_SESSION['return_url'] = $a->query_string;
358
359         $cb = array('items' => $items, 'mode' => $mode, 'update' => $update, 'preview' => $preview);
360         call_hooks('conversation_start',$cb);
361
362         $items = $cb['items'];
363
364         $cmnt_tpl    = get_markup_template('comment_item.tpl');
365         $tpl         = 'wall_item.tpl';
366         $wallwall    = 'wallwall_item.tpl';
367         $hide_comments_tpl = get_markup_template('hide_comments.tpl');
368
369         $conv_responses = array(
370                 'like' => array('title' => t('Likes','title')), 'dislike' => array('title' => t('Dislikes','title')),
371                 'attendyes' => array('title' => t('Attending','title')), 'attendno' => array('title' => t('Not attending','title')), 'attendmaybe' => array('title' => t('Might attend','title'))
372         );
373
374
375         // array with html for each thread (parent+comments)
376         $threads = array();
377         $threadsid = -1;
378
379         if ($items && count($items)) {
380
381                 if ($mode === 'network-new' || $mode === 'search' || $mode === 'community') {
382
383                         // "New Item View" on network page or search page results
384                         // - just loop through the items and format them minimally for display
385
386                         //$tpl = get_markup_template('search_item.tpl');
387                         $tpl = 'search_item.tpl';
388
389                         foreach ($items as $item) {
390                                 $threadsid++;
391
392                                 $comment     = '';
393                                 $owner_url   = '';
394                                 $owner_photo = '';
395                                 $owner_name  = '';
396                                 $sparkle     = '';
397
398                                 if ($mode === 'search' || $mode === 'community') {
399                                         if (((activity_match($item['verb'],ACTIVITY_LIKE))
400                                                 || (activity_match($item['verb'],ACTIVITY_DISLIKE))
401                                                 || activity_match($item['verb'],ACTIVITY_ATTEND)
402                                                 || activity_match($item['verb'],ACTIVITY_ATTENDNO)
403                                                 || activity_match($item['verb'],ACTIVITY_ATTENDMAYBE))
404                                                 && ($item['id'] != $item['parent']))
405                                                 continue;
406                                         $nickname = $item['nickname'];
407                                 }
408                                 else
409                                         $nickname = $a->user['nickname'];
410
411                                 // prevent private email from leaking.
412                                 if ($item['network'] === NETWORK_MAIL && local_user() != $item['uid'])
413                                                 continue;
414
415                                 $profile_name   = ((strlen($item['author-name']))   ? $item['author-name']   : $item['name']);
416                                 if ($item['author-link'] && (! $item['author-name']))
417                                         $profile_name = $item['author-link'];
418
419
420
421                                 $sp = false;
422                                 $profile_link = best_link_url($item,$sp);
423                                 if ($profile_link === 'mailbox')
424                                         $profile_link = '';
425                                 if ($sp)
426                                         $sparkle = ' sparkle';
427                                 else
428                                         $profile_link = zrl($profile_link);
429
430                                 // Don't rely on the author-avatar. It is better to use the data from the contact table
431                                 $author_contact = get_contact_details_by_url($item['author-link'], $profile_owner);
432                                 if ($author_contact["thumb"])
433                                         $profile_avatar = $author_contact["thumb"];
434                                 else
435                                         $profile_avatar = $item['author-avatar'];
436
437                                 $locate = array('location' => $item['location'], 'coord' => $item['coord'], 'html' => '');
438                                 call_hooks('render_location',$locate);
439
440                                 $location = ((strlen($locate['html'])) ? $locate['html'] : render_location_dummy($locate));
441
442                                 localize_item($item);
443                                 if ($mode === 'network-new')
444                                         $dropping = true;
445                                 else
446                                         $dropping = false;
447
448
449                                 $drop = array(
450                                         'dropping' => $dropping,
451                                         'select' => t('Select'),
452                                         'delete' => t('Delete'),
453                                 );
454
455                                 $star = false;
456                                 $isstarred = "unstarred";
457
458                                 $lock = false;
459                                 $likebuttons = false;
460                                 $shareable = false;
461
462                                 $body = prepare_body($item,true);
463
464                                 if ($a->theme['template_engine'] === 'internal') {
465                                         $name_e = template_escape($profile_name);
466                                         $title_e = template_escape($item['title']);
467                                         $body_e = template_escape($body);
468                                         $text_e = strip_tags(template_escape($body));
469                                         $location_e = template_escape($location);
470                                         $owner_name_e = template_escape($owner_name);
471                                 }
472                                 else {
473                                         $name_e = $profile_name;
474                                         $title_e = $item['title'];
475                                         $body_e = $body;
476                                         $text_e = strip_tags($body);
477                                         $location_e = $location;
478                                         $owner_name_e = $owner_name;
479                                 }
480
481                                 //$tmp_item = replace_macros($tpl,array(
482                                 $tmp_item = array(
483                                         'template' => $tpl,
484                                         'id' => (($preview) ? 'P0' : $item['item_id']),
485                                         'linktitle' => sprintf( t('View %s\'s profile @ %s'), $profile_name, ((strlen($item['author-link'])) ? $item['author-link'] : $item['url'])),
486                                         'profile_url' => $profile_link,
487                                         'item_photo_menu' => item_photo_menu($item),
488                                         'name' => $name_e,
489                                         'sparkle' => $sparkle,
490                                         'lock' => $lock,
491                                         'thumb' => proxy_url($profile_avatar, false, PROXY_SIZE_THUMB),
492                                         'title' => $title_e,
493                                         'body' => $body_e,
494                                         'text' => $text_e,
495                                         'ago' => (($item['app']) ? sprintf( t('%s from %s'),relative_date($item['created']),$item['app']) : relative_date($item['created'])),
496                                         'location' => $location_e,
497                                         'indent' => '',
498                                         'owner_name' => $owner_name_e,
499                                         'owner_url' => $owner_url,
500                                         'owner_photo' => proxy_url($owner_photo, false, PROXY_SIZE_THUMB),
501                                         'plink' => get_plink($item),
502                                         'edpost' => false,
503                                         'isstarred' => $isstarred,
504                                         'star' => $star,
505                                         'drop' => $drop,
506                                         'vote' => $likebuttons,
507                                         'like' => '',
508                                         'dislike' => '',
509                                         'comment' => '',
510                                         //'conv' => (($preview) ? '' : array('href'=> App::get_baseurl($ssl_state) . '/display/' . $nickname . '/' . $item['id'], 'title'=> t('View in context'))),
511                                         'conv' => (($preview) ? '' : array('href'=> App::get_baseurl($ssl_state).'/display/'.$item['guid'], 'title'=> t('View in context'))),
512                                         'previewing' => $previewing,
513                                         'wait' => t('Please wait'),
514                                 );
515
516                                 $arr = array('item' => $item, 'output' => $tmp_item);
517                                 call_hooks('display_item', $arr);
518
519                                 $threads[$threadsid]['id'] = $item['item_id'];
520                                 $threads[$threadsid]['items'] = array($arr['output']);
521
522                         }
523
524                 }
525                 else
526                 {
527                         // Normal View
528
529
530                         // Figure out how many comments each parent has
531                         // (Comments all have gravity of 6)
532                         // Store the result in the $comments array
533
534                         $comments = array();
535                         foreach ($items as $item) {
536                                 if ((intval($item['gravity']) == 6) && ($item['id'] != $item['parent'])) {
537                                         if (! x($comments,$item['parent']))
538                                                 $comments[$item['parent']] = 1;
539                                         else
540                                                 $comments[$item['parent']] += 1;
541                                 } elseif (! x($comments,$item['parent']))
542                                         $comments[$item['parent']] = 0; // avoid notices later on
543                         }
544
545                         // map all the like/dislike/attendance activities for each parent item
546                         // Store these in the $alike and $dlike arrays
547
548                         foreach ($items as $item) {
549                                 builtin_activity_puller($item, $conv_responses);
550                         }
551
552                         $comments_collapsed = false;
553                         $comments_seen = 0;
554                         $comment_lastcollapsed = false;
555                         $comment_firstcollapsed = false;
556                         $blowhard = 0;
557                         $blowhard_count = 0;
558
559
560                         foreach ($items as $item) {
561
562                                 $comment = '';
563                                 $template = $tpl;
564                                 $commentww = '';
565                                 $sparkle = '';
566                                 $owner_url = $owner_photo = $owner_name = '';
567
568                                 // We've already parsed out like/dislike for special treatment. We can ignore them now
569
570                                 if (((activity_match($item['verb'],ACTIVITY_LIKE))
571                                         || (activity_match($item['verb'],ACTIVITY_DISLIKE)
572                                         || activity_match($item['verb'],ACTIVITY_ATTEND)
573                                         || activity_match($item['verb'],ACTIVITY_ATTENDNO)
574                                         || activity_match($item['verb'],ACTIVITY_ATTENDMAYBE)))
575                                         && ($item['id'] != $item['parent']))
576                                         continue;
577
578                                 $toplevelpost = (($item['id'] == $item['parent']) ? true : false);
579
580
581                                 // Take care of author collapsing and comment collapsing
582                                 // (author collapsing is currently disabled)
583                                 // If a single author has more than 3 consecutive top-level posts, squash the remaining ones.
584                                 // If there are more than two comments, squash all but the last 2.
585
586                                 if ($toplevelpost) {
587
588                                         $item_writeable = (($item['writable'] || $item['self']) ? true : false);
589
590                                         $comments_seen = 0;
591                                         $comments_collapsed = false;
592                                         $comment_lastcollapsed  = false;
593                                         $comment_firstcollapsed = false;
594
595                                         $threadsid++;
596                                         $threads[$threadsid]['id'] = $item['item_id'];
597                                         $threads[$threadsid]['private'] = $item['private'];
598                                         $threads[$threadsid]['items'] = array();
599
600                                 }
601                                 else {
602
603                                         // prevent private email reply to public conversation from leaking.
604                                         if ($item['network'] === NETWORK_MAIL && local_user() != $item['uid'])
605                                                         continue;
606
607                                         $comments_seen ++;
608                                         $comment_lastcollapsed  = false;
609                                         $comment_firstcollapsed = false;
610                                 }
611
612                                 $override_comment_box = ((($page_writeable) && ($item_writeable)) ? true : false);
613                                 $show_comment_box = ((($page_writeable) && ($item_writeable) && ($comments_seen == $comments[$item['parent']])) ? true : false);
614
615
616                                 if (($comments[$item['parent']] > 2) && ($comments_seen <= ($comments[$item['parent']] - 2)) && ($item['gravity'] == 6)) {
617
618                                         if (!$comments_collapsed){
619                                                 $threads[$threadsid]['num_comments'] = sprintf( tt('%d comment','%d comments',$comments[$item['parent']]),$comments[$item['parent']] );
620                                                 $threads[$threadsid]['hidden_comments_num'] = $comments[$item['parent']];
621                                                 $threads[$threadsid]['hidden_comments_text'] = tt('comment', 'comments', $comments[$item['parent']]);
622                                                 $threads[$threadsid]['hide_text'] = t('show more');
623                                                 $comments_collapsed = true;
624                                                 $comment_firstcollapsed = true;
625                                         }
626                                 }
627                                 if (($comments[$item['parent']] > 2) && ($comments_seen == ($comments[$item['parent']] - 1))) {
628
629                                         $comment_lastcollapsed = true;
630                                 }
631
632                                 $redirect_url = 'redir/' . $item['cid'] ;
633
634                                 $lock = ((($item['private'] == 1) || (($item['uid'] == local_user()) && (strlen($item['allow_cid']) || strlen($item['allow_gid'])
635                                         || strlen($item['deny_cid']) || strlen($item['deny_gid']))))
636                                         ? t('Private Message')
637                                         : false);
638
639
640                                 // Top-level wall post not written by the wall owner (wall-to-wall)
641                                 // First figure out who owns it.
642
643                                 $osparkle = '';
644
645                                 if (($toplevelpost) && (! $item['self']) && ($mode !== 'profile')) {
646
647                                         if ($item['wall']) {
648
649                                                 // On the network page, I am the owner. On the display page it will be the profile owner.
650                                                 // This will have been stored in $a->page_contact by our calling page.
651                                                 // Put this person as the wall owner of the wall-to-wall notice.
652
653                                                 $owner_url = zrl($a->page_contact['url']);
654                                                 $owner_photo = $a->page_contact['thumb'];
655                                                 $owner_name = $a->page_contact['name'];
656                                                 $template = $wallwall;
657                                                 $commentww = 'ww';
658                                         }
659
660                                         if ((! $item['wall']) && $item['owner-link']) {
661
662                                                 $owner_linkmatch = (($item['owner-link']) && link_compare($item['owner-link'],$item['author-link']));
663                                                 $alias_linkmatch = (($item['alias']) && link_compare($item['alias'],$item['author-link']));
664                                                 $owner_namematch = (($item['owner-name']) && $item['owner-name'] == $item['author-name']);
665                                                 if ((! $owner_linkmatch) && (! $alias_linkmatch) && (! $owner_namematch)) {
666
667                                                         // The author url doesn't match the owner (typically the contact)
668                                                         // and also doesn't match the contact alias.
669                                                         // The name match is a hack to catch several weird cases where URLs are
670                                                         // all over the park. It can be tricked, but this prevents you from
671                                                         // seeing "Bob Smith to Bob Smith via Wall-to-wall" and you know darn
672                                                         // well that it's the same Bob Smith.
673
674                                                         // But it could be somebody else with the same name. It just isn't highly likely.
675
676
677                                                         $owner_url = $item['owner-link'];
678                                                         $owner_photo = $item['owner-avatar'];
679                                                         $owner_name = $item['owner-name'];
680                                                         $template = $wallwall;
681                                                         $commentww = 'ww';
682                                                         // If it is our contact, use a friendly redirect link
683                                                         if ((link_compare($item['owner-link'],$item['url']))
684                                                                 && ($item['network'] === NETWORK_DFRN)) {
685                                                                 $owner_url = $redirect_url;
686                                                                 $osparkle = ' sparkle';
687                                                         }
688                                                         else
689                                                                 $owner_url = zrl($owner_url);
690                                                 }
691                                         }
692                                 }
693
694                                 $likebuttons = '';
695                                 $shareable = ((($profile_owner == local_user()) && ($item['private'] != 1)) ? true : false);
696
697                                 if ($page_writeable) {
698 /*                                      if ($toplevelpost) {  */
699                                                 $likebuttons = array(
700                                                         'like' => array( t("I like this \x28toggle\x29"), t("like")),
701                                                         'dislike' => array( t("I don't like this \x28toggle\x29"), t("dislike")),
702                                                 );
703                                                 if ($shareable) $likebuttons['share'] = array( t('Share this'), t('share'));
704 /*                                      } */
705
706                                         $qc = $qcomment =  null;
707
708                                         if (in_array('qcomment',$a->plugins)) {
709                                                 $qc = ((local_user()) ? get_pconfig(local_user(),'qcomment','words') : null);
710                                                 $qcomment = (($qc) ? explode("\n",$qc) : null);
711                                         }
712
713                                         if (($show_comment_box) || (($show_comment_box == false) && ($override_comment_box == false) && ($item['last-child']))) {
714                                                 $comment = replace_macros($cmnt_tpl,array(
715                                                         '$return_path' => '',
716                                                         '$jsreload' => (($mode === 'display') ? $_SESSION['return_url'] : ''),
717                                                         '$type' => (($mode === 'profile') ? 'wall-comment' : 'net-comment'),
718                                                         '$id' => $item['item_id'],
719                                                         '$parent' => $item['parent'],
720                                                         '$qcomment' => $qcomment,
721                                                         '$profile_uid' =>  $profile_owner,
722                                                         '$mylink' => $a->contact['url'],
723                                                         '$mytitle' => t('This is you'),
724                                                         '$myphoto' => $a->contact['thumb'],
725                                                         '$comment' => t('Comment'),
726                                                         '$submit' => t('Submit'),
727                                                         '$edbold' => t('Bold'),
728                                                         '$editalic' => t('Italic'),
729                                                         '$eduline' => t('Underline'),
730                                                         '$edquote' => t('Quote'),
731                                                         '$edcode' => t('Code'),
732                                                         '$edimg' => t('Image'),
733                                                         '$edurl' => t('Link'),
734                                                         '$edvideo' => t('Video'),
735                                                         '$preview' => t('Preview'),
736                                                         '$sourceapp' => t($a->sourcename),
737                                                         '$ww' => (($mode === 'network') ? $commentww : ''),
738                                                         '$rand_num' => random_digits(12)
739                                                 ));
740                                         }
741                                 }
742
743                                 if (local_user() && link_compare($a->contact['url'],$item['author-link'])) {
744                                         $edpost = array(App::get_baseurl($ssl_state)."/editpost/".$item['id'], t("Edit"));
745                                 } else {
746                                         $edpost = false;
747                                 }
748
749                                 $drop = '';
750                                 $dropping = false;
751
752                                 if ((intval($item['contact-id']) && $item['contact-id'] == remote_user()) || ($item['uid'] == local_user()))
753                                         $dropping = true;
754
755                                 $drop = array(
756                                         'dropping' => $dropping,
757                                         'select' => t('Select'),
758                                         'delete' => t('Delete'),
759                                 );
760
761                                 $star = false;
762                                 $filer = false;
763
764                                 $isstarred = "unstarred";
765                                 if ($profile_owner == local_user()) {
766                                         if ($toplevelpost) {
767                                                 $isstarred = (($item['starred']) ? "starred" : "unstarred");
768
769                                                 $star = array(
770                                                         'do' => t("add star"),
771                                                         'undo' => t("remove star"),
772                                                         'toggle' => t("toggle star status"),
773                                                         'classdo' => (($item['starred']) ? "hidden" : ""),
774                                                         'classundo' => (($item['starred']) ? "" : "hidden"),
775                                                         'starred' =>  t('starred'),
776                                                         'tagger' => t("add tag"),
777                                                         'classtagger' => "",
778                                                 );
779
780                                                 $r = q("SELECT `ignored` FROM `thread` WHERE `uid` = %d AND `iid` = %d LIMIT 1",
781                                                         intval($item['uid']),
782                                                         intval($item['id'])
783                                                 );
784
785                                                 if (dbm::is_result($r)) {
786                                                         $ignore = array(
787                                                                 'do' => t("ignore thread"),
788                                                                 'undo' => t("unignore thread"),
789                                                                 'toggle' => t("toggle ignore status"),
790                                                                 'classdo' => (($r[0]['ignored']) ? "hidden" : ""),
791                                                                 'classundo' => (($r[0]['ignored']) ? "" : "hidden"),
792                                                                 'ignored' =>  t('ignored'),
793                                                         );
794                                                 }
795                                                 $tagger = '';
796                                                 if (feature_enabled($profile_owner,'commtag')) {
797                                                         $tagger = array(
798                                                                 'add' => t("add tag"),
799                                                                 'class' => "",
800                                                         );
801                                                 }
802                                         }
803                                         $filer = t("save to folder");
804                                 }
805
806
807                                 $photo = $item['photo'];
808                                 $thumb = $item['thumb'];
809
810                                 // Post was remotely authored.
811
812                                 $diff_author    = ((link_compare($item['url'],$item['author-link'])) ? false : true);
813
814                                 $profile_name   = (((strlen($item['author-name']))   && $diff_author) ? $item['author-name']   : $item['name']);
815
816                                 if ($item['author-link'] && (! $item['author-name']))
817                                         $profile_name = $item['author-link'];
818
819                                 $sp = false;
820                                 $profile_link = best_link_url($item,$sp);
821                                 if ($profile_link === 'mailbox') {
822                                         $profile_link = '';
823                                 }
824                                 if ($sp) {
825                                         $sparkle = ' sparkle';
826                                 } else {
827                                         $profile_link = zrl($profile_link);
828                                 }
829
830                                 // Don't rely on the author-avatar. It is better to use the data from the contact table
831                                 $author_contact = get_contact_details_by_url($item['author-link'], $profile_owner);
832                                 if ($author_contact["thumb"]) {
833                                         $profile_avatar = $author_contact["thumb"];
834                                 } else {
835                                         $profile_avatar = $item['author-avatar'];
836                                 }
837
838                                 $like    = ((x($conv_responses['like'],$item['uri'])) ? format_like($conv_responses['like'][$item['uri']],$conv_responses['like'][$item['uri'] . '-l'],'like',$item['uri']) : '');
839                                 $dislike = ((x($conv_responses['dislike'],$item['uri'])) ? format_like($conv_responses['dislike'][$item['uri']],$conv_responses['dislike'][$item['uri'] . '-l'],'dislike',$item['uri']) : '');
840
841                                 // process action responses - e.g. like/dislike/attend/agree/whatever
842                                 $response_verbs = array('like');
843                                 if (feature_enabled($profile_owner,'dislike')) {
844                                         $response_verbs[] = 'dislike';
845                                 }
846                                 if ($item['object-type'] === ACTIVITY_OBJ_EVENT) {
847                                         $response_verbs[] = 'attendyes';
848                                         $response_verbs[] = 'attendno';
849                                         $response_verbs[] = 'attendmaybe';
850                                         if ($page_writeable) {
851                                                 $isevent = true;
852                                                 $attend = array( t('I will attend'), t('I will not attend'), t('I might attend'));
853                                         }
854                                 }
855                                 $responses = get_responses($conv_responses,$response_verbs,'',$item);
856
857                                 $locate = array('location' => $item['location'], 'coord' => $item['coord'], 'html' => '');
858                                 call_hooks('render_location',$locate);
859
860                                 $location = ((strlen($locate['html'])) ? $locate['html'] : render_location_dummy($locate));
861
862                                 $indent = (($toplevelpost) ? '' : ' comment');
863
864                                 $shiny = "";
865                                 if (strcmp(datetime_convert('UTC','UTC',$item['created']),datetime_convert('UTC','UTC','now - 12 hours')) > 0) {
866                                         $shiny = 'shiny';
867                                 }
868
869                                 //
870                                 localize_item($item);
871
872
873                                 $tags=array();
874                                 foreach (explode(',',$item['tag']) as $tag){
875                                         $tag = trim($tag);
876                                         if ($tag!="") {
877                                                 $tags[] = bbcode($tag);
878                                         }
879                                 }
880
881                                 // Build the HTML
882
883                                 $body = prepare_body($item,true);
884                                 //$tmp_item = replace_macros($template,
885
886                                 if ($a->theme['template_engine'] === 'internal') {
887                                         $body_e = template_escape($body);
888                                         $text_e = strip_tags(template_escape($body));
889                                         $name_e = template_escape($profile_name);
890                                         $title_e = template_escape($item['title']);
891                                         $location_e = template_escape($location);
892                                         $owner_name_e = template_escape($owner_name);
893                                 } else {
894                                         $body_e = $body;
895                                         $text_e = strip_tags($body);
896                                         $name_e = $profile_name;
897                                         $title_e = $item['title'];
898                                         $location_e = $location;
899                                         $owner_name_e = $owner_name;
900                                 }
901
902                                 $tmp_item = array(
903                                         // collapse comments in template. I don't like this much...
904                                         'comment_firstcollapsed' => $comment_firstcollapsed,
905                                         'comment_lastcollapsed' => $comment_lastcollapsed,
906                                         // template to use to render item (wall, walltowall, search)
907                                         'template' => $template,
908
909                                         'type' => implode("",array_slice(explode("/",$item['verb']),-1)),
910                                         'tags' => $tags,
911                                         'body' => $body_e,
912                                         'text' => $text_e,
913                                         'id' => $item['item_id'],
914                                         'isevent' => $isevent,
915                                         'attend' => $attend,
916                                         'linktitle' => sprintf( t('View %s\'s profile @ %s'), $profile_name, ((strlen($item['author-link'])) ? $item['author-link'] : $item['url'])),
917                                         'olinktitle' => sprintf( t('View %s\'s profile @ %s'), $profile_name, ((strlen($item['owner-link'])) ? $item['owner-link'] : $item['url'])),
918                                         'to' => t('to'),
919                                         'wall' => t('Wall-to-Wall'),
920                                         'vwall' => t('via Wall-To-Wall:'),
921                                         'profile_url' => $profile_link,
922                                         'item_photo_menu' => item_photo_menu($item),
923                                         'name' => $name_e,
924                                         'thumb' => proxy_url($profile_avatar, false, PROXY_SIZE_THUMB),
925                                         'osparkle' => $osparkle,
926                                         'sparkle' => $sparkle,
927                                         'title' => $title_e,
928                                         'localtime' => datetime_convert('UTC', date_default_timezone_get(), $item['created'], 'r'),
929                                         'ago' => (($item['app']) ? sprintf( t('%s from %s'),relative_date($item['created']),$item['app']) : relative_date($item['created'])),
930                                         'app' => $item['app'],
931                                         'created' => relative_date($item['created']),
932                                         'lock' => $lock,
933                                         'location' => $location_e,
934                                         'indent' => $indent,
935                                         'shiny' => $shiny,
936                                         'owner_url' => $owner_url,
937                                         'owner_photo' => proxy_url($owner_photo, false, PROXY_SIZE_THUMB),
938                                         'owner_name' => $owner_name_e,
939                                         'plink' => get_plink($item),
940                                         'edpost' => $edpost,
941                                         'isstarred' => $isstarred,
942                                         'star' => $star,
943                                         'ignore'  => ((feature_enabled($profile_owner,'ignore_posts')) ? $ignore : ''),
944                                         'tagger' => $tagger,
945                                         'filer' => ((feature_enabled($profile_owner,'filing')) ? $filer : ''),
946                                         'drop' => $drop,
947                                         'vote' => $likebuttons,
948                                         'responses' => $responses,
949                                         'like' => $like,
950                                         'dislike' => $dislike,
951                                         'switchcomment' => t('Comment'),
952                                         'comment' => $comment,
953                                         'previewing' => $previewing,
954                                         'wait' => t('Please wait'),
955                                         'edited' => $edited,
956                                         'network' => $item["item_network"],
957                                         'network_name' => network_to_name($item['network'], $profile_link),
958
959                                 );
960
961
962                                 $arr = array('item' => $item, 'output' => $tmp_item);
963                                 call_hooks('display_item', $arr);
964
965                                 $threads[$threadsid]['items'][] = $arr['output'];
966                         }
967                 }
968         }
969
970
971         return $threads;
972
973 }