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