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