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