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