]> git.mxchange.org Git - friendica.git/blobdiff - mod/network.php
Merge pull request #4243 from MrPetovan/task/switch-to-array-new-style
[friendica.git] / mod / network.php
index 81e6754c235595faaef302d12030794c4d463559..e9d60fd5ca97df1430b57b8068b3a4e736dd8a16 100644 (file)
@@ -5,15 +5,17 @@
 use Friendica\App;
 use Friendica\Content\Feature;
 use Friendica\Content\ForumManager;
+use Friendica\Content\Widget;
 use Friendica\Core\System;
 use Friendica\Core\Config;
 use Friendica\Core\PConfig;
 use Friendica\Database\DBM;
 use Friendica\Model\Contact;
 use Friendica\Model\Group;
+use Friendica\Model\Profile;
+use Friendica\Module\Login;
 
 require_once 'include/conversation.php';
-require_once 'include/contact_widgets.php';
 require_once 'include/items.php';
 require_once 'include/acl_selectors.php';
 
@@ -30,13 +32,13 @@ function network_init(App $a) {
        }
 
        if (x($_GET, 'save')) {
-               $exists = dba::exists('search', array('uid' => local_user(), 'term' => $search));
+               $exists = dba::exists('search', ['uid' => local_user(), 'term' => $search]);
                if (!$exists) {
-                       dba::insert('search', array('uid' => local_user(), 'term' => $search));
+                       dba::insert('search', ['uid' => local_user(), 'term' => $search]);
                }
        }
        if (x($_GET, 'remove')) {
-               dba::delete('search', array('uid' => local_user(), 'term' => $search));
+               dba::delete('search', ['uid' => local_user(), 'term' => $search]);
        }
 
        $is_a_date_query = false;
@@ -62,7 +64,7 @@ function network_init(App $a) {
        }
 
        // convert query string to array. remove friendica args
-       $query_array = array();
+       $query_array = [];
        $query_string = str_replace($a->cmd."?", "", $a->query_string);
        parse_str($query_string, $query_array);
        array_shift($query_array);
