]> git.mxchange.org Git - friendica.git/blobdiff - mod/network.php
Keeping an offset for page switching: This helps so that you don't see old messages
[friendica.git] / mod / network.php
index bbdd0130d72bb02f2280b3261f143d988c925fb6..de5e45e6e1df89723262f88c74d5d35cb7e80e94 100644 (file)
@@ -6,7 +6,7 @@ function network_init(&$a) {
                notice( t('Permission denied.') . EOL);
                return;
        }
-       
+
        $is_a_date_query = false;
 
        if($a->argc > 1) {
@@ -17,12 +17,12 @@ function network_init(&$a) {
                        }
                }
        }
-    
+
     // convert query string to array and remove first element (which is friendica args)
     $query_array = array();
     parse_str($a->query_string, $query_array);
     array_shift($query_array);
-    
+
        // fetch last used network view and redirect if needed
        if(! $is_a_date_query) {
                $sel_tabs = network_query_get_sel_tab($a);
@@ -115,21 +115,21 @@ function network_init(&$a) {
                        '/network?f=&bmark=1',                  //bookmarked
                        '/network?f=&spam=1',                   //spam
                );
-               
+
                // redirect if current selected tab is 'no_active' and
-               // last selected tab is _not_ 'all_active'. 
+               // last selected tab is _not_ 'all_active'.
                // and this isn't a date query
 
                if ($sel_tabs[0] == 'active' && $last_sel_tabs[0]!='active' && (! $is_a_date_query)) {
                        $k = array_search('active', $last_sel_tabs);
-          
+
             // merge tab querystring with request querystring
             $dest_qa = array();
             list($dest_url,$dest_qs) = explode("?", $tab_urls[$k]);
             parse_str( $dest_qs, $dest_qa);
             $dest_qa = array_merge($query_array, $dest_qa);
             $dest_qs = build_querystring($dest_qa);
-            
+
             // groups filter is in form of "network/nnn". Add it to $dest_url, if it's possible
             if ($a->argc==2 && is_numeric($a->argv[1]) && strpos($dest_url, "/",1)===false){
                 $dest_url .= "/".$a->argv[1];
@@ -141,11 +141,11 @@ function network_init(&$a) {
 
        if(x($_GET['nets']) && $_GET['nets'] === 'all')
                unset($_GET['nets']);
-       
+
        $group_id = (($a->argc > 1 && is_numeric($a->argv[1])) ? intval($a->argv[1]) : 0);
 
        set_pconfig(local_user(), 'network.view', 'group.selected', $group_id);
-                 
+
        require_once('include/group.php');
        require_once('include/contact_widgets.php');
        require_once('include/items.php');
@@ -168,20 +168,18 @@ function network_init(&$a) {
                }
        }
        if(x($_GET,'remove')) {
-               q("delete from `search` where `uid` = %d and `term` = '%s' limit 1",
+               q("delete from `search` where `uid` = %d and `term` = '%s'",
                        intval(local_user()),
                        dbesc($search)
                );
        }
 
-
-       
        // search terms header
        if(x($_GET,'search')) {
                $a->page['content'] .= '<h2>' . t('Search Results For:') . ' '  . $search . '</h2>';
        }
 
-       $a->page['aside'] .= group_side('network/0','network',true,$group_id);
+       $a->page['aside'] .= (feature_enabled(local_user(),'groups') ? group_side('network/0','network',true,$group_id) : '');
        $a->page['aside'] .= posted_date_widget($a->get_baseurl() . '/network',local_user(),false);     
        $a->page['aside'] .= networks_widget($a->get_baseurl(true) . '/network',(x($_GET, 'nets') ? $_GET['nets'] : ''));
        $a->page['aside'] .= saved_searches($search);
@@ -191,10 +189,13 @@ function network_init(&$a) {
 
 function saved_searches($search) {
 
+       if(! feature_enabled(local_user(),'savedsearch'))
+               return '';
+
        $a = get_app();
 
-       $srchurl = '/network?f=' 
-               . ((x($_GET,'cid'))   ? '&cid='   . $_GET['cid']   : '') 
+       $srchurl = '/network?f='
+               . ((x($_GET,'cid'))   ? '&cid='   . $_GET['cid']   : '')
                . ((x($_GET,'star'))  ? '&star='  . $_GET['star']  : '')
                . ((x($_GET,'bmark')) ? '&bmark=' . $_GET['bmark'] : '')
                . ((x($_GET,'conv'))  ? '&conv='  . $_GET['conv']  : '')
@@ -203,7 +204,7 @@ function saved_searches($search) {
                . ((x($_GET,'cmax'))  ? '&cmax='  . $_GET['cmax']  : '')
                . ((x($_GET,'file'))  ? '&file='  . $_GET['file']  : '');
        ;
-       
+
        $o = '';
 
        $r = q("select `id`,`term` from `search` WHERE `uid` = %d",
@@ -222,9 +223,9 @@ function saved_searches($search) {
                                'selected'              => ($search==$rr['term']),
                        );
                }
-       }               
+       }
+
 
-       
        $tpl = get_markup_template("saved_searches_aside.tpl");
        $o = replace_macros($tpl, array(
                '$title'         => t('Saved Searches'),
@@ -232,7 +233,7 @@ function saved_searches($search) {
                '$searchbox' => search($search,'netsearch-box',$srchurl,true),
                '$saved'         => $saved,
        ));
-       
+
        return $o;
 
 }
@@ -263,19 +264,19 @@ function network_query_get_sel_tab($a) {
        $spam_active = '';
        $postord_active = '';
 
-       if(($a->argc > 1 && $a->argv[1] === 'new') 
+       if(($a->argc > 1 && $a->argv[1] === 'new')
                || ($a->argc > 2 && $a->argv[2] === 'new')) {
                        $new_active = 'active';
        }
-       
+
        if(x($_GET,'search')) {
                $search_active = 'active';
        }
-       
+
        if(x($_GET,'star')) {
                $starred_active = 'active';
        }
-       
+
        if(x($_GET,'bmark')) {
                $bookmarked_active = 'active';
        }
@@ -288,10 +289,10 @@ function network_query_get_sel_tab($a) {
                $spam_active = 'active';
        }
 
-       
-       
-       if (($new_active == '') 
-               && ($starred_active == '') 
+
+
+       if (($new_active == '')
+               && ($starred_active == '')
                && ($bookmarked_active == '')
                && ($conv_active == '')
                && ($search_active == '')
@@ -305,7 +306,7 @@ function network_query_get_sel_tab($a) {
                 case 'comment' : $all_active = 'active'; $no_active=''; break;
                }
        }
-       
+
        return array($no_active, $all_active, $postord_active, $conv_active, $new_active, $starred_active, $bookmarked_active, $spam_active);
 }
 
@@ -318,7 +319,7 @@ function network_query_get_sel_net() {
        if(x($_GET,'nets')) {
                $network = $_GET['nets'];
        }
-       
+
        return $network;
 }
 
@@ -373,20 +374,19 @@ function network_content(&$a, $update = 0) {
                }
        }
 
-
        $o = '';
 
        // item filter tabs
        // TODO: fix this logic, reduce duplication
        //$a->page['content'] .= '<div class="tabs-wrapper">';
-       
+
        list($no_active, $all_active, $postord_active, $conv_active, $new_active, $starred_active, $bookmarked_active, $spam_active) = network_query_get_sel_tab($a);
        // if no tabs are selected, defaults to comments
        if ($no_active=='active') $all_active='active';
        //echo "<pre>"; var_dump($no_active, $all_active, $postord_active, $conv_active, $new_active, $starred_active, $bookmarked_active, $spam_active); killme();
 
        $cmd = (($datequery) ? '' : $a->cmd);
-       $len_naked_cmd = strlen(str_replace('/new','',$cmd));           
+       $len_naked_cmd = strlen(str_replace('/new','',$cmd));
 
        // tabs
        $tabs = array(
@@ -403,39 +403,86 @@ function network_content(&$a, $update = 0) {
                        'title' => t('Sort by Post Date'),
                ),
 
-               array(
+/*             array(
                        'label' => t('Personal'),
                        'url' => $a->get_baseurl(true) . '/' . str_replace('/new', '', $cmd) . ((x($_GET,'cid')) ? '/?f=&cid=' . $_GET['cid'] : '') . '&conv=1',
                        'sel' => $conv_active,
                        'title' => t('Posts that mention or involve you'),
-               ),
-               array(
+               ),*/
+/*             array(
                        'label' => t('New'),
                        'url' => $a->get_baseurl(true) . '/' . str_replace('/new', '', $cmd) . ($len_naked_cmd ? '/' : '') . 'new' . ((x($_GET,'cid')) ? '/?f=&cid=' . $_GET['cid'] : ''),
                        'sel' => $new_active,
                        'title' => t('Activity Stream - by date'),
-               ),
-               array(
+               ),*/
+/*             array(
                        'label' => t('Starred'),
                        'url'=>$a->get_baseurl(true) . '/' . str_replace('/new', '', $cmd) . ((x($_GET,'cid')) ? '/?f=&cid=' . $_GET['cid'] : '') . '&star=1',
                        'sel'=>$starred_active,
                        'title' => t('Favourite Posts'),
-               ),
-               array(
+               ),*/
+/*             array(
                        'label' => t('Shared Links'),
                        'url'=>$a->get_baseurl(true) . '/' . str_replace('/new', '', $cmd) . ((x($_GET,'cid')) ? '/?f=&cid=' . $_GET['cid'] : '') . '&bmark=1',
                        'sel'=>$bookmarked_active,
                        'title'=> t('Interesting Links'),
-               ),      
+               ),      */
 //             array(
 //                     'label' => t('Spam'),
 //                     'url'=>$a->get_baseurl(true) . '/network?f=&spam=1'
 //                     'sel'=> $spam_active,
 //                     'title' => t('Posts flagged as SPAM'),
-//             ),      
+//             ),
 
        );
-       
+
+       if(feature_enabled(local_user(),'personal_tab')) {
+               $tabs[] = array(
+                       'label' => t('Personal'),
+                       'url' => $a->get_baseurl(true) . '/' . str_replace('/new', '', $cmd) . ((x($_GET,'cid')) ? '/?f=&cid=' . $_GET['cid'] : '') . '&conv=1',
+                       'sel' => $conv_active,
+                       'title' => t('Posts that mention or involve you'),
+               );
+       }
+
+       if(feature_enabled(local_user(),'new_tab')) {
+               $tabs[] = array(
+                       'label' => t('New'),
+                       'url' => $a->get_baseurl(true) . '/' . str_replace('/new', '', $cmd) . ($len_naked_cmd ? '/' : '') . 'new' . ((x($_GET,'cid')) ? '/?f=&cid=' . $_GET['cid'] : ''),
+                       'sel' => $new_active,
+                       'title' => t('Activity Stream - by date'),
+               );
+       }
+
+       if(feature_enabled(local_user(),'link_tab')) {
+               $tabs[] = array(
+                       'label' => t('Shared Links'),
+                       'url'=>$a->get_baseurl(true) . '/' . str_replace('/new', '', $cmd) . ((x($_GET,'cid')) ? '/?f=&cid=' . $_GET['cid'] : '') . '&bmark=1',
+                       'sel'=>$bookmarked_active,
+                       'title'=> t('Interesting Links'),
+               );
+       }
+
+       if(feature_enabled(local_user(),'star_posts')) {
+               $tabs[] = array(
+                       'label' => t('Starred'),
+                       'url'=>$a->get_baseurl(true) . '/' . str_replace('/new', '', $cmd) . ((x($_GET,'cid')) ? '/?f=&cid=' . $_GET['cid'] : '') . '&star=1',
+                       'sel'=>$starred_active,
+                       'title' => t('Favourite Posts'),
+               );
+       }
+
+       // Not yet implemented
+
+/*     if(feature_enabled(local_user(),'spam_filter'))  {
+               $tabs[] = array(
+                       'label' => t('Spam'),
+                       'url'=>$a->get_baseurl(true) . '/network?f=&spam=1',
+                       'sel'=> $spam_active,
+                       'title' => t('Posts flagged as SPAM'),
+               );
+       }*/
+
        // save selected tab, but only if not in search or file mode
        if(!x($_GET,'search') && !x($_GET,'file')) {
                set_pconfig( local_user(), 'network.view','tab.selected',array($all_active, $postord_active, $conv_active, $new_active, $starred_active, $bookmarked_active, $spam_active) );
@@ -448,10 +495,6 @@ function network_content(&$a, $update = 0) {
 
        // --- end item filter tabs
 
-
-
-       
-
        $contact_id = $a->cid;
 
        require_once('include/acl_selectors.php');
@@ -510,26 +553,27 @@ function network_content(&$a, $update = 0) {
                        'default_location' => $a->user['default-location'],
                        'nickname' => $a->user['nickname'],
                        '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'),
+                       'default_perms' => get_acl_permissions($a->user),
                        'acl' => populate_acl((($group || $cid || $nets) ? $def_acl : $a->user), $celeb),
                        'bang' => (($group || $cid || $nets) ? '!' : ''),
                        'visitor' => 'block',
-                       'profile_uid' => local_user()
+                       'profile_uid' => local_user(),
+                       'acl_data' => construct_acl_data($a, $a->user), // For non-Javascript ACL selector
                );
 
                $o .= status_editor($a,$x);
 
        }
 
-
-       // We don't have to deal with ACL's on this page. You're looking at everything
+       // We don't have to deal with ACLs on this page. You're looking at everything
        // that belongs to you, hence you can see all of it. We will filter by group if
        // desired. 
 
-       
        $sql_options  = (($star) ? " and starred = 1 " : '');
        $sql_options .= (($bmark) ? " and bookmark = 1 " : '');
 
-       $sql_nets = (($nets) ? sprintf(" and `contact`.`network` = '%s' ", dbesc($nets)) : '');
+       //$sql_nets = (($nets) ? sprintf(" and `contact`.`network` = '%s' ", dbesc($nets)) : '');
+       $sql_nets = (($nets) ? sprintf(" and `item`.`network` = '%s' ", dbesc($nets)) : '');
 
        $sql_extra = " AND `item`.`parent` IN ( SELECT `parent` FROM `item` WHERE `id` = `parent` $sql_options ) ";
 
@@ -542,7 +586,7 @@ function network_content(&$a, $update = 0) {
                        if($update)
                                killme();
                        notice( t('No such group') . EOL );
-                       goaway($a->get_baseurl(true) . '/network');
+                       goaway($a->get_baseurl(true) . '/network/0');
                        // NOTREACHED
                }
 
@@ -555,7 +599,9 @@ function network_content(&$a, $update = 0) {
                                info( t('Group is empty'));
                }
 
-               $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 ) ";
+               $sql_table = "`item` INNER JOIN (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 ORDER BY `created` DESC) AS `temp1` ON item.parent = `temp1`.parent ";
+               $sql_extra = "";
+               //$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 ) ";
                $o = '<h2>' . t('Group: ') . $r[0]['name'] . '</h2>' . $o;
        }
        elseif($cid) {
@@ -565,7 +611,9 @@ function network_content(&$a, $update = 0) {
                        intval($cid)
                );
                if(count($r)) {
-                       $sql_extra = " AND `item`.`parent` IN ( SELECT DISTINCT(`parent`) FROM `item` WHERE 1 $sql_options AND `contact-id` = " . intval($cid) . " and deleted = 0 ) ";
+                       $sql_table = "`item` INNER JOIN (SELECT DISTINCT(`parent`) FROM `item` WHERE 1 $sql_options AND `contact-id` = ".intval($cid)." and deleted = 0 ORDER BY `item`.`received` DESC) AS `temp1` ON item.parent = `temp1`.parent ";
+                       $sql_extra = "";
+                       //$sql_extra = " AND `item`.`parent` IN ( SELECT DISTINCT(`parent`) FROM `item` WHERE 1 $sql_options AND `contact-id` = " . intval($cid) . " and deleted = 0 ) ";
                        $o = '<h2>' . t('Contact: ') . $r[0]['name'] . '</h2>' . $o;
                        if($r[0]['network'] === NETWORK_OSTATUS && $r[0]['writable'] && (! get_pconfig(local_user(),'system','nowarn_insecure'))) {
                                notice( t('Private messages to this person are at risk of public disclosure.') . EOL);
@@ -595,24 +643,46 @@ function network_content(&$a, $update = 0) {
 
        $sql_extra2 = (($nouveau) ? '' : " AND `item`.`parent` = `item`.`id` ");
        $sql_extra3 = (($nouveau) ? '' : $sql_extra3);
+       //$sql_order = "`item`.`received`";
+       $sql_order = "";
+       $order_mode = "received";
+
+       if ($sql_table == "")
+               $sql_table = "`item`";
 
        if(x($_GET,'search')) {
                $search = escape_tags($_GET['search']);
-               if (get_config('system','use_fulltext_engine')) {
-                       if(strpos($search,'#') === 0)
-                               $sql_extra .= sprintf(" AND (MATCH(tag) AGAINST ('".'"%s"'."' in boolean mode)) ",
-                                       dbesc(protect_sprintf($search))
-                               );
-                       else
-                               $sql_extra .= sprintf(" AND (MATCH(`item`.`body`) AGAINST ('".'"%s"'."' in boolean mode) or MATCH(tag) AGAINST ('".'"%s"'."' in boolean mode)) ",
-                                       dbesc(protect_sprintf($search)),
-                                       dbesc(protect_sprintf($search))
-                               );
+
+               if(strpos($search,'#') === 0) {
+                       $tag = true;
+                       $search = substr($search,1);
+               }
+
+               if (get_config('system','only_tag_search'))
+                       $tag = true;
+
+               if($tag) {
+                       //$sql_extra = sprintf(" AND `term`.`term` = '%s' AND `term`.`otype` = %d AND `term`.`type` = %d ",
+                       //              dbesc(protect_sprintf($search)), intval(TERM_OBJ_POST), intval(TERM_HASHTAG));
+                       //$sql_table = "`term` LEFT JOIN `item` ON `item`.`id` = `term`.`oid` AND `item`.`uid` = `term`.`uid` ";
+
+                       //$sql_order = "`term`.`tid`";
+
+                       $sql_extra = "";
+
+                       $sql_table = sprintf("`item` INNER JOIN (SELECT `oid` FROM `term` WHERE `term` = '%s' AND `otype` = %d AND `type` = %d AND `uid` = %d ORDER BY `tid` DESC) AS `term` ON `item`.`id` = `term`.`oid` ",
+                                       dbesc(protect_sprintf($search)), intval(TERM_OBJ_POST), intval(TERM_HASHTAG), intval(local_user()));
+
+                       $sql_order = "`item`.`received`";
+                       $order_mode = "received";
                } else {
-                       $sql_extra .= sprintf(" AND ( `item`.`body` like '%s' OR `item`.`tag` like '%s' ) ",
-                                       dbesc(protect_sprintf('%' . $search . '%')),
-                                       dbesc(protect_sprintf('%]' . $search . '[%'))
-                       );
+                       if (get_config('system','use_fulltext_engine'))
+                               $sql_extra = sprintf(" AND MATCH (`item`.`body`, `item`.`title`) AGAINST ('%s' in boolean mode) ", dbesc(protect_sprintf($search)));
+                       else
+                               $sql_extra = sprintf(" AND `item`.`body` REGEXP '%s' ", dbesc(protect_sprintf(preg_quote($search))));
+
+                       $sql_order = "`item`.`received`";
+                       $order_mode = "received";
                }
        }
        if(strlen($file)) {
@@ -624,19 +694,11 @@ function network_content(&$a, $update = 0) {
                $myurl = substr($myurl,strpos($myurl,'://')+3);
                $myurl = str_replace('www.','',$myurl);
                $diasp_url = str_replace('/profile/','/u/',$myurl);
-               if (get_config('system','use_fulltext_engine'))
-                       $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))) ",
-                               dbesc(protect_sprintf($myurl)),
-                               dbesc(protect_sprintf($myurl)),
-                               dbesc(protect_sprintf($diasp_url))
-                       );
-               else
-                       $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' )) ",
-                               dbesc(protect_sprintf('%' . $myurl)),
-                               dbesc(protect_sprintf('%' . $myurl . ']%')),
-                               dbesc(protect_sprintf('%' . $diasp_url . ']%'))
-                       );
 
+               $sql_extra .= sprintf(" AND `item`.`parent` IN (SELECT distinct(`parent`) from item where `author-link` IN ('https://%s', 'http://%s') OR `mention`)",
+                       dbesc(protect_sprintf($myurl)),
+                       dbesc(protect_sprintf($myurl))
+               );
        }
 
        if($update) {
@@ -646,9 +708,9 @@ function network_content(&$a, $update = 0) {
 
        }
        else {
-               if(! get_pconfig(local_user(),'system','alt_pager')) {
+               if( (! get_config('alt_pager', 'global')) && (! get_pconfig(local_user(),'system','alt_pager')) ) {
                        $r = q("SELECT COUNT(*) AS `total`
-                               FROM `item` LEFT JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
+                               FROM $sql_table LEFT JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
                                WHERE `item`.`uid` = %d AND `item`.`visible` = 1 AND `item`.`deleted` = 0
                                AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
                                $sql_extra2 $sql_extra3
@@ -661,8 +723,17 @@ function network_content(&$a, $update = 0) {
                        }
                }
 
-               $itemspage_network = get_pconfig(local_user(),'system','itemspage_network');
-               $itemspage_network = ((intval($itemspage_network)) ? $itemspage_network : 40);
+               //  check if we serve a mobile device and get the user settings 
+               //  accordingly
+               if ($a->is_mobile) { 
+                   $itemspage_network = get_pconfig(local_user(),'system','itemspage_mobile_network');
+                   $itemspage_network = ((intval($itemspage_network)) ? $itemspage_network : 20);
+               } else { 
+                   $itemspage_network = get_pconfig(local_user(),'system','itemspage_network');
+                   $itemspage_network = ((intval($itemspage_network)) ? $itemspage_network : 40);
+               }
+               //  now that we have the user settings, see if the theme forces 
+               //  a maximum item number which is lower then the user choice
                if(($a->force_max_items > 0) && ($a->force_max_items < $itemspage_network))
                        $itemspage_network = $a->force_max_items;
 
@@ -679,11 +750,10 @@ function network_content(&$a, $update = 0) {
                        `contact`.`name`, `contact`.`photo`, `contact`.`url`, `contact`.`rel`, `contact`.`writable`,
                        `contact`.`network`, `contact`.`thumb`, `contact`.`dfrn-id`, `contact`.`self`,
                        `contact`.`id` AS `cid`, `contact`.`uid` AS `contact-uid`
-                       FROM `item`, `contact`
+                       FROM $sql_table LEFT JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
                        WHERE `item`.`uid` = %d AND `item`.`visible` = 1 
                        AND `item`.`deleted` = 0 and `item`.`moderated` = 0
                        $simple_update
-                       AND `contact`.`id` = `item`.`contact-id`
                        AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
                        $sql_extra $sql_nets
                        ORDER BY `item`.`received` DESC $pager_sql ",
@@ -697,16 +767,27 @@ function network_content(&$a, $update = 0) {
                // Normal conversation view
 
 
-               if($order === 'post')
-                               $ordering = "`created`";
-               else
-                               $ordering = "`commented`";
+               if($order === 'post') {
+                       $ordering = "`created`";
+                       if ($sql_order == "")
+                               $order_mode = "created";
+               } else {
+                       $ordering = "`commented`";
+                       if ($sql_order == "")
+                               $order_mode = "commented";
+               }
+
+               if ($sql_order == "")
+                       $sql_order = "`item`.$ordering";
+
+               if (($_GET["offset"] != ""))
+                       $sql_extra3 .= sprintf(" AND $sql_order <= '%s'", dbesc($_GET["offset"]));
 
                // Fetch a page full of parent items for this page
 
                if($update) {
-                       $r = q("SELECT `parent` AS `item_id`, `contact`.`uid` AS `contact_uid`
-                               FROM `item` LEFT JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
+                       $r = q("SELECT `item`.`parent` AS `item_id`, `contact`.`uid` AS `contact_uid`
+                               FROM $sql_table LEFT JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
                                WHERE `item`.`uid` = %d AND `item`.`visible` = 1 AND
                                (`item`.`deleted` = 0 OR item.verb = '" . ACTIVITY_LIKE ."' OR item.verb = '" . ACTIVITY_DISLIKE . "')
                                and `item`.`moderated` = 0 and `item`.`unseen` = 1
@@ -717,12 +798,12 @@ function network_content(&$a, $update = 0) {
                }
                else {
                        $r = q("SELECT `item`.`id` AS `item_id`, `contact`.`uid` AS `contact_uid`
-                               FROM `item` LEFT JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
+                               FROM $sql_table LEFT JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
                                WHERE `item`.`uid` = %d AND `item`.`visible` = 1 AND `item`.`deleted` = 0
                                AND `item`.`moderated` = 0 AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
                                AND `item`.`parent` = `item`.`id`
                                $sql_extra3 $sql_extra $sql_nets
-                               ORDER BY `item`.$ordering DESC $pager_sql ",
+                               ORDER BY $sql_order DESC $pager_sql ",
                                intval(local_user())
                        );
                }
@@ -731,42 +812,63 @@ function network_content(&$a, $update = 0) {
 
                $parents_arr = array();
                $parents_str = '';
+               $date_offset = "";
 
                if(count($r)) {
                        foreach($r as $rr)
                                if(! in_array($rr['item_id'],$parents_arr))
                                        $parents_arr[] = $rr['item_id'];
-                       $parents_str = implode(', ', $parents_arr);
 
-                       $items = q("SELECT `item`.*, `item`.`id` AS `item_id`,
-                               `contact`.`name`, `contact`.`photo`, `contact`.`url`, `contact`.`alias`, `contact`.`rel`, `contact`.`writable`,
-                               `contact`.`network`, `contact`.`thumb`, `contact`.`dfrn-id`, `contact`.`self`,
-                               `contact`.`id` AS `cid`, `contact`.`uid` AS `contact-uid`
-                               FROM `item`, `contact`
-                               WHERE `item`.`uid` = %d AND `item`.`visible` = 1 AND `item`.`deleted` = 0
-                               AND `item`.`moderated` = 0 AND `contact`.`id` = `item`.`contact-id`
-                               AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
-                               AND `item`.`parent` IN ( %s )
-                               $sql_extra ",
-                               intval(local_user()),
-                               dbesc($parents_str)
-                       );
+                       //$parents_str = implode(', ', $parents_arr);
+
+                       // splitted into separate queries to avoid the problem with very long threads
+                       // so always the last X comments are loaded
+                       // This problem can occur expecially with imported facebook posts
+                       $max_comments = get_config("system", "max_comments");
+                       if ($max_comments == 0)
+                               $max_comments = 1000;
+
+                       $items = array();
 
+                       foreach ($parents_arr AS $parents_str) {
+
+                               $thread_items = q("SELECT `item`.*, `item`.`id` AS `item_id`,
+                                       `contact`.`name`, `contact`.`photo`, `contact`.`url`, `contact`.`alias`, `contact`.`rel`, `contact`.`writable`,
+                                       `contact`.`network`, `contact`.`thumb`, `contact`.`dfrn-id`, `contact`.`self`,
+                                       `contact`.`id` AS `cid`, `contact`.`uid` AS `contact-uid`
+                                       FROM $sql_table LEFT JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
+                                       WHERE `item`.`uid` = %d AND `item`.`visible` = 1 AND `item`.`deleted` = 0
+                                       AND `item`.`moderated` = 0
+                                       AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
+                                       AND `item`.`parent` IN ( %s )
+                                       $sql_extra ORDER BY `item`.`commented` DESC LIMIT %d",
+                                       intval(local_user()),
+                                       dbesc($parents_str),
+                                       intval($max_comments + 1)
+                               );
+                               $items = array_merge($items, $thread_items);
+                       }
                        $items = conv_sort($items,$ordering);
 
                } else {
                        $items = array();
                }
 
+               if ($_GET["offset"] == "")
+                       $date_offset = $items[0][$order_mode];
+               else
+                       $date_offset = $_GET["offset"];
+
+               $a->page_offset = $date_offset;
+
                if($parents_str)
                        $update_unseen = ' WHERE uid = ' . intval(local_user()) . ' AND unseen = 1 AND parent IN ( ' . dbesc($parents_str) . ' )';
        }
 
-
        // We aren't going to try and figure out at the item, group, and page
        // level which items you've seen and which you haven't. If you're looking
        // at the top level network page just mark everything seen. 
-       
+
 
 // The $update_unseen is a bit unreliable if you have stuff coming into your stream from a new contact - 
 // and other feeds that bring in stuff from the past. One can't find it all. 
@@ -777,7 +879,7 @@ function network_content(&$a, $update = 0) {
 // on they just get buried deeper. It has happened to me a couple of times also.
 
        if((! $group) && (! $cid) && (! $star)) {
-               $r = q("UPDATE `item` SET `unseen` = 0 
+               $r = q("UPDATE `item` SET `unseen` = 0
                        WHERE `unseen` = 1 AND `uid` = %d",
                        intval(local_user())
                );
@@ -794,14 +896,15 @@ function network_content(&$a, $update = 0) {
 
        $o .= conversation($a,$items,$mode,$update);
 
-       if(! $update) {
-               if(! get_pconfig(local_user(),'system','alt_pager')) {
-                       $o .= paginate($a);
+       if(!$update) {
+               if( get_config('alt_pager', 'global') || get_pconfig(local_user(),'system','alt_pager') ) {
+                       $o .= alt_pager($a,count($items));
                }
                else {
-                       $o .= alt_pager($a,count($items));
+                       $o .= paginate($a);
                }
        }
 
        return $o;
 }
+