]> git.mxchange.org Git - friendica.git/blob - include/conversation.php
some testbubble stuff
[friendica.git] / include / conversation.php
1 <?php
2
3 /**
4  * Render actions localized
5  */
6 function localize_item(&$item){
7
8         $Text = $item['body'];
9         $saved_image = '';
10         $img_start = strpos($Text,'[img]data:');
11         $img_end = strpos($Text,'[/img]');
12
13         if($img_start !== false && $img_end !== false && $img_end > $img_start) {
14                 $start_fragment = substr($Text,0,$img_start);
15                 $img_start += strlen('[img]');
16                 $saved_image = substr($Text,$img_start,$img_end - $img_start);
17                 $end_fragment = substr($Text,$img_end + strlen('[/img]'));              
18                 $Text = $start_fragment . '[!#saved_image#!]' . $end_fragment;
19                 $search = '/\[url\=(.*?)\]\[!#saved_image#!\]\[\/url\]' . '/is';
20                 $replace = '[url=' . z_path() . '/redir/' . $item['contact-id'] 
21                         . '?f=1&url=' . '$1' . '][!#saved_image#!][/url]' ;
22
23                 $Text = preg_replace($search,$replace,$Text);
24
25                 if(strlen($saved_image))
26                         $item['body'] = str_replace('[!#saved_image#!]', '[img]' . $saved_image . '[/img]',$Text);
27         }
28
29         $xmlhead="<"."?xml version='1.0' encoding='UTF-8' ?".">";
30         if ($item['verb']=== ACTIVITY_LIKE || $item['verb']=== ACTIVITY_DISLIKE){
31
32                 $r = q("SELECT * from `item`,`contact` WHERE 
33                                 `item`.`contact-id`=`contact`.`id` AND `item`.`uri`='%s';",
34                                  dbesc($item['parent-uri']));
35                 if(count($r)==0) return;
36                 $obj=$r[0];
37                 
38                 $author  = '[url=' . $item['author-link'] . ']' . $item['author-name'] . '[/url]';
39                 $objauthor =  '[url=' . $obj['author-link'] . ']' . $obj['author-name'] . '[/url]';
40                 
41                 switch($obj['verb']){
42                         case ACTIVITY_POST:
43                                 switch ($obj['object-type']){
44                                         case ACTIVITY_OBJ_EVENT:
45                                                 $post_type = t('event');
46                                                 break;
47                                         default:
48                                                 $post_type = t('status');
49                                 }
50                                 break;
51                         default:
52                                 if($obj['resource-id']){
53                                         $post_type = t('photo');
54                                         $m=array();     preg_match("/\[url=([^]]*)\]/", $obj['body'], $m);
55                                         $rr['plink'] = $m[1];
56                                 } else {
57                                         $post_type = t('status');
58                                 }
59                 }
60         
61                 $plink = '[url=' . $obj['plink'] . ']' . $post_type . '[/url]';
62                 
63                 switch($item['verb']){
64                         case ACTIVITY_LIKE :
65                                 $bodyverb = t('%1$s likes %2$s\'s %3$s');
66                                 break;
67                         case ACTIVITY_DISLIKE:
68                                 $bodyverb = t('%1$s doesn\'t like %2$s\'s %3$s');
69                                 break;
70                 }
71                 $item['body'] = sprintf($bodyverb, $author, $objauthor, $plink);
72                         
73         }
74         if ($item['verb']=== ACTIVITY_FRIEND){
75
76                 if ($item['object-type']=="" || $item['object-type']!== ACTIVITY_OBJ_PERSON) return;
77
78                 $Aname = $item['author-name'];
79                 $Alink = $item['author-link'];
80                 
81                 $xmlhead="<"."?xml version='1.0' encoding='UTF-8' ?".">";
82                 
83                 $obj = parse_xml_string($xmlhead.$item['object']);
84                 $links = parse_xml_string($xmlhead."<links>".unxmlify($obj->link)."</links>");
85                 
86                 $Bname = $obj->title;
87                 $Blink = ""; $Bphoto = "";
88                 foreach ($links->link as $l){
89                         $atts = $l->attributes();
90                         switch($atts['rel']){
91                                 case "alternate": $Blink = $atts['href'];
92                                 case "photo": $Bphoto = $atts['href'];
93                         }
94                         
95                 }
96                 
97                 $A = '[url=' . $Alink . ']' . $Aname . '[/url]';
98                 $B = '[url=' . $Blink . ']' . $Bname . '[/url]';
99                 if ($Bphoto!="") $Bphoto = '[url=' . $Blink . '][img]' . $Bphoto . '[/img][/url]';
100
101                 $item['body'] = sprintf( t('%1$s is now friends with %2$s'), $A, $B)."\n\n\n".$Bphoto;
102
103         }
104     if ($item['verb']===ACTIVITY_TAG){
105                 $r = q("SELECT * from `item`,`contact` WHERE 
106                 `item`.`contact-id`=`contact`.`id` AND `item`.`uri`='%s';",
107                  dbesc($item['parent-uri']));
108                 if(count($r)==0) return;
109                 $obj=$r[0];
110                 
111                 $author  = '[url=' . $item['author-link'] . ']' . $item['author-name'] . '[/url]';
112                 $objauthor =  '[url=' . $obj['author-link'] . ']' . $obj['author-name'] . '[/url]';
113                 
114                 switch($obj['verb']){
115                         case ACTIVITY_POST:
116                                 switch ($obj['object-type']){
117                                         case ACTIVITY_OBJ_EVENT:
118                                                 $post_type = t('event');
119                                                 break;
120                                         default:
121                                                 $post_type = t('status');
122                                 }
123                                 break;
124                         default:
125                                 if($obj['resource-id']){
126                                         $post_type = t('photo');
127                                         $m=array();     preg_match("/\[url=([^]]*)\]/", $obj['body'], $m);
128                                         $rr['plink'] = $m[1];
129                                 } else {
130                                         $post_type = t('status');
131                                 }
132                 }
133                 $plink = '[url=' . $obj['plink'] . ']' . $post_type . '[/url]';
134                 
135                 $parsedobj = parse_xml_string($xmlhead.$item['object']);
136                 
137                 $tag = sprintf('#[url=%s]%s[/url]', $parsedobj->id, $parsedobj->content);
138                 $item['body'] = sprintf( t('%1$s tagged %2$s\'s %3$s with %4$s'), $author, $objauthor, $plink, $tag );
139                 
140         }
141         if ($item['verb']=== ACTIVITY_FAVORITE){
142
143                 if ($item['object-type']== "")
144                         return;
145
146                 $Aname = $item['author-name'];
147                 $Alink = $item['author-link'];
148                 
149                 $xmlhead="<"."?xml version='1.0' encoding='UTF-8' ?".">";
150                 
151                 $obj = parse_xml_string($xmlhead.$item['object']);
152                 if(strlen($obj->id)) {
153                         $r = q("select * from item where uri = '%s' and uid = %d limit 1",
154                                         dbesc($obj->id),
155                                         intval($item['uid'])
156                         );
157                         if(count($r) && $r[0]['plink']) {
158                                 $target = $r[0];
159                                 $Bname = $target['author-name'];
160                                 $Blink = $target['author-link'];
161                                 $A = '[url=' . $Alink . ']' . $Aname . '[/url]';
162                                 $B = '[url=' . $Blink . ']' . $Bname . '[/url]';
163                                 $P = '[url=' . $target['plink'] . ']' . t('post/item') . '[/url]';
164                                 $item['body'] = sprintf( t('%1$s marked %2$s\'s %3$s as favorite'), $A, $B, $P)."\n";
165
166                         }
167                 }
168         }
169
170 }
171
172 /**
173  * "Render" a conversation or list of items for HTML display.
174  * There are two major forms of display:
175  *      - Sequential or unthreaded ("New Item View" or search results)
176  *      - conversation view
177  * The $mode parameter decides between the various renderings and also
178  * figures out how to determine page owner and other contextual items 
179  * that are based on unique features of the calling module.
180  *
181  */
182  if(!function_exists('conversation')){
183 function conversation(&$a, $items, $mode, $update, $preview = false) {
184
185         require_once('bbcode.php');
186
187         $profile_owner = 0;
188         $page_writeable      = false;
189
190         $previewing = (($preview) ? ' preview ' : '');
191
192         if($mode === 'network') {
193                 $profile_owner = local_user();
194                 $page_writeable = true;
195         }
196
197         if($mode === 'profile') {
198                 $profile_owner = $a->profile['profile_uid'];
199                 $page_writeable = can_write_wall($a,$profile_owner);
200         }
201
202         if($mode === 'notes') {
203                 $profile_owner = local_user();
204                 $page_writeable = true;
205         }
206
207         if($mode === 'display') {
208                 $profile_owner = $a->profile['uid'];
209                 $page_writeable = can_write_wall($a,$profile_owner);
210         }
211
212         if($mode === 'community') {
213                 $profile_owner = 0;
214                 $page_writeable = false;
215         }
216
217         if($update)
218                 $return_url = $_SESSION['return_url'];
219         else
220                 $return_url = $_SESSION['return_url'] = $a->cmd;
221
222         load_contact_links(local_user());
223
224         $cb = array('items' => $items, 'mode' => $mode, 'update' => $update, 'preview' => $preview);
225         call_hooks('conversation_start',$cb);
226
227         $items = $cb['items'];
228
229         $cmnt_tpl    = get_markup_template('comment_item.tpl');
230         $tpl         = 'wall_item.tpl';
231         $wallwall    = 'wallwall_item.tpl';
232         $hide_comments_tpl = get_markup_template('hide_comments.tpl');
233
234         $alike = array();
235         $dlike = array();
236         
237         
238         // array with html for each thread (parent+comments)
239         $threads = array();
240         $threadsid = -1;
241         
242         if(count($items)) {
243
244                 if($mode === 'network-new' || $mode === 'search' || $mode === 'community') {
245
246                         // "New Item View" on network page or search page results 
247                         // - just loop through the items and format them minimally for display
248
249                         //$tpl = get_markup_template('search_item.tpl');
250                         $tpl = 'search_item.tpl';
251
252                         foreach($items as $item) {
253                                 $threadsid++;
254
255                                 $comment     = '';
256                                 $owner_url   = '';
257                                 $owner_photo = '';
258                                 $owner_name  = '';
259                                 $sparkle     = '';
260
261                                 if($mode === 'search' || $mode === 'community') {
262                                         if(((activity_match($item['verb'],ACTIVITY_LIKE)) || (activity_match($item['verb'],ACTIVITY_DISLIKE))) 
263                                                 && ($item['id'] != $item['parent']))
264                                                 continue;
265                                         $nickname = $item['nickname'];
266                                 }
267                                 else
268                                         $nickname = $a->user['nickname'];
269                                 
270                         
271                                 $profile_name   = ((strlen($item['author-name']))   ? $item['author-name']   : $item['name']);
272                                 if($item['author-link'] && (! $item['author-name']))
273                                         $profile_name = $item['author-link'];
274
275                                 $sp = false;
276                                 $profile_link = best_link_url($item,$sp);
277                                 if($sp)
278                                         $sparkle = ' sparkle';
279                                 if($profile_link === 'mailbox')
280                                         $profile_link = '';
281
282
283                                 $normalised = normalise_link((strlen($item['author-link'])) ? $item['author-link'] : $item['url']);
284                                 if(($normalised != 'mailbox') && (x($a->contacts[$normalised])))
285                                         $profile_avatar = $a->contacts[$normalised]['thumb'];
286                                 else
287                                         $profile_avatar = ((strlen($item['author-avatar'])) ? $item['author-avatar'] : $item['thumb']);
288
289                                 $locate = array('location' => $item['location'], 'coord' => $item['coord'], 'html' => '');
290                                 call_hooks('render_location',$locate);
291
292                                 $location = ((strlen($locate['html'])) ? $locate['html'] : render_location_google($locate));
293
294                                 localize_item($item);
295                                 if($mode === 'network-new')
296                                         $dropping = true;
297                                 else
298                                         $dropping = false;
299
300
301                                 $drop = array(
302                                         'dropping' => $dropping,
303                                         'select' => t('Select'), 
304                                         'delete' => t('Delete'),
305                                 );
306
307                                 $star = false;
308                                 $isstarred = "unstarred";
309                                 
310                                 $lock = false;
311                                 $likebuttons = false;
312                                 $shareable = false;
313
314                                 $body = prepare_body($item,true);
315                                 
316                                 //$tmp_item = replace_macros($tpl,array(
317                                 $tmp_item = array(
318                                         'template' => $tpl,
319                                         'id' => (($preview) ? 'P0' : $item['item_id']),
320                                         'linktitle' => sprintf( t('View %s\'s profile @ %s'), $profile_name, ((strlen($item['author-link'])) ? $item['author-link'] : $item['url'])),
321                                         'profile_url' => $profile_link,
322                                         'item_photo_menu' => item_photo_menu($item),
323                                         'name' => template_escape($profile_name),
324                                         'sparkle' => $sparkle,
325                                         'lock' => $lock,
326                                         'thumb' => $profile_avatar,
327                                         'title' => template_escape($item['title']),
328                                         'body' => template_escape($body),
329                                         'text' => strip_tags(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]['items'] = array($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                         $comment_lastcollapsed = false;
389                         $comment_firstcollapsed = false;
390                         $blowhard = 0;
391                         $blowhard_count = 0;
392
393
394                         foreach($items as $item) {
395
396                                 $comment = '';
397                                 $template = $tpl;
398                                 $commentww = '';
399                                 $sparkle = '';
400                                 $owner_url = $owner_photo = $owner_name = '';
401
402                                 // We've already parsed out like/dislike for special treatment. We can ignore them now
403
404                                 if(((activity_match($item['verb'],ACTIVITY_LIKE)) 
405                                         || (activity_match($item['verb'],ACTIVITY_DISLIKE))) 
406                                         && ($item['id'] != $item['parent']))
407                                         continue;
408
409                                 $toplevelpost = (($item['id'] == $item['parent']) ? true : false);
410                                 $toplevelprivate = false;
411
412                                 // Take care of author collapsing and comment collapsing
413                                 // (author collapsing is currently disabled)
414                                 // If a single author has more than 3 consecutive top-level posts, squash the remaining ones.
415                                 // If there are more than two comments, squash all but the last 2.
416                         
417                                 if($toplevelpost) {
418                                         $toplevelprivate = (($toplevelpost && $item['private']) ? true : false);
419                                         $item_writeable = (($item['writable'] || $item['self']) ? true : false);
420
421                                         // DISABLED
422                                         /*
423                                         if($blowhard == $item['cid'] && (! $item['self']) && ($mode != 'profile') && ($mode != 'notes')) {
424                                                 $blowhard_count ++;
425                                                 if($blowhard_count == 3) {
426                                                         $o .= '<div class="icollapse-wrapper fakelink" id="icollapse-wrapper-' . $item['parent'] 
427                                                                 . '" onclick="openClose(' . '\'icollapse-' . $item['parent'] . '\'); $(\'#icollapse-wrapper-' . $item['parent'] . '\').hide();" >' 
428                                                                 . t('See more posts like this') . '</div>' . '<div class="icollapse" id="icollapse-' 
429                                                                 . $item['parent'] . '" style="display: none;" >';
430                                                 }
431                                         }
432                                         else {
433                                                 $blowhard = $item['cid'];                                       
434                                                 if($blowhard_count >= 3)
435                                                         $o .= '</div>';
436                                                 $blowhard_count = 0;
437                                         }
438                                         // END DISABLED
439                                         */
440
441                                         $comments_seen = 0;
442                                         $comments_collapsed = false;
443                                         $comment_lastcollapsed  = false;
444                                         $comment_firstcollapsed = false;
445                                         
446                                         $threadsid++;
447                                         $threads[$threadsid]['id'] = $item['item_id'];
448                                         $threads[$threadsid]['items'] = array();
449
450                                 }
451                                 else {
452                                         // prevent private email from leaking into public conversation
453                                         if((! $toplevelpost) && (! $toplevelprivate) && ($item['private']) && ($profile_owner != local_user()))
454                                                 continue;
455                                         $comments_seen ++;
456                                         $comment_lastcollapsed  = false;
457                                         $comment_firstcollapsed = false;
458                                 }       
459
460                                 $override_comment_box = ((($page_writeable) && ($item_writeable)) ? true : false);
461                                 $show_comment_box = ((($page_writeable) && ($item_writeable) && ($comments_seen == $comments[$item['parent']])) ? true : false);
462
463
464                                 if(($comments[$item['parent']] > 2) && ($comments_seen <= ($comments[$item['parent']] - 2)) && ($item['gravity'] == 6)) {
465
466                                         if (!$comments_collapsed){
467                                                 $threads[$threadsid]['num_comments'] = sprintf( tt('%d comment','%d comments',$comments[$item['parent']]),$comments[$item['parent']] );
468                                                 $threads[$threadsid]['hide_text'] = t('show more');
469                                                 $comments_collapsed = true;
470                                                 $comment_firstcollapsed = true;
471                                         }
472                                 }
473                                 if(($comments[$item['parent']] > 2) && ($comments_seen == ($comments[$item['parent']] - 1))) {
474
475                                         $comment_lastcollapsed = true;
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                                 //$tmp_item = replace_macros($template,
653                                 $tmp_item = array(
654                                         // collapse comments in template. I don't like this much...
655                                         'comment_firstcollapsed' => $comment_firstcollapsed,
656                                         'comment_lastcollapsed' => $comment_lastcollapsed,
657                                         // template to use to render item (wall, walltowall, search)
658                                         'template' => $template,
659                                         
660                                         'type' => implode("",array_slice(split("/",$item['verb']),-1)),
661                                         'tags' => $tags,
662                                         'body' => template_escape($body),
663                                         'text' => strip_tags(template_escape($body)),
664                                         'id' => $item['item_id'],
665                                         'linktitle' => sprintf( t('View %s\'s profile @ %s'), $profile_name, ((strlen($item['author-link'])) ? $item['author-link'] : $item['url'])),
666                                         'olinktitle' => sprintf( t('View %s\'s profile @ %s'), $profile_name, ((strlen($item['owner-link'])) ? $item['owner-link'] : $item['url'])),
667                                         'to' => t('to'),
668                                         'wall' => t('Wall-to-Wall'),
669                                         'vwall' => t('via Wall-To-Wall:'),
670                                         'profile_url' => $profile_link,
671                                         'item_photo_menu' => item_photo_menu($item),
672                                         'name' => template_escape($profile_name),
673                                         'thumb' => $profile_avatar,
674                                         'osparkle' => $osparkle,
675                                         'sparkle' => $sparkle,
676                                         'title' => template_escape($item['title']),
677                                         'ago' => (($item['app']) ? sprintf( t('%s from %s'),relative_date($item['created']),$item['app']) : relative_date($item['created'])),
678                                         'lock' => $lock,
679                                         'location' => template_escape($location),
680                                         'indent' => $indent,
681                                         'owner_url' => $owner_url,
682                                         'owner_photo' => $owner_photo,
683                                         'owner_name' => template_escape($owner_name),
684                                         'plink' => get_plink($item),
685                                         'edpost' => $edpost,
686                                         'isstarred' => $isstarred,
687                                         'star' => $star,
688                                         'drop' => $drop,
689                                         'vote' => $likebuttons,
690                                         'like' => $like,
691                                         'dislike' => $dislike,
692                                         'comment' => $comment,
693                                         'previewing' => $previewing,
694                                         'wait' => t('Please wait'),
695
696                                 );
697
698
699                                 $arr = array('item' => $item, 'output' => $tmp_item);
700                                 call_hooks('display_item', $arr);
701
702                                 $threads[$threadsid]['items'][] = $arr['output'];
703                         }
704                 }
705         }
706
707         $page_template = get_markup_template("conversation.tpl");
708         $o .= replace_macros($page_template, array(
709                 '$baseurl' => $a->get_baseurl(),
710                 '$mode' => $mode,
711                 '$user' => $a->user,
712                 '$threads' => $threads,
713                 '$dropping' => ($dropping?t('Delete Selected Items'):False),
714         ));
715
716         return $o;
717 }}
718
719 function best_link_url($item,&$sparkle) {
720
721         $a = get_app();
722
723         $best_url = '';
724         $sparkle  = false;
725
726         $clean_url = normalise_link($item['author-link']);
727
728         if((local_user()) && (local_user() == $item['uid'])) {
729                 if(isset($a->contacts) && x($a->contacts,$clean_url)) {
730                         if($a->contacts[$clean_url]['network'] === NETWORK_DFRN) {
731                                 $best_url = $a->get_baseurl() . '/redir/' . $a->contacts[$clean_url]['id'];
732                                 $sparkle = true;
733                         }
734                         else
735                                 $best_url = $a->contacts[$clean_url]['url'];
736                 }
737         }
738         if(! $best_url) {
739                 if(strlen($item['author-link']))
740                         $best_url = $item['author-link'];
741                 else
742                         $best_url = $item['url'];
743         }
744
745         return $best_url;
746 }
747
748
749 if(! function_exists('item_photo_menu')){
750 function item_photo_menu($item){
751         $a = get_app();
752         
753         if (local_user() && (! count($a->contacts)))
754                 load_contact_links(local_user());
755
756         $contact_url="";
757         $pm_url="";
758         $status_link="";
759         $photos_link="";
760         $posts_link="";
761
762         $sparkle = false;
763     $profile_link = best_link_url($item,$sparkle);
764         if($profile_link === 'mailbox')
765                 $profile_link = '';
766
767         if($sparkle) {
768                 $cid = intval(basename($profile_link));
769                 $status_link = $profile_link . "?url=status";
770                 $photos_link = $profile_link . "?url=photos";
771                 $profile_link = $profile_link . "?url=profile";
772                 $pm_url = $a->get_baseurl() . '/message/new/' . $cid;
773         }
774         else {
775                 if(local_user() && local_user() == $item['uid'] && link_compare($item['url'],$item['author-link'])) {
776                         $cid = $item['contact-id'];
777                 }               
778                 else {
779                         $cid = 0;
780                 }
781         }
782         if(($cid) && (! $item['self'])) {
783                 $contact_url = $a->get_baseurl() . '/contacts/' . $cid;
784                 $posts_link = $a->get_baseurl() . '/network/?cid=' . $cid;
785         }
786
787         $menu = Array(
788                 t("View status") => $status_link,
789                 t("View profile") => $profile_link,
790                 t("View photos") => $photos_link,               
791                 t("View recent") => $posts_link, 
792                 t("Edit contact") => $contact_url,
793                 t("Send PM") => $pm_url,
794         );
795         
796         
797         $args = array('item' => $item, 'menu' => $menu);
798         
799         call_hooks('item_photo_menu', $args);
800
801         $menu = $args['menu'];  
802
803         $o = "";
804         foreach($menu as $k=>$v){
805                 if ($v!="") $o .= "<li><a href='$v'>$k</a></li>\n";
806         }
807         return $o;
808 }}
809
810 if(! function_exists('like_puller')) {
811 function like_puller($a,$item,&$arr,$mode) {
812
813         $url = '';
814         $sparkle = '';
815         $verb = (($mode === 'like') ? ACTIVITY_LIKE : ACTIVITY_DISLIKE);
816
817         if((activity_match($item['verb'],$verb)) && ($item['id'] != $item['parent'])) {
818                 $url = $item['author-link'];
819                 if((local_user()) && (local_user() == $item['uid']) && ($item['network'] === 'dfrn') && (! $item['self']) && (link_compare($item['author-link'],$item['url']))) {
820                         $url = $a->get_baseurl() . '/redir/' . $item['contact-id'];
821                         $sparkle = ' class="sparkle" ';
822                 }
823                 if(! ((isset($arr[$item['parent'] . '-l'])) && (is_array($arr[$item['parent'] . '-l']))))
824                         $arr[$item['parent'] . '-l'] = array();
825                 if(! isset($arr[$item['parent']]))
826                         $arr[$item['parent']] = 1;
827                 else    
828                         $arr[$item['parent']] ++;
829                 $arr[$item['parent'] . '-l'][] = '<a href="'. $url . '"'. $sparkle .'>' . $item['author-name'] . '</a>';
830         }
831         return;
832 }}
833
834 // Format the like/dislike text for a profile item
835 // $cnt = number of people who like/dislike the item
836 // $arr = array of pre-linked names of likers/dislikers
837 // $type = one of 'like, 'dislike'
838 // $id  = item id
839 // returns formatted text
840
841 if(! function_exists('format_like')) {
842 function format_like($cnt,$arr,$type,$id) {
843         $o = '';
844         if($cnt == 1)
845                 $o .= (($type === 'like') ? sprintf( t('%s likes this.'), $arr[0]) : sprintf( t('%s doesn\'t like this.'), $arr[0])) . EOL ;
846         else {
847                 $spanatts = 'class="fakelink" onclick="openClose(\'' . $type . 'list-' . $id . '\');"';
848                 $o .= (($type === 'like') ? 
849                                         sprintf( t('<span  %1$s>%2$d people</span> like this.'), $spanatts, $cnt)
850                                          : 
851                                         sprintf( t('<span  %1$s>%2$d people</span> don\'t like this.'), $spanatts, $cnt) ); 
852                 $o .= EOL ;
853                 $total = count($arr);
854                 if($total >= MAX_LIKERS)
855                         $arr = array_slice($arr, 0, MAX_LIKERS - 1);
856                 if($total < MAX_LIKERS)
857                         $arr[count($arr)-1] = t('and') . ' ' . $arr[count($arr)-1];
858                 $str = implode(', ', $arr);
859                 if($total >= MAX_LIKERS)
860                         $str .= sprintf( t(', and %d other people'), $total - MAX_LIKERS );
861                 $str = (($type === 'like') ? sprintf( t('%s like this.'), $str) : sprintf( t('%s don\'t like this.'), $str));
862                 $o .= "\t" . '<div id="' . $type . 'list-' . $id . '" style="display: none;" >' . $str . '</div>';
863         }
864         return $o;
865 }}
866
867
868 function status_editor($a,$x, $notes_cid = 0, $popup=false) {
869
870         $o = '';
871                 
872         $geotag = (($x['allow_location']) ? get_markup_template('jot_geotag.tpl') : '');
873
874         $plaintext = false;
875         if(local_user() && intval(get_pconfig(local_user(),'system','plaintext')))
876                 $plaintext = true;
877
878         $tpl = get_markup_template('jot-header.tpl');
879         
880         $a->page['htmlhead'] .= replace_macros($tpl, array(
881                 '$newpost' => 'true',
882                 '$baseurl' => $a->get_baseurl(),
883                 '$editselect' => (($plaintext) ? 'none' : '/(profile-jot-text|prvmail-text)/'),
884                 '$geotag' => $geotag,
885                 '$nickname' => $x['nickname'],
886                 '$ispublic' => t('Visible to <strong>everybody</strong>'),
887                 '$linkurl' => t('Please enter a link URL:'),
888                 '$vidurl' => t("Please enter a video link/URL:"),
889                 '$audurl' => t("Please enter an audio link/URL:"),
890                 '$term' => t('Tag term:'),
891                 '$whereareu' => t('Where are you right now?'),
892                 '$title' => t('Enter a title for this item') 
893         ));
894
895
896         $tpl = get_markup_template("jot.tpl");
897                 
898         $jotplugins = '';
899         $jotnets = '';
900
901         $mail_disabled = ((function_exists('imap_open') && (! get_config('system','imap_disabled'))) ? 0 : 1);
902
903         $mail_enabled = false;
904         $pubmail_enabled = false;
905
906         if(($x['is_owner']) && (! $mail_disabled)) {
907                 $r = q("SELECT * FROM `mailacct` WHERE `uid` = %d AND `server` != '' LIMIT 1",
908                         intval(local_user())
909                 );
910                 if(count($r)) {
911                         $mail_enabled = true;
912                         if(intval($r[0]['pubmail']))
913                                 $pubmail_enabled = true;
914                 }
915         }
916
917         if($mail_enabled) {
918                 $selected = (($pubmail_enabled) ? ' checked="checked" ' : '');
919                 $jotnets .= '<div class="profile-jot-net"><input type="checkbox" name="pubmail_enable"' . $selected . ' value="1" /> ' . t("Post to Email") . '</div>';
920         }
921
922         call_hooks('jot_tool', $jotplugins);
923         call_hooks('jot_networks', $jotnets);
924
925         if($notes_cid)
926                 $jotnets .= '<input type="hidden" name="contact_allow[]" value="' . $notes_cid .'" />';
927
928         $tpl = replace_macros($tpl,array('$jotplugins' => $jotplugins));        
929
930         $o .= replace_macros($tpl,array(
931                 '$return_path' => $a->cmd,
932                 '$action' =>  $a->get_baseurl().'/item',
933                 '$share' => (($x['button']) ? $x['button'] : t('Share')),
934                 '$upload' => t('Upload photo'),
935                 '$shortupload' => t('upload photo'),
936                 '$attach' => t('Attach file'),
937                 '$shortattach' => t('attach file'),
938                 '$weblink' => t('Insert web link'),
939                 '$shortweblink' => t('web link'),
940                 '$video' => t('Insert video link'),
941                 '$shortvideo' => t('video link'),
942                 '$audio' => t('Insert audio link'),
943                 '$shortaudio' => t('audio link'),
944                 '$setloc' => t('Set your location'),
945                 '$shortsetloc' => t('set location'),
946                 '$noloc' => t('Clear browser location'),
947                 '$shortnoloc' => t('clear location'),
948                 '$title' => "",
949                 '$placeholdertitle' => t('Set title'),
950                 '$wait' => t('Please wait'),
951                 '$permset' => t('Permission settings'),
952                 '$shortpermset' => t('permissions'),
953                 '$ptyp' => (($notes_cid) ? 'note' : 'wall'),
954                 '$content' => '',
955                 '$post_id' => '',
956                 '$baseurl' => $a->get_baseurl(),
957                 '$defloc' => $x['default_location'],
958                 '$visitor' => $x['visitor'],
959                 '$pvisit' => (($notes_cid) ? 'none' : $x['visitor']),
960                 '$emailcc' => t('CC: email addresses'),
961                 '$public' => t('Public post'),
962                 '$jotnets' => $jotnets,
963                 '$emtitle' => t('Example: bob@example.com, mary@example.com'),
964                 '$lockstate' => $x['lockstate'],
965                 '$acl' => $x['acl'],
966                 '$bang' => $x['bang'],
967                 '$profile_uid' => $x['profile_uid'],
968                 '$preview' => t('Preview'),
969         ));
970
971
972         if ($popup==true){
973                 $o = '<div id="jot-popup" style="display: none;">'.$o.'</div>';
974                 
975         }
976
977         return $o;
978 }
979
980
981 function conv_sort($arr,$order) {
982
983         if((!(is_array($arr) && count($arr))))
984                 return array();
985
986         $parents = array();
987
988         foreach($arr as $x)
989                 if($x['id'] == $x['parent'])
990                                 $parents[] = $x;
991
992         if(stristr($order,'created'))
993                 usort($parents,'sort_thr_created');
994         elseif(stristr($order,'commented'))
995                 usort($parents,'sort_thr_commented');
996
997         if(count($parents))
998                 foreach($parents as $x) 
999                         $x['children'] = array();
1000
1001         foreach($arr as $x) {
1002                 if($x['id'] != $x['parent']) {
1003                         $p = find_thread_parent_index($parents,$x);
1004                         if($p !== false)
1005                                 $parents[$p]['children'][] = $x;
1006                 }
1007         }
1008         if(count($parents)) {
1009                 foreach($parents as $k => $v) {
1010                         if(count($parents[$k]['children'])) {
1011                                 $y = $parents[$k]['children'];
1012                                 usort($y,'sort_thr_created_rev');
1013                                 $parents[$k]['children'] = $y;
1014                         }
1015                 }       
1016         }
1017
1018         $ret = array();
1019         if(count($parents)) {
1020                 foreach($parents as $x) {
1021                         $ret[] = $x;
1022                         if(count($x['children']))
1023                                 foreach($x['children'] as $y)
1024                                         $ret[] = $y;
1025                 }
1026         }
1027
1028         return $ret;
1029 }
1030
1031
1032 function sort_thr_created($a,$b) {
1033         return strcmp($b['created'],$a['created']);
1034 }
1035
1036 function sort_thr_created_rev($a,$b) {
1037         return strcmp($a['created'],$b['created']);
1038 }
1039
1040 function sort_thr_commented($a,$b) {
1041         return strcmp($b['commented'],$a['commented']);
1042 }
1043
1044 function find_thread_parent_index($arr,$x) {
1045         foreach($arr as $k => $v)
1046                 if($v['id'] == $x['parent'])
1047                         return $k;
1048         return false;
1049 }
1050
1051 function render_location_google($item) {
1052         $location = '';
1053         $location = (($item['location']) ? '<a target="map" title="' . $item['location'] . '" href="http://maps.google.com/?q=' . urlencode($item['location']) . '">' . $item['location'] . '</a>' : '');
1054         $coord = (($item['coord']) ? '<a target="map" title="' . $item['coord'] . '" href="http://maps.google.com/?q=' . urlencode($item['coord']) . '">' . $item['coord'] . '</a>' : '');
1055         if($coord) {
1056                 if($location)
1057                         $location .= '<br /><span class="smalltext">(' . $coord . ')</span>';
1058                 else
1059                         $location = '<span class="smalltext">' . $coord . '</span>';
1060         }
1061         return $location;
1062 }
1063