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