@@ -81,7 +83,7 @@ function network_init(App $a) {
                $remember_group = ($sel_groups === false && $last_sel_groups && $last_sel_groups != 0);
 
                $net_baseurl = '/network';
-               $net_args = array();
+               $net_args = [];
 
                if ($remember_group) {
                        $net_baseurl .= '/' . $last_sel_groups; // Note that the group number must come before the "/new" tab selection
@@ -94,7 +96,7 @@ function network_init(App $a) {
                        // last selected tab is _not_ '/network?f=&order=comment'.
                        // and this isn't a date query
 
-                       $tab_baseurls = array(
+                       $tab_baseurls = [
                                '',             //all
                                '',             //postord
                                '',             //conv
@@ -102,8 +104,8 @@ function network_init(App $a) {
                                '',             //starred
                                '',             //bookmarked
                                '',             //spam
-                       );
-                       $tab_args = array(
+                       ];
+                       $tab_args = [
                                'f=&order=comment',     //all
                                'f=&order=post',        //postord
                                'f=&conv=1',            //conv
@@ -111,7 +113,7 @@ function network_init(App $a) {
                                'f=&star=1',            //starred
                                'f=&bmark=1',           //bookmarked
                                'f=&spam=1',            //spam
-                       );
+                       ];
 
                        $k = array_search('active', $last_sel_tabs);
 
@@ -119,7 +121,7 @@ function network_init(App $a) {
                                $net_baseurl .= $tab_baseurls[$k];
 
                                // parse out tab queries
-                               $dest_qa = array();
+                               $dest_qa = [];
                                $dest_qs = $tab_args[$k];
                                parse_str($dest_qs, $dest_qa);
                                $net_args = array_merge($net_args, $dest_qa);
@@ -157,12 +159,12 @@ function network_init(App $a) {
                $a->page['aside'] = '';
        }
 
-       $a->page['aside'] .= (Feature::isEnabled(local_user(),'groups') ? Group::sidebarWidget('network/0','network','standard',$group_id) : '');
+       $a->page['aside'] .= (Feature::isEnabled(local_user(), 'groups') ? Group::sidebarWidget('network/0', 'network', 'standard', $group_id) : '');
        $a->page['aside'] .= (Feature::isEnabled(local_user(), 'forumlist_widget') ? ForumManager::widget(local_user(), $cid) : '');
-       $a->page['aside'] .= posted_date_widget('network',local_user(),false);
-       $a->page['aside'] .= networks_widget('network',(x($_GET, 'nets') ? $_GET['nets'] : ''));
+       $a->page['aside'] .= posted_date_widget('network', local_user(),false);
+       $a->page['aside'] .= Widget::networks('network', (x($_GET, 'nets') ? $_GET['nets'] : ''));
        $a->page['aside'] .= saved_searches($search);
-       $a->page['aside'] .= fileas_widget('network',(x($_GET, 'file') ? $_GET['file'] : ''));
+       $a->page['aside'] .= Widget::fileAs('network', (x($_GET, 'file') ? $_GET['file'] : ''));
 }
 
 function saved_searches($search) {
@@ -186,26 +188,26 @@ function saved_searches($search) {
 
        $o = '';
 
-       $terms = dba::select('search', array('id', 'term'), array('uid' => local_user()));
-       $saved = array();
+       $terms = dba::select('search', ['id', 'term'], ['uid' => local_user()]);
+       $saved = [];
 
        while ($rr = dba::fetch($terms)) {
-               $saved[] = array(
+               $saved[] = [
                        'id'          => $rr['id'],
                        'term'        => $rr['term'],
                        'encodedterm' => urlencode($rr['term']),
                        'delete'      => t('Remove term'),
                        'selected'    => ($search==$rr['term']),
-               );
+               ];
        }
 
        $tpl = get_markup_template("saved_searches_aside.tpl");
-       $o = replace_macros($tpl, array(
+       $o = replace_macros($tpl, [
                '$title'     => t('Saved Searches'),
                '$add'       => t('add'),
                '$searchbox' => search($search,'netsearch-box',$srchurl,true),
                '$saved'     => $saved,
-       ));
+       ]);
 
        return $o;
 }
@@ -273,7 +275,7 @@ function network_query_get_sel_tab(App $a) {
                }
        }
 
-       return array($no_active, $all_active, $postord_active, $conv_active, $new_active, $starred_active, $bookmarked_active, $spam_active);
+       return [$no_active, $all_active, $postord_active, $conv_active, $new_active, $starred_active, $bookmarked_active, $spam_active];
 }
 
 /**
@@ -348,7 +350,7 @@ function networkSetSeen($condition) {
        $unseen = dba::exists('item', $condition);
 
        if ($unseen) {
-               $r = dba::update('item', array('unseen' => false), $condition);
+               $r = dba::update('item', ['unseen' => false], $condition);
        }
 }
 
@@ -380,12 +382,11 @@ function networkConversation($a, $items, $mode, $update) {
 
 function network_content(App $a, $update = 0) {
        if (!local_user()) {
-               $_SESSION['return_url'] = $a->query_string;
-               return login(false);
+               return Login::form();
        }
 
        /// @TODO Is this really necessary? $a is already available to hooks
-       $arr = array('query' => $a->query_string);
+       $arr = ['query' => $a->query_string];
        call_hooks('network_content_init', $arr);
 
        $nouveau = false;
@@ -441,7 +442,7 @@ function networkFlatView(App $a, $update = 0) {
 
                nav_set_selected('network');
 
-               $x = array(
+               $x = [
                        'is_owner' => true,
                        'allow_location' => $a->user['allow_location'],
                        'default_location' => $a->user['default-location'],
@@ -454,15 +455,14 @@ function networkFlatView(App $a, $update = 0) {
                        'bang'  => '',
                        'visitor' => 'block',
                        'profile_uid' => local_user(),
-                       'acl_data' => construct_acl_data($a, $a->user), // For non-Javascript ACL selector
                        'content' => '',
-               );
+               ];
 
-               $o .= status_editor($a,$x);
+               $o .= status_editor($a, $x);
 
-               if (!Config::get('theme','hide_eventlist')) {
-                       $o .= get_birthdays();
-                       $o .= get_events();
+               if (!Config::get('theme', 'hide_eventlist')) {
+                       $o .= Profile::getBirthdays();
+                       $o .= Profile::getEvents();
                }
        }
 
@@ -483,7 +483,7 @@ function networkFlatView(App $a, $update = 0) {
                intval($_SESSION['uid'])
        );
 
-       $condition = array('unseen' => true, 'uid' => local_user());
+       $condition = ['unseen' => true, 'uid' => local_user()];
        networkSetSeen($condition);
 
        $mode = 'network-new';
@@ -518,7 +518,7 @@ function networkThreadedView(App $a, $update = 0) {
 
        $datequery = $datequery2 = '';
 
-       $group = 0;
+       $gid = 0;
 
        if ($a->argc > 1) {
                for ($x = 1; $x < $a->argc; $x ++) {
@@ -530,8 +530,8 @@ function networkThreadedView(App $a, $update = 0) {
                                        $_GET['order'] = 'post';
                                }
                        } elseif (intval($a->argv[$x])) {
-                               $group = intval($a->argv[$x]);
-                               $def_acl = array('allow_gid' => '<' . $group . '>');
+                               $gid = intval($a->argv[$x]);
+                               $def_acl = ['allow_gid' => '<' . $gid . '>'];
                        }
                }
        }
@@ -546,18 +546,18 @@ function networkThreadedView(App $a, $update = 0) {
        $nets = ((x($_GET,'nets')) ? $_GET['nets'] : '');
 
        if ($cid) {
-               $def_acl = array('allow_cid' => '<' . intval($cid) . '>');
+               $def_acl = ['allow_cid' => '<' . intval($cid) . '>'];
        }
 
        if ($nets) {
-               $r = dba::select('contact', array('id'), array('uid' => local_user(), 'network' => $nets), array('self' => false));
+               $r = dba::select('contact', ['id'], ['uid' => local_user(), 'network' => $nets], ['self' => false]);
 
                $str = '';
                while ($rr = dba::fetch($r)) {
                        $str .= '<' . $rr['id'] . '>';
                }
                if (strlen($str)) {
-                       $def_acl = array('allow_cid' => $str);
+                       $def_acl = ['allow_cid' => $str];
                }
        }
        PConfig::set(local_user(), 'network.view', 'net.selected', ($nets ? $nets : 'all'));
@@ -566,8 +566,8 @@ function networkThreadedView(App $a, $update = 0) {
                $tabs = network_tabs($a);
                $o .= $tabs;
 
-               if ($group) {
-                       if (($t = Contact::getOStatusCountByGroupId($group)) && !PConfig::get(local_user(), 'system', 'nowarn_insecure')) {
+               if ($gid) {
+                       if (($t = Contact::getOStatusCountByGroupId($gid)) && !PConfig::get(local_user(), 'system', 'nowarn_insecure')) {
                                notice(tt("Warning: This group contains %s member from a network that doesn't allow non public messages.",
                                                "Warning: This group contains %s members from a network that doesn't allow non public messages.",
                                                $t) . EOL);
@@ -581,8 +581,8 @@ function networkThreadedView(App $a, $update = 0) {
 
                if ($cid) {
                        // If $cid belongs to a communitity forum or a privat goup,.add a mention to the status editor
-                       $condition = array("`id` = ? AND (`forum` OR `prv`)", $cid);
-                       $contact = dba::select('contact', array('addr', 'nick'), $condition, array('limit' => 1));
+                       $condition = ["`id` = ? AND (`forum` OR `prv`)", $cid];
+                       $contact = dba::selectFirst('contact', ['addr', 'nick'], $condition);
                        if (DBM::is_result($contact)) {
                                if ($contact["addr"] != '') {
                                        $content = "!".$contact["addr"];
@@ -592,25 +592,23 @@ function networkThreadedView(App $a, $update = 0) {
                        }
                }
 
-               $x = array(
+               $x = [
                        'is_owner' => true,
                        'allow_location' => $a->user['allow_location'],
                        'default_location' => $a->user['default-location'],
                        'nickname' => $a->user['nickname'],
-                       'lockstate'=> ((($group) || ($cid) || ($nets) || (is_array($a->user) &&
+                       'lockstate'=> ((($gid) || ($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), true),
-                       'bang'  => (($group || $cid || $nets) ? '!' : ''),
+                       'acl'   => populate_acl((($gid || $cid || $nets) ? $def_acl : $a->user), true),
+                       'bang'  => (($gid || $cid || $nets) ? '!' : ''),
                        'visitor' => 'block',
                        'profile_uid' => local_user(),
-                       'acl_data' => construct_acl_data($a, $a->user), // For non-Javascript ACL selector
                        'content' => $content,
-               );
-
-               $o .= status_editor($a,$x);
+               ];
 
+               $o .= status_editor($a, $x);
        }
 
        // We don't have to deal with ACLs on this page. You're looking at everything
@@ -632,65 +630,67 @@ function networkThreadedView(App $a, $update = 0) {
                $sql_post_table = " INNER JOIN `thread` ON `thread`.`iid` = `item`.`parent`";
        }
 
-       $sql_nets = (($nets) ? sprintf(" and $sql_table.`network` = '%s' ", dbesc($nets)) : '');
+       $sql_nets = (($nets) ? sprintf(" AND $sql_table.`network` = '%s' ", dbesc($nets)) : '');
+       $sql_tag_nets = (($nets) ? sprintf(" AND `item`.`network` = '%s' ", dbesc($nets)) : '');
 
-       if ($group) {
-               $r = dba::select('group', array('name'), array('id' => $group, 'uid' => $_SESSION['uid']), array('limit' => 1));
-               if (!DBM::is_result($r)) {
-                       if ($update)
+       if ($gid) {
+               $group = dba::selectFirst('group', ['name'], ['id' => $gid, 'uid' => $_SESSION['uid']]);
+               if (!DBM::is_result($group)) {
+                       if ($update) {
                                killme();
+                       }
                        notice(t('No such group') . EOL);
                        goaway('network/0');
                        // NOTREACHED
                }
 
-               $contacts = Group::expand(array($group));
+               $contacts = Group::expand([$gid]);
 
                if ((is_array($contacts)) && count($contacts)) {
                        $contact_str_self = "";
 
                        $contact_str = implode(',',$contacts);
-                       $self = dba::select('contact', array('id'), array('uid' => $_SESSION['uid'], 'self' => true), array('limit' => 1));
+                       $self = dba::selectFirst('contact', ['id'], ['uid' => $_SESSION['uid'], 'self' => true]);
                        if (DBM::is_result($self)) {
                                $contact_str_self = $self["id"];
                        }
 
                        $sql_post_table .= " INNER JOIN `item` AS `temp1` ON `temp1`.`id` = ".$sql_table.".".$sql_parent;
                        $sql_extra3 .= " AND (`thread`.`contact-id` IN ($contact_str) ";
-                       $sql_extra3 .= " OR (`thread`.`contact-id` = '$contact_str_self' AND `temp1`.`allow_gid` LIKE '".protect_sprintf('%<'.intval($group).'>%')."' AND `temp1`.`private`))";
+                       $sql_extra3 .= " OR (`thread`.`contact-id` = '$contact_str_self' AND `temp1`.`allow_gid` LIKE '".protect_sprintf('%<'.intval($gid).'>%')."' AND `temp1`.`private`))";
                } else {
                        $sql_extra3 .= " AND false ";
                        info(t('Group is empty'));
                }
 
-               $o = replace_macros(get_markup_template("section_title.tpl"),array(
-                       '$title' => t('Group: %s', $r['name'])
-               )) . $o;
+               $o = replace_macros(get_markup_template("section_title.tpl"),[
+                       '$title' => t('Group: %s', $group['name'])
+               ]) . $o;
 
        } elseif ($cid) {
-               $fields = array('id', 'name', 'network', 'writable', 'nurl',
-                               'forum', 'prv', 'contact-type', 'addr', 'thumb', 'location');
-               $condition = array("`id` = ? AND (NOT `blocked` OR `pending`)", $cid);
-               $r = dba::select('contact', $fields, $condition, array('limit' => 1));
-               if (DBM::is_result($r)) {
+               $fields = ['id', 'name', 'network', 'writable', 'nurl',
+                               'forum', 'prv', 'contact-type', 'addr', 'thumb', 'location'];
+               $condition = ["`id` = ? AND (NOT `blocked` OR `pending`)", $cid];
+               $contact = dba::selectFirst('contact', $fields, $condition);
+               if (DBM::is_result($contact)) {
                        $sql_extra = " AND ".$sql_table.".`contact-id` = ".intval($cid);
 
-                       $entries[0] = array(
+                       $entries[0] = [
                                'id' => 'network',
-                               'name' => htmlentities($r['name']),
-                               'itemurl' => (($r['addr']) ? ($r['addr']) : ($r['nurl'])),
-                               'thumb' => proxy_url($r['thumb'], false, PROXY_SIZE_THUMB),
-                               'details' => $r['location'],
-                       );
+                               'name' => htmlentities($contact['name']),
+                               'itemurl' => defaults($contact, 'addr', $contact['nurl']),
+                               'thumb' => proxy_url($contact['thumb'], false, PROXY_SIZE_THUMB),
+                               'details' => $contact['location'],
+                       ];
 
-                       $entries[0]["account_type"] = Contact::getAccountType($r);
+                       $entries[0]["account_type"] = Contact::getAccountType($contact);
 
-                       $o = replace_macros(get_markup_template("viewcontact_template.tpl"),array(
+                       $o = replace_macros(get_markup_template("viewcontact_template.tpl"),[
                                'contacts' => $entries,
                                'id' => 'network',
-                       )) . $o;
+                       ]) . $o;
 
-                       if ($r['network'] === NETWORK_OSTATUS && $r['writable'] && !PConfig::get(local_user(),'system','nowarn_insecure')) {
+                       if ($contact['network'] === NETWORK_OSTATUS && $contact['writable'] && !PConfig::get(local_user(),'system','nowarn_insecure')) {
                                notice(t('Private messages to this person are at risk of public disclosure.') . EOL);
                        }
 
@@ -701,9 +701,9 @@ function networkThreadedView(App $a, $update = 0) {
                }
        }
 
-       if (!$group && !$cid && !$update && !Config::get('theme','hide_eventlist')) {
-               $o .= get_birthdays();
-               $o .= get_events();
+       if (!$gid && !$cid && !$update && !Config::get('theme', 'hide_eventlist')) {
+               $o .= Profile::getBirthdays();
+               $o .= Profile::getEvents();
        }
 
        if ($datequery) {
@@ -738,36 +738,43 @@ function networkThreadedView(App $a, $update = 0) {
        }
 
        if (x($_GET, 'offset')) {
-               $sql_extra3 .= sprintf(" AND $sql_order <= '%s'", dbesc($_GET["offset"]));
+               $sql_range = sprintf(" AND $sql_order <= '%s'", dbesc($_GET["offset"]));
+       } else {
+               $sql_range = '';
        }
 
        $pager_sql = networkPager($a, $update);
 
+       $last_date = '';
+
        switch ($order_mode) {
                case 'received':
                        if ($last_received != '') {
-                               $sql_extra3 .= sprintf(" AND $sql_table.`received` < '%s'", dbesc($last_received));
+                               $last_date = $last_received;
+                               $sql_range .= sprintf(" AND $sql_table.`received` < '%s'", dbesc($last_received));
                                $a->set_pager_page(1);
                                $pager_sql = sprintf(" LIMIT %d, %d ",intval($a->pager['start']), intval($a->pager['itemspage']));
                        }
                        break;
                case 'commented':
                        if ($last_commented != '') {
-                               $sql_extra3 .= sprintf(" AND $sql_table.`commented` < '%s'", dbesc($last_commented));
+                               $last_date = $last_commented;
+                               $sql_range .= sprintf(" AND $sql_table.`commented` < '%s'", dbesc($last_commented));
                                $a->set_pager_page(1);
                                $pager_sql = sprintf(" LIMIT %d, %d ",intval($a->pager['start']), intval($a->pager['itemspage']));
                        }
                        break;
                case 'created':
                        if ($last_created != '') {
-                               $sql_extra3 .= sprintf(" AND $sql_table.`created` < '%s'", dbesc($last_created));
+                               $last_date = $last_created;
+                               $sql_range .= sprintf(" AND $sql_table.`created` < '%s'", dbesc($last_created));
                                $a->set_pager_page(1);
                                $pager_sql = sprintf(" LIMIT %d, %d ",intval($a->pager['start']), intval($a->pager['itemspage']));
                        }
                        break;
                case 'id':
                        if (($last_id > 0) && ($sql_table == "`thread`")) {
-                               $sql_extra3 .= sprintf(" AND $sql_table.`iid` < '%s'", dbesc($last_id));
+                               $sql_range .= sprintf(" AND $sql_table.`iid` < '%s'", dbesc($last_id));
                                $a->set_pager_page(1);
                                $pager_sql = sprintf(" LIMIT %d, %d ",intval($a->pager['start']), intval($a->pager['itemspage']));
                        }
@@ -781,35 +788,74 @@ function networkThreadedView(App $a, $update = 0) {
                } else {
                        $sql_extra4 = "";
                }
-
-               $r = q("SELECT `item`.`parent` AS `item_id`, `item`.`network` AS `item_network`, `contact`.`uid` AS `contact_uid`
+               $r = q("SELECT `item`.`parent` AS `item_id`, `item`.`network` AS `item_network`, `contact`.`uid` AS `contact_uid`, $sql_order AS `order_date`
                        FROM $sql_table $sql_post_table INNER JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
                        AND (NOT `contact`.`blocked` OR `contact`.`pending`)
                        WHERE `item`.`uid` = %d AND `item`.`visible` AND NOT `item`.`deleted` $sql_extra4
                        AND NOT `item`.`moderated` AND `item`.`unseen`
-                       $sql_extra3 $sql_extra $sql_nets
-                       ORDER BY `item_id` DESC LIMIT 100",
+                       $sql_extra3 $sql_extra $sql_range $sql_nets
+                       ORDER BY `order_date` DESC LIMIT 100",
                        intval(local_user())
                );
        } else {
-               $r = q("SELECT `thread`.`iid` AS `item_id`, `thread`.`network` AS `item_network`, `contact`.`uid` AS `contact_uid`
+               $r = q("SELECT `thread`.`iid` AS `item_id`, `thread`.`network` AS `item_network`, `contact`.`uid` AS `contact_uid`, $sql_order AS `order_date`
                        FROM $sql_table $sql_post_table STRAIGHT_JOIN `contact` ON `contact`.`id` = `thread`.`contact-id`
                        AND (NOT `contact`.`blocked` OR `contact`.`pending`)
                        WHERE `thread`.`uid` = %d AND `thread`.`visible` AND NOT `thread`.`deleted`
                        AND NOT `thread`.`moderated`
-                       $sql_extra2 $sql_extra3 $sql_extra $sql_nets
+                       $sql_extra2 $sql_extra3 $sql_range $sql_extra $sql_nets
                        ORDER BY $sql_order DESC $pager_sql",
                        intval(local_user())
                );
        }
 
+       // Only show it when unfiltered (no groups, no networks, ...)
+       if (Config::get('system', 'comment_public') && in_array($nets, ['', NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_OSTATUS])
+               && (strlen($sql_extra . $sql_extra2 . $sql_extra3) == 0)) {
+
+               if (DBM::is_result($r)) {
+                       $top_limit = current($r)['order_date'];
+                       $bottom_limit = end($r)['order_date'];
+               } else {
+                       $top_limit = datetime_convert();
+                       $bottom_limit = datetime_convert();
+               }
+
+               // When checking for updates we need to fetch from the newest date to the newest date before
+               if ($update && !empty($_SESSION['network_last_date']) && ($bottom_limit > $_SESSION['network_last_date'])) {
+                       $bottom_limit = $_SESSION['network_last_date'];
+               }
+               $_SESSION['network_last_date'] = $top_limit;
+
+               if ($last_date > $top_limit) {
+                       $top_limit = $last_date;
+               } elseif ($a->pager['page'] == 1) {
+                       // Highest possible top limit when we are on the first page
+                       $top_limit = datetime_convert();
+               }
+
+               $items = dba::p("SELECT `item`.`id` AS `item_id`, `item`.`network` AS `item_network`, `contact`.`uid` AS `contact_uid` FROM `item`
+                       INNER JOIN (SELECT `oid` FROM `term` WHERE `term` IN
+                               (SELECT SUBSTR(`term`, 2) FROM `search` WHERE `uid` = ? AND `term` LIKE '#%') AND `otype` = ? AND `type` = ? AND `uid` = 0) AS `term`
+                       ON `item`.`id` = `term`.`oid`
+                       INNER JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
+                       WHERE `item`.`uid` = 0 AND `item`.$ordering < ? AND `item`.$ordering > ?".$sql_tag_nets,
+                       local_user(), TERM_OBJ_POST, TERM_HASHTAG, $top_limit, $bottom_limit);
+               $data = dba::inArray($items);
+
+               if (count($data) > 0) {
+                       logger('Tagged items: '.count($data).' - '.$bottom_limit." - ".$top_limit.' - '.local_user()); //$last_date);
+                       $r = array_merge($r, $data);
+               }
+       }
+
        // Then fetch all the children of the parents that are on this page
 
-       $parents_arr = array();
+       $parents_arr = [];
        $parents_str = '';
        $date_offset = "";
 
-       $items = array();
+       $items = [];
        if (DBM::is_result($r)) {
                foreach ($r as $rr) {
                        if (!in_array($rr['item_id'], $parents_arr)) {
@@ -828,10 +874,8 @@ function networkThreadedView(App $a, $update = 0) {
                }
 
                foreach ($parents_arr AS $parents) {
-                       $thread_items = dba::p(item_query() . " AND `item`.`uid` = ?
-                               AND `item`.`parent` = ?
+                       $thread_items = dba::p(item_query() . " AND `item`.`parent` = ?
                                ORDER BY `item`.`commented` DESC LIMIT " . intval($max_comments + 1),
-                               local_user(),
                                $parents
                        );
 
@@ -856,11 +900,11 @@ function networkThreadedView(App $a, $update = 0) {
        // 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.
 
-       if (!$group && !$cid && !$star) {
-               $condition = array('unseen' => true, 'uid' => local_user());
+       if (!$gid && !$cid && !$star) {
+               $condition = ['unseen' => true, 'uid' => local_user()];
                networkSetSeen($condition);
        } elseif ($parents_str) {
-               $condition = array("`uid` = ? AND `unseen` AND `parent` IN (" . dbesc($parents_str) . ")", local_user());
+               $condition = ["`uid` = ? AND `unseen` AND `parent` IN (" . dbesc($parents_str) . ")", local_user()];
                networkSetSeen($condition);
        }
 
@@ -892,80 +936,80 @@ function network_tabs(App $a)
        $cmd = $a->cmd;
 
        // tabs
-       $tabs = array(
-               array(
+       $tabs = [
+               [
                        'label' => t('Commented Order'),
                        'url'   => str_replace('/new', '', $cmd) . '?f=&order=comment' . ((x($_GET,'cid')) ? '&cid=' . $_GET['cid'] : ''),
                        'sel'   => $all_active,
                        'title' => t('Sort by Comment Date'),
                        'id'    => 'commented-order-tab',
                        'accesskey' => "e",
-               ),
-               array(
+               ],
+               [
                        'label' => t('Posted Order'),
                        'url'   => str_replace('/new', '', $cmd) . '?f=&order=post' . ((x($_GET,'cid')) ? '&cid=' . $_GET['cid'] : ''),
                        'sel'   => $postord_active,
                        'title' => t('Sort by Post Date'),
                        'id'    => 'posted-order-tab',
                        'accesskey' => "t",
-               ),
-       );
+               ],
+       ];
 
        if (Feature::isEnabled(local_user(),'personal_tab')) {
-               $tabs[] = array(
+               $tabs[] = [
                        'label' => t('Personal'),
                        'url'   => str_replace('/new', '', $cmd) . ((x($_GET,'cid')) ? '/?f=&cid=' . $_GET['cid'] : '/?f=') . '&conv=1',
                        'sel'   => $conv_active,
                        'title' => t('Posts that mention or involve you'),
                        'id'    => 'personal-tab',
                        'accesskey' => "r",
-               );
+               ];
        }
 
        if (Feature::isEnabled(local_user(),'new_tab')) {
-               $tabs[] = array(
+               $tabs[] = [
                        'label' => t('New'),
                        'url'   => 'network/new' . ((x($_GET,'cid')) ? '/?f=&cid=' . $_GET['cid'] : ''),
                        'sel'   => $new_active,
                        'title' => t('Activity Stream - by date'),
                        'id'    => 'activitiy-by-date-tab',
                        'accesskey' => "w",
-               );
+               ];
        }
 
        if (Feature::isEnabled(local_user(),'link_tab')) {
-               $tabs[] = array(
+               $tabs[] = [
                        'label' => t('Shared Links'),
                        'url'   => str_replace('/new', '', $cmd) . ((x($_GET,'cid')) ? '/?f=&cid=' . $_GET['cid'] : '/?f=') . '&bmark=1',
                        'sel'   => $bookmarked_active,
                        'title' => t('Interesting Links'),
                        'id'    => 'shared-links-tab',
                        'accesskey' => "b",
-               );
+               ];
        }
 
        if (Feature::isEnabled(local_user(),'star_posts')) {
-               $tabs[] = array(
+               $tabs[] = [
                        'label' => t('Starred'),
                        'url'   => str_replace('/new', '', $cmd) . ((x($_GET,'cid')) ? '/?f=&cid=' . $_GET['cid'] : '/?f=') . '&star=1',
                        'sel'   => $starred_active,
                        'title' => t('Favourite Posts'),
                        'id'    => 'starred-posts-tab',
                        'accesskey' => "m",
-               );
+               ];
        }
 
        // save selected tab, but only if not in file mode
        if (!x($_GET,'file')) {
-               PConfig::set(local_user(), 'network.view','tab.selected',array($all_active, $postord_active, $conv_active, $new_active, $starred_active, $bookmarked_active, $spam_active));
+               PConfig::set(local_user(), 'network.view','tab.selected',[$all_active, $postord_active, $conv_active, $new_active, $starred_active, $bookmarked_active, $spam_active]);
        }
 
-       $arr = array('tabs' => $tabs);
+       $arr = ['tabs' => $tabs];
        call_hooks('network_tabs', $arr);
 
        $tpl = get_markup_template('common_tabs.tpl');
 
-       return replace_macros($tpl, array('$tabs' => $arr['tabs']));
+       return replace_macros($tpl, ['$tabs' => $arr['tabs']]);
 
        // --- end item filter tabs
 }