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