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