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