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