]> git.mxchange.org Git - friendica.git/blob - include/conversation.php
Merge branch 'master' of github.com:annando/friendica
[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                                 // prevent private email from leaking.
282                                 if($item['network'] === NETWORK_MAIL && local_user() != $item['uid'])
283                                                 continue;
284                         
285                                 $profile_name   = ((strlen($item['author-name']))   ? $item['author-name']   : $item['name']);
286                                 if($item['author-link'] && (! $item['author-name']))
287                                         $profile_name = $item['author-link'];
288
289
290
291                                 $sp = false;
292                                 $profile_link = best_link_url($item,$sp);
293                                 if($profile_link === 'mailbox')
294                                         $profile_link = '';
295                                 if($sp)
296                                         $sparkle = ' sparkle';
297                                 else
298                                         $profile_link = zrl($profile_link);                                     
299
300                                 $normalised = normalise_link((strlen($item['author-link'])) ? $item['author-link'] : $item['url']);
301                                 if(($normalised != 'mailbox') && (x($a->contacts[$normalised])))
302                                         $profile_avatar = $a->contacts[$normalised]['thumb'];
303                                 else
304                                         $profile_avatar = ((strlen($item['author-avatar'])) ? $item['author-avatar'] : $item['thumb']);
305
306                                 $locate = array('location' => $item['location'], 'coord' => $item['coord'], 'html' => '');
307                                 call_hooks('render_location',$locate);
308
309                                 $location = ((strlen($locate['html'])) ? $locate['html'] : render_location_google($locate));
310
311                                 localize_item($item);
312                                 if($mode === 'network-new')
313                                         $dropping = true;
314                                 else
315                                         $dropping = false;
316
317
318                                 $drop = array(
319                                         'dropping' => $dropping,
320                                         'select' => t('Select'), 
321                                         'delete' => t('Delete'),
322                                 );
323
324                                 $star = false;
325                                 $isstarred = "unstarred";
326                                 
327                                 $lock = false;
328                                 $likebuttons = false;
329                                 $shareable = false;
330
331                                 $body = prepare_body($item,true);
332                                 
333                                 //$tmp_item = replace_macros($tpl,array(
334                                 $tmp_item = array(
335                                         'template' => $tpl,
336                                         'id' => (($preview) ? 'P0' : $item['item_id']),
337                                         'linktitle' => sprintf( t('View %s\'s profile @ %s'), $profile_name, ((strlen($item['author-link'])) ? $item['author-link'] : $item['url'])),
338                                         'profile_url' => $profile_link,
339                                         'item_photo_menu' => item_photo_menu($item),
340                                         'name' => template_escape($profile_name),
341                                         'sparkle' => $sparkle,
342                                         'lock' => $lock,
343                                         'thumb' => $profile_avatar,
344                                         'title' => template_escape($item['title']),
345                                         'body' => template_escape($body),
346                                         'text' => strip_tags(template_escape($body)),
347                                         'ago' => (($item['app']) ? sprintf( t('%s from %s'),relative_date($item['created']),$item['app']) : relative_date($item['created'])),
348                                         'location' => template_escape($location),
349                                         'indent' => '',
350                                         'owner_name' => template_escape($owner_name),
351                                         'owner_url' => $owner_url,
352                                         'owner_photo' => $owner_photo,
353                                         'plink' => get_plink($item),
354                                         'edpost' => false,
355                                         'isstarred' => $isstarred,
356                                         'star' => $star,
357                                         'drop' => $drop,
358                                         'vote' => $likebuttons,
359                                         'like' => '',
360                                         'dislike' => '',
361                                         'comment' => '',
362                                         'conv' => (($preview) ? '' : array('href'=> $a->get_baseurl($ssl_state) . '/display/' . $nickname . '/' . $item['id'], 'title'=> t('View in context'))),
363                                         'previewing' => $previewing,
364                                         'wait' => t('Please wait'),
365                                 );
366
367                                 $arr = array('item' => $item, 'output' => $tmp_item);
368                                 call_hooks('display_item', $arr);
369
370                                 $threads[$threadsid]['id'] = $item['item_id'];
371                                 $threads[$threadsid]['items'] = array($arr['output']);
372
373                         }
374
375                 }
376                 else
377                 {
378                         // Normal View
379
380
381                         // Figure out how many comments each parent has
382                         // (Comments all have gravity of 6)
383                         // Store the result in the $comments array
384
385                         $comments = array();
386                         foreach($items as $item) {
387                                 if((intval($item['gravity']) == 6) && ($item['id'] != $item['parent'])) {
388                                         if(! x($comments,$item['parent']))
389                                                 $comments[$item['parent']] = 1;
390                                         else
391                                                 $comments[$item['parent']] += 1;
392                                 } elseif(! x($comments,$item['parent'])) 
393                                         $comments[$item['parent']] = 0; // avoid notices later on
394                         }
395
396                         // map all the like/dislike activities for each parent item 
397                         // Store these in the $alike and $dlike arrays
398
399                         foreach($items as $item) {
400                                 like_puller($a,$item,$alike,'like');
401                                 like_puller($a,$item,$dlike,'dislike');
402                         }
403
404                         $comments_collapsed = false;
405                         $comments_seen = 0;
406                         $comment_lastcollapsed = false;
407                         $comment_firstcollapsed = false;
408                         $blowhard = 0;
409                         $blowhard_count = 0;
410
411
412                         foreach($items as $item) {
413
414                                 $comment = '';
415                                 $template = $tpl;
416                                 $commentww = '';
417                                 $sparkle = '';
418                                 $owner_url = $owner_photo = $owner_name = '';
419
420                                 // We've already parsed out like/dislike for special treatment. We can ignore them now
421
422                                 if(((activity_match($item['verb'],ACTIVITY_LIKE)) 
423                                         || (activity_match($item['verb'],ACTIVITY_DISLIKE))) 
424                                         && ($item['id'] != $item['parent']))
425                                         continue;
426
427                                 $toplevelpost = (($item['id'] == $item['parent']) ? true : false);
428                                 $toplevelprivate = false;
429
430                                 // Take care of author collapsing and comment collapsing
431                                 // (author collapsing is currently disabled)
432                                 // If a single author has more than 3 consecutive top-level posts, squash the remaining ones.
433                                 // If there are more than two comments, squash all but the last 2.
434                         
435                                 if($toplevelpost) {
436                                         $toplevelprivate = (($toplevelpost && $item['private']) ? true : false);
437                                         $item_writeable = (($item['writable'] || $item['self']) ? true : false);
438
439                                         $comments_seen = 0;
440                                         $comments_collapsed = false;
441                                         $comment_lastcollapsed  = false;
442                                         $comment_firstcollapsed = false;
443                                         
444                                         $threadsid++;
445                                         $threads[$threadsid]['id'] = $item['item_id'];
446                                         $threads[$threadsid]['private'] = $item['private'];
447                                         $threads[$threadsid]['items'] = array();
448
449                                 }
450                                 else {
451
452                                         // prevent private email reply to public conversation from leaking.
453                                         if($item['network'] === NETWORK_MAIL && local_user() != $item['uid'])
454                                                         continue;
455
456                                         $comments_seen ++;
457                                         $comment_lastcollapsed  = false;
458                                         $comment_firstcollapsed = false;
459                                 }       
460
461                                 $override_comment_box = ((($page_writeable) && ($item_writeable)) ? true : false);
462                                 $show_comment_box = ((($page_writeable) && ($item_writeable) && ($comments_seen == $comments[$item['parent']])) ? true : false);
463
464
465                                 if(($comments[$item['parent']] > 2) && ($comments_seen <= ($comments[$item['parent']] - 2)) && ($item['gravity'] == 6)) {
466
467                                         if (!$comments_collapsed){
468                                                 $threads[$threadsid]['num_comments'] = sprintf( tt('%d comment','%d comments',$comments[$item['parent']]),$comments[$item['parent']] );
469                                                 $threads[$threadsid]['hide_text'] = t('show more');
470                                                 $comments_collapsed = true;
471                                                 $comment_firstcollapsed = true;
472                                         }
473                                 }
474                                 if(($comments[$item['parent']] > 2) && ($comments_seen == ($comments[$item['parent']] - 1))) {
475
476                                         $comment_lastcollapsed = true;
477                                 }
478
479                                 $redirect_url = $a->get_baseurl($ssl_state) . '/redir/' . $item['cid'] ;
480
481                                 $lock = ((($item['private']) || (($item['uid'] == local_user()) && (strlen($item['allow_cid']) || strlen($item['allow_gid']) 
482                                         || strlen($item['deny_cid']) || strlen($item['deny_gid']))))
483                                         ? t('Private Message')
484                                         : false);
485
486
487                                 // Top-level wall post not written by the wall owner (wall-to-wall)
488                                 // First figure out who owns it. 
489
490                                 $osparkle = '';
491
492                                 if(($toplevelpost) && (! $item['self']) && ($mode !== 'profile')) {
493
494                                         if($item['wall']) {
495
496                                                 // On the network page, I am the owner. On the display page it will be the profile owner.
497                                                 // This will have been stored in $a->page_contact by our calling page.
498                                                 // Put this person on the left of the wall-to-wall notice.
499
500                                                 $owner_url = zrl($a->page_contact['url']);
501                                                 $owner_photo = $a->page_contact['thumb'];
502                                                 $owner_name = $a->page_contact['name'];
503                                                 $template = $wallwall;
504                                                 $commentww = 'ww';      
505                                         }
506                                         if((! $item['wall']) && (strlen($item['owner-link'])) && (! link_compare($item['owner-link'],$item['author-link']))) {
507
508                                                 // Could be anybody. 
509
510                                                 $owner_url = $item['owner-link'];
511                                                 $owner_photo = $item['owner-avatar'];
512                                                 $owner_name = $item['owner-name'];
513                                                 $template = $wallwall;
514                                                 $commentww = 'ww';
515                                                 // If it is our contact, use a friendly redirect link
516                                                 if((link_compare($item['owner-link'],$item['url'])) 
517                                                         && ($item['network'] === NETWORK_DFRN)) {
518                                                         $owner_url = $redirect_url;
519                                                         $osparkle = ' sparkle';
520                                                 }
521                                                 else
522                                                         $owner_url = zrl($owner_url);
523                                         }
524                                 }
525
526                                 $likebuttons = '';
527                                 $shareable = ((($profile_owner == local_user()) &&  (! $item['private'])) ? true : false); //($mode != 'display') &&
528
529                                 if($page_writeable) {
530                                         if($toplevelpost) {
531                                                 $likebuttons = array(
532                                                         'like' => array( t("I like this \x28toggle\x29"), t("like")),
533                                                         'dislike' => array( t("I don't like this \x28toggle\x29"), t("dislike")),
534                                                 );
535                                                 if ($shareable) $likebuttons['share'] = array( t('Share this'), t('share'));
536                                         }
537
538                                         $qc = $qcomment =  null;
539
540                                         if(in_array('qcomment',$a->plugins)) {
541                                                 $qc = ((local_user()) ? get_pconfig(local_user(),'qcomment','words') : null);
542                                                 $qcomment = (($qc) ? explode("\n",$qc) : null);
543                                         }
544
545                                         if(($show_comment_box) || (($show_comment_box == false) && ($override_comment_box == false) && ($item['last-child']))) {
546                                                 $comment = replace_macros($cmnt_tpl,array(
547                                                         '$return_path' => '', 
548                                                         '$jsreload' => (($mode === 'display') ? $_SESSION['return_url'] : ''),
549                                                         '$type' => (($mode === 'profile') ? 'wall-comment' : 'net-comment'),
550                                                         '$id' => $item['item_id'],
551                                                         '$parent' => $item['parent'],
552                                                         '$qcomment' => $qcomment,
553                                                         '$profile_uid' =>  $profile_owner,
554                                                         '$mylink' => $a->contact['url'],
555                                                         '$mytitle' => t('This is you'),
556                                                         '$myphoto' => $a->contact['thumb'],
557                                                         '$comment' => t('Comment'),
558                                                         '$submit' => t('Submit'),
559                                                         '$edbold' => t('Bold'),
560                                                         '$editalic' => t('Italic'),
561                                                         '$eduline' => t('Underline'),
562                                                         '$edquote' => t('Quote'),
563                                                         '$edcode' => t('Code'),
564                                                         '$edimg' => t('Image'),
565                                                         '$edurl' => t('Link'),
566                                                         '$edvideo' => t('Video'),
567                                                         '$preview' => t('Preview'),
568                                                         '$ww' => (($mode === 'network') ? $commentww : '')
569                                                 ));
570                                         }
571                                 }
572
573                                 if(local_user() && link_compare($a->contact['url'],$item['author-link']))
574                                         $edpost = array($a->get_baseurl($ssl_state)."/editpost/".$item['id'], t("Edit"));
575                                 else
576                                         $edpost = false;
577
578                                 $drop = '';
579                                 $dropping = false;
580
581                                 if((intval($item['contact-id']) && $item['contact-id'] == remote_user()) || ($item['uid'] == local_user()))
582                                         $dropping = true;
583
584                                 $drop = array(
585                                         'dropping' => $dropping,
586                                         'select' => t('Select'), 
587                                         'delete' => t('Delete'),
588                                 );
589
590                                 $star = false;
591                                 $filer = false;
592
593                                 $isstarred = "unstarred";
594                                 if ($profile_owner == local_user()) {
595                                         if($toplevelpost) {
596                                                 $isstarred = (($item['starred']) ? "starred" : "unstarred");
597
598                                                 $star = array(
599                                                         'do' => t("add star"),
600                                                         'undo' => t("remove star"),
601                                                         'toggle' => t("toggle star status"),
602                                                         'classdo' => (($item['starred']) ? "hidden" : ""),
603                                                         'classundo' => (($item['starred']) ? "" : "hidden"),
604                                                         'starred' =>  t('starred'),
605                                                         'tagger' => t("add tag"),
606                                                         'classtagger' => "",
607                                                 );
608                                         }
609                                         $filer = t("save to folder");
610                                 }
611
612
613                                 $photo = $item['photo'];
614                                 $thumb = $item['thumb'];
615
616                                 // Post was remotely authored.
617
618                                 $diff_author    = ((link_compare($item['url'],$item['author-link'])) ? false : true);
619
620                                 $profile_name   = (((strlen($item['author-name']))   && $diff_author) ? $item['author-name']   : $item['name']);
621
622                                 if($item['author-link'] && (! $item['author-name']))
623                                         $profile_name = $item['author-link'];
624
625                                 $sp = false;
626                                 $profile_link = best_link_url($item,$sp);
627                                 if($profile_link === 'mailbox')
628                                         $profile_link = '';
629                                 if($sp)
630                                         $sparkle = ' sparkle';
631                                 else
632                                         $profile_link = zrl($profile_link);                                     
633
634                                 $normalised = normalise_link((strlen($item['author-link'])) ? $item['author-link'] : $item['url']);
635                                 if(($normalised != 'mailbox') && (x($a->contacts,$normalised)))
636                                         $profile_avatar = $a->contacts[$normalised]['thumb'];
637                                 else
638                                         $profile_avatar = (((strlen($item['author-avatar'])) && $diff_author) ? $item['author-avatar'] : $thumb);
639
640                                 $like    = ((x($alike,$item['id'])) ? format_like($alike[$item['id']],$alike[$item['id'] . '-l'],'like',$item['id']) : '');
641                                 $dislike = ((x($dlike,$item['id'])) ? format_like($dlike[$item['id']],$dlike[$item['id'] . '-l'],'dislike',$item['id']) : '');
642
643                                 $locate = array('location' => $item['location'], 'coord' => $item['coord'], 'html' => '');
644                                 call_hooks('render_location',$locate);
645
646                                 $location = ((strlen($locate['html'])) ? $locate['html'] : render_location_google($locate));
647
648                                 $indent = (($toplevelpost) ? '' : ' comment');
649
650                                 if(strcmp(datetime_convert('UTC','UTC',$item['created']),datetime_convert('UTC','UTC','now - 12 hours')) > 0)
651                                         $indent .= ' shiny'; 
652
653                                 // 
654                                 localize_item($item);
655
656
657                                 $tags=array();
658                                 foreach(explode(',',$item['tag']) as $tag){
659                                         $tag = trim($tag);
660                                         if ($tag!="") $tags[] = bbcode($tag);
661                                 }
662
663                                 // Build the HTML
664
665                                 $body = prepare_body($item,true);
666                                 //$tmp_item = replace_macros($template,
667                                 $tmp_item = array(
668                                         // collapse comments in template. I don't like this much...
669                                         'comment_firstcollapsed' => $comment_firstcollapsed,
670                                         'comment_lastcollapsed' => $comment_lastcollapsed,
671                                         // template to use to render item (wall, walltowall, search)
672                                         'template' => $template,
673                                         
674                                         'type' => implode("",array_slice(explode("/",$item['verb']),-1)),
675                                         'tags' => $tags,
676                                         'body' => template_escape($body),
677                                         'text' => strip_tags(template_escape($body)),
678                                         'id' => $item['item_id'],
679                                         'linktitle' => sprintf( t('View %s\'s profile @ %s'), $profile_name, ((strlen($item['author-link'])) ? $item['author-link'] : $item['url'])),
680                                         'olinktitle' => sprintf( t('View %s\'s profile @ %s'), $profile_name, ((strlen($item['owner-link'])) ? $item['owner-link'] : $item['url'])),
681                                         'to' => t('to'),
682                                         'wall' => t('Wall-to-Wall'),
683                                         'vwall' => t('via Wall-To-Wall:'),
684                                         'profile_url' => $profile_link,
685                                         'item_photo_menu' => item_photo_menu($item),
686                                         'name' => template_escape($profile_name),
687                                         'thumb' => $profile_avatar,
688                                         'osparkle' => $osparkle,
689                                         'sparkle' => $sparkle,
690                                         'title' => template_escape($item['title']),
691                                         'ago' => (($item['app']) ? sprintf( t('%s from %s'),relative_date($item['created']),$item['app']) : relative_date($item['created'])),
692                                         'lock' => $lock,
693                                         'location' => template_escape($location),
694                                         'indent' => $indent,
695                                         'owner_url' => $owner_url,
696                                         'owner_photo' => $owner_photo,
697                                         'owner_name' => template_escape($owner_name),
698                                         'plink' => get_plink($item),
699                                         'edpost' => $edpost,
700                                         'isstarred' => $isstarred,
701                                         'star' => $star,
702                                         'filer' => $filer,
703                                         'drop' => $drop,
704                                         'vote' => $likebuttons,
705                                         'like' => $like,
706                                         'dislike' => $dislike,
707                                         'comment' => $comment,
708                                         'previewing' => $previewing,
709                                         'wait' => t('Please wait'),
710
711                                 );
712
713
714                                 $arr = array('item' => $item, 'output' => $tmp_item);
715                                 call_hooks('display_item', $arr);
716
717                                 $threads[$threadsid]['items'][] = $arr['output'];
718                         }
719                 }
720         }
721
722         $page_template = get_markup_template("conversation.tpl");
723         $o = replace_macros($page_template, array(
724                 '$baseurl' => $a->get_baseurl($ssl_state),
725                 '$mode' => $mode,
726                 '$user' => $a->user,
727                 '$threads' => $threads,
728                 '$dropping' => ($dropping?t('Delete Selected Items'):False),
729         ));
730
731         return $o;
732 }}
733
734 function best_link_url($item,&$sparkle,$ssl_state = false) {
735
736         $a = get_app();
737
738         $best_url = '';
739         $sparkle  = false;
740
741         $clean_url = normalise_link($item['author-link']);
742
743         if((local_user()) && (local_user() == $item['uid'])) {
744                 if(isset($a->contacts) && x($a->contacts,$clean_url)) {
745                         if($a->contacts[$clean_url]['network'] === NETWORK_DFRN) {
746                                 $best_url = $a->get_baseurl($ssl_state) . '/redir/' . $a->contacts[$clean_url]['id'];
747                                 $sparkle = true;
748                         }
749                         else
750                                 $best_url = $a->contacts[$clean_url]['url'];
751                 }
752         }
753         if(! $best_url) {
754                 if(strlen($item['author-link']))
755                         $best_url = $item['author-link'];
756                 else
757                         $best_url = $item['url'];
758         }
759
760         return $best_url;
761 }
762
763
764 if(! function_exists('item_photo_menu')){
765 function item_photo_menu($item){
766         $a = get_app();
767
768         $ssl_state = false;
769
770         if(local_user()) {
771                 $ssl_state = true;
772                  if(! count($a->contacts))
773                         load_contact_links(local_user());
774         }
775         $contact_url="";
776         $pm_url="";
777         $status_link="";
778         $photos_link="";
779         $posts_link="";
780
781         $sparkle = false;
782     $profile_link = best_link_url($item,$sparkle,$ssl_state);
783         if($profile_link === 'mailbox')
784                 $profile_link = '';
785
786         if($sparkle) {
787                 $cid = intval(basename($profile_link));
788                 $status_link = $profile_link . "?url=status";
789                 $photos_link = $profile_link . "?url=photos";
790                 $profile_link = $profile_link . "?url=profile";
791                 $pm_url = $a->get_baseurl($ssl_state) . '/message/new/' . $cid;
792                 $zurl = '';
793         }
794         else {
795                 $profile_link = zrl($profile_link);
796                 if(local_user() && local_user() == $item['uid'] && link_compare($item['url'],$item['author-link'])) {
797                         $cid = $item['contact-id'];
798                 }               
799                 else {
800                         $cid = 0;
801                 }
802         }
803         if(($cid) && (! $item['self'])) {
804                 $contact_url = $a->get_baseurl($ssl_state) . '/contacts/' . $cid;
805                 $posts_link = $a->get_baseurl($ssl_state) . '/network/?cid=' . $cid;
806
807                 $clean_url = normalise_link($item['author-link']);
808
809                 if((local_user()) && (local_user() == $item['uid'])) {
810                         if(isset($a->contacts) && x($a->contacts,$clean_url)) {
811                                 if($a->contacts[$clean_url]['network'] === NETWORK_DIASPORA) {
812                                         $pm_url = $a->get_baseurl($ssl_state) . '/message/new/' . $cid;
813                                 }
814                         }
815                 }
816
817         }
818
819         $menu = Array(
820                 t("View Status") => $status_link,
821                 t("View Profile") => $profile_link,
822                 t("View Photos") => $photos_link,
823                 t("Network Posts") => $posts_link, 
824                 t("Edit Contact") => $contact_url,
825                 t("Send PM") => $pm_url,
826         );
827         
828         
829         $args = array('item' => $item, 'menu' => $menu);
830         
831         call_hooks('item_photo_menu', $args);
832
833         $menu = $args['menu'];  
834
835         $o = "";
836         foreach($menu as $k=>$v){
837                 if ($v!="") $o .= "<li><a href='$v'>$k</a></li>\n";
838         }
839         return $o;
840 }}
841
842 if(! function_exists('like_puller')) {
843 function like_puller($a,$item,&$arr,$mode) {
844
845         $url = '';
846         $sparkle = '';
847         $verb = (($mode === 'like') ? ACTIVITY_LIKE : ACTIVITY_DISLIKE);
848
849         if((activity_match($item['verb'],$verb)) && ($item['id'] != $item['parent'])) {
850                 $url = $item['author-link'];
851                 if((local_user()) && (local_user() == $item['uid']) && ($item['network'] === 'dfrn') && (! $item['self']) && (link_compare($item['author-link'],$item['url']))) {
852                         $url = $a->get_baseurl(true) . '/redir/' . $item['contact-id'];
853                         $sparkle = ' class="sparkle" ';
854                 }
855                 else
856                         $url = zrl($url);
857                 if(! ((isset($arr[$item['parent'] . '-l'])) && (is_array($arr[$item['parent'] . '-l']))))
858                         $arr[$item['parent'] . '-l'] = array();
859                 if(! isset($arr[$item['parent']]))
860                         $arr[$item['parent']] = 1;
861                 else    
862                         $arr[$item['parent']] ++;
863                 $arr[$item['parent'] . '-l'][] = '<a href="'. $url . '"'. $sparkle .'>' . $item['author-name'] . '</a>';
864         }
865         return;
866 }}
867
868 // Format the like/dislike text for a profile item
869 // $cnt = number of people who like/dislike the item
870 // $arr = array of pre-linked names of likers/dislikers
871 // $type = one of 'like, 'dislike'
872 // $id  = item id
873 // returns formatted text
874
875 if(! function_exists('format_like')) {
876 function format_like($cnt,$arr,$type,$id) {
877         $o = '';
878         if($cnt == 1)
879                 $o .= (($type === 'like') ? sprintf( t('%s likes this.'), $arr[0]) : sprintf( t('%s doesn\'t like this.'), $arr[0])) . EOL ;
880         else {
881                 $spanatts = 'class="fakelink" onclick="openClose(\'' . $type . 'list-' . $id . '\');"';
882                 $o .= (($type === 'like') ? 
883                                         sprintf( t('<span  %1$s>%2$d people</span> like this.'), $spanatts, $cnt)
884                                          : 
885                                         sprintf( t('<span  %1$s>%2$d people</span> don\'t like this.'), $spanatts, $cnt) ); 
886                 $o .= EOL ;
887                 $total = count($arr);
888                 if($total >= MAX_LIKERS)
889                         $arr = array_slice($arr, 0, MAX_LIKERS - 1);
890                 if($total < MAX_LIKERS)
891                         $arr[count($arr)-1] = t('and') . ' ' . $arr[count($arr)-1];
892                 $str = implode(', ', $arr);
893                 if($total >= MAX_LIKERS)
894                         $str .= sprintf( t(', and %d other people'), $total - MAX_LIKERS );
895                 $str = (($type === 'like') ? sprintf( t('%s like this.'), $str) : sprintf( t('%s don\'t like this.'), $str));
896                 $o .= "\t" . '<div id="' . $type . 'list-' . $id . '" style="display: none;" >' . $str . '</div>';
897         }
898         return $o;
899 }}
900
901
902 function status_editor($a,$x, $notes_cid = 0, $popup=false) {
903
904         $o = '';
905                 
906         $geotag = (($x['allow_location']) ? get_markup_template('jot_geotag.tpl') : '');
907
908         $plaintext = false;
909         if(local_user() && intval(get_pconfig(local_user(),'system','plaintext')))
910                 $plaintext = true;
911
912         $tpl = get_markup_template('jot-header.tpl');
913         
914         $a->page['htmlhead'] .= replace_macros($tpl, array(
915                 '$newpost' => 'true',
916                 '$baseurl' => $a->get_baseurl(true),
917                 '$editselect' => (($plaintext) ? 'none' : '/(profile-jot-text|prvmail-text)/'),
918                 '$geotag' => $geotag,
919                 '$nickname' => $x['nickname'],
920                 '$ispublic' => t('Visible to <strong>everybody</strong>'),
921                 '$linkurl' => t('Please enter a link URL:'),
922                 '$vidurl' => t("Please enter a video link/URL:"),
923                 '$audurl' => t("Please enter an audio link/URL:"),
924                 '$term' => t('Tag term:'),
925                 '$fileas' => t('Save to Folder:'),
926                 '$whereareu' => t('Where are you right now?')
927         ));
928
929
930         $tpl = get_markup_template("jot.tpl");
931                 
932         $jotplugins = '';
933         $jotnets = '';
934
935         $mail_disabled = ((function_exists('imap_open') && (! get_config('system','imap_disabled'))) ? 0 : 1);
936
937         $mail_enabled = false;
938         $pubmail_enabled = false;
939
940         if(($x['is_owner']) && (! $mail_disabled)) {
941                 $r = q("SELECT * FROM `mailacct` WHERE `uid` = %d AND `server` != '' LIMIT 1",
942                         intval(local_user())
943                 );
944                 if(count($r)) {
945                         $mail_enabled = true;
946                         if(intval($r[0]['pubmail']))
947                                 $pubmail_enabled = true;
948                 }
949         }
950
951         if($mail_enabled) {
952                 $selected = (($pubmail_enabled) ? ' checked="checked" ' : '');
953                 $jotnets .= '<div class="profile-jot-net"><input type="checkbox" name="pubmail_enable"' . $selected . ' value="1" /> ' . t("Post to Email") . '</div>';
954         }
955
956         call_hooks('jot_tool', $jotplugins);
957         call_hooks('jot_networks', $jotnets);
958
959         if($notes_cid)
960                 $jotnets .= '<input type="hidden" name="contact_allow[]" value="' . $notes_cid .'" />';
961
962         $tpl = replace_macros($tpl,array('$jotplugins' => $jotplugins));        
963
964         $o .= replace_macros($tpl,array(
965                 '$return_path' => $a->query_string,
966                 '$action' =>  $a->get_baseurl(true) . '/item',
967                 '$share' => (x($x,'button') ? $x['button'] : t('Share')),
968                 '$upload' => t('Upload photo'),
969                 '$shortupload' => t('upload photo'),
970                 '$attach' => t('Attach file'),
971                 '$shortattach' => t('attach file'),
972                 '$weblink' => t('Insert web link'),
973                 '$shortweblink' => t('web link'),
974                 '$video' => t('Insert video link'),
975                 '$shortvideo' => t('video link'),
976                 '$audio' => t('Insert audio link'),
977                 '$shortaudio' => t('audio link'),
978                 '$setloc' => t('Set your location'),
979                 '$shortsetloc' => t('set location'),
980                 '$noloc' => t('Clear browser location'),
981                 '$shortnoloc' => t('clear location'),
982                 '$title' => "",
983                 '$placeholdertitle' => t('Set title'),
984                 '$category' => "",
985                 '$placeholdercategory' => t('Categories (comma-separated list)'),
986                 '$wait' => t('Please wait'),
987                 '$permset' => t('Permission settings'),
988                 '$shortpermset' => t('permissions'),
989                 '$ptyp' => (($notes_cid) ? 'note' : 'wall'),
990                 '$content' => '',
991                 '$post_id' => '',
992                 '$baseurl' => $a->get_baseurl(true),
993                 '$defloc' => $x['default_location'],
994                 '$visitor' => $x['visitor'],
995                 '$pvisit' => (($notes_cid) ? 'none' : $x['visitor']),
996                 '$emailcc' => t('CC: email addresses'),
997                 '$public' => t('Public post'),
998                 '$jotnets' => $jotnets,
999                 '$emtitle' => t('Example: bob@example.com, mary@example.com'),
1000                 '$lockstate' => $x['lockstate'],
1001                 '$acl' => $x['acl'],
1002                 '$bang' => $x['bang'],
1003                 '$profile_uid' => $x['profile_uid'],
1004                 '$preview' => t('Preview'),
1005         ));
1006
1007
1008         if ($popup==true){
1009                 $o = '<div id="jot-popup" style="display: none;">'.$o.'</div>';
1010                 
1011         }
1012
1013         return $o;
1014 }
1015
1016
1017 function conv_sort($arr,$order) {
1018
1019         if((!(is_array($arr) && count($arr))))
1020                 return array();
1021
1022         $parents = array();
1023
1024         foreach($arr as $x)
1025                 if($x['id'] == $x['parent'])
1026                                 $parents[] = $x;
1027
1028         if(stristr($order,'created'))
1029                 usort($parents,'sort_thr_created');
1030         elseif(stristr($order,'commented'))
1031                 usort($parents,'sort_thr_commented');
1032
1033         if(count($parents))
1034                 foreach($parents as $i=>$_x) 
1035                         $parents[$i]['children'] = array();
1036
1037         foreach($arr as $x) {
1038                 if($x['id'] != $x['parent']) {
1039                         $p = find_thread_parent_index($parents,$x);
1040                         if($p !== false)
1041                                 $parents[$p]['children'][] = $x;
1042                 }
1043         }
1044         if(count($parents)) {
1045                 foreach($parents as $k => $v) {
1046                         if(count($parents[$k]['children'])) {
1047                                 $y = $parents[$k]['children'];
1048                                 usort($y,'sort_thr_created_rev');
1049                                 $parents[$k]['children'] = $y;
1050                         }
1051                 }       
1052         }
1053
1054         $ret = array();
1055         if(count($parents)) {
1056                 foreach($parents as $x) {
1057                         $ret[] = $x;
1058                         if(count($x['children']))
1059                                 foreach($x['children'] as $y)
1060                                         $ret[] = $y;
1061                 }
1062         }
1063
1064         return $ret;
1065 }
1066
1067
1068 function sort_thr_created($a,$b) {
1069         return strcmp($b['created'],$a['created']);
1070 }
1071
1072 function sort_thr_created_rev($a,$b) {
1073         return strcmp($a['created'],$b['created']);
1074 }
1075
1076 function sort_thr_commented($a,$b) {
1077         return strcmp($b['commented'],$a['commented']);
1078 }
1079
1080 function find_thread_parent_index($arr,$x) {
1081         foreach($arr as $k => $v)
1082                 if($v['id'] == $x['parent'])
1083                         return $k;
1084         return false;
1085 }
1086
1087 function render_location_google($item) {
1088         $location = (($item['location']) ? '<a target="map" title="' . $item['location'] . '" href="http://maps.google.com/?q=' . urlencode($item['location']) . '">' . $item['location'] . '</a>' : '');
1089         $coord = (($item['coord']) ? '<a target="map" title="' . $item['coord'] . '" href="http://maps.google.com/?q=' . urlencode($item['coord']) . '">' . $item['coord'] . '</a>' : '');
1090         if($coord) {
1091                 if($location)
1092                         $location .= '<br /><span class="smalltext">(' . $coord . ')</span>';
1093                 else
1094                         $location = '<span class="smalltext">' . $coord . '</span>';
1095         }
1096         return $location;
1097 }