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