]> git.mxchange.org Git - friendica.git/blob - mod/network.php
Merge pull request #535 from fabrixxm/master
[friendica.git] / mod / network.php
1 <?php
2
3
4 function network_init(&$a) {
5         if(! local_user()) {
6                 notice( t('Permission denied.') . EOL);
7                 return;
8         }
9         
10         $is_a_date_query = false;
11
12         if($a->argc > 1) {
13                 for($x = 1; $x < $a->argc; $x ++) {
14                         if(is_a_date_arg($a->argv[$x])) {
15                                 $is_a_date_query = true;
16                                 break;
17                         }
18                 }
19         }
20     
21     // convert query string to array and remove first element (which is friendica args)
22     $query_array = array();
23     parse_str($a->query_string, $query_array);
24     array_shift($query_array);
25     
26         // fetch last used network view and redirect if needed
27         if(! $is_a_date_query) {
28                 $sel_tabs = network_query_get_sel_tab($a);
29                 $sel_nets = network_query_get_sel_net();
30                 $sel_groups = network_query_get_sel_group($a);
31                 $last_sel_tabs = get_pconfig(local_user(), 'network.view','tab.selected');
32                 $last_sel_nets = get_pconfig(local_user(), 'network.view', 'net.selected');
33                 $last_sel_groups = get_pconfig(local_user(), 'network.view', 'group.selected');
34
35                 $remember_tab = ($sel_tabs[0] === 'active' && is_array($last_sel_tabs) && $last_sel_tabs[0] !== 'active');
36                 $remember_net = ($sel_nets === false && $last_sel_nets && $last_sel_nets !== 'all');
37                 $remember_group = ($sel_groups === false && $last_sel_groups && $last_sel_groups != 0);
38
39                 $net_baseurl = '/network';
40                 $net_args = array();
41
42                 if($remember_group) {
43                         $net_baseurl .= '/' . $last_sel_groups; // Note that the group number must come before the "/new" tab selection
44                 }
45                 else if($sel_groups !== false) {
46                         $net_baseurl .= '/' . $sel_groups;
47                 }
48
49                 if($remember_tab) {
50                         // redirect if current selected tab is '/network' and
51                         // last selected tab is _not_ '/network?f=&order=comment'. 
52                         // and this isn't a date query
53
54                         $tab_baseurls = array(
55                                 '',             //all
56                                 '',             //postord
57                                 '',             //conv
58                                 '/new', //new
59                                 '',             //starred
60                                 '',             //bookmarked
61                                 '',             //spam
62                         );
63                         $tab_args = array(
64                                 'f=&order=comment',     //all
65                                 'f=&order=post',                //postord
66                                 'f=&conv=1',                    //conv
67                                 '',                                     //new
68                                 'f=&star=1',                    //starred
69                                 'f=&bmark=1',                   //bookmarked
70                                 'f=&spam=1',                    //spam
71                         );
72
73                         $k = array_search('active', $last_sel_tabs);
74
75                         $net_baseurl .= $tab_baseurls[$k];
76
77             // parse out tab queries
78             $dest_qa = array();
79             $dest_qs = $tab_args[$k];
80             parse_str( $dest_qs, $dest_qa);
81             $net_args = array_merge($net_args, $dest_qa);
82                 }
83                 else if($sel_tabs[4] === 'active') {
84                         // The '/new' tab is selected
85                         $net_baseurl .= '/new';
86                 }
87
88                 if($remember_net) {
89                         $net_args['nets'] = $last_sel_nets;
90                 }
91
92                 if($remember_tab || $remember_net || $remember_group) {
93             $net_args = array_merge($query_array, $net_args);
94             $net_queries = build_querystring($net_args);
95
96             // groups filter is in form of "network/nnn". Add it to $dest_url, if it's possible
97             //if ($a->argc==2 && is_numeric($a->argv[1]) && strpos($net_baseurl, "/",1)===false){
98             //    $net_baseurl .= "/".$a->argv[1];
99             //}
100
101                         $redir_url = ($net_queries ? $net_baseurl."?".$net_queries : $net_baseurl);
102                         goaway($a->get_baseurl() . $redir_url);
103                 }
104         }
105
106 /*      $sel_tabs = network_query_get_sel_tab($a);
107         $last_sel_tabs = get_pconfig(local_user(), 'network.view','tab.selected');
108         if (is_array($last_sel_tabs)){
109                 $tab_urls = array(
110                         '/network?f=&order=comment',//all
111                         '/network?f=&order=post',               //postord
112                         '/network?f=&conv=1',                   //conv
113                         '/network/new',                                 //new
114                         '/network?f=&star=1',                   //starred
115                         '/network?f=&bmark=1',                  //bookmarked
116                         '/network?f=&spam=1',                   //spam
117                 );
118                 
119                 // redirect if current selected tab is 'no_active' and
120                 // last selected tab is _not_ 'all_active'. 
121                 // and this isn't a date query
122
123                 if ($sel_tabs[0] == 'active' && $last_sel_tabs[0]!='active' && (! $is_a_date_query)) {
124                         $k = array_search('active', $last_sel_tabs);
125           
126             // merge tab querystring with request querystring
127             $dest_qa = array();
128             list($dest_url,$dest_qs) = explode("?", $tab_urls[$k]);
129             parse_str( $dest_qs, $dest_qa);
130             $dest_qa = array_merge($query_array, $dest_qa);
131             $dest_qs = build_querystring($dest_qa);
132             
133             // groups filter is in form of "network/nnn". Add it to $dest_url, if it's possible
134             if ($a->argc==2 && is_numeric($a->argv[1]) && strpos($dest_url, "/",1)===false){
135                 $dest_url .= "/".$a->argv[1];
136             }
137
138                         goaway($a->get_baseurl() . $dest_url."?".$dest_qs);
139                 }
140         }*/
141
142         if(x($_GET['nets']) && $_GET['nets'] === 'all')
143                 unset($_GET['nets']);
144         
145         $group_id = (($a->argc > 1 && is_numeric($a->argv[1])) ? intval($a->argv[1]) : 0);
146
147         set_pconfig(local_user(), 'network.view', 'group.selected', $group_id);
148                   
149         require_once('include/group.php');
150         require_once('include/contact_widgets.php');
151         require_once('include/items.php');
152
153         if(! x($a->page,'aside'))
154                 $a->page['aside'] = '';
155
156         $search = ((x($_GET,'search')) ? escape_tags($_GET['search']) : '');
157
158         if(x($_GET,'save')) {
159                 $r = q("select * from `search` where `uid` = %d and `term` = '%s' limit 1",
160                         intval(local_user()),
161                         dbesc($search)
162                 );
163                 if(! count($r)) {
164                         q("insert into `search` ( `uid`,`term` ) values ( %d, '%s') ",
165                                 intval(local_user()),
166                                 dbesc($search)
167                         );
168                 }
169         }
170         if(x($_GET,'remove')) {
171                 q("delete from `search` where `uid` = %d and `term` = '%s' limit 1",
172                         intval(local_user()),
173                         dbesc($search)
174                 );
175         }
176
177
178         
179         // search terms header
180         if(x($_GET,'search')) {
181                 $a->page['content'] .= '<h2>' . t('Search Results For:') . ' '  . $search . '</h2>';
182         }
183
184         $a->page['aside'] .= group_side('network/0','network',true,$group_id);
185         $a->page['aside'] .= posted_date_widget($a->get_baseurl() . '/network',local_user(),false);     
186         $a->page['aside'] .= networks_widget($a->get_baseurl(true) . '/network',(x($_GET, 'nets') ? $_GET['nets'] : ''));
187         $a->page['aside'] .= saved_searches($search);
188         $a->page['aside'] .= fileas_widget($a->get_baseurl(true) . '/network',(x($_GET, 'file') ? $_GET['file'] : ''));
189
190 }
191
192 function saved_searches($search) {
193
194         $a = get_app();
195
196         $srchurl = '/network?f=' 
197                 . ((x($_GET,'cid'))   ? '&cid='   . $_GET['cid']   : '') 
198                 . ((x($_GET,'star'))  ? '&star='  . $_GET['star']  : '')
199                 . ((x($_GET,'bmark')) ? '&bmark=' . $_GET['bmark'] : '')
200                 . ((x($_GET,'conv'))  ? '&conv='  . $_GET['conv']  : '')
201                 . ((x($_GET,'nets'))  ? '&nets='  . $_GET['nets']  : '')
202                 . ((x($_GET,'cmin'))  ? '&cmin='  . $_GET['cmin']  : '')
203                 . ((x($_GET,'cmax'))  ? '&cmax='  . $_GET['cmax']  : '')
204                 . ((x($_GET,'file'))  ? '&file='  . $_GET['file']  : '');
205         ;
206         
207         $o = '';
208
209         $r = q("select `id`,`term` from `search` WHERE `uid` = %d",
210                 intval(local_user())
211         );
212
213         $saved = array();
214
215         if(count($r)) {
216                 foreach($r as $rr) {
217                         $saved[] = array(
218                                 'id'            => $rr['id'],
219                                 'term'                  => $rr['term'],
220                                 'encodedterm'   => urlencode($rr['term']),
221                                 'delete'                => t('Remove term'),
222                                 'selected'              => ($search==$rr['term']),
223                         );
224                 }
225         }               
226
227         
228         $tpl = get_markup_template("saved_searches_aside.tpl");
229         $o = replace_macros($tpl, array(
230                 '$title'         => t('Saved Searches'),
231                 '$add'           => t('add'),
232                 '$searchbox' => search($search,'netsearch-box',$srchurl,true),
233                 '$saved'         => $saved,
234         ));
235         
236         return $o;
237
238 }
239
240 /**
241  * Return selected tab from query
242  * 
243  * urls -> returns
244  *              '/network'                                      => $no_active = 'active'
245  *              '/network?f=&order=comment'     => $comment_active = 'active'
246  *              '/network?f=&order=post'        => $postord_active = 'active'
247  *              '/network?f=&conv=1',           => $conv_active = 'active'
248  *              '/network/new',                         => $new_active = 'active'
249  *              '/network?f=&star=1',           => $starred_active = 'active'
250  *              '/network?f=&bmark=1',          => $bookmarked_active = 'active'
251  *              '/network?f=&spam=1',           => $spam_active = 'active'
252  * 
253  * @return Array ( $no_active, $comment_active, $postord_active, $conv_active, $new_active, $starred_active, $bookmarked_active, $spam_active );
254  */
255 function network_query_get_sel_tab($a) {
256         $no_active='';
257         $starred_active = '';
258         $new_active = '';
259         $bookmarked_active = '';
260         $all_active = '';
261         $search_active = '';
262         $conv_active = '';
263         $spam_active = '';
264         $postord_active = '';
265
266         if(($a->argc > 1 && $a->argv[1] === 'new') 
267                 || ($a->argc > 2 && $a->argv[2] === 'new')) {
268                         $new_active = 'active';
269         }
270         
271         if(x($_GET,'search')) {
272                 $search_active = 'active';
273         }
274         
275         if(x($_GET,'star')) {
276                 $starred_active = 'active';
277         }
278         
279         if(x($_GET,'bmark')) {
280                 $bookmarked_active = 'active';
281         }
282
283         if(x($_GET,'conv')) {
284                 $conv_active = 'active';
285         }
286
287         if(x($_GET,'spam')) {
288                 $spam_active = 'active';
289         }
290
291         
292         
293         if (($new_active == '') 
294                 && ($starred_active == '') 
295                 && ($bookmarked_active == '')
296                 && ($conv_active == '')
297                 && ($search_active == '')
298                 && ($spam_active == '')) {
299                         $no_active = 'active';
300         }
301
302         if ($no_active=='active' && x($_GET,'order')) {
303                 switch($_GET['order']){
304                  case 'post': $postord_active = 'active'; $no_active=''; break;
305                  case 'comment' : $all_active = 'active'; $no_active=''; break;
306                 }
307         }
308         
309         return array($no_active, $all_active, $postord_active, $conv_active, $new_active, $starred_active, $bookmarked_active, $spam_active);
310 }
311
312 /**
313  * Return selected network from query
314  */
315 function network_query_get_sel_net() {
316         $network = false;
317
318         if(x($_GET,'nets')) {
319                 $network = $_GET['nets'];
320         }
321         
322         return $network;
323 }
324
325 function network_query_get_sel_group($a) {
326         $group = false;
327
328         if($a->argc >= 2 && is_numeric($a->argv[1])) {
329                 $group = $a->argv[1];
330         }
331
332         return $group;
333 }
334
335
336 function network_content(&$a, $update = 0) {
337
338         require_once('include/conversation.php');
339
340         if(! local_user()) {
341                 $_SESSION['return_url'] = $a->query_string;
342         return login(false);
343         }
344
345         $arr = array('query' => $a->query_string);
346
347         call_hooks('network_content_init', $arr);
348
349
350         $datequery = $datequery2 = '';
351
352         $group = 0;
353
354         $nouveau = false;
355
356         if($a->argc > 1) {
357                 for($x = 1; $x < $a->argc; $x ++) {
358                         if(is_a_date_arg($a->argv[$x])) {
359                                 if($datequery)
360                                         $datequery2 = escape_tags($a->argv[$x]);
361                                 else {
362                                         $datequery = escape_tags($a->argv[$x]);
363                                         $_GET['order'] = 'post';
364                                 }
365                         }
366                         elseif($a->argv[$x] === 'new') {
367                                 $nouveau = true;
368                         }
369                         elseif(intval($a->argv[$x])) {
370                                 $group = intval($a->argv[$x]);
371                                 $def_acl = array('allow_gid' => '<' . $group . '>');
372                         }
373                 }
374         }
375
376
377         $o = '';
378
379         // item filter tabs
380         // TODO: fix this logic, reduce duplication
381         //$a->page['content'] .= '<div class="tabs-wrapper">';
382         
383         list($no_active, $all_active, $postord_active, $conv_active, $new_active, $starred_active, $bookmarked_active, $spam_active) = network_query_get_sel_tab($a);
384         // if no tabs are selected, defaults to comments
385         if ($no_active=='active') $all_active='active';
386         //echo "<pre>"; var_dump($no_active, $all_active, $postord_active, $conv_active, $new_active, $starred_active, $bookmarked_active, $spam_active); killme();
387
388         $cmd = (($datequery) ? '' : $a->cmd);
389         $len_naked_cmd = strlen(str_replace('/new','',$cmd));           
390
391         // tabs
392         $tabs = array(
393                 array(
394                         'label' => t('Commented Order'),
395                         'url'=>$a->get_baseurl(true) . '/' . str_replace('/new', '', $cmd) . '?f=&order=comment' . ((x($_GET,'cid')) ? '&cid=' . $_GET['cid'] : ''), 
396                         'sel'=>$all_active,
397                         'title'=> t('Sort by Comment Date'),
398                 ),
399                 array(
400                         'label' => t('Posted Order'),
401                         'url'=>$a->get_baseurl(true) . '/' . str_replace('/new', '', $cmd) . '?f=&order=post' . ((x($_GET,'cid')) ? '&cid=' . $_GET['cid'] : ''), 
402                         'sel'=>$postord_active,
403                         'title' => t('Sort by Post Date'),
404                 ),
405
406                 array(
407                         'label' => t('Personal'),
408                         'url' => $a->get_baseurl(true) . '/' . str_replace('/new', '', $cmd) . ((x($_GET,'cid')) ? '/?f=&cid=' . $_GET['cid'] : '') . '&conv=1',
409                         'sel' => $conv_active,
410                         'title' => t('Posts that mention or involve you'),
411                 ),
412                 array(
413                         'label' => t('New'),
414                         'url' => $a->get_baseurl(true) . '/' . str_replace('/new', '', $cmd) . ($len_naked_cmd ? '/' : '') . 'new' . ((x($_GET,'cid')) ? '/?f=&cid=' . $_GET['cid'] : ''),
415                         'sel' => $new_active,
416                         'title' => t('Activity Stream - by date'),
417                 ),
418                 array(
419                         'label' => t('Starred'),
420                         'url'=>$a->get_baseurl(true) . '/' . str_replace('/new', '', $cmd) . ((x($_GET,'cid')) ? '/?f=&cid=' . $_GET['cid'] : '') . '&star=1',
421                         'sel'=>$starred_active,
422                         'title' => t('Favourite Posts'),
423                 ),
424                 array(
425                         'label' => t('Shared Links'),
426                         'url'=>$a->get_baseurl(true) . '/' . str_replace('/new', '', $cmd) . ((x($_GET,'cid')) ? '/?f=&cid=' . $_GET['cid'] : '') . '&bmark=1',
427                         'sel'=>$bookmarked_active,
428                         'title'=> t('Interesting Links'),
429                 ),      
430 //              array(
431 //                      'label' => t('Spam'),
432 //                      'url'=>$a->get_baseurl(true) . '/network?f=&spam=1'
433 //                      'sel'=> $spam_active,
434 //                      'title' => t('Posts flagged as SPAM'),
435 //              ),      
436
437         );
438         
439         // save selected tab, but only if not in search or file mode
440         if(!x($_GET,'search') && !x($_GET,'file')) {
441                 set_pconfig( local_user(), 'network.view','tab.selected',array($all_active, $postord_active, $conv_active, $new_active, $starred_active, $bookmarked_active, $spam_active) );
442         }
443
444         $arr = array('tabs' => $tabs);
445         call_hooks('network_tabs', $arr);
446
447         $o .= replace_macros(get_markup_template('common_tabs.tpl'), array('$tabs'=> $arr['tabs']));
448
449         // --- end item filter tabs
450
451
452
453         
454
455         $contact_id = $a->cid;
456
457         require_once('include/acl_selectors.php');
458
459         $cid = ((x($_GET,'cid')) ? intval($_GET['cid']) : 0);
460         $star = ((x($_GET,'star')) ? intval($_GET['star']) : 0);
461         $bmark = ((x($_GET,'bmark')) ? intval($_GET['bmark']) : 0);
462         $order = ((x($_GET,'order')) ? notags($_GET['order']) : 'comment');
463         $liked = ((x($_GET,'liked')) ? intval($_GET['liked']) : 0);
464         $conv = ((x($_GET,'conv')) ? intval($_GET['conv']) : 0);
465         $spam = ((x($_GET,'spam')) ? intval($_GET['spam']) : 0);
466         $nets = ((x($_GET,'nets')) ? $_GET['nets'] : '');
467         $cmin = ((x($_GET,'cmin')) ? intval($_GET['cmin']) : 0);
468         $cmax = ((x($_GET,'cmax')) ? intval($_GET['cmax']) : 99);
469         $file = ((x($_GET,'file')) ? $_GET['file'] : '');
470
471
472
473         if(x($_GET,'search') || x($_GET,'file'))
474                 $nouveau = true;
475         if($cid)
476                 $def_acl = array('allow_cid' => '<' . intval($cid) . '>');
477
478         if($nets) {
479                 $r = q("select id from contact where uid = %d and network = '%s' and self = 0",
480                         intval(local_user()),
481                         dbesc($nets)
482                 );
483
484                 $str = '';
485                 if(count($r))
486                         foreach($r as $rr)
487                                 $str .= '<' . $rr['id'] . '>';
488                 if(strlen($str))
489                         $def_acl = array('allow_cid' => $str);
490         }
491         set_pconfig(local_user(), 'network.view', 'net.selected', ($nets ? $nets : 'all'));
492
493         if(! $update) {
494                 if($group) {
495                         if(($t = group_public_members($group)) && (! get_pconfig(local_user(),'system','nowarn_insecure'))) {
496                                 notice( sprintf( tt('Warning: This group contains %s member from an insecure network.',
497                                                                         'Warning: This group contains %s members from an insecure network.',
498                                                                         $t), $t ) . EOL);
499                                 notice( t('Private messages to this group are at risk of public disclosure.') . EOL);
500                         }
501                 }
502
503                 nav_set_selected('network');
504
505                 $celeb = ((($a->user['page-flags'] == PAGE_SOAPBOX) || ($a->user['page-flags'] == PAGE_COMMUNITY)) ? true : false);
506
507                 $x = array(
508                         'is_owner' => true,
509                         'allow_location' => $a->user['allow_location'],
510                         'default_location' => $a->user['default-location'],
511                         'nickname' => $a->user['nickname'],
512                         'lockstate' => ((($group) || ($cid) || ($nets) || (is_array($a->user) && ((strlen($a->user['allow_cid'])) || (strlen($a->user['allow_gid'])) || (strlen($a->user['deny_cid'])) || (strlen($a->user['deny_gid']))))) ? 'lock' : 'unlock'),
513                         'acl' => populate_acl((($group || $cid || $nets) ? $def_acl : $a->user), $celeb),
514                         'bang' => (($group || $cid || $nets) ? '!' : ''),
515                         'visitor' => 'block',
516                         'profile_uid' => local_user()
517                 );
518
519                 $o .= status_editor($a,$x);
520
521         }
522
523
524         // We don't have to deal with ACL's on this page. You're looking at everything
525         // that belongs to you, hence you can see all of it. We will filter by group if
526         // desired. 
527
528         
529         $sql_options  = (($star) ? " and starred = 1 " : '');
530         $sql_options .= (($bmark) ? " and bookmark = 1 " : '');
531
532         $sql_nets = (($nets) ? sprintf(" and `contact`.`network` = '%s' ", dbesc($nets)) : '');
533
534         $sql_extra = " AND `item`.`parent` IN ( SELECT `parent` FROM `item` WHERE `id` = `parent` $sql_options ) ";
535
536         if($group) {
537                 $r = q("SELECT `name`, `id` FROM `group` WHERE `id` = %d AND `uid` = %d LIMIT 1",
538                         intval($group),
539                         intval($_SESSION['uid'])
540                 );
541                 if(! count($r)) {
542                         if($update)
543                                 killme();
544                         notice( t('No such group') . EOL );
545                         goaway($a->get_baseurl(true) . '/network');
546                         // NOTREACHED
547                 }
548
549                 $contacts = expand_groups(array($group));
550                 if((is_array($contacts)) && count($contacts)) {
551                         $contact_str = implode(',',$contacts);
552                 }
553                 else {
554                                 $contact_str = ' 0 ';
555                                 info( t('Group is empty'));
556                 }
557
558                 $sql_extra = " AND `item`.`parent` IN ( SELECT DISTINCT(`parent`) FROM `item` WHERE 1 $sql_options AND ( `contact-id` IN ( $contact_str ) OR `allow_gid` like '" . protect_sprintf('%<' . intval($group) . '>%') . "' ) and deleted = 0 ) ";
559                 $o = '<h2>' . t('Group: ') . $r[0]['name'] . '</h2>' . $o;
560         }
561         elseif($cid) {
562
563                 $r = q("SELECT `id`,`name`,`network`,`writable`,`nurl` FROM `contact` WHERE `id` = %d 
564                                 AND `blocked` = 0 AND `pending` = 0 LIMIT 1",
565                         intval($cid)
566                 );
567                 if(count($r)) {
568                         $sql_extra = " AND `item`.`parent` IN ( SELECT DISTINCT(`parent`) FROM `item` WHERE 1 $sql_options AND `contact-id` = " . intval($cid) . " and deleted = 0 ) ";
569                         $o = '<h2>' . t('Contact: ') . $r[0]['name'] . '</h2>' . $o;
570                         if($r[0]['network'] === NETWORK_OSTATUS && $r[0]['writable'] && (! get_pconfig(local_user(),'system','nowarn_insecure'))) {
571                                 notice( t('Private messages to this person are at risk of public disclosure.') . EOL);
572                         }
573
574                 }
575                 else {
576                         notice( t('Invalid contact.') . EOL);
577                         goaway($a->get_baseurl(true) . '/network');
578                         // NOTREACHED
579                 }
580         }
581
582         if((! $group) && (! $cid) && (! $update) && (! get_config('theme','hide_eventlist'))) {
583                 $o .= get_birthdays();
584                 $o .= get_events();
585         }
586
587         $sql_extra3 = '';
588
589         if($datequery) {
590                 $sql_extra3 .= protect_sprintf(sprintf(" AND item.created <= '%s' ", dbesc(datetime_convert(date_default_timezone_get(),'',$datequery))));
591         }
592         if($datequery2) {
593                 $sql_extra3 .= protect_sprintf(sprintf(" AND item.created >= '%s' ", dbesc(datetime_convert(date_default_timezone_get(),'',$datequery2))));
594         }
595
596         $sql_extra2 = (($nouveau) ? '' : " AND `item`.`parent` = `item`.`id` ");
597         $sql_extra3 = (($nouveau) ? '' : $sql_extra3);
598
599         if(x($_GET,'search')) {
600                 $search = escape_tags($_GET['search']);
601                 if (get_config('system','use_fulltext_engine')) {
602                         if(strpos($search,'#') === 0)
603                                 $sql_extra .= sprintf(" AND (MATCH(tag) AGAINST ('".'"%s"'."' in boolean mode)) ",
604                                         dbesc(protect_sprintf($search))
605                                 );
606                         else
607                                 $sql_extra .= sprintf(" AND (MATCH(`item`.`body`) AGAINST ('".'"%s"'."' in boolean mode) or MATCH(tag) AGAINST ('".'"%s"'."' in boolean mode)) ",
608                                         dbesc(protect_sprintf($search)),
609                                         dbesc(protect_sprintf($search))
610                                 );
611                 } else {
612                         $sql_extra .= sprintf(" AND ( `item`.`body` like '%s' OR `item`.`tag` like '%s' ) ",
613                                         dbesc(protect_sprintf('%' . $search . '%')),
614                                         dbesc(protect_sprintf('%]' . $search . '[%'))
615                         );
616                 }
617         }
618         if(strlen($file)) {
619                 $sql_extra .= file_tag_file_query('item',unxmlify($file));
620         }
621
622         if($conv) {
623                 $myurl = $a->get_baseurl() . '/profile/'. $a->user['nickname'];
624                 $myurl = substr($myurl,strpos($myurl,'://')+3);
625                 $myurl = str_replace('www.','',$myurl);
626                 $diasp_url = str_replace('/profile/','/u/',$myurl);
627                 if (get_config('system','use_fulltext_engine'))
628                         $sql_extra .= sprintf(" AND `item`.`parent` IN (SELECT distinct(`parent`) from item where (MATCH(`author-link`) AGAINST ('".'"%s"'."' in boolean mode) or MATCH(`tag`) AGAINST ('".'"%s"'."' in boolean mode) or MATCH(tag) AGAINST ('".'"%s"'."' in boolean mode))) ",
629                                 dbesc(protect_sprintf($myurl)),
630                                 dbesc(protect_sprintf($myurl)),
631                                 dbesc(protect_sprintf($diasp_url))
632                         );
633                 else
634                         $sql_extra .= sprintf(" AND `item`.`parent` IN (SELECT distinct(`parent`) from item where ( `author-link` like '%s' or `tag` like '%s' or tag like '%s' )) ",
635                                 dbesc(protect_sprintf('%' . $myurl)),
636                                 dbesc(protect_sprintf('%' . $myurl . ']%')),
637                                 dbesc(protect_sprintf('%' . $diasp_url . ']%'))
638                         );
639
640         }
641
642         if($update) {
643
644                 // only setup pagination on initial page view
645                 $pager_sql = '';
646
647         }
648         else {
649                 if(! get_pconfig(local_user(),'system','alt_pager')) {
650                         $r = q("SELECT COUNT(*) AS `total`
651                                 FROM `item` LEFT JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
652                                 WHERE `item`.`uid` = %d AND `item`.`visible` = 1 AND `item`.`deleted` = 0
653                                 AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
654                                 $sql_extra2 $sql_extra3
655                                 $sql_extra $sql_nets ",
656                                 intval($_SESSION['uid'])
657                         );
658
659                         if(count($r)) {
660                                 $a->set_pager_total($r[0]['total']);
661                         }
662                 }
663
664                 $itemspage_network = get_pconfig(local_user(),'system','itemspage_network');
665                 $itemspage_network = ((intval($itemspage_network)) ? $itemspage_network : 40);
666                 if(($a->force_max_items > 0) && ($a->force_max_items < $itemspage_network))
667                         $itemspage_network = $a->force_max_items;
668
669                 $a->set_pager_itemspage($itemspage_network);
670                 $pager_sql = sprintf(" LIMIT %d, %d ",intval($a->pager['start']), intval($a->pager['itemspage']));
671         }
672
673         $simple_update = (($update) ? " and `item`.`unseen` = 1 " : '');
674
675         if($nouveau) {
676                 // "New Item View" - show all items unthreaded in reverse created date order
677
678                 $items = q("SELECT `item`.*, `item`.`id` AS `item_id`, 
679                         `contact`.`name`, `contact`.`photo`, `contact`.`url`, `contact`.`rel`, `contact`.`writable`,
680                         `contact`.`network`, `contact`.`thumb`, `contact`.`dfrn-id`, `contact`.`self`,
681                         `contact`.`id` AS `cid`, `contact`.`uid` AS `contact-uid`
682                         FROM `item`, `contact`
683                         WHERE `item`.`uid` = %d AND `item`.`visible` = 1 
684                         AND `item`.`deleted` = 0 and `item`.`moderated` = 0
685                         $simple_update
686                         AND `contact`.`id` = `item`.`contact-id`
687                         AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
688                         $sql_extra $sql_nets
689                         ORDER BY `item`.`received` DESC $pager_sql ",
690                         intval($_SESSION['uid'])
691                 );
692
693                 $update_unseen = ' WHERE uid = ' . intval($_SESSION['uid']) . " AND unseen = 1 $sql_extra $sql_nets";
694         }
695         else {
696
697                 // Normal conversation view
698
699
700                 if($order === 'post')
701                                 $ordering = "`created`";
702                 else
703                                 $ordering = "`commented`";
704
705                 // Fetch a page full of parent items for this page
706
707                 if($update) {
708                         $r = q("SELECT `parent` AS `item_id`, `contact`.`uid` AS `contact_uid`
709                                 FROM `item` LEFT JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
710                                 WHERE `item`.`uid` = %d AND `item`.`visible` = 1 AND
711                                 (`item`.`deleted` = 0 OR item.verb = '" . ACTIVITY_LIKE ."' OR item.verb = '" . ACTIVITY_DISLIKE . "')
712                                 and `item`.`moderated` = 0 and `item`.`unseen` = 1
713                                 AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
714                                 $sql_extra3 $sql_extra $sql_nets ",
715                                 intval(local_user())
716                         );
717                 }
718                 else {
719                         $r = q("SELECT `item`.`id` AS `item_id`, `contact`.`uid` AS `contact_uid`
720                                 FROM `item` LEFT JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
721                                 WHERE `item`.`uid` = %d AND `item`.`visible` = 1 AND `item`.`deleted` = 0
722                                 AND `item`.`moderated` = 0 AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
723                                 AND `item`.`parent` = `item`.`id`
724                                 $sql_extra3 $sql_extra $sql_nets
725                                 ORDER BY `item`.$ordering DESC $pager_sql ",
726                                 intval(local_user())
727                         );
728                 }
729
730                 // Then fetch all the children of the parents that are on this page
731
732                 $parents_arr = array();
733                 $parents_str = '';
734
735                 if(count($r)) {
736                         foreach($r as $rr)
737                                 if(! in_array($rr['item_id'],$parents_arr))
738                                         $parents_arr[] = $rr['item_id'];
739                         $parents_str = implode(', ', $parents_arr);
740
741                         $items = q("SELECT `item`.*, `item`.`id` AS `item_id`,
742                                 `contact`.`name`, `contact`.`photo`, `contact`.`url`, `contact`.`alias`, `contact`.`rel`, `contact`.`writable`,
743                                 `contact`.`network`, `contact`.`thumb`, `contact`.`dfrn-id`, `contact`.`self`,
744                                 `contact`.`id` AS `cid`, `contact`.`uid` AS `contact-uid`
745                                 FROM `item`, `contact`
746                                 WHERE `item`.`uid` = %d AND `item`.`visible` = 1 AND `item`.`deleted` = 0
747                                 AND `item`.`moderated` = 0 AND `contact`.`id` = `item`.`contact-id`
748                                 AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
749                                 AND `item`.`parent` IN ( %s )
750                                 $sql_extra ",
751                                 intval(local_user()),
752                                 dbesc($parents_str)
753                         );
754
755                         $items = conv_sort($items,$ordering);
756
757                 } else {
758                         $items = array();
759                 }
760
761                 if($parents_str)
762                         $update_unseen = ' WHERE uid = ' . intval(local_user()) . ' AND unseen = 1 AND parent IN ( ' . dbesc($parents_str) . ' )';
763         }
764
765
766         // We aren't going to try and figure out at the item, group, and page
767         // level which items you've seen and which you haven't. If you're looking
768         // at the top level network page just mark everything seen. 
769         
770
771 // The $update_unseen is a bit unreliable if you have stuff coming into your stream from a new contact - 
772 // and other feeds that bring in stuff from the past. One can't find it all. 
773 // I'm reviving this block to mark everything seen on page 1 of the network as a temporary measure.
774 // The correct solution is to implement a network notifications box just like the system notifications popup
775 // with the ability in the popup to "mark all seen".
776 // Several people are complaining because there are unseen messages they can't find and as time goes
777 // on they just get buried deeper. It has happened to me a couple of times also.
778
779         if((! $group) && (! $cid) && (! $star)) {
780                 $r = q("UPDATE `item` SET `unseen` = 0 
781                         WHERE `unseen` = 1 AND `uid` = %d",
782                         intval(local_user())
783                 );
784         }
785         else {
786                 if($update_unseen)
787                         $r = q("UPDATE `item` SET `unseen` = 0 $update_unseen");
788         }
789
790         // Set this so that the conversation function can find out contact info for our wall-wall items
791         $a->page_contact = $a->contact;
792
793         $mode = (($nouveau) ? 'network-new' : 'network');
794
795         $o .= conversation($a,$items,$mode,$update);
796
797         if(! $update) {
798                 if(! get_pconfig(local_user(),'system','alt_pager')) {
799                         $o .= paginate($a);
800                 }
801                 else {
802                         $o .= alt_pager($a,count($items));
803                 }
804         }
805
806         return $o;
807 }