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