]> git.mxchange.org Git - friendica.git/blob - include/conversation.php
Merge branch 'master', remote-tracking branch 'remotes/upstream/master'
[friendica.git] / include / conversation.php
1 <?php
2
3 /**
4  * Render actions localized
5  */
6 function localize_item(&$item){
7
8         $Text = $item['body'];
9         $saved_image = '';
10         $img_start = strpos($Text,'[img]data:');
11         $img_end = strpos($Text,'[/img]');
12
13         if($img_start !== false && $img_end !== false && $img_end > $img_start) {
14                 $start_fragment = substr($Text,0,$img_start);
15                 $img_start += strlen('[img]');
16                 $saved_image = substr($Text,$img_start,$img_end - $img_start);
17                 $end_fragment = substr($Text,$img_end + strlen('[/img]'));              
18                 $Text = $start_fragment . '[!#saved_image#!]' . $end_fragment;
19                 $search = '/\[url\=(.*?)\]\[!#saved_image#!\]\[\/url\]' . '/is';
20                 $replace = '[url=' . z_path() . '/redir/' . $item['contact-id'] 
21                         . '?f=1&url=' . '$1' . '][!#saved_image#!][/url]' ;
22
23                 $Text = preg_replace($search,$replace,$Text);
24
25                 if(strlen($saved_image))
26                         $item['body'] = str_replace('[!#saved_image#!]', '[img]' . $saved_image . '[/img]',$Text);
27         }
28
29         $xmlhead="<"."?xml version='1.0' encoding='UTF-8' ?".">";
30         if ($item['verb']=== ACTIVITY_LIKE || $item['verb']=== ACTIVITY_DISLIKE){
31
32                 $r = q("SELECT * from `item`,`contact` WHERE 
33                                 `item`.`contact-id`=`contact`.`id` AND `item`.`uri`='%s';",
34                                  dbesc($item['parent-uri']));
35                 if(count($r)==0) return;
36                 $obj=$r[0];
37                 
38                 $author  = '[url=' . $item['author-link'] . ']' . $item['author-name'] . '[/url]';
39                 $objauthor =  '[url=' . $obj['author-link'] . ']' . $obj['author-name'] . '[/url]';
40                 
41                 switch($obj['verb']){
42                         case ACTIVITY_POST:
43                                 switch ($obj['object-type']){
44                                         case ACTIVITY_OBJ_EVENT:
45                                                 $post_type = t('event');
46                                                 break;
47                                         default:
48                                                 $post_type = t('status');
49                                 }
50                                 break;
51                         default:
52                                 if($obj['resource-id']){
53                                         $post_type = t('photo');
54                                         $m=array();     preg_match("/\[url=([^]]*)\]/", $obj['body'], $m);
55                                         $rr['plink'] = $m[1];
56                                 } else {
57                                         $post_type = t('status');
58                                 }
59                 }
60         
61                 $plink = '[url=' . $obj['plink'] . ']' . $post_type . '[/url]';
62                 
63                 switch($item['verb']){
64                         case ACTIVITY_LIKE :
65                                 $bodyverb = t('%1$s likes %2$s\'s %3$s');
66                                 break;
67                         case ACTIVITY_DISLIKE:
68                                 $bodyverb = t('%1$s doesn\'t like %2$s\'s %3$s');
69                                 break;
70                 }
71                 $item['body'] = sprintf($bodyverb, $author, $objauthor, $plink);
72                         
73         }
74         if ($item['verb']=== ACTIVITY_FRIEND){
75
76                 if ($item['object-type']=="" || $item['object-type']!== ACTIVITY_OBJ_PERSON) return;
77
78                 $Aname = $item['author-name'];
79                 $Alink = $item['author-link'];
80                 
81                 $xmlhead="<"."?xml version='1.0' encoding='UTF-8' ?".">";
82                 
83                 $obj = parse_xml_string($xmlhead.$item['object']);
84                 $links = parse_xml_string($xmlhead."<links>".unxmlify($obj->link)."</links>");
85                 
86                 $Bname = $obj->title;
87                 $Blink = ""; $Bphoto = "";
88                 foreach ($links->link as $l){
89                         $atts = $l->attributes();
90                         switch($atts['rel']){
91                                 case "alternate": $Blink = $atts['href'];
92                                 case "photo": $Bphoto = $atts['href'];
93                         }
94                         
95                 }
96                 
97                 $A = '[url=' . zrl($Alink) . ']' . $Aname . '[/url]';
98                 $B = '[url=' . zrl($Blink) . ']' . $Bname . '[/url]';
99                 if ($Bphoto!="") $Bphoto = '[url=' . zrl($Blink) . '][img]' . $Bphoto . '[/img][/url]';
100
101                 $item['body'] = sprintf( t('%1$s is now friends with %2$s'), $A, $B)."\n\n\n".$Bphoto;
102
103         }
104     if ($item['verb']===ACTIVITY_TAG){
105                 $r = q("SELECT * from `item`,`contact` WHERE 
106                 `item`.`contact-id`=`contact`.`id` AND `item`.`uri`='%s';",
107                  dbesc($item['parent-uri']));
108                 if(count($r)==0) return;
109                 $obj=$r[0];
110                 
111                 $author  = '[url=' . zrl($item['author-link']) . ']' . $item['author-name'] . '[/url]';
112                 $objauthor =  '[url=' . zrl($obj['author-link']) . ']' . $obj['author-name'] . '[/url]';
113                 
114                 switch($obj['verb']){
115                         case ACTIVITY_POST:
116                                 switch ($obj['object-type']){
117                                         case ACTIVITY_OBJ_EVENT:
118                                                 $post_type = t('event');
119                                                 break;
120                                         default:
121                                                 $post_type = t('status');
122                                 }
123                                 break;
124                         default:
125                                 if($obj['resource-id']){
126                                         $post_type = t('photo');
127                                         $m=array();     preg_match("/\[url=([^]]*)\]/", $obj['body'], $m);
128                                         $rr['plink'] = $m[1];
129                                 } else {
130                                         $post_type = t('status');
131                                 }
132                 }
133                 $plink = '[url=' . $obj['plink'] . ']' . $post_type . '[/url]';
134                 
135                 $parsedobj = parse_xml_string($xmlhead.$item['object']);
136                 
137                 $tag = sprintf('#[url=%s]%s[/url]', $parsedobj->id, $parsedobj->content);
138                 $item['body'] = sprintf( t('%1$s tagged %2$s\'s %3$s with %4$s'), $author, $objauthor, $plink, $tag );
139                 
140         }
141         if ($item['verb']=== ACTIVITY_FAVORITE){
142
143                 if ($item['object-type']== "")
144                         return;
145
146                 $Aname = $item['author-name'];
147                 $Alink = $item['author-link'];
148                 
149                 $xmlhead="<"."?xml version='1.0' encoding='UTF-8' ?".">";
150                 
151                 $obj = parse_xml_string($xmlhead.$item['object']);
152                 if(strlen($obj->id)) {
153                         $r = q("select * from item where uri = '%s' and uid = %d limit 1",
154                                         dbesc($obj->id),
155                                         intval($item['uid'])
156                         );
157                         if(count($r) && $r[0]['plink']) {
158                                 $target = $r[0];
159                                 $Bname = $target['author-name'];
160                                 $Blink = $target['author-link'];
161                                 $A = '[url=' . zrl($Alink) . ']' . $Aname . '[/url]';
162                                 $B = '[url=' . zrl($Blink) . ']' . $Bname . '[/url]';
163                                 $P = '[url=' . $target['plink'] . ']' . t('post/item') . '[/url]';
164                                 $item['body'] = sprintf( t('%1$s marked %2$s\'s %3$s as favorite'), $A, $B, $P)."\n";
165
166                         }
167                 }
168         }
169         $matches = null;
170         if(preg_match_all('/@\[url=(.*?)\]/is',$item['body'],$matches,PREG_SET_ORDER)) {
171                 foreach($matches as $mtch) {
172                         if(! strpos($mtch[1],'zrl='))
173                                 $item['body'] = str_replace($mtch[0],'@[url=' . zrl($mtch[1]). ']',$item['body']);
174                 }
175         }
176
177 }
178
179 /**
180  * "Render" a conversation or list of items for HTML display.
181  * There are two major forms of display:
182  *      - Sequential or unthreaded ("New Item View" or search results)
183  *      - conversation view
184  * The $mode parameter decides between the various renderings and also
185  * figures out how to determine page owner and other contextual items 
186  * that are based on unique features of the calling module.
187  *
188  */
189
190 if(!function_exists('conversation')) {
191 function conversation(&$a, $items, $mode, $update, $preview = false) {
192
193
194         require_once('bbcode.php');
195
196         $ssl_state = ((local_user()) ? true : false);
197
198         $profile_owner = 0;
199         $page_writeable      = false;
200
201         $previewing = (($preview) ? ' preview ' : '');
202
203         if($mode === 'network') {
204                 $profile_owner = local_user();
205                 $page_writeable = true;
206         }
207
208         if($mode === 'profile') {
209                 $profile_owner = $a->profile['profile_uid'];
210                 $page_writeable = can_write_wall($a,$profile_owner);
211         }
212
213         if($mode === 'notes') {
214                 $profile_owner = local_user();
215                 $page_writeable = true;
216         }
217
218         if($mode === 'display') {
219                 $profile_owner = $a->profile['uid'];
220                 $page_writeable = can_write_wall($a,$profile_owner);
221         }
222
223         if($mode === 'community') {
224                 $profile_owner = 0;
225                 $page_writeable = false;
226         }
227
228         if($update)
229                 $return_url = $_SESSION['return_url'];
230         else
231                 $return_url = $_SESSION['return_url'] = $a->query_string;
232
233         load_contact_links(local_user());
234
235         $cb = array('items' => $items, 'mode' => $mode, 'update' => $update, 'preview' => $preview);
236         call_hooks('conversation_start',$cb);
237
238         $items = $cb['items'];
239
240         $cmnt_tpl    = get_markup_template('comment_item.tpl');
241         $tpl         = 'wall_item.tpl';
242         $wallwall    = 'wallwall_item.tpl';
243         $hide_comments_tpl = get_markup_template('hide_comments.tpl');
244
245         $alike = array();
246         $dlike = array();
247         
248         
249         // array with html for each thread (parent+comments)
250         $threads = array();
251         $threadsid = -1;
252         
253         if($items && count($items)) {
254
255                 if($mode === 'network-new' || $mode === 'search' || $mode === 'community') {
256
257                         // "New Item View" on network page or search page results 
258                         // - just loop through the items and format them minimally for display
259
260                         //$tpl = get_markup_template('search_item.tpl');
261                         $tpl = 'search_item.tpl';
262
263                         foreach($items as $item) {
264                                 $threadsid++;
265
266                                 $comment     = '';
267                                 $owner_url   = '';
268                                 $owner_photo = '';
269                                 $owner_name  = '';
270                                 $sparkle     = '';
271
272                                 if($mode === 'search' || $mode === 'community') {
273                                         if(((activity_match($item['verb'],ACTIVITY_LIKE)) || (activity_match($item['verb'],ACTIVITY_DISLIKE))) 
274                                                 && ($item['id'] != $item['parent']))
275                                                 continue;
276                                         $nickname = $item['nickname'];
277                                 }
278                                 else
279                                         $nickname = $a->user['nickname'];
280                                 
281                         
282                                 $profile_name   = ((strlen($item['author-name']))   ? $item['author-name']   : $item['name']);
283                                 if($item['author-link'] && (! $item['author-name']))
284                                         $profile_name = $item['author-link'];
285
286
287
288                                 $sp = false;
289                                 $profile_link = best_link_url($item,$sp);
290                                 if($profile_link === 'mailbox')
291                                         $profile_link = '';
292                                 if($sp)
293                                         $sparkle = ' sparkle';
294                                 else
295                                         $profile_link = zrl($profile_link);                                     
296
297                                 $normalised = normalise_link((strlen($item['author-link'])) ? $item['author-link'] : $item['url']);
298                                 if(($normalised != 'mailbox') && (x($a->contacts[$normalised])))
299                                         $profile_avatar = $a->contacts[$normalised]['thumb'];
300                                 else
301                                         $profile_avatar = ((strlen($item['author-avatar'])) ? $item['author-avatar'] : $item['thumb']);
302
303                                 $locate = array('location' => $item['location'], 'coord' => $item['coord'], 'html' => '');
304                                 call_hooks('render_location',$locate);
305
306                                 $location = ((strlen($locate['html'])) ? $locate['html'] : render_location_google($locate));
307
308                                 localize_item($item);
309                                 if($mode === 'network-new')
310                                         $dropping = true;
311                                 else
312                                         $dropping = false;
313
314
315                                 $drop = array(
316                                         'dropping' => $dropping,
317                                         'select' => t('Select'), 
318                                         'delete' => t('Delete'),
319                                 );
320
321                                 $star = false;
322                                 $isstarred = "unstarred";
323                                 
324                                 $lock = false;
325                                 $likebuttons = false;
326                                 $shareable = false;
327
328                                 $body = prepare_body($item,true);
329                                 
330                                 //$tmp_item = replace_macros($tpl,array(
331                                 $tmp_item = array(
332                                         'template' => $tpl,
333                                         'id' => (($preview) ? 'P0' : $item['item_id']),
334                                         'linktitle' => sprintf( t('View %s\'s profile @ %s'), $profile_name, ((strlen($item['author-link'])) ? $item['author-link'] : $item['url'])),
335                                         'profile_url' => $profile_link,
336                                         'item_photo_menu' => item_photo_menu($item),
337                                         'name' => template_escape($profile_name),
338                                         'sparkle' => $sparkle,
339                                         'lock' => $lock,
340                                         'thumb' => $profile_avatar,
341                                         'title' => template_escape($item['title']),
342                                         'body' => template_escape($body),
343                                         'text' => strip_tags(template_escape($body)),
344                                         'ago' => (($item['app']) ? sprintf( t('%s from %s'),relative_date($item['created']),$item['app']) : relative_date($item['created'])),
345                                         'location' => template_escape($location),
346                                         'indent' => '',
347                                         'owner_name' => template_escape($owner_name),
348                                         'owner_url' => $owner_url,
349                                         'owner_photo' => $owner_photo,
350                                         'plink' => get_plink($item),
351                                         'edpost' => false,
352                                         'isstarred' => $isstarred,
353                                         'star' => $star,
354                                         'drop' => $drop,
355                                         'vote' => $likebuttons,
356                                         'like' => '',
357                                         'dislike' => '',
358                                         'comment' => '',
359                                         'conv' => (($preview) ? '' : array('href'=> $a->get_baseurl($ssl_state) . '/display/' . $nickname . '/' . $item['id'], 'title'=> t('View in context'))),
360                                         'previewing' => $previewing,
361                                         'wait' => t('Please wait'),
362                                 );
363
364                                 $arr = array('item' => $item, 'output' => $tmp_item);
365                                 call_hooks('display_item', $arr);
366
367                                 $threads[$threadsid]['id'] = $item['item_id'];
368                                 $threads[$threadsid]['items'] = array($arr['output']);
369
370                         }
371
372                 }
373                 else
374                 {
375                         // Normal View
376
377
378                         // Figure out how many comments each parent has
379                         // (Comments all have gravity of 6)
380                         // Store the result in the $comments array
381
382                         $comments = array();
383                         foreach($items as $item) {
384                                 if((intval($item['gravity']) == 6) && ($item['id'] != $item['parent'])) {
385                                         if(! x($comments,$item['parent']))
386                                                 $comments[$item['parent']] = 1;
387                                         else
388                                                 $comments[$item['parent']] += 1;
389                                 } elseif(! x($comments,$item['parent'])) 
390                                         $comments[$item['parent']] = 0; // avoid notices later on
391                         }
392
393                         // map all the like/dislike activities for each parent item 
394                         // Store these in the $alike and $dlike arrays
395
396                         foreach($items as $item) {
397                                 like_puller($a,$item,$alike,'like');
398                                 like_puller($a,$item,$dlike,'dislike');
399                         }
400
401                         $comments_collapsed = false;
402                         $comments_seen = 0;
403                         $comment_lastcollapsed = false;
404                         $comment_firstcollapsed = false;
405                         $blowhard = 0;
406                         $blowhard_count = 0;
407
408
409                         foreach($items as $item) {
410
411                                 $comment = '';
412                                 $template = $tpl;
413                                 $commentww = '';
414                                 $sparkle = '';
415                                 $owner_url = $owner_photo = $owner_name = '';
416
417                                 // We've already parsed out like/dislike for special treatment. We can ignore them now
418
419                                 if(((activity_match($item['verb'],ACTIVITY_LIKE)) 
420                                         || (activity_match($item['verb'],ACTIVITY_DISLIKE))) 
421                                         && ($item['id'] != $item['parent']))
422                                         continue;
423
424                                 $toplevelpost = (($item['id'] == $item['parent']) ? true : false);
425                                 $toplevelprivate = false;
426
427                                 // Take care of author collapsing and comment collapsing
428                                 // (author collapsing is currently disabled)
429                                 // If a single author has more than 3 consecutive top-level posts, squash the remaining ones.
430                                 // If there are more than two comments, squash all but the last 2.
431                         
432                                 if($toplevelpost) {
433                                         $toplevelprivate = (($toplevelpost && $item['private']) ? true : false);
434                                         $item_writeable = (($item['writable'] || $item['self']) ? true : false);
435
436                                         $comments_seen = 0;
437                                         $comments_collapsed = false;
438                                         $comment_lastcollapsed  = false;
439                                         $comment_firstcollapsed = false;
440                                         
441                                         $threadsid++;
442                                         $threads[$threadsid]['id'] = $item['item_id'];
443                                         $threads[$threadsid]['private'] = $item['private'];
444                                         $threads[$threadsid]['items'] = array();
445
446                                 }
447                                 else {
448
449                                         // prevent private email reply to public conversation from leaking.
450                                         if($item['private'] && ! $threads[$threadsid]['private'])
451                                                 continue;
452
453                                         $comments_seen ++;
454                                         $comment_lastcollapsed  = false;
455                                         $comment_firstcollapsed = false;
456                                 }       
457
458                                 $override_comment_box = ((($page_writeable) && ($item_writeable)) ? true : false);
459                                 $show_comment_box = ((($page_writeable) && ($item_writeable) && ($comments_seen == $comments[$item['parent']])) ? true : false);
460
461
462                                 if(($comments[$item['parent']] > 2) && ($comments_seen <= ($comments[$item['parent']] - 2)) && ($item['gravity'] == 6)) {
463
464                                         if (!$comments_collapsed){
465                                                 $threads[$threadsid]['num_comments'] = sprintf( tt('%d comment','%d comments',$comments[$item['parent']]),$comments[$item['parent']] );
466                                                 $threads[$threadsid]['hide_text'] = t('show more');
467                                                 $comments_collapsed = true;
468                                                 $comment_firstcollapsed = true;
469                                         }
470                                 }
471                                 if(($comments[$item['parent']] > 2) && ($comments_seen == ($comments[$item['parent']] - 1))) {
472
473                                         $comment_lastcollapsed = true;
474                                 }
475
476                                 $redirect_url = $a->get_baseurl($ssl_state) . '/redir/' . $item['cid'] ;
477
478                                 $lock = ((($item['private']) || (($item['uid'] == local_user()) && (strlen($item['allow_cid']) || strlen($item['allow_gid']) 
479                                         || strlen($item['deny_cid']) || strlen($item['deny_gid']))))
480                                         ? t('Private Message')
481                                         : false);
482
483
484                                 // Top-level wall post not written by the wall owner (wall-to-wall)
485                                 // First figure out who owns it. 
486
487                                 $osparkle = '';
488
489                                 if(($toplevelpost) && (! $item['self']) && ($mode !== 'profile')) {
490
491                                         if($item['wall']) {
492
493                                                 // On the network page, I am the owner. On the display page it will be the profile owner.
494                                                 // This will have been stored in $a->page_contact by our calling page.
495                                                 // Put this person on the left of the wall-to-wall notice.
496
497                                                 $owner_url = zrl($a->page_contact['url']);
498                                                 $owner_photo = $a->page_contact['thumb'];
499                                                 $owner_name = $a->page_contact['name'];
500                                                 $template = $wallwall;
501                                                 $commentww = 'ww';      
502                                         }
503                                         if((! $item['wall']) && (strlen($item['owner-link'])) && (! link_compare($item['owner-link'],$item['author-link']))) {
504
505                                                 // Could be anybody. 
506
507                                                 $owner_url = $item['owner-link'];
508                                                 $owner_photo = $item['owner-avatar'];
509                                                 $owner_name = $item['owner-name'];
510                                                 $template = $wallwall;
511                                                 $commentww = 'ww';
512                                                 // If it is our contact, use a friendly redirect link
513                                                 if((link_compare($item['owner-link'],$item['url'])) 
514                                                         && ($item['network'] === NETWORK_DFRN)) {
515                                                         $owner_url = $redirect_url;
516                                                         $osparkle = ' sparkle';
517                                                 }
518                                                 else
519                                                         $owner_url = zrl($owner_url);
520                                         }
521                                 }
522
523                                 $likebuttons = '';
524                                 $shareable = ((($profile_owner == local_user()) &&  (! $item['private'])) ? true : false); //($mode != 'display') &&
525
526                                 if($page_writeable) {
527                                         if($toplevelpost) {
528                                                 $likebuttons = array(
529                                                         'like' => array( t("I like this \x28toggle\x29"), t("like")),
530                                                         'dislike' => array( t("I don't like this \x28toggle\x29"), t("dislike")),
531                                                 );
532                                                 if ($shareable) $likebuttons['share'] = array( t('Share this'), t('share'));
533                                         }
534
535                                         $qc = $qcomment =  null;
536
537                                         if(in_array('qcomment',$a->plugins)) {
538                                                 $qc = ((local_user()) ? get_pconfig(local_user(),'qcomment','words') : null);
539                                                 $qcomment = (($qc) ? explode("\n",$qc) : null);
540                                         }
541
542                                         if(($show_comment_box) || (($show_comment_box == false) && ($override_comment_box == false) && ($item['last-child']))) {
543                                                 $comment = replace_macros($cmnt_tpl,array(
544                                                         '$return_path' => '', 
545                                                         '$jsreload' => (($mode === 'display') ? $_SESSION['return_url'] : ''),
546                                                         '$type' => (($mode === 'profile') ? 'wall-comment' : 'net-comment'),
547                                                         '$id' => $item['item_id'],
548                                                         '$parent' => $item['parent'],
549                                                         '$qcomment' => $qcomment,
550                                                         '$profile_uid' =>  $profile_owner,
551                                                         '$mylink' => $a->contact['url'],
552                                                         '$mytitle' => t('This is you'),
553                                                         '$myphoto' => $a->contact['thumb'],
554                                                         '$comment' => t('Comment'),
555                                                         '$submit' => t('Submit'),
556                                                         '$edbold' => t('Bold'),
557                                                         '$editalic' => t('Italic'),
558                                                         '$eduline' => t('Underline'),
559                                                         '$edquote' => t('Quote'),
560                                                         '$edcode' => t('Code'),
561                                                         '$edimg' => t('Image'),
562                                                         '$edurl' => t('Link'),
563                                                         '$edvideo' => t('Video'),
564                                                         '$preview' => t('Preview'),
565                                                         '$ww' => (($mode === 'network') ? $commentww : '')
566                                                 ));
567                                         }
568                                 }
569
570                                 if(local_user() && link_compare($a->contact['url'],$item['author-link']))
571                                         $edpost = array($a->get_baseurl($ssl_state)."/editpost/".$item['id'], t("Edit"));
572                                 else
573                                         $edpost = false;
574
575                                 $drop = '';
576                                 $dropping = false;
577
578                                 if((intval($item['contact-id']) && $item['contact-id'] == remote_user()) || ($item['uid'] == local_user()))
579                                         $dropping = true;
580
581                                 $drop = array(
582                                         'dropping' => $dropping,
583                                         'select' => t('Select'), 
584                                         'delete' => t('Delete'),
585                                 );
586
587                                 $star = false;
588                                 $filer = false;
589
590                                 $isstarred = "unstarred";
591                                 if ($profile_owner == local_user()) {
592                                         if($toplevelpost) {
593                                                 $isstarred = (($item['starred']) ? "starred" : "unstarred");
594
595                                                 $star = array(
596                                                         'do' => t("add star"),
597                                                         'undo' => t("remove star"),
598                                                         'toggle' => t("toggle star status"),
599                                                         'classdo' => (($item['starred']) ? "hidden" : ""),
600                                                         'classundo' => (($item['starred']) ? "" : "hidden"),
601                                                         'starred' =>  t('starred'),
602                                                         'tagger' => t("add tag"),
603                                                         'classtagger' => "",
604                                                 );
605                                         }
606                                         $filer = t("save to folder");
607                                 }
608
609
610                                 $photo = $item['photo'];
611                                 $thumb = $item['thumb'];
612
613                                 // Post was remotely authored.
614
615                                 $diff_author    = ((link_compare($item['url'],$item['author-link'])) ? false : true);
616
617                                 $profile_name   = (((strlen($item['author-name']))   && $diff_author) ? $item['author-name']   : $item['name']);
618
619                                 if($item['author-link'] && (! $item['author-name']))
620                                         $profile_name = $item['author-link'];
621
622                                 $sp = false;
623                                 $profile_link = best_link_url($item,$sp);
624                                 if($profile_link === 'mailbox')
625                                         $profile_link = '';
626                                 if($sp)
627                                         $sparkle = ' sparkle';
628                                 else
629                                         $profile_link = zrl($profile_link);                                     
630
631                                 $normalised = normalise_link((strlen($item['author-link'])) ? $item['author-link'] : $item['url']);
632                                 if(($normalised != 'mailbox') && (x($a->contacts,$normalised)))
633                                         $profile_avatar = $a->contacts[$normalised]['thumb'];
634                                 else
635                                         $profile_avatar = (((strlen($item['author-avatar'])) && $diff_author) ? $item['author-avatar'] : $thumb);
636
637                                 $like    = ((x($alike,$item['id'])) ? format_like($alike[$item['id']],$alike[$item['id'] . '-l'],'like',$item['id']) : '');
638                                 $dislike = ((x($dlike,$item['id'])) ? format_like($dlike[$item['id']],$dlike[$item['id'] . '-l'],'dislike',$item['id']) : '');
639
640                                 $locate = array('location' => $item['location'], 'coord' => $item['coord'], 'html' => '');
641                                 call_hooks('render_location',$locate);
642
643                                 $location = ((strlen($locate['html'])) ? $locate['html'] : render_location_google($locate));
644
645                                 $indent = (($toplevelpost) ? '' : ' comment');
646
647                                 if(strcmp(datetime_convert('UTC','UTC',$item['created']),datetime_convert('UTC','UTC','now - 12 hours')) > 0)
648                                         $indent .= ' shiny'; 
649
650                                 // 
651                                 localize_item($item);
652
653
654                                 $tags=array();
655                                 foreach(explode(',',$item['tag']) as $tag){
656                                         $tag = trim($tag);
657                                         if ($tag!="") $tags[] = bbcode($tag);
658                                 }
659
660                                 // Build the HTML
661
662                                 $body = prepare_body($item,true);
663                                 //$tmp_item = replace_macros($template,
664                                 $tmp_item = array(
665                                         // collapse comments in template. I don't like this much...
666                                         'comment_firstcollapsed' => $comment_firstcollapsed,
667                                         'comment_lastcollapsed' => $comment_lastcollapsed,
668                                         // template to use to render item (wall, walltowall, search)
669                                         'template' => $template,
670                                         
671                                         'type' => implode("",array_slice(explode("/",$item['verb']),-1)),
672                                         'tags' => $tags,
673                                         'body' => template_escape($body),
674                                         'text' => strip_tags(template_escape($body)),
675                                         'id' => $item['item_id'],
676                                         'linktitle' => sprintf( t('View %s\'s profile @ %s'), $profile_name, ((strlen($item['author-link'])) ? $item['author-link'] : $item['url'])),
677                                         'olinktitle' => sprintf( t('View %s\'s profile @ %s'), $profile_name, ((strlen($item['owner-link'])) ? $item['owner-link'] : $item['url'])),
678                                         'to' => t('to'),
679                                         'wall' => t('Wall-to-Wall'),
680                                         'vwall' => t('via Wall-To-Wall:'),
681                                         'profile_url' => $profile_link,
682                                         'item_photo_menu' => item_photo_menu($item),
683                                         'name' => template_escape($profile_name),
684                                         'thumb' => $profile_avatar,
685                                         'osparkle' => $osparkle,
686                                         'sparkle' => $sparkle,
687                                         'title' => template_escape($item['title']),
688                                         'ago' => (($item['app']) ? sprintf( t('%s from %s'),relative_date($item['created']),$item['app']) : relative_date($item['created'])),
689                                         'lock' => $lock,
690                                         'location' => template_escape($location),
691                                         'indent' => $indent,
692                                         'owner_url' => $owner_url,
693                                         'owner_photo' => $owner_photo,
694                                         'owner_name' => template_escape($owner_name),
695                                         'plink' => get_plink($item),
696                                         'edpost' => $edpost,
697                                         'isstarred' => $isstarred,
698                                         'star' => $star,
699                                         'filer' => $filer,
700                                         'drop' => $drop,
701                                         'vote' => $likebuttons,
702                                         'like' => $like,
703                                         'dislike' => $dislike,
704                                         'comment' => $comment,
705                                         'previewing' => $previewing,
706                                         'wait' => t('Please wait'),
707
708                                 );
709
710
711                                 $arr = array('item' => $item, 'output' => $tmp_item);
712                                 call_hooks('display_item', $arr);
713
714                                 $threads[$threadsid]['items'][] = $arr['output'];
715                         }
716                 }
717         }
718
719         $page_template = get_markup_template("conversation.tpl");
720         $o = replace_macros($page_template, array(
721                 '$baseurl' => $a->get_baseurl($ssl_state),
722                 '$mode' => $mode,
723                 '$user' => $a->user,
724                 '$threads' => $threads,
725                 '$dropping' => ($dropping?t('Delete Selected Items'):False),
726         ));
727
728         return $o;
729 }}
730
731 function best_link_url($item,&$sparkle,$ssl_state = false) {
732
733         $a = get_app();
734
735         $best_url = '';
736         $sparkle  = false;
737
738         $clean_url = normalise_link($item['author-link']);
739
740         if((local_user()) && (local_user() == $item['uid'])) {
741                 if(isset($a->contacts) && x($a->contacts,$clean_url)) {
742                         if($a->contacts[$clean_url]['network'] === NETWORK_DFRN) {
743                                 $best_url = $a->get_baseurl($ssl_state) . '/redir/' . $a->contacts[$clean_url]['id'];
744                                 $sparkle = true;
745                         }
746                         else
747                                 $best_url = $a->contacts[$clean_url]['url'];
748                 }
749         }
750         if(! $best_url) {
751                 if(strlen($item['author-link']))
752                         $best_url = $item['author-link'];
753                 else
754                         $best_url = $item['url'];
755         }
756
757         return $best_url;
758 }
759
760
761 if(! function_exists('item_photo_menu')){
762 function item_photo_menu($item){
763         $a = get_app();
764
765         $ssl_state = false;
766
767         if(local_user()) {
768                 $ssl_state = true;
769                  if(! count($a->contacts))
770                         load_contact_links(local_user());
771         }
772         $contact_url="";
773         $pm_url="";
774         $status_link="";
775         $photos_link="";
776         $posts_link="";
777
778         $sparkle = false;
779     $profile_link = best_link_url($item,$sparkle,$ssl_state);
780         if($profile_link === 'mailbox')
781                 $profile_link = '';
782
783         if($sparkle) {
784                 $cid = intval(basename($profile_link));
785                 $status_link = $profile_link . "?url=status";
786                 $photos_link = $profile_link . "?url=photos";
787                 $profile_link = $profile_link . "?url=profile";
788                 $pm_url = $a->get_baseurl($ssl_state) . '/message/new/' . $cid;
789                 $zurl = '';
790         }
791         else {
792                 $profile_link = zrl($profile_link);
793                 if(local_user() && local_user() == $item['uid'] && link_compare($item['url'],$item['author-link'])) {
794                         $cid = $item['contact-id'];
795                 }               
796                 else {
797                         $cid = 0;
798                 }
799         }
800         if(($cid) && (! $item['self'])) {
801                 $contact_url = $a->get_baseurl($ssl_state) . '/contacts/' . $cid;
802                 $posts_link = $a->get_baseurl($ssl_state) . '/network/?cid=' . $cid;
803
804                 $clean_url = normalise_link($item['author-link']);
805
806                 if((local_user()) && (local_user() == $item['uid'])) {
807                         if(isset($a->contacts) && x($a->contacts,$clean_url)) {
808                                 if($a->contacts[$clean_url]['network'] === NETWORK_DIASPORA) {
809                                         $pm_url = $a->get_baseurl($ssl_state) . '/message/new/' . $cid;
810                                 }
811                         }
812                 }
813
814         }
815
816         $menu = Array(
817                 t("View Status") => $status_link,
818                 t("View Profile") => $profile_link,
819                 t("View Photos") => $photos_link,
820                 t("Network Posts") => $posts_link, 
821                 t("Edit Contact") => $contact_url,
822                 t("Send PM") => $pm_url,
823         );
824         
825         
826         $args = array('item' => $item, 'menu' => $menu);
827         
828         call_hooks('item_photo_menu', $args);
829
830         $menu = $args['menu'];  
831
832         $o = "";
833         foreach($menu as $k=>$v){
834                 if ($v!="") $o .= "<li><a href='$v'>$k</a></li>\n";
835         }
836         return $o;
837 }}
838
839 if(! function_exists('like_puller')) {
840 function like_puller($a,$item,&$arr,$mode) {
841
842         $url = '';
843         $sparkle = '';
844         $verb = (($mode === 'like') ? ACTIVITY_LIKE : ACTIVITY_DISLIKE);
845
846         if((activity_match($item['verb'],$verb)) && ($item['id'] != $item['parent'])) {
847                 $url = $item['author-link'];
848                 if((local_user()) && (local_user() == $item['uid']) && ($item['network'] === 'dfrn') && (! $item['self']) && (link_compare($item['author-link'],$item['url']))) {
849                         $url = $a->get_baseurl(true) . '/redir/' . $item['contact-id'];
850                         $sparkle = ' class="sparkle" ';
851                 }
852                 else
853                         $url = zrl($url);
854                 if(! ((isset($arr[$item['parent'] . '-l'])) && (is_array($arr[$item['parent'] . '-l']))))
855                         $arr[$item['parent'] . '-l'] = array();
856                 if(! isset($arr[$item['parent']]))
857                         $arr[$item['parent']] = 1;
858                 else    
859                         $arr[$item['parent']] ++;
860                 $arr[$item['parent'] . '-l'][] = '<a href="'. $url . '"'. $sparkle .'>' . $item['author-name'] . '</a>';
861         }
862         return;
863 }}
864
865 // Format the like/dislike text for a profile item
866 // $cnt = number of people who like/dislike the item
867 // $arr = array of pre-linked names of likers/dislikers
868 // $type = one of 'like, 'dislike'
869 // $id  = item id
870 // returns formatted text
871
872 if(! function_exists('format_like')) {
873 function format_like($cnt,$arr,$type,$id) {
874         $o = '';
875         if($cnt == 1)
876                 $o .= (($type === 'like') ? sprintf( t('%s likes this.'), $arr[0]) : sprintf( t('%s doesn\'t like this.'), $arr[0])) . EOL ;
877         else {
878                 $spanatts = 'class="fakelink" onclick="openClose(\'' . $type . 'list-' . $id . '\');"';
879                 $o .= (($type === 'like') ? 
880                                         sprintf( t('<span  %1$s>%2$d people</span> like this.'), $spanatts, $cnt)
881                                          : 
882                                         sprintf( t('<span  %1$s>%2$d people</span> don\'t like this.'), $spanatts, $cnt) ); 
883                 $o .= EOL ;
884                 $total = count($arr);
885                 if($total >= MAX_LIKERS)
886                         $arr = array_slice($arr, 0, MAX_LIKERS - 1);
887                 if($total < MAX_LIKERS)
888                         $arr[count($arr)-1] = t('and') . ' ' . $arr[count($arr)-1];
889                 $str = implode(', ', $arr);
890                 if($total >= MAX_LIKERS)
891                         $str .= sprintf( t(', and %d other people'), $total - MAX_LIKERS );
892                 $str = (($type === 'like') ? sprintf( t('%s like this.'), $str) : sprintf( t('%s don\'t like this.'), $str));
893                 $o .= "\t" . '<div id="' . $type . 'list-' . $id . '" style="display: none;" >' . $str . '</div>';
894         }
895         return $o;
896 }}
897
898
899 function status_editor($a,$x, $notes_cid = 0, $popup=false) {
900
901         $o = '';
902                 
903         $geotag = (($x['allow_location']) ? get_markup_template('jot_geotag.tpl') : '');
904
905         $plaintext = false;
906         if(local_user() && intval(get_pconfig(local_user(),'system','plaintext')))
907                 $plaintext = true;
908
909         $tpl = get_markup_template('jot-header.tpl');
910         
911         $a->page['htmlhead'] .= replace_macros($tpl, array(
912                 '$newpost' => 'true',
913                 '$baseurl' => $a->get_baseurl(true),
914                 '$editselect' => (($plaintext) ? 'none' : '/(profile-jot-text|prvmail-text)/'),
915                 '$geotag' => $geotag,
916                 '$nickname' => $x['nickname'],
917                 '$ispublic' => t('Visible to <strong>everybody</strong>'),
918                 '$linkurl' => t('Please enter a link URL:'),
919                 '$vidurl' => t("Please enter a video link/URL:"),
920                 '$audurl' => t("Please enter an audio link/URL:"),
921                 '$term' => t('Tag term:'),
922                 '$fileas' => t('Save to Folder:'),
923                 '$whereareu' => t('Where are you right now?')
924         ));
925
926
927         $tpl = get_markup_template("jot.tpl");
928                 
929         $jotplugins = '';
930         $jotnets = '';
931
932         $mail_disabled = ((function_exists('imap_open') && (! get_config('system','imap_disabled'))) ? 0 : 1);
933
934         $mail_enabled = false;
935         $pubmail_enabled = false;
936
937         if(($x['is_owner']) && (! $mail_disabled)) {
938                 $r = q("SELECT * FROM `mailacct` WHERE `uid` = %d AND `server` != '' LIMIT 1",
939                         intval(local_user())
940                 );
941                 if(count($r)) {
942                         $mail_enabled = true;
943                         if(intval($r[0]['pubmail']))
944                                 $pubmail_enabled = true;
945                 }
946         }
947
948         if($mail_enabled) {
949                 $selected = (($pubmail_enabled) ? ' checked="checked" ' : '');
950                 $jotnets .= '<div class="profile-jot-net"><input type="checkbox" name="pubmail_enable"' . $selected . ' value="1" /> ' . t("Post to Email") . '</div>';
951         }
952
953         call_hooks('jot_tool', $jotplugins);
954         call_hooks('jot_networks', $jotnets);
955
956         if($notes_cid)
957                 $jotnets .= '<input type="hidden" name="contact_allow[]" value="' . $notes_cid .'" />';
958
959         $tpl = replace_macros($tpl,array('$jotplugins' => $jotplugins));        
960
961         $o .= replace_macros($tpl,array(
962                 '$return_path' => $a->cmd,
963                 '$action' =>  $a->get_baseurl(true) . '/item',
964                 '$share' => (x($x,'button') ? $x['button'] : t('Share')),
965                 '$upload' => t('Upload photo'),
966                 '$shortupload' => t('upload photo'),
967                 '$attach' => t('Attach file'),
968                 '$shortattach' => t('attach file'),
969                 '$weblink' => t('Insert web link'),
970                 '$shortweblink' => t('web link'),
971                 '$video' => t('Insert video link'),
972                 '$shortvideo' => t('video link'),
973                 '$audio' => t('Insert audio link'),
974                 '$shortaudio' => t('audio link'),
975                 '$setloc' => t('Set your location'),
976                 '$shortsetloc' => t('set location'),
977                 '$noloc' => t('Clear browser location'),
978                 '$shortnoloc' => t('clear location'),
979                 '$title' => "",
980                 '$placeholdertitle' => t('Set title'),
981                 '$category' => "",
982                 '$placeholdercategory' => t('Categories (comma-separated list)'),
983                 '$wait' => t('Please wait'),
984                 '$permset' => t('Permission settings'),
985                 '$shortpermset' => t('permissions'),
986                 '$ptyp' => (($notes_cid) ? 'note' : 'wall'),
987                 '$content' => '',
988                 '$post_id' => '',
989                 '$baseurl' => $a->get_baseurl(true),
990                 '$defloc' => $x['default_location'],
991                 '$visitor' => $x['visitor'],
992                 '$pvisit' => (($notes_cid) ? 'none' : $x['visitor']),
993                 '$emailcc' => t('CC: email addresses'),
994                 '$public' => t('Public post'),
995                 '$jotnets' => $jotnets,
996                 '$emtitle' => t('Example: bob@example.com, mary@example.com'),
997                 '$lockstate' => $x['lockstate'],
998                 '$acl' => $x['acl'],
999                 '$bang' => $x['bang'],
1000                 '$profile_uid' => $x['profile_uid'],
1001                 '$preview' => t('Preview'),
1002         ));
1003
1004
1005         if ($popup==true){
1006                 $o = '<div id="jot-popup" style="display: none;">'.$o.'</div>';
1007                 
1008         }
1009
1010         return $o;
1011 }
1012
1013
1014 function conv_sort($arr,$order) {
1015
1016         if((!(is_array($arr) && count($arr))))
1017                 return array();
1018
1019         $parents = array();
1020
1021         foreach($arr as $x)
1022                 if($x['id'] == $x['parent'])
1023                                 $parents[] = $x;
1024
1025         if(stristr($order,'created'))
1026                 usort($parents,'sort_thr_created');
1027         elseif(stristr($order,'commented'))
1028                 usort($parents,'sort_thr_commented');
1029
1030         if(count($parents))
1031                 foreach($parents as $i=>$_x) 
1032                         $parents[$i]['children'] = array();
1033
1034         foreach($arr as $x) {
1035                 if($x['id'] != $x['parent']) {
1036                         $p = find_thread_parent_index($parents,$x);
1037                         if($p !== false)
1038                                 $parents[$p]['children'][] = $x;
1039                 }
1040         }
1041         if(count($parents)) {
1042                 foreach($parents as $k => $v) {
1043                         if(count($parents[$k]['children'])) {
1044                                 $y = $parents[$k]['children'];
1045                                 usort($y,'sort_thr_created_rev');
1046                                 $parents[$k]['children'] = $y;
1047                         }
1048                 }       
1049         }
1050
1051         $ret = array();
1052         if(count($parents)) {
1053                 foreach($parents as $x) {
1054                         $ret[] = $x;
1055                         if(count($x['children']))
1056                                 foreach($x['children'] as $y)
1057                                         $ret[] = $y;
1058                 }
1059         }
1060
1061         return $ret;
1062 }
1063
1064
1065 function sort_thr_created($a,$b) {
1066         return strcmp($b['created'],$a['created']);
1067 }
1068
1069 function sort_thr_created_rev($a,$b) {
1070         return strcmp($a['created'],$b['created']);
1071 }
1072
1073 function sort_thr_commented($a,$b) {
1074         return strcmp($b['commented'],$a['commented']);
1075 }
1076
1077 function find_thread_parent_index($arr,$x) {
1078         foreach($arr as $k => $v)
1079                 if($v['id'] == $x['parent'])
1080                         return $k;
1081         return false;
1082 }
1083
1084 function render_location_google($item) {
1085         $location = (($item['location']) ? '<a target="map" title="' . $item['location'] . '" href="http://maps.google.com/?q=' . urlencode($item['location']) . '">' . $item['location'] . '</a>' : '');
1086         $coord = (($item['coord']) ? '<a target="map" title="' . $item['coord'] . '" href="http://maps.google.com/?q=' . urlencode($item['coord']) . '">' . $item['coord'] . '</a>' : '');
1087         if($coord) {
1088                 if($location)
1089                         $location .= '<br /><span class="smalltext">(' . $coord . ')</span>';
1090                 else
1091                         $location = '<span class="smalltext">' . $coord . '</span>';
1092         }
1093         return $location;
1094 }