]> git.mxchange.org Git - friendica.git/blob - mod/content.php
extend checkbox template for customized attributes
[friendica.git] / mod / content.php
1 <?php
2
3 // This is a purely experimental module and is not yet generally useful.
4
5 // The eventual goal is to provide a json backend to fetch content and fill the current page.
6 // The page will be filled in on the frontend using javascript.
7 // At the present time this page is based on "network", but the hope is to extend to serving
8 // any content (wall, community, search, etc.).
9 // All search parameters, etc. will be managed in javascript and sent as request params.
10 // Security will be managed on the backend.
11 // There is no "pagination query", but we will manage the "current page" on the client
12 // and provide a link to fetch the next page - until there are no pages left to fetch.
13
14 // With the exception of complex tag and text searches, this prototype is incredibly
15 // fast - e.g. one or two milliseconds to fetch parent items for the current content,
16 // and 10-20 milliseconds to fetch all the child items.
17
18 use Friendica\App;
19 use Friendica\Core\System;
20
21 function content_content(App $a, $update = 0) {
22
23         require_once('include/conversation.php');
24
25
26         // Currently security is based on the logged in user
27
28         if (! local_user()) {
29                 return;
30         }
31
32         $arr = array('query' => $a->query_string);
33
34         call_hooks('content_content_init', $arr);
35
36
37         $datequery = $datequery2 = '';
38
39         $group = 0;
40
41         $nouveau = false;
42
43         if($a->argc > 1) {
44                 for($x = 1; $x < $a->argc; $x ++) {
45                         if(is_a_date_arg($a->argv[$x])) {
46                                 if($datequery)
47                                         $datequery2 = escape_tags($a->argv[$x]);
48                                 else {
49                                         $datequery = escape_tags($a->argv[$x]);
50                                         $_GET['order'] = 'post';
51                                 }
52                         }
53                         elseif($a->argv[$x] === 'new') {
54                                 $nouveau = true;
55                         }
56                         elseif(intval($a->argv[$x])) {
57                                 $group = intval($a->argv[$x]);
58                                 $def_acl = array('allow_gid' => '<' . $group . '>');
59                         }
60                 }
61         }
62
63
64         $o = '';
65
66
67
68         $contact_id = $a->cid;
69
70         require_once('include/acl_selectors.php');
71
72         $cid = ((x($_GET,'cid')) ? intval($_GET['cid']) : 0);
73         $star = ((x($_GET,'star')) ? intval($_GET['star']) : 0);
74         $bmark = ((x($_GET,'bmark')) ? intval($_GET['bmark']) : 0);
75         $order = ((x($_GET,'order')) ? notags($_GET['order']) : 'comment');
76         $liked = ((x($_GET,'liked')) ? intval($_GET['liked']) : 0);
77         $conv = ((x($_GET,'conv')) ? intval($_GET['conv']) : 0);
78         $spam = ((x($_GET,'spam')) ? intval($_GET['spam']) : 0);
79         $nets = ((x($_GET,'nets')) ? $_GET['nets'] : '');
80         $cmin = ((x($_GET,'cmin')) ? intval($_GET['cmin']) : 0);
81         $cmax = ((x($_GET,'cmax')) ? intval($_GET['cmax']) : 99);
82         $file = ((x($_GET,'file')) ? $_GET['file'] : '');
83
84
85
86         if(x($_GET,'search') || x($_GET,'file'))
87                 $nouveau = true;
88         if($cid)
89                 $def_acl = array('allow_cid' => '<' . intval($cid) . '>');
90
91         if($nets) {
92                 $r = q("select id from contact where uid = %d and network = '%s' and self = 0",
93                         intval(local_user()),
94                         dbesc($nets)
95                 );
96
97                 $str = '';
98                 if (dbm::is_result($r))
99                         foreach($r as $rr)
100                                 $str .= '<' . $rr['id'] . '>';
101                 if(strlen($str))
102                         $def_acl = array('allow_cid' => $str);
103         }
104
105
106         $sql_options  = (($star) ? " and starred = 1 " : '');
107         $sql_options .= (($bmark) ? " and bookmark = 1 " : '');
108
109         $sql_nets = (($nets) ? sprintf(" and `contact`.`network` = '%s' ", dbesc($nets)) : '');
110
111         $sql_extra = " AND `item`.`parent` IN ( SELECT `parent` FROM `item` WHERE `id` = `parent` $sql_options ) ";
112
113         if($group) {
114                 $r = q("SELECT `name`, `id` FROM `group` WHERE `id` = %d AND `uid` = %d LIMIT 1",
115                         intval($group),
116                         intval($_SESSION['uid'])
117                 );
118                 if (! dbm::is_result($r)) {
119                         if($update)
120                                 killme();
121                         notice( t('No such group') . EOL );
122                         goaway(System::baseUrl(true) . '/network');
123                         // NOTREACHED
124                 }
125
126                 $contacts = expand_groups(array($group));
127                 if((is_array($contacts)) && count($contacts)) {
128                         $contact_str = implode(',',$contacts);
129                 }
130                 else {
131                                 $contact_str = ' 0 ';
132                                 info( t('Group is empty'));
133                 }
134
135                 $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 ) ";
136                 $o = replace_macros(get_markup_template("section_title.tpl"),array(
137                         '$title' => sprintf( t('Group: %s'), $r[0]['name'])
138                 )) . $o;
139         }
140         elseif($cid) {
141
142                 $r = q("SELECT `id`,`name`,`network`,`writable`,`nurl` FROM `contact` WHERE `id` = %d
143                                 AND `blocked` = 0 AND `pending` = 0 LIMIT 1",
144                         intval($cid)
145                 );
146                 if (dbm::is_result($r)) {
147                         $sql_extra = " AND `item`.`parent` IN ( SELECT DISTINCT(`parent`) FROM `item` WHERE 1 $sql_options AND `contact-id` = " . intval($cid) . " and deleted = 0 ) ";
148
149                 }
150                 else {
151                         killme();
152                 }
153         }
154
155
156         $sql_extra3 = '';
157
158         if($datequery) {
159                 $sql_extra3 .= protect_sprintf(sprintf(" AND item.created <= '%s' ", dbesc(datetime_convert(date_default_timezone_get(),'',$datequery))));
160         }
161         if($datequery2) {
162                 $sql_extra3 .= protect_sprintf(sprintf(" AND item.created >= '%s' ", dbesc(datetime_convert(date_default_timezone_get(),'',$datequery2))));
163         }
164
165         $sql_extra2 = (($nouveau) ? '' : " AND `item`.`parent` = `item`.`id` ");
166         $sql_extra3 = (($nouveau) ? '' : $sql_extra3);
167         $sql_table = "`item`";
168
169         if(x($_GET,'search')) {
170                 $search = escape_tags($_GET['search']);
171
172                 if(strpos($search,'#') === 0) {
173                         $tag = true;
174                         $search = substr($search,1);
175                 }
176
177                 if (get_config('system','only_tag_search'))
178                         $tag = true;
179
180                 if($tag) {
181                         //$sql_extra = sprintf(" AND `term`.`term` = '%s' AND `term`.`otype` = %d AND `term`.`type` = %d ",
182                         //      dbesc(protect_sprintf($search)), intval(TERM_OBJ_POST), intval(TERM_HASHTAG));
183                         //$sql_table = "`term` INNER JOIN `item` ON `item`.`id` = `term`.`oid` AND `item`.`uid` = `term`.`uid` ";
184
185                         $sql_extra = "";
186                         $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` ",
187                                 dbesc(protect_sprintf($search)), intval(TERM_OBJ_POST), intval(TERM_HASHTAG), intval(local_user()));
188
189                 } else {
190                         if (get_config('system','use_fulltext_engine'))
191                                 $sql_extra = sprintf(" AND MATCH (`item`.`body`, `item`.`title`) AGAINST ('%s' in boolean mode) ", dbesc(protect_sprintf($search)));
192                         else
193                                 $sql_extra = sprintf(" AND `item`.`body` REGEXP '%s' ", dbesc(protect_sprintf(preg_quote($search))));
194                 }
195
196         }
197         if(strlen($file)) {
198                 $sql_extra .= file_tag_file_query('item',unxmlify($file));
199         }
200
201         if($conv) {
202                 $myurl = System::baseUrl() . '/profile/'. $a->user['nickname'];
203                 $myurl = substr($myurl,strpos($myurl,'://')+3);
204                 $myurl = str_replace('www.','',$myurl);
205                 $diasp_url = str_replace('/profile/','/u/',$myurl);
206
207                 $sql_extra .= sprintf(" AND `item`.`parent` IN (SELECT distinct(`parent`) from item where `author-link` IN ('https://%s', 'http://%s') OR `mention`)",
208                         dbesc(protect_sprintf($myurl)),
209                         dbesc(protect_sprintf($myurl))
210                 );
211         }
212
213         $pager_sql = sprintf(" LIMIT %d, %d ",intval($a->pager['start']), intval($a->pager['itemspage']));
214
215
216         if($nouveau) {
217                 // "New Item View" - show all items unthreaded in reverse created date order
218
219                 $items = q("SELECT `item`.*, `item`.`id` AS `item_id`,
220                         `contact`.`name`, `contact`.`photo`, `contact`.`url`, `contact`.`rel`, `contact`.`writable`,
221                         `contact`.`network`, `contact`.`thumb`, `contact`.`dfrn-id`, `contact`.`self`,
222                         `contact`.`id` AS `cid`
223                         FROM $sql_table INNER JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
224                         WHERE `item`.`uid` = %d AND `item`.`visible` = 1
225                         AND `item`.`deleted` = 0 and `item`.`moderated` = 0
226                         $simple_update
227                         AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
228                         $sql_extra $sql_nets
229                         ORDER BY `item`.`id` DESC $pager_sql ",
230                         intval($_SESSION['uid'])
231                 );
232
233         }
234         else {
235
236                 // Normal conversation view
237
238
239                 if($order === 'post')
240                                 $ordering = "`created`";
241                 else
242                                 $ordering = "`commented`";
243
244                 $start = dba_timer();
245
246                 $r = q("SELECT `item`.`id` AS `item_id`, `contact`.`uid` AS `contact_uid`
247                         FROM $sql_table INNER JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
248                         WHERE `item`.`uid` = %d AND `item`.`visible` = 1 AND `item`.`deleted` = 0
249                         AND `item`.`moderated` = 0 AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
250                         AND `item`.`parent` = `item`.`id`
251                         $sql_extra3 $sql_extra $sql_nets
252                         ORDER BY `item`.$ordering DESC $pager_sql ",
253                         intval(local_user())
254                 );
255
256                 $first = dba_timer();
257
258
259                 // Then fetch all the children of the parents that are on this page
260
261                 $parents_arr = array();
262                 $parents_str = '';
263
264                 if (dbm::is_result($r)) {
265                         foreach($r as $rr)
266                                 if(! in_array($rr['item_id'],$parents_arr))
267                                         $parents_arr[] = $rr['item_id'];
268                         $parents_str = implode(', ', $parents_arr);
269
270                         $items = q("SELECT `item`.*, `item`.`id` AS `item_id`,
271                                 `contact`.`name`, `contact`.`photo`, `contact`.`url`, `contact`.`alias`, `contact`.`rel`, `contact`.`writable`,
272                                 `contact`.`network`, `contact`.`thumb`, `contact`.`dfrn-id`, `contact`.`self`,
273                                 `contact`.`id` AS `cid`
274                                 FROM $sql_table INNER JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
275                                 WHERE `item`.`uid` = %d AND `item`.`visible` = 1 AND `item`.`deleted` = 0
276                                 AND `item`.`moderated` = 0
277                                 AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
278                                 AND `item`.`parent` IN ( %s )
279                                 $sql_extra ",
280                                 intval(local_user()),
281                                 dbesc($parents_str)
282                         );
283
284                         $second = dba_timer();
285
286                         $items = conv_sort($items,$ordering);
287
288                 } else {
289                         $items = array();
290                 }
291         }
292
293
294         logger('parent dba_timer: ' . sprintf('%01.4f',$first - $start));
295         logger('child  dba_timer: ' . sprintf('%01.4f',$second - $first));
296
297         // Set this so that the conversation function can find out contact info for our wall-wall items
298         $a->page_contact = $a->contact;
299
300         $mode = (($nouveau) ? 'network-new' : 'network');
301
302         $o = render_content($a,$items,$mode,false);
303
304
305         header('Content-type: application/json');
306         echo json_encode($o);
307         killme();
308 }
309
310
311
312 function render_content(App $a, $items, $mode, $update, $preview = false) {
313
314         require_once('include/bbcode.php');
315         require_once('mod/proxy.php');
316
317         $ssl_state = ((local_user()) ? true : false);
318
319         $profile_owner = 0;
320         $page_writeable      = false;
321
322         $previewing = (($preview) ? ' preview ' : '');
323
324         $edited = false;
325         if (strcmp($item['created'], $item['edited'])<>0) {
326                 $edited = array(
327                         'label' => t('This entry was edited'),
328                         'date' => datetime_convert('UTC', date_default_timezone_get(), $item['edited'], 'r'),
329                         'relative' => relative_date($item['edited'])
330                 );
331         }
332
333         if($mode === 'network') {
334                 $profile_owner = local_user();
335                 $page_writeable = true;
336         }
337
338         if($mode === 'profile') {
339                 $profile_owner = $a->profile['profile_uid'];
340                 $page_writeable = can_write_wall($a,$profile_owner);
341         }
342
343         if($mode === 'notes') {
344                 $profile_owner = local_user();
345                 $page_writeable = true;
346         }
347
348         if($mode === 'display') {
349                 $profile_owner = $a->profile['uid'];
350                 $page_writeable = can_write_wall($a,$profile_owner);
351         }
352
353         if($mode === 'community') {
354                 $profile_owner = 0;
355                 $page_writeable = false;
356         }
357
358         if($update)
359                 $return_url = $_SESSION['return_url'];
360         else
361                 $return_url = $_SESSION['return_url'] = $a->query_string;
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'=> System::baseUrl($ssl_state) . '/display/' . $nickname . '/' . $item['id'], 'title'=> t('View in context'))),
515                                         'conv' => (($preview) ? '' : array('href'=> System::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(System::baseUrl($ssl_state)."/editpost/".$item['id'], t("Edit"));
749                                 } else {
750                                         $edpost = false;
751                                 }
752
753                                 $drop = '';
754                                 $dropping = false;
755
756                                 if((intval($item['contact-id']) && $item['contact-id'] == remote_user()) || ($item['uid'] == local_user()))
757                                         $dropping = true;
758
759                                 $drop = array(
760                                         'dropping' => $dropping,
761                                         'select' => t('Select'),
762                                         'delete' => t('Delete'),
763                                 );
764
765                                 $star = false;
766                                 $filer = false;
767
768                                 $isstarred = "unstarred";
769                                 if ($profile_owner == local_user()) {
770                                         if ($toplevelpost) {
771                                                 $isstarred = (($item['starred']) ? "starred" : "unstarred");
772
773                                                 $star = array(
774                                                         'do' => t("add star"),
775                                                         'undo' => t("remove star"),
776                                                         'toggle' => t("toggle star status"),
777                                                         'classdo' => (($item['starred']) ? "hidden" : ""),
778                                                         'classundo' => (($item['starred']) ? "" : "hidden"),
779                                                         'starred' =>  t('starred'),
780                                                         'tagger' => t("add tag"),
781                                                         'classtagger' => "",
782                                                 );
783
784                                                 $r = q("SELECT `ignored` FROM `thread` WHERE `uid` = %d AND `iid` = %d LIMIT 1",
785                                                         intval($item['uid']),
786                                                         intval($item['id'])
787                                                 );
788
789                                                 if (dbm::is_result($r)) {
790                                                         $ignore = array(
791                                                                 'do' => t("ignore thread"),
792                                                                 'undo' => t("unignore thread"),
793                                                                 'toggle' => t("toggle ignore status"),
794                                                                 'classdo' => (($r[0]['ignored']) ? "hidden" : ""),
795                                                                 'classundo' => (($r[0]['ignored']) ? "" : "hidden"),
796                                                                 'ignored' =>  t('ignored'),
797                                                         );
798                                                 }
799                                                 $tagger = '';
800                                                 if (feature_enabled($profile_owner,'commtag')) {
801                                                         $tagger = array(
802                                                                 'add' => t("add tag"),
803                                                                 'class' => "",
804                                                         );
805                                                 }
806                                         }
807                                         $filer = t("save to folder");
808                                 }
809
810
811                                 $photo = $item['photo'];
812                                 $thumb = $item['thumb'];
813
814                                 // Post was remotely authored.
815
816                                 $diff_author    = ((link_compare($item['url'],$item['author-link'])) ? false : true);
817
818                                 $profile_name   = (((strlen($item['author-name']))   && $diff_author) ? $item['author-name']   : $item['name']);
819
820                                 if($item['author-link'] && (! $item['author-name']))
821                                         $profile_name = $item['author-link'];
822
823                                 $sp = false;
824                                 $profile_link = best_link_url($item,$sp);
825                                 if ($profile_link === 'mailbox') {
826                                         $profile_link = '';
827                                 }
828                                 if ($sp) {
829                                         $sparkle = ' sparkle';
830                                 } else {
831                                         $profile_link = zrl($profile_link);
832                                 }
833
834                                 // Don't rely on the author-avatar. It is better to use the data from the contact table
835                                 $author_contact = get_contact_details_by_url($item['author-link'], $profile_owner);
836                                 if ($author_contact["thumb"]) {
837                                         $profile_avatar = $author_contact["thumb"];
838                                 } else {
839                                         $profile_avatar = $item['author-avatar'];
840                                 }
841
842                                 $like    = ((x($conv_responses['like'],$item['uri'])) ? format_like($conv_responses['like'][$item['uri']],$conv_responses['like'][$item['uri'] . '-l'],'like',$item['uri']) : '');
843                                 $dislike = ((x($conv_responses['dislike'],$item['uri'])) ? format_like($conv_responses['dislike'][$item['uri']],$conv_responses['dislike'][$item['uri'] . '-l'],'dislike',$item['uri']) : '');
844
845                                 // process action responses - e.g. like/dislike/attend/agree/whatever
846                                 $response_verbs = array('like');
847                                 if(feature_enabled($profile_owner,'dislike'))
848                                         $response_verbs[] = 'dislike';
849                                 if($item['object-type'] === ACTIVITY_OBJ_EVENT) {
850                                         $response_verbs[] = 'attendyes';
851                                         $response_verbs[] = 'attendno';
852                                         $response_verbs[] = 'attendmaybe';
853                                         if($page_writeable) {
854                                                 $isevent = true;
855                                                 $attend = array( t('I will attend'), t('I will not attend'), t('I might attend'));
856                                         }
857                                 }
858                                 $responses = get_responses($conv_responses,$response_verbs,'',$item);
859
860                                 $locate = array('location' => $item['location'], 'coord' => $item['coord'], 'html' => '');
861                                 call_hooks('render_location',$locate);
862
863                                 $location = ((strlen($locate['html'])) ? $locate['html'] : render_location_dummy($locate));
864
865                                 $indent = (($toplevelpost) ? '' : ' comment');
866
867                                 $shiny = "";
868                                 if(strcmp(datetime_convert('UTC','UTC',$item['created']),datetime_convert('UTC','UTC','now - 12 hours')) > 0)
869                                         $shiny = 'shiny';
870
871                                 //
872                                 localize_item($item);
873
874
875                                 $tags=array();
876                                 foreach(explode(',',$item['tag']) as $tag){
877                                         $tag = trim($tag);
878                                         if ($tag!="") $tags[] = bbcode($tag);
879                                 }
880
881                                 // Build the HTML
882
883                                 $body = prepare_body($item,true);
884                                 //$tmp_item = replace_macros($template,
885
886                                 if($a->theme['template_engine'] === 'internal') {
887                                         $body_e = template_escape($body);
888                                         $text_e = strip_tags(template_escape($body));
889                                         $name_e = template_escape($profile_name);
890                                         $title_e = template_escape($item['title']);
891                                         $location_e = template_escape($location);
892                                         $owner_name_e = template_escape($owner_name);
893                                 }
894                                 else {
895                                         $body_e = $body;
896                                         $text_e = strip_tags($body);
897                                         $name_e = $profile_name;
898                                         $title_e = $item['title'];
899                                         $location_e = $location;
900                                         $owner_name_e = $owner_name;
901                                 }
902
903                                 $tmp_item = array(
904                                         // collapse comments in template. I don't like this much...
905                                         'comment_firstcollapsed' => $comment_firstcollapsed,
906                                         'comment_lastcollapsed' => $comment_lastcollapsed,
907                                         // template to use to render item (wall, walltowall, search)
908                                         'template' => $template,
909
910                                         'type' => implode("",array_slice(explode("/",$item['verb']),-1)),
911                                         'tags' => $tags,
912                                         'body' => $body_e,
913                                         'text' => $text_e,
914                                         'id' => $item['item_id'],
915                                         'isevent' => $isevent,
916                                         'attend' => $attend,
917                                         'linktitle' => sprintf( t('View %s\'s profile @ %s'), $profile_name, ((strlen($item['author-link'])) ? $item['author-link'] : $item['url'])),
918                                         'olinktitle' => sprintf( t('View %s\'s profile @ %s'), $profile_name, ((strlen($item['owner-link'])) ? $item['owner-link'] : $item['url'])),
919                                         'to' => t('to'),
920                                         'wall' => t('Wall-to-Wall'),
921                                         'vwall' => t('via Wall-To-Wall:'),
922                                         'profile_url' => $profile_link,
923                                         'item_photo_menu' => item_photo_menu($item),
924                                         'name' => $name_e,
925                                         'thumb' => proxy_url($profile_avatar, false, PROXY_SIZE_THUMB),
926                                         'osparkle' => $osparkle,
927                                         'sparkle' => $sparkle,
928                                         'title' => $title_e,
929                                         'localtime' => datetime_convert('UTC', date_default_timezone_get(), $item['created'], 'r'),
930                                         'ago' => (($item['app']) ? sprintf( t('%s from %s'),relative_date($item['created']),$item['app']) : relative_date($item['created'])),
931                                         'app' => $item['app'],
932                                         'created' => relative_date($item['created']),
933                                         'lock' => $lock,
934                                         'location' => $location_e,
935                                         'indent' => $indent,
936                                         'shiny' => $shiny,
937                                         'owner_url' => $owner_url,
938                                         'owner_photo' => proxy_url($owner_photo, false, PROXY_SIZE_THUMB),
939                                         'owner_name' => $owner_name_e,
940                                         'plink' => get_plink($item),
941                                         'edpost' => $edpost,
942                                         'isstarred' => $isstarred,
943                                         'star' => $star,
944                                         'ignore'  => ((feature_enabled($profile_owner,'ignore_posts')) ? $ignore : ''),
945                                         'tagger' => $tagger,
946                                         'filer' => ((feature_enabled($profile_owner,'filing')) ? $filer : ''),
947                                         'drop' => $drop,
948                                         'vote' => $likebuttons,
949                                         'responses' => $responses,
950                                         'like' => $like,
951                                         'dislike' => $dislike,
952                                         'switchcomment' => t('Comment'),
953                                         'comment' => $comment,
954                                         'previewing' => $previewing,
955                                         'wait' => t('Please wait'),
956                                         'edited' => $edited,
957                                         'network' => $item["item_network"],
958                                         'network_name' => network_to_name($item['network'], $profile_link),
959
960                                 );
961
962
963                                 $arr = array('item' => $item, 'output' => $tmp_item);
964                                 call_hooks('display_item', $arr);
965
966                                 $threads[$threadsid]['items'][] = $arr['output'];
967                         }
968                 }
969         }
970
971
972         return $threads;
973
974 }