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