]> git.mxchange.org Git - friendica.git/blob - include/conversation.php
Merge remote branch '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
10
11         // find private image (w/data url) if present and convert image 
12         // link to a magic-auth redirect.
13
14         $saved_image = '';
15         $img_start = strpos($Text,'[img]data:');
16         $img_end = strpos($Text,'[/img]');
17
18         if($img_start !== false && $img_end !== false && $img_end > $img_start) {
19                 $start_fragment = substr($Text,0,$img_start);
20                 $img_start += strlen('[img]');
21                 $saved_image = substr($Text,$img_start,$img_end - $img_start);
22                 $end_fragment = substr($Text,$img_end + strlen('[/img]'));              
23                 $Text = $start_fragment . '[!#saved_image#!]' . $end_fragment;
24                 $search = '/\[url\=(.*?)\]\[!#saved_image#!\]\[\/url\]' . '/is';
25                 $replace = '[url=' . z_path() . '/redir/' . $item['contact-id'] 
26                         . '?f=1&url=' . '$1' . '][!#saved_image#!][/url]' ;
27
28                 $Text = preg_replace($search,$replace,$Text);
29
30                 if(strlen($saved_image))
31                         $item['body'] = str_replace('[!#saved_image#!]', '[img]' . $saved_image . '[/img]',$Text);
32         }
33
34         $xmlhead="<"."?xml version='1.0' encoding='UTF-8' ?".">";
35         if ($item['verb']=== ACTIVITY_LIKE || $item['verb']=== ACTIVITY_DISLIKE){
36
37                 $r = q("SELECT * from `item`,`contact` WHERE 
38                                 `item`.`contact-id`=`contact`.`id` AND `item`.`uri`='%s';",
39                                  dbesc($item['parent-uri']));
40                 if(count($r)==0) return;
41                 $obj=$r[0];
42                 
43                 $author  = '[url=' . $item['author-link'] . ']' . $item['author-name'] . '[/url]';
44                 $objauthor =  '[url=' . $obj['author-link'] . ']' . $obj['author-name'] . '[/url]';
45                 
46                 switch($obj['verb']){
47                         case ACTIVITY_POST:
48                                 switch ($obj['object-type']){
49                                         case ACTIVITY_OBJ_EVENT:
50                                                 $post_type = t('event');
51                                                 break;
52                                         default:
53                                                 $post_type = t('status');
54                                 }
55                                 break;
56                         default:
57                                 if($obj['resource-id']){
58                                         $post_type = t('photo');
59                                         $m=array();     preg_match("/\[url=([^]]*)\]/", $obj['body'], $m);
60                                         $rr['plink'] = $m[1];
61                                 } else {
62                                         $post_type = t('status');
63                                 }
64                 }
65         
66                 $plink = '[url=' . $obj['plink'] . ']' . $post_type . '[/url]';
67                 
68                 switch($item['verb']){
69                         case ACTIVITY_LIKE :
70                                 $bodyverb = t('%1$s likes %2$s\'s %3$s');
71                                 break;
72                         case ACTIVITY_DISLIKE:
73                                 $bodyverb = t('%1$s doesn\'t like %2$s\'s %3$s');
74                                 break;
75                 }
76                 $item['body'] = sprintf($bodyverb, $author, $objauthor, $plink);
77                         
78         }
79         if ($item['verb']=== ACTIVITY_FRIEND){
80
81                 if ($item['object-type']=="" || $item['object-type']!== ACTIVITY_OBJ_PERSON) return;
82
83                 $Aname = $item['author-name'];
84                 $Alink = $item['author-link'];
85                 
86                 $xmlhead="<"."?xml version='1.0' encoding='UTF-8' ?".">";
87                 
88                 $obj = parse_xml_string($xmlhead.$item['object']);
89                 $links = parse_xml_string($xmlhead."<links>".unxmlify($obj->link)."</links>");
90                 
91                 $Bname = $obj->title;
92                 $Blink = ""; $Bphoto = "";
93                 foreach ($links->link as $l){
94                         $atts = $l->attributes();
95                         switch($atts['rel']){
96                                 case "alternate": $Blink = $atts['href'];
97                                 case "photo": $Bphoto = $atts['href'];
98                         }
99                         
100                 }
101                 
102                 $A = '[url=' . $Alink . ']' . $Aname . '[/url]';
103                 $B = '[url=' . $Blink . ']' . $Bname . '[/url]';
104                 if ($Bphoto!="") $Bphoto = '[url=' . $Blink . '][img]' . $Bphoto . '[/img][/url]';
105
106                 $item['body'] = sprintf( t('%1$s is now friends with %2$s'), $A, $B)."\n\n\n".$Bphoto;
107
108         }
109     if ($item['verb']===ACTIVITY_TAG){
110                 $r = q("SELECT * from `item`,`contact` WHERE 
111                 `item`.`contact-id`=`contact`.`id` AND `item`.`uri`='%s';",
112                  dbesc($item['parent-uri']));
113                 if(count($r)==0) return;
114                 $obj=$r[0];
115                 
116                 $author  = '[url=' . $item['author-link'] . ']' . $item['author-name'] . '[/url]';
117                 $objauthor =  '[url=' . $obj['author-link'] . ']' . $obj['author-name'] . '[/url]';
118                 
119                 switch($obj['verb']){
120                         case ACTIVITY_POST:
121                                 switch ($obj['object-type']){
122                                         case ACTIVITY_OBJ_EVENT:
123                                                 $post_type = t('event');
124                                                 break;
125                                         default:
126                                                 $post_type = t('status');
127                                 }
128                                 break;
129                         default:
130                                 if($obj['resource-id']){
131                                         $post_type = t('photo');
132                                         $m=array();     preg_match("/\[url=([^]]*)\]/", $obj['body'], $m);
133                                         $rr['plink'] = $m[1];
134                                 } else {
135                                         $post_type = t('status');
136                                 }
137                 }
138                 $plink = '[url=' . $obj['plink'] . ']' . $post_type . '[/url]';
139                 
140                 $parsedobj = parse_xml_string($xmlhead.$item['object']);
141                 
142                 $tag = sprintf('#[url=%s]%s[/url]', $parsedobj->id, $parsedobj->content);
143                 $item['body'] = sprintf( t('%1$s tagged %2$s\'s %3$s with %4$s'), $author, $objauthor, $plink, $tag );
144                 
145         }
146         if ($item['verb']=== ACTIVITY_FAVORITE){
147
148                 if ($item['object-type']== "")
149                         return;
150
151                 $Aname = $item['author-name'];
152                 $Alink = $item['author-link'];
153                 
154                 $xmlhead="<"."?xml version='1.0' encoding='UTF-8' ?".">";
155                 
156                 $obj = parse_xml_string($xmlhead.$item['object']);
157                 if(strlen($obj->id)) {
158                         $r = q("select * from item where uri = '%s' and uid = %d limit 1",
159                                         dbesc($obj->id),
160                                         intval($item['uid'])
161                         );
162                         if(count($r) && $r[0]['plink']) {
163                                 $target = $r[0];
164                                 $Bname = $target['author-name'];
165                                 $Blink = $target['author-link'];
166                                 $A = '[url=' . $Alink . ']' . $Aname . '[/url]';
167                                 $B = '[url=' . $Blink . ']' . $Bname . '[/url]';
168                                 $P = '[url=' . $target['plink'] . ']' . t('post/item') . '[/url]';
169                                 $item['body'] = sprintf( t('%1$s marked %2$s\'s %3$s as favorite'), $A, $B, $P)."\n";
170
171                         }
172                 }
173         }
174
175 }
176
177 /**
178  * "Render" a conversation or list of items for HTML display.
179  * There are two major forms of display:
180  *      - Sequential or unthreaded ("New Item View" or search results)
181  *      - conversation view
182  * The $mode parameter decides between the various renderings and also
183  * figures out how to determine page owner and other contextual items 
184  * that are based on unique features of the calling module.
185  *
186  */
187 function conversation(&$a, $items, $mode, $update, $preview = false) {
188
189         require_once('bbcode.php');
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->cmd;
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         = get_markup_template('wall_item.tpl');
235         $wallwall    = get_markup_template('wallwall_item.tpl');
236         $hide_comments_tpl = get_markup_template('hide_comments.tpl');
237
238         $alike = array();
239         $dlike = array();
240         $o = "";
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
255                         foreach($items as $item) {
256                                 $threadsid++;
257
258                                 $comment     = '';
259                                 $owner_url   = '';
260                                 $owner_photo = '';
261                                 $owner_name  = '';
262                                 $sparkle     = '';
263
264                                 if($mode === 'search' || $mode === 'community') {
265                                         if(((activity_match($item['verb'],ACTIVITY_LIKE)) || (activity_match($item['verb'],ACTIVITY_DISLIKE))) 
266                                                 && ($item['id'] != $item['parent']))
267                                                 continue;
268                                         $nickname = $item['nickname'];
269                                 }
270                                 else
271                                         $nickname = $a->user['nickname'];
272                                 
273                         
274                                 $profile_name   = ((strlen($item['author-name']))   ? $item['author-name']   : $item['name']);
275                                 if($item['author-link'] && (! $item['author-name']))
276                                         $profile_name = $item['author-link'];
277
278                                 $sp = false;
279                                 $profile_link = best_link_url($item,$sp);
280                                 if($sp)
281                                         $sparkle = ' sparkle';
282                                 if($profile_link === 'mailbox')
283                                         $profile_link = '';
284
285
286                                 $normalised = normalise_link((strlen($item['author-link'])) ? $item['author-link'] : $item['url']);
287                                 if(($normalised != 'mailbox') && (x($a->contacts[$normalised])))
288                                         $profile_avatar = $a->contacts[$normalised]['thumb'];
289                                 else
290                                         $profile_avatar = ((strlen($item['author-avatar'])) ? $item['author-avatar'] : $item['thumb']);
291
292                                 $locate = array('location' => $item['location'], 'coord' => $item['coord'], 'html' => '');
293                                 call_hooks('render_location',$locate);
294
295                                 $location = ((strlen($locate['html'])) ? $locate['html'] : render_location_google($locate));
296
297                                 localize_item($item);
298                                 if($mode === 'network-new')
299                                         $dropping = true;
300                                 else
301                                         $dropping = false;
302
303
304                                 $drop = array(
305                                         'dropping' => $dropping,
306                                         'select' => t('Select'), 
307                                         'delete' => t('Delete'),
308                                 );
309
310                                 $star = false;
311                                 $isstarred = "unstarred";
312                                 
313                                 $lock = false;
314                                 $likebuttons = false;
315                                 $shareable = false;
316
317                                 $body = prepare_body($item,true);
318                                 
319                                 $tmp_item = replace_macros($tpl,array(
320                                         '$id' => (($preview) ? 'P0' : $item['item_id']),
321                                         '$linktitle' => sprintf( t('View %s\'s profile @ %s'), $profile_name, ((strlen($item['author-link'])) ? $item['author-link'] : $item['url'])),
322                                         '$profile_url' => $profile_link,
323                                         '$item_photo_menu' => item_photo_menu($item),
324                                         '$name' => template_escape($profile_name),
325                                         '$sparkle' => $sparkle,
326                                         '$lock' => $lock,
327                                         '$thumb' => $profile_avatar,
328                                         '$title' => template_escape($item['title']),
329                                         '$body' => template_escape($body),
330                                         '$ago' => (($item['app']) ? sprintf( t('%s from %s'),relative_date($item['created']),$item['app']) : relative_date($item['created'])),
331                                         '$lock' => $lock,
332                                         '$location' => template_escape($location),
333                                         '$indent' => '',
334                                         '$owner_name' => template_escape($owner_name),
335                                         '$owner_url' => $owner_url,
336                                         '$owner_photo' => $owner_photo,
337                                         '$plink' => get_plink($item),
338                                         '$edpost' => false,
339                                         '$isstarred' => $isstarred,
340                                         '$star' => $star,
341                                         '$drop' => $drop,
342                                         '$vote' => $likebuttons,
343                                         '$like' => '',
344                                         '$dislike' => '',
345                                         '$comment' => '',
346                                         '$conv' => (($preview) ? '' : array('href'=> $a->get_baseurl() . '/display/' . $nickname . '/' . $item['id'], 'title'=> t('View in context'))),
347                                         '$previewing' => $previewing,
348                                         '$wait' => t('Please wait'),
349                                 ));
350
351                                 $arr = array('item' => $item, 'output' => $tmp_item);
352                                 call_hooks('display_item', $arr);
353
354                                 $threads[$threadsid]['id'] = $item['item_id'];
355                                 $threads[$threadsid]['html'] .= $arr['output'];
356
357                         }
358
359                 }
360                 else
361                 {
362                         // Normal View
363
364
365                         // Figure out how many comments each parent has
366                         // (Comments all have gravity of 6)
367                         // Store the result in the $comments array
368
369                         $comments = array();
370                         foreach($items as $item) {
371                                 if((intval($item['gravity']) == 6) && ($item['id'] != $item['parent'])) {
372                                         if(! x($comments,$item['parent']))
373                                                 $comments[$item['parent']] = 1;
374                                         else
375                                                 $comments[$item['parent']] += 1;
376                                 }
377                         }
378
379                         // map all the like/dislike activities for each parent item 
380                         // Store these in the $alike and $dlike arrays
381
382                         foreach($items as $item) {
383                                 like_puller($a,$item,$alike,'like');
384                                 like_puller($a,$item,$dlike,'dislike');
385                         }
386
387                         $comments_collapsed = false;
388                         $blowhard = 0;
389                         $blowhard_count = 0;
390
391
392                         foreach($items as $item) {
393
394                                 $comment = '';
395                                 $template = $tpl;
396                                 $commentww = '';
397                                 $sparkle = '';
398                                 $owner_url = $owner_photo = $owner_name = '';
399
400                                 // We've already parsed out like/dislike for special treatment. We can ignore them now
401
402                                 if(((activity_match($item['verb'],ACTIVITY_LIKE)) 
403                                         || (activity_match($item['verb'],ACTIVITY_DISLIKE))) 
404                                         && ($item['id'] != $item['parent']))
405                                         continue;
406
407                                 $toplevelpost = (($item['id'] == $item['parent']) ? true : false);
408                                 $toplevelprivate = false;
409
410                                 // Take care of author collapsing and comment collapsing
411                                 // (author collapsing is currently disabled)
412                                 // If a single author has more than 3 consecutive top-level posts, squash the remaining ones.
413                                 // If there are more than two comments, squash all but the last 2.
414                         
415                                 if($toplevelpost) {
416                                         $toplevelprivate = (($toplevelpost && $item['private']) ? true : false);
417                                         $item_writeable = (($item['writable'] || $item['self']) ? true : false);
418
419                                         // DISABLED
420                                         /*
421                                         if($blowhard == $item['cid'] && (! $item['self']) && ($mode != 'profile') && ($mode != 'notes')) {
422                                                 $blowhard_count ++;
423                                                 if($blowhard_count == 3) {
424                                                         $o .= '<div class="icollapse-wrapper fakelink" id="icollapse-wrapper-' . $item['parent'] 
425                                                                 . '" onclick="openClose(' . '\'icollapse-' . $item['parent'] . '\'); $(\'#icollapse-wrapper-' . $item['parent'] . '\').hide();" >' 
426                                                                 . t('See more posts like this') . '</div>' . '<div class="icollapse" id="icollapse-' 
427                                                                 . $item['parent'] . '" style="display: none;" >';
428                                                 }
429                                         }
430                                         else {
431                                                 $blowhard = $item['cid'];                                       
432                                                 if($blowhard_count >= 3)
433                                                         $o .= '</div>';
434                                                 $blowhard_count = 0;
435                                         }
436                                         // END DISABLED
437                                         */
438
439                                         $comments_seen = 0;
440                                         $comments_collapsed = false;
441                                         
442                                         $threadsid++;
443                                         $threads[$threadsid]['id'] = $item['item_id'];
444                                         $threads[$threadsid]['html'] = "";
445
446                                 }
447                                 else {
448                                         // prevent private email from leaking into public conversation
449                                         if((! $toplevelpost) && (! $toplevelprivate) && ($item['private']) && ($profile_owner != local_user()))
450                                                 continue;
451                                         $comments_seen ++;
452                                 }       
453
454                                 $override_comment_box = ((($page_writeable) && ($item_writeable)) ? true : false);
455                                 $show_comment_box = ((($page_writeable) && ($item_writeable) && ($comments_seen == $comments[$item['parent']])) ? true : false);
456
457
458                                 if(($comments[$item['parent']] > 2) && ($comments_seen <= ($comments[$item['parent']] - 2)) && ($item['gravity'] == 6)) {
459                                         if(! $comments_collapsed) {
460
461                                                 // IMPORTANT: the closing </div> in the hide_comments template
462                                                 // is supplied below in code. 
463
464                                                 $threads[$threadsid]['html'] .= replace_macros($hide_comments_tpl,array(
465                                                         '$id' => $item['parent'],
466                                                         '$num_comments' => sprintf( tt('%d comment','%d comments',$comments[$item['parent']]),
467                                                                 $comments[$item['parent']]),
468                                                         '$display' => 'none',
469                                                         '$hide_text' => t('show more')
470                                                 ));
471                                                 $comments_collapsed = true;
472                                         }
473                                 }
474                                 if(($comments[$item['parent']] > 2) && ($comments_seen == ($comments[$item['parent']] - 1))) {
475                                         $threads[$threadsid]['html'] .= '</div>';
476                                 }
477
478                                 $redirect_url = $a->get_baseurl() . '/redir/' . $item['cid'] ;
479
480                                 $lock = ((($item['private']) || (($item['uid'] == local_user()) && (strlen($item['allow_cid']) || strlen($item['allow_gid']) 
481                                         || strlen($item['deny_cid']) || strlen($item['deny_gid']))))
482                                         ? t('Private Message')
483                                         : false);
484
485
486                                 // Top-level wall post not written by the wall owner (wall-to-wall)
487                                 // First figure out who owns it. 
488
489                                 $osparkle = '';
490
491                                 if(($toplevelpost) && (! $item['self']) && ($mode !== 'profile')) {
492
493                                         if($item['wall']) {
494
495                                                 // On the network page, I am the owner. On the display page it will be the profile owner.
496                                                 // This will have been stored in $a->page_contact by our calling page.
497                                                 // Put this person on the left of the wall-to-wall notice.
498
499                                                 $owner_url = $a->page_contact['url'];
500                                                 $owner_photo = $a->page_contact['thumb'];
501                                                 $owner_name = $a->page_contact['name'];
502                                                 $template = $wallwall;
503                                                 $commentww = 'ww';      
504                                         }
505                                         if((! $item['wall']) && (strlen($item['owner-link'])) && ($item['owner-link'] != $item['author-link'])) {
506
507                                                 // Could be anybody. 
508
509                                                 $owner_url = $item['owner-link'];
510                                                 $owner_photo = $item['owner-avatar'];
511                                                 $owner_name = $item['owner-name'];
512                                                 $template = $wallwall;
513                                                 $commentww = 'ww';
514                                                 // If it is our contact, use a friendly redirect link
515                                                 if((link_compare($item['owner-link'],$item['url'])) 
516                                                         && ($item['network'] === 'dfrn')) {
517                                                         $owner_url = $redirect_url;
518                                                         $osparkle = ' sparkle';
519                                                 }
520                                         }
521                                 }
522
523                                 $likebuttons = '';
524                                 $shareable = ((($profile_owner == local_user()) &&  (! $item['private'])) ? true : false); //($mode != 'display') &&
525
526                                 if($page_writeable) {
527                                         if($toplevelpost) {
528                                                 $likebuttons = array(
529                                                         'like' => array( t("I like this \x28toggle\x29"), t("like")),
530                                                         'dislike' => array( t("I don't like this \x28toggle\x29"), t("dislike")),
531                                                 );
532                                                 if ($shareable) $likebuttons['share'] = array( t('Share this'), t('share'));
533                                         }
534
535
536                                         $qc = ((local_user()) ? get_pconfig(local_user(),'qcomment','words') : null);
537                                         $qcomment = (($qc) ? explode("\n",$qc) : null);
538
539                                         if(($show_comment_box) || (($show_comment_box == false) && ($override_comment_box == false) && ($item['last-child']))) {
540                                                 $comment = replace_macros($cmnt_tpl,array(
541                                                         '$return_path' => '', 
542                                                         '$jsreload' => (($mode === 'display') ? $_SESSION['return_url'] : ''),
543                                                         '$type' => (($mode === 'profile') ? 'wall-comment' : 'net-comment'),
544                                                         '$id' => $item['item_id'],
545                                                         '$parent' => $item['parent'],
546                                                         '$qcomment' => $qcomment,
547                                                         '$profile_uid' =>  $profile_owner,
548                                                         '$mylink' => $a->contact['url'],
549                                                         '$mytitle' => t('This is you'),
550                                                         '$myphoto' => $a->contact['thumb'],
551                                                         '$comment' => t('Comment'),
552                                                         '$submit' => t('Submit'),
553                                                         '$preview' => t('Preview'),
554                                                         '$ww' => (($mode === 'network') ? $commentww : '')
555                                                 ));
556                                         }
557                                 }
558
559                                 $edpost = (((($profile_owner == local_user()) && ($toplevelpost) && (intval($item['wall']) == 1)) || ($mode === 'notes'))
560                                                 ? array($a->get_baseurl()."/editpost/".$item['id'], t("Edit"))
561                                                 : False);
562
563
564                                 $drop = '';
565                                 $dropping = false;
566
567                                 if((intval($item['contact-id']) && $item['contact-id'] == remote_user()) || ($item['uid'] == local_user()))
568                                         $dropping = true;
569
570                                 $drop = array(
571                                         'dropping' => $dropping,
572                                         'select' => t('Select'), 
573                                         'delete' => t('Delete'),
574                                 );
575
576                                 $star = false;
577                                 $isstarred = "unstarred";
578                                 if ($profile_owner == local_user() && $toplevelpost) {
579                                         $isstarred = (($item['starred']) ? "starred" : "unstarred");
580
581                                         $star = array(
582                                                 'do' => t("add star"),
583                                                 'undo' => t("remove star"),
584                                                 'toggle' => t("toggle star status"),
585                                                 'classdo' => (($item['starred']) ? "hidden" : ""),
586                                                 'classundo' => (($item['starred']) ? "" : "hidden"),
587                                                 'starred' =>  t('starred'),
588                                                 'tagger' => t("add tag"),
589                                                 'classtagger' => "",
590                                         );
591                                 }
592
593
594
595                                 $photo = $item['photo'];
596                                 $thumb = $item['thumb'];
597
598                                 // Post was remotely authored.
599
600                                 $diff_author    = ((link_compare($item['url'],$item['author-link'])) ? false : true);
601
602                                 $profile_name   = (((strlen($item['author-name']))   && $diff_author) ? $item['author-name']   : $item['name']);
603
604                                 if($item['author-link'] && (! $item['author-name']))
605                                         $profile_name = $item['author-link'];
606
607
608                                 $sp = false;
609                                 $profile_link = best_link_url($item,$sp);
610                                 if($sp)
611                                         $sparkle = ' sparkle';
612
613                                 if($profile_link === 'mailbox')
614                                         $profile_link = '';
615
616                                 $normalised = normalise_link((strlen($item['author-link'])) ? $item['author-link'] : $item['url']);
617                                 if(($normalised != 'mailbox') && (x($a->contacts,$normalised)))
618                                         $profile_avatar = $a->contacts[$normalised]['thumb'];
619                                 else
620                                         $profile_avatar = (((strlen($item['author-avatar'])) && $diff_author) ? $item['author-avatar'] : $thumb);
621
622
623
624
625
626                                 $like    = ((x($alike,$item['id'])) ? format_like($alike[$item['id']],$alike[$item['id'] . '-l'],'like',$item['id']) : '');
627                                 $dislike = ((x($dlike,$item['id'])) ? format_like($dlike[$item['id']],$dlike[$item['id'] . '-l'],'dislike',$item['id']) : '');
628
629                                 $locate = array('location' => $item['location'], 'coord' => $item['coord'], 'html' => '');
630                                 call_hooks('render_location',$locate);
631
632                                 $location = ((strlen($locate['html'])) ? $locate['html'] : render_location_google($locate));
633
634                                 $indent = (($toplevelpost) ? '' : ' comment');
635
636                                 if(strcmp(datetime_convert('UTC','UTC',$item['created']),datetime_convert('UTC','UTC','now - 12 hours')) > 0)
637                                         $indent .= ' shiny'; 
638
639                                 // 
640                                 localize_item($item);
641
642
643                                 $tags=array();
644                                 foreach(explode(',',$item['tag']) as $tag){
645                                         $tag = trim($tag);
646                                         if ($tag!="") $tags[] = bbcode($tag);
647                                 }
648
649                                 // Build the HTML
650
651                                 $body = prepare_body($item,true);
652
653                                 $tmp_item = replace_macros($template,array(
654                                         '$type' => implode("",array_slice(explode("/",$item['verb']),-1)),
655                                         '$tags' => $tags,
656                                         '$body' => template_escape($body),
657                                         '$id' => $item['item_id'],
658                                         '$linktitle' => sprintf( t('View %s\'s profile @ %s'), $profile_name, ((strlen($item['author-link'])) ? $item['author-link'] : $item['url'])),
659                                         '$olinktitle' => sprintf( t('View %s\'s profile @ %s'), $profile_name, ((strlen($item['owner-link'])) ? $item['owner-link'] : $item['url'])),
660                                         '$to' => t('to'),
661                                         '$wall' => t('Wall-to-Wall'),
662                                         '$vwall' => t('via Wall-To-Wall:'),
663                                         '$profile_url' => $profile_link,
664                                         '$item_photo_menu' => item_photo_menu($item),
665                                         '$name' => template_escape($profile_name),
666                                         '$thumb' => $profile_avatar,
667                                         '$osparkle' => $osparkle,
668                                         '$sparkle' => $sparkle,
669                                         '$title' => template_escape($item['title']),
670                                         '$ago' => (($item['app']) ? sprintf( t('%s from %s'),relative_date($item['created']),$item['app']) : relative_date($item['created'])),
671                                         '$lock' => $lock,
672                                         '$location' => template_escape($location),
673                                         '$indent' => $indent,
674                                         '$owner_url' => $owner_url,
675                                         '$owner_photo' => $owner_photo,
676                                         '$owner_name' => template_escape($owner_name),
677                                         '$plink' => get_plink($item),
678                                         '$edpost' => $edpost,
679                                         '$isstarred' => $isstarred,
680                                         '$star' => $star,
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]['html'] .= $arr['output'];
696                         }
697                 }
698         }
699
700
701         $page_template = get_markup_template("conversation.tpl");
702         $o .= replace_macros($page_template, array(
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                 '$whereareu' => t('Where are you right now?'),
883                 '$title' => t('Enter a title for this item') 
884         ));
885
886
887         $tpl = get_markup_template("jot.tpl");
888                 
889         $jotplugins = '';
890         $jotnets = '';
891
892         $mail_disabled = ((function_exists('imap_open') && (! get_config('system','imap_disabled'))) ? 0 : 1);
893
894         $mail_enabled = false;
895         $pubmail_enabled = false;
896
897         if(($x['is_owner']) && (! $mail_disabled)) {
898                 $r = q("SELECT * FROM `mailacct` WHERE `uid` = %d AND `server` != '' LIMIT 1",
899                         intval(local_user())
900                 );
901                 if(count($r)) {
902                         $mail_enabled = true;
903                         if(intval($r[0]['pubmail']))
904                                 $pubmail_enabled = true;
905                 }
906         }
907
908         if($mail_enabled) {
909                 $selected = (($pubmail_enabled) ? ' checked="checked" ' : '');
910                 $jotnets .= '<div class="profile-jot-net"><input type="checkbox" name="pubmail_enable"' . $selected . ' value="1" /> ' . t("Post to Email") . '</div>';
911         }
912
913         call_hooks('jot_tool', $jotplugins);
914         call_hooks('jot_networks', $jotnets);
915
916         if($notes_cid)
917                 $jotnets .= '<input type="hidden" name="contact_allow[]" value="' . $notes_cid .'" />';
918
919         $tpl = replace_macros($tpl,array('$jotplugins' => $jotplugins));        
920
921         $o .= replace_macros($tpl,array(
922                 '$return_path' => $a->cmd,
923                 '$action' =>  $a->get_baseurl().'/item',
924                 '$share' => (($x['button']) ? $x['button'] : t('Share')),
925                 '$upload' => t('Upload photo'),
926                 '$shortupload' => t('upload photo'),
927                 '$attach' => t('Attach file'),
928                 '$shortattach' => t('attach file'),
929                 '$weblink' => t('Insert web link'),
930                 '$shortweblink' => t('web link'),
931                 '$video' => t('Insert video link'),
932                 '$shortvideo' => t('video link'),
933                 '$audio' => t('Insert audio link'),
934                 '$shortaudio' => t('audio link'),
935                 '$setloc' => t('Set your location'),
936                 '$shortsetloc' => t('set location'),
937                 '$noloc' => t('Clear browser location'),
938                 '$shortnoloc' => t('clear location'),
939                 '$title' => "",
940                 '$placeholdertitle' => t('Set title'),
941                 '$wait' => t('Please wait'),
942                 '$permset' => t('Permission settings'),
943                 '$shortpermset' => t('permissions'),
944                 '$ptyp' => (($notes_cid) ? 'note' : 'wall'),
945                 '$content' => '',
946                 '$post_id' => '',
947                 '$baseurl' => $a->get_baseurl(),
948                 '$defloc' => $x['default_location'],
949                 '$visitor' => $x['visitor'],
950                 '$pvisit' => (($notes_cid) ? 'none' : $x['visitor']),
951                 '$emailcc' => t('CC: email addresses'),
952                 '$public' => t('Public post'),
953                 '$jotnets' => $jotnets,
954                 '$emtitle' => t('Example: bob@example.com, mary@example.com'),
955                 '$lockstate' => $x['lockstate'],
956                 '$acl' => $x['acl'],
957                 '$bang' => $x['bang'],
958                 '$profile_uid' => $x['profile_uid'],
959                 '$preview' => t('Preview'),
960         ));
961
962
963         if ($popup==true){
964                 $o = '<div id="jot-popup" style="display: none;">'.$o.'</div>';
965                 
966         }
967
968         return $o;
969 }
970
971
972 function conv_sort($arr,$order) {
973
974         if((!(is_array($arr) && count($arr))))
975                 return array();
976
977         $parents = array();
978
979         foreach($arr as $x)
980                 if($x['id'] == $x['parent'])
981                                 $parents[] = $x;
982
983         if(stristr($order,'created'))
984                 usort($parents,'sort_thr_created');
985         elseif(stristr($order,'commented'))
986                 usort($parents,'sort_thr_commented');
987
988         if(count($parents))
989                 foreach($parents as $i=>$_x) 
990                         $parents[$i]['children'] = array();
991
992         foreach($arr as $x) {
993                 if($x['id'] != $x['parent']) {
994                         $p = find_thread_parent_index($parents,$x);
995                         if($p !== false)
996                                 $parents[$p]['children'][] = $x;
997                 }
998         }
999         if(count($parents)) {
1000                 foreach($parents as $k => $v) {
1001                         if(count($parents[$k]['children'])) {
1002                                 $y = $parents[$k]['children'];
1003                                 usort($y,'sort_thr_created_rev');
1004                                 $parents[$k]['children'] = $y;
1005                         }
1006                 }       
1007         }
1008
1009         $ret = array();
1010         if(count($parents)) {
1011                 foreach($parents as $x) {
1012                         $ret[] = $x;
1013                         if(count($x['children']))
1014                                 foreach($x['children'] as $y)
1015                                         $ret[] = $y;
1016                 }
1017         }
1018
1019         return $ret;
1020 }
1021
1022
1023 function sort_thr_created($a,$b) {
1024         return strcmp($b['created'],$a['created']);
1025 }
1026
1027 function sort_thr_created_rev($a,$b) {
1028         return strcmp($a['created'],$b['created']);
1029 }
1030
1031 function sort_thr_commented($a,$b) {
1032         return strcmp($b['commented'],$a['commented']);
1033 }
1034
1035 function find_thread_parent_index($arr,$x) {
1036         foreach($arr as $k => $v)
1037                 if($v['id'] == $x['parent'])
1038                         return $k;
1039         return false;
1040 }
1041
1042 function render_location_google($item) {
1043         $location = '';
1044         $location = (($item['location']) ? '<a target="map" title="' . $item['location'] . '" href="http://maps.google.com/?q=' . urlencode($item['location']) . '">' . $item['location'] . '</a>' : '');
1045         $coord = (($item['coord']) ? '<a target="map" title="' . $item['coord'] . '" href="http://maps.google.com/?q=' . urlencode($item['coord']) . '">' . $item['coord'] . '</a>' : '');
1046         if($coord) {
1047                 if($location)
1048                         $location .= '<br /><span class="smalltext">(' . $coord . ')</span>';
1049                 else
1050                         $location = '<span class="smalltext">' . $coord . '</span>';
1051         }
1052         return $location;
1053 }
1054