4 * @file mod/network.php
8 use Friendica\Content\Feature;
9 use Friendica\Content\ForumManager;
10 use Friendica\Content\Nav;
11 use Friendica\Content\Widget;
12 use Friendica\Core\Acl;
13 use Friendica\Core\Addon;
14 use Friendica\Core\Config;
15 use Friendica\Core\L10n;
16 use Friendica\Core\PConfig;
17 use Friendica\Core\System;
18 use Friendica\Database\DBM;
19 use Friendica\Model\Contact;
20 use Friendica\Model\Group;
21 use Friendica\Model\Item;
22 use Friendica\Model\Profile;
23 use Friendica\Module\Login;
24 use Friendica\Util\DateTimeFormat;
26 require_once 'include/conversation.php';
27 require_once 'include/items.php';
28 require_once 'include/acl_selectors.php';
30 function network_init(App $a)
33 notice(L10n::t('Permission denied.') . EOL);
37 $search = (x($_GET, 'search') ? escape_tags($_GET['search']) : '');
39 if (($search != '') && !empty($_GET['submit'])) {
40 goaway('search?search=' . urlencode($search));
43 if (x($_GET, 'save')) {
44 $exists = dba::exists('search', ['uid' => local_user(), 'term' => $search]);
46 dba::insert('search', ['uid' => local_user(), 'term' => $search]);
49 if (x($_GET, 'remove')) {
50 dba::delete('search', ['uid' => local_user(), 'term' => $search]);
53 $is_a_date_query = false;
55 $group_id = (($a->argc > 1 && is_numeric($a->argv[1])) ? intval($a->argv[1]) : 0);
58 if (x($_GET, 'cid') && intval($_GET['cid']) != 0) {
60 $_GET['nets'] = 'all';
65 for ($x = 1; $x < $a->argc; $x ++) {
66 if (is_a_date_arg($a->argv[$x])) {
67 $is_a_date_query = true;
73 // convert query string to array. remove friendica args
75 $query_string = str_replace($a->cmd . '?', '', $a->query_string);
76 parse_str($query_string, $query_array);
77 array_shift($query_array);
79 // fetch last used network view and redirect if needed
80 if (!$is_a_date_query) {
81 $sel_nets = defaults($_GET, 'nets', false);
82 $sel_tabs = network_query_get_sel_tab($a);
83 $sel_groups = network_query_get_sel_group($a);
84 $last_sel_tabs = PConfig::get(local_user(), 'network.view', 'tab.selected');
86 $remember_tab = ($sel_tabs[0] === 'active' && is_array($last_sel_tabs) && $last_sel_tabs[0] !== 'active');
88 $net_baseurl = '/network';
91 if ($sel_groups !== false) {
92 $net_baseurl .= '/' . $sel_groups;
96 // redirect if current selected tab is '/network' and
97 // last selected tab is _not_ '/network?f=&order=comment'.
98 // and this isn't a date query
110 'f=&order=comment', //all
111 'f=&order=post', //postord
114 'f=&star=1', //starred
115 'f=&bmark=1', //bookmarked
119 $k = array_search('active', $last_sel_tabs);
122 $net_baseurl .= $tab_baseurls[$k];
124 // parse out tab queries
126 $dest_qs = $tab_args[$k];
127 parse_str($dest_qs, $dest_qa);
128 $net_args = array_merge($net_args, $dest_qa);
130 $remember_tab = false;
134 if ($sel_nets !== false) {
135 $net_args['nets'] = $sel_nets;
139 $net_args = array_merge($query_array, $net_args);
140 $net_queries = build_querystring($net_args);
142 $redir_url = ($net_queries ? $net_baseurl . '?' . $net_queries : $net_baseurl);
144 goaway(System::baseUrl() . $redir_url);
148 // If nets is set to all, unset it
149 if (x($_GET, 'nets') && $_GET['nets'] === 'all') {
150 unset($_GET['nets']);
153 if (!x($a->page, 'aside')) {
154 $a->page['aside'] = '';
157 $a->page['aside'] .= (Feature::isEnabled(local_user(), 'groups') ?
158 Group::sidebarWidget('network/0', 'network', 'standard', $group_id) : '');
159 $a->page['aside'] .= (Feature::isEnabled(local_user(), 'forumlist_widget') ? ForumManager::widget(local_user(), $cid) : '');
160 $a->page['aside'] .= posted_date_widget('network', local_user(), false);
161 $a->page['aside'] .= Widget::networks('network', (x($_GET, 'nets') ? $_GET['nets'] : ''));
162 $a->page['aside'] .= saved_searches($search);
163 $a->page['aside'] .= Widget::fileAs('network', (x($_GET, 'file') ? $_GET['file'] : ''));
166 function saved_searches($search)
168 if (!Feature::isEnabled(local_user(), 'savedsearch')) {
174 $srchurl = '/network?f='
175 . ((x($_GET, 'cid')) ? '&cid=' . $_GET['cid'] : '')
176 . ((x($_GET, 'star')) ? '&star=' . $_GET['star'] : '')
177 . ((x($_GET, 'bmark')) ? '&bmark=' . $_GET['bmark'] : '')
178 . ((x($_GET, 'conv')) ? '&conv=' . $_GET['conv'] : '')
179 . ((x($_GET, 'nets')) ? '&nets=' . $_GET['nets'] : '')
180 . ((x($_GET, 'cmin')) ? '&cmin=' . $_GET['cmin'] : '')
181 . ((x($_GET, 'cmax')) ? '&cmax=' . $_GET['cmax'] : '')
182 . ((x($_GET, 'file')) ? '&file=' . $_GET['file'] : '');
187 $terms = dba::select('search', ['id', 'term'], ['uid' => local_user()]);
190 while ($rr = dba::fetch($terms)) {
193 'term' => $rr['term'],
194 'encodedterm' => urlencode($rr['term']),
195 'delete' => L10n::t('Remove term'),
196 'selected' => ($search == $rr['term']),
200 $tpl = get_markup_template('saved_searches_aside.tpl');
201 $o = replace_macros($tpl, [
202 '$title' => L10n::t('Saved Searches'),
203 '$add' => L10n::t('add'),
204 '$searchbox' => search($search, 'netsearch-box', $srchurl, true),
212 * Return selected tab from query
215 * '/network' => $no_active = 'active'
216 * '/network?f=&order=comment' => $comment_active = 'active'
217 * '/network?f=&order=post' => $postord_active = 'active'
218 * '/network?f=&conv=1', => $conv_active = 'active'
219 * '/network/new', => $new_active = 'active'
220 * '/network?f=&star=1', => $starred_active = 'active'
221 * '/network?f=&bmark=1', => $bookmarked_active = 'active'
222 * '/network?f=&spam=1', => $spam_active = 'active'
224 * @return Array ($no_active, $comment_active, $postord_active, $conv_active, $new_active, $starred_active, $bookmarked_active, $spam_active);
226 function network_query_get_sel_tab(App $a)
229 $starred_active = '';
231 $bookmarked_active = '';
235 $postord_active = '';
237 if (($a->argc > 1 && $a->argv[1] === 'new') || ($a->argc > 2 && $a->argv[2] === 'new')) {
238 $new_active = 'active';
241 if (x($_GET, 'star')) {
242 $starred_active = 'active';
245 if (x($_GET, 'bmark')) {
246 $bookmarked_active = 'active';
249 if (x($_GET, 'conv')) {
250 $conv_active = 'active';
253 if (x($_GET, 'spam')) {
254 $spam_active = 'active';
257 if (($new_active == '') && ($starred_active == '') && ($bookmarked_active == '') && ($conv_active == '') && ($spam_active == '')) {
258 $no_active = 'active';
261 if ($no_active == 'active' && x($_GET, 'order')) {
262 switch($_GET['order']) {
263 case 'post' : $postord_active = 'active'; $no_active=''; break;
264 case 'comment' : $all_active = 'active'; $no_active=''; break;
268 return [$no_active, $all_active, $postord_active, $conv_active, $new_active, $starred_active, $bookmarked_active, $spam_active];
271 function network_query_get_sel_group(App $a)
275 if ($a->argc >= 2 && is_numeric($a->argv[1])) {
276 $group = $a->argv[1];
283 * @brief Sets the pager data and returns SQL
285 * @param App $a The global App
286 * @param integer $update Used for the automatic reloading
287 * @return string SQL with the appropriate LIMIT clause
289 function networkPager($a, $update)
292 // only setup pagination on initial page view
296 // check if we serve a mobile device and get the user settings
299 $itemspage_network = PConfig::get(local_user(), 'system', 'itemspage_mobile_network');
300 $itemspage_network = ((intval($itemspage_network)) ? $itemspage_network : 20);
302 $itemspage_network = PConfig::get(local_user(), 'system', 'itemspage_network');
303 $itemspage_network = ((intval($itemspage_network)) ? $itemspage_network : 40);
306 // now that we have the user settings, see if the theme forces
307 // a maximum item number which is lower then the user choice
308 if (($a->force_max_items > 0) && ($a->force_max_items < $itemspage_network)) {
309 $itemspage_network = $a->force_max_items;
312 $a->set_pager_itemspage($itemspage_network);
314 return sprintf(" LIMIT %d, %d ", intval($a->pager['start']), intval($a->pager['itemspage']));
318 * @brief Sets items as seen
320 * @param array $condition The array with the SQL condition
322 function networkSetSeen($condition)
324 if (empty($condition)) {
328 $unseen = dba::exists('item', $condition);
331 $r = Item::update(['unseen' => false], $condition);
336 * @brief Create the conversation HTML
338 * @param App $a The global App
339 * @param array $items Items of the conversation
340 * @param string $mode Display mode for the conversation
341 * @param integer $update Used for the automatic reloading
342 * @return string HTML of the conversation
344 function networkConversation($a, $items, $mode, $update, $ordering = '')
346 // Set this so that the conversation function can find out contact info for our wall-wall items
347 $a->page_contact = $a->contact;
349 $o = conversation($a, $items, $mode, $update, false, $ordering);
352 if (PConfig::get(local_user(), 'system', 'infinite_scroll')) {
353 $o .= scroll_loader();
355 $o .= alt_pager($a, count($items));
362 function network_content(App $a, $update = 0, $parent = 0)
365 return Login::form();
368 /// @TODO Is this really necessary? $a is already available to hooks
369 $arr = ['query' => $a->query_string];
370 Addon::callHooks('network_content_init', $arr);
375 for ($x = 1; $x < $a->argc; $x ++) {
376 if ($a->argv[$x] === 'new') {
382 if (x($_GET, 'file')) {
387 $o = networkFlatView($a, $update);
389 $o = networkThreadedView($a, $update, $parent);
396 * @brief Get the network content in flat view
398 * @param App $a The global App
399 * @param integer $update Used for the automatic reloading
400 * @return string HTML of the network content in flat view
402 function networkFlatView(App $a, $update = 0)
404 // Rawmode is used for fetching new content at the end of the page
405 $rawmode = (isset($_GET['mode']) AND ( $_GET['mode'] == 'raw'));
407 if (isset($_GET['last_id'])) {
408 $last_id = intval($_GET['last_id']);
415 $file = ((x($_GET, 'file')) ? $_GET['file'] : '');
417 if (!$update && !$rawmode) {
418 $tabs = network_tabs($a);
421 Nav::setSelected('network');
425 'allow_location' => $a->user['allow_location'],
426 'default_location' => $a->user['default-location'],
427 'nickname' => $a->user['nickname'],
428 'lockstate' => (((is_array($a->user) &&
429 ((strlen($a->user['allow_cid'])) || (strlen($a->user['allow_gid'])) ||
430 (strlen($a->user['deny_cid'])) || (strlen($a->user['deny_gid']))))) ? 'lock' : 'unlock'),
431 'default_perms' => Acl::getDefaultUserPermissions($a->user),
432 'acl' => Acl::getFullSelectorHTML($a->user, true),
434 'visitor' => 'block',
435 'profile_uid' => local_user(),
439 $o .= status_editor($a, $x);
441 if (!Config::get('theme', 'hide_eventlist')) {
442 $o .= Profile::getBirthdays();
443 $o .= Profile::getEventsReminderHTML();
448 $sql_post_table = sprintf("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` ",
449 dbesc(protect_sprintf($file)), intval(TERM_OBJ_POST), intval(TERM_FILE), intval(local_user()));
451 $sql_post_table = " INNER JOIN `thread` ON `thread`.`iid` = `item`.`parent`";
454 $pager_sql = networkPager($a, $update);
456 // show all items unthreaded in reverse created date order
457 $items = q("SELECT %s FROM `item` $sql_post_table %s
458 WHERE %s AND `item`.`uid` = %d
459 ORDER BY `item`.`id` DESC $pager_sql ",
460 item_fieldlists(), item_joins(), item_condition(),
461 intval($_SESSION['uid'])
464 $condition = ['unseen' => true, 'uid' => local_user()];
465 networkSetSeen($condition);
467 $mode = 'network-new';
468 $o .= networkConversation($a, $items, $mode, $update);
474 * @brief Get the network content in threaded view
476 * @param App $a The global App
477 * @param integer $update Used for the automatic reloading
478 * @return string HTML of the network content in flat view
480 function networkThreadedView(App $a, $update, $parent)
482 // Rawmode is used for fetching new content at the end of the page
483 $rawmode = (isset($_GET['mode']) AND ( $_GET['mode'] == 'raw'));
485 if (isset($_GET['last_received']) && isset($_GET['last_commented']) && isset($_GET['last_created']) && isset($_GET['last_id'])) {
486 $last_received = DBM::date($_GET['last_received']);
487 $last_commented = DBM::date($_GET['last_commented']);
488 $last_created = DBM::date($_GET['last_created']);
489 $last_id = intval($_GET['last_id']);
492 $last_commented = '';
497 $datequery = $datequery2 = '';
502 for ($x = 1; $x < $a->argc; $x ++) {
503 if (is_a_date_arg($a->argv[$x])) {
505 $datequery2 = escape_tags($a->argv[$x]);
507 $datequery = escape_tags($a->argv[$x]);
508 $_GET['order'] = 'post';
510 } elseif (intval($a->argv[$x])) {
511 $gid = intval($a->argv[$x]);
512 $def_acl = ['allow_gid' => '<' . $gid . '>'];
519 $cid = intval(defaults($_GET, 'cid' , 0));
520 $star = intval(defaults($_GET, 'star' , 0));
521 $bmark = intval(defaults($_GET, 'bmark', 0));
522 $conv = intval(defaults($_GET, 'conv' , 0));
523 $order = notags(defaults($_GET, 'order', 'comment'));
524 $nets = defaults($_GET, 'nets' , '');
527 $def_acl = ['allow_cid' => '<' . intval($cid) . '>'];
531 $r = dba::select('contact', ['id'], ['uid' => local_user(), 'network' => $nets], ['self' => false]);
534 while ($rr = dba::fetch($r)) {
535 $str .= '<' . $rr['id'] . '>';
538 $def_acl = ['allow_cid' => $str];
542 if (!$update && !$rawmode) {
543 $tabs = network_tabs($a);
547 if (($t = Contact::getOStatusCountByGroupId($gid)) && !PConfig::get(local_user(), 'system', 'nowarn_insecure')) {
548 notice(L10n::tt("Warning: This group contains %s member from a network that doesn't allow non public messages.",
549 "Warning: This group contains %s members from a network that doesn't allow non public messages.",
551 notice(L10n::t("Messages in this group won't be send to these receivers.").EOL);
555 Nav::setSelected('network');
560 // If $cid belongs to a communitity forum or a privat goup,.add a mention to the status editor
561 $condition = ["`id` = ? AND (`forum` OR `prv`)", $cid];
562 $contact = dba::selectFirst('contact', ['addr', 'nick'], $condition);
563 if (DBM::is_result($contact)) {
564 if ($contact['addr'] != '') {
565 $content = '!' . $contact['addr'];
567 $content = '!' . $contact['nick'] . '+' . $cid;
574 'allow_location' => $a->user['allow_location'],
575 'default_location' => $a->user['default-location'],
576 'nickname' => $a->user['nickname'],
577 'lockstate' => ((($gid) || ($cid) || ($nets) || (is_array($a->user) &&
578 ((strlen($a->user['allow_cid'])) || (strlen($a->user['allow_gid'])) ||
579 (strlen($a->user['deny_cid'])) || (strlen($a->user['deny_gid']))))) ? 'lock' : 'unlock'),
580 'default_perms' => Acl::getDefaultUserPermissions($a->user),
581 'acl' => Acl::getFullSelectorHTML((($gid || $cid || $nets) ? $def_acl : $a->user), true),
582 'bang' => (($gid || $cid || $nets) ? '!' : ''),
583 'visitor' => 'block',
584 'profile_uid' => local_user(),
585 'content' => $content,
588 $o .= status_editor($a, $x);
591 // We don't have to deal with ACLs on this page. You're looking at everything
592 // that belongs to you, hence you can see all of it. We will filter by group if
595 $sql_post_table = '';
596 $sql_options = (($star) ? " AND `thread`.`starred` " : '');
597 $sql_options .= (($bmark) ? " AND `thread`.`bookmark` " : '');
598 $sql_extra = $sql_options;
601 $sql_table = '`thread`';
602 $sql_parent = '`iid`';
605 $sql_table = '`item`';
606 $sql_parent = '`parent`';
607 $sql_post_table = " INNER JOIN `thread` ON `thread`.`iid` = `item`.`parent`";
610 $sql_nets = (($nets) ? sprintf(" AND $sql_table.`network` = '%s' ", dbesc($nets)) : '');
611 $sql_tag_nets = (($nets) ? sprintf(" AND `item`.`network` = '%s' ", dbesc($nets)) : '');
614 $group = dba::selectFirst('group', ['name'], ['id' => $gid, 'uid' => $_SESSION['uid']]);
615 if (!DBM::is_result($group)) {
619 notice(L10n::t('No such group') . EOL);
624 $contacts = Group::expand([$gid]);
626 if ((is_array($contacts)) && count($contacts)) {
627 $contact_str_self = '';
629 $contact_str = implode(',', $contacts);
630 $self = dba::selectFirst('contact', ['id'], ['uid' => $_SESSION['uid'], 'self' => true]);
631 if (DBM::is_result($self)) {
632 $contact_str_self = $self['id'];
635 $sql_post_table .= " INNER JOIN `item` AS `temp1` ON `temp1`.`id` = " . $sql_table . "." . $sql_parent;
636 $sql_extra3 .= " AND (`thread`.`contact-id` IN ($contact_str) ";
637 $sql_extra3 .= " OR (`thread`.`contact-id` = '$contact_str_self' AND `temp1`.`allow_gid` LIKE '" . protect_sprintf('%<' . intval($gid) . '>%') . "' AND `temp1`.`private`))";
639 $sql_extra3 .= " AND false ";
640 info(L10n::t('Group is empty'));
643 $o = replace_macros(get_markup_template('section_title.tpl'), [
644 '$title' => L10n::t('Group: %s', $group['name'])
647 $fields = ['id', 'name', 'network', 'writable', 'nurl',
648 'forum', 'prv', 'contact-type', 'addr', 'thumb', 'location'];
649 $condition = ["`id` = ? AND (NOT `blocked` OR `pending`)", $cid];
650 $contact = dba::selectFirst('contact', $fields, $condition);
651 if (DBM::is_result($contact)) {
652 $sql_extra = " AND " . $sql_table . ".`contact-id` = " . intval($cid);
656 'name' => htmlentities($contact['name']),
657 'itemurl' => defaults($contact, 'addr', $contact['nurl']),
658 'thumb' => proxy_url($contact['thumb'], false, PROXY_SIZE_THUMB),
659 'details' => $contact['location'],
662 $entries[0]['account_type'] = Contact::getAccountType($contact);
664 $o = replace_macros(get_markup_template('viewcontact_template.tpl'), [
665 'contacts' => $entries,
669 if ($contact['network'] === NETWORK_OSTATUS && $contact['writable'] && !PConfig::get(local_user(),'system','nowarn_insecure')) {
670 notice(L10n::t('Private messages to this person are at risk of public disclosure.') . EOL);
673 notice(L10n::t('Invalid contact.') . EOL);
679 if (!$gid && !$cid && !$update && !Config::get('theme', 'hide_eventlist')) {
680 $o .= Profile::getBirthdays();
681 $o .= Profile::getEventsReminderHTML();
685 $sql_extra3 .= protect_sprintf(sprintf(" AND $sql_table.created <= '%s' ",
686 dbesc(DateTimeFormat::convert($datequery, 'UTC', date_default_timezone_get()))));
689 $sql_extra3 .= protect_sprintf(sprintf(" AND $sql_table.created >= '%s' ",
690 dbesc(DateTimeFormat::convert($datequery2, 'UTC', date_default_timezone_get()))));
694 $sql_extra3 .= " AND $sql_table.`mention`";
697 // Normal conversation view
698 if ($order === 'post') {
699 $ordering = '`created`';
700 $order_mode = 'created';
702 $ordering = '`commented`';
703 $order_mode = 'commented';
706 if ($sql_order == '') {
707 $sql_order = "$sql_table.$ordering";
710 if (x($_GET, 'offset')) {
711 $sql_range = sprintf(" AND $sql_order <= '%s'", dbesc($_GET['offset']));
716 $pager_sql = networkPager($a, $update);
720 switch ($order_mode) {
722 if ($last_received != '') {
723 $last_date = $last_received;
724 $sql_range .= sprintf(" AND $sql_table.`received` < '%s'", dbesc($last_received));
725 $a->set_pager_page(1);
726 $pager_sql = sprintf(" LIMIT %d, %d ", intval($a->pager['start']), intval($a->pager['itemspage']));
730 if ($last_commented != '') {
731 $last_date = $last_commented;
732 $sql_range .= sprintf(" AND $sql_table.`commented` < '%s'", dbesc($last_commented));
733 $a->set_pager_page(1);
734 $pager_sql = sprintf(" LIMIT %d, %d ", intval($a->pager['start']), intval($a->pager['itemspage']));
738 if ($last_created != '') {
739 $last_date = $last_created;
740 $sql_range .= sprintf(" AND $sql_table.`created` < '%s'", dbesc($last_created));
741 $a->set_pager_page(1);
742 $pager_sql = sprintf(" LIMIT %d, %d ", intval($a->pager['start']), intval($a->pager['itemspage']));
746 if (($last_id > 0) && ($sql_table == '`thread`')) {
747 $sql_range .= sprintf(" AND $sql_table.`iid` < '%s'", dbesc($last_id));
748 $a->set_pager_page(1);
749 $pager_sql = sprintf(" LIMIT %d, %d ", intval($a->pager['start']), intval($a->pager['itemspage']));
754 // Fetch a page full of parent items for this page
756 if (!empty($parent)) {
757 // Load only a single thread
758 $sql_extra4 = "`item`.`id` = ".intval($parent);
760 // Load all unseen items
761 $sql_extra4 = "`item`.`unseen`";
762 if (Config::get("system", "like_no_comment")) {
763 $sql_extra4 .= " AND `item`.`verb` = '".ACTIVITY_POST."'";
765 if ($order === 'post') {
766 // Only show toplevel posts when updating posts in this order mode
767 $sql_extra4 .= " AND `item`.`id` = `item`.`parent`";
771 $r = q("SELECT `item`.`parent-uri` AS `uri`, `item`.`parent` AS `item_id`, $sql_order AS `order_date`
772 FROM `item` $sql_post_table
773 STRAIGHT_JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
774 AND (NOT `contact`.`blocked` OR `contact`.`pending`)
775 WHERE `item`.`uid` = %d AND `item`.`visible` AND NOT `item`.`deleted`
776 AND NOT `item`.`moderated` AND $sql_extra4
777 $sql_extra3 $sql_extra $sql_range $sql_nets
778 ORDER BY `order_date` DESC LIMIT 100",
782 $r = q("SELECT `item`.`uri`, `thread`.`iid` AS `item_id`, $sql_order AS `order_date`
783 FROM `thread` $sql_post_table
784 STRAIGHT_JOIN `contact` ON `contact`.`id` = `thread`.`contact-id`
785 AND (NOT `contact`.`blocked` OR `contact`.`pending`)
786 STRAIGHT_JOIN `item` ON `item`.`id` = `thread`.`iid`
787 WHERE `thread`.`uid` = %d AND `thread`.`visible` AND NOT `thread`.`deleted`
788 AND NOT `thread`.`moderated`
789 $sql_extra2 $sql_extra3 $sql_range $sql_extra $sql_nets
790 ORDER BY `order_date` DESC $pager_sql",
795 // Only show it when unfiltered (no groups, no networks, ...)
796 if (in_array($nets, ['', NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_OSTATUS]) && (strlen($sql_extra . $sql_extra2 . $sql_extra3) == 0)) {
797 if (DBM::is_result($r)) {
798 $top_limit = current($r)['order_date'];
799 $bottom_limit = end($r)['order_date'];
800 if (empty($_SESSION['network_last_top_limit']) || ($_SESSION['network_last_top_limit'] < $top_limit)) {
801 $_SESSION['network_last_top_limit'] = $top_limit;
804 $top_limit = $bottom_limit = DateTimeFormat::utcNow();
807 // When checking for updates we need to fetch from the newest date to the newest date before
808 // Only do this, when the last stored date isn't too long ago (10 times the update interval)
809 $browser_update = PConfig::get(local_user(), 'system', 'update_interval', 40000) / 1000;
811 if (($browser_update > 0) && $update && !empty($_SESSION['network_last_date']) &&
812 (($bottom_limit < $_SESSION['network_last_date']) || ($top_limit == $bottom_limit)) &&
813 ((time() - $_SESSION['network_last_date_timestamp']) < ($browser_update * 10))) {
814 $bottom_limit = $_SESSION['network_last_date'];
816 $_SESSION['network_last_date'] = defaults($_SESSION, 'network_last_top_limit', $top_limit);
817 $_SESSION['network_last_date_timestamp'] = time();
819 if ($last_date > $top_limit) {
820 $top_limit = $last_date;
821 } elseif ($a->pager['page'] == 1) {
822 // Highest possible top limit when we are on the first page
823 $top_limit = DateTimeFormat::utcNow();
826 $items = dba::p("SELECT `item`.`parent-uri` AS `uri`, 0 AS `item_id`, `item`.$ordering AS `order_date` FROM `item`
827 STRAIGHT_JOIN (SELECT `oid` FROM `term` WHERE `term` IN
828 (SELECT SUBSTR(`term`, 2) FROM `search` WHERE `uid` = ? AND `term` LIKE '#%') AND `otype` = ? AND `type` = ? AND `uid` = 0) AS `term`
829 ON `item`.`id` = `term`.`oid`
830 STRAIGHT_JOIN `contact` ON `contact`.`id` = `item`.`author-id`
831 WHERE `item`.`uid` = 0 AND `item`.$ordering < ? AND `item`.$ordering > ?
832 AND NOT `contact`.`hidden` AND NOT `contact`.`blocked`" . $sql_tag_nets,
833 local_user(), TERM_OBJ_POST, TERM_HASHTAG, $top_limit, $bottom_limit);
835 $data = dba::inArray($items);
837 if (count($data) > 0) {
838 $tag_top_limit = current($data)['order_date'];
839 if ($_SESSION['network_last_date'] < $tag_top_limit) {
840 $_SESSION['network_last_date'] = $tag_top_limit;
843 logger('Tagged items: ' . count($data) . ' - ' . $bottom_limit . ' - ' . $top_limit . ' - ' . local_user().' - '.(int)$update);
845 foreach ($r as $item) {
846 $s[$item['uri']] = $item;
848 foreach ($data as $item) {
849 $s[$item['uri']] = $item;
860 if (DBM::is_result($items)) {
863 foreach ($items as $item) {
864 if ($date_offset < $item['order_date']) {
865 $date_offset = $item['order_date'];
867 if (!in_array($item['item_id'], $parents_arr) && ($item['item_id'] > 0)) {
868 $parents_arr[] = $item['item_id'];
871 $parents_str = implode(', ', $parents_arr);
874 if (x($_GET, 'offset')) {
875 $date_offset = $_GET['offset'];
878 $a->page_offset = $date_offset;
880 // We aren't going to try and figure out at the item, group, and page
881 // level which items you've seen and which you haven't. If you're looking
882 // at the top level network page just mark everything seen.
884 if (!$gid && !$cid && !$star) {
885 $condition = ['unseen' => true, 'uid' => local_user()];
886 networkSetSeen($condition);
887 } elseif ($parents_str) {
888 $condition = ["`uid` = ? AND `unseen` AND `parent` IN (" . dbesc($parents_str) . ")", local_user()];
889 networkSetSeen($condition);
894 $o .= networkConversation($a, $items, $mode, $update, $ordering);
900 * @brief Get the network tabs menu
902 * @param App $a The global App
903 * @return string Html of the networktab
905 function network_tabs(App $a)
908 /// @TODO fix this logic, reduce duplication
909 /// $a->page['content'] .= '<div class="tabs-wrapper">';
910 list($no_active, $all_active, $postord_active, $conv_active, $new_active, $starred_active, $bookmarked_active, $spam_active) = network_query_get_sel_tab($a);
912 // if no tabs are selected, defaults to comments
913 if ($no_active == 'active') {
914 $all_active = 'active';
922 'label' => L10n::t('Commented Order'),
923 'url' => str_replace('/new', '', $cmd) . '?f=&order=comment' . ((x($_GET,'cid')) ? '&cid=' . $_GET['cid'] : ''),
924 'sel' => $all_active,
925 'title' => L10n::t('Sort by Comment Date'),
926 'id' => 'commented-order-tab',
930 'label' => L10n::t('Posted Order'),
931 'url' => str_replace('/new', '', $cmd) . '?f=&order=post' . ((x($_GET,'cid')) ? '&cid=' . $_GET['cid'] : ''),
932 'sel' => $postord_active,
933 'title' => L10n::t('Sort by Post Date'),
934 'id' => 'posted-order-tab',
939 if (Feature::isEnabled(local_user(), 'personal_tab')) {
941 'label' => L10n::t('Personal'),
942 'url' => str_replace('/new', '', $cmd) . ((x($_GET,'cid')) ? '/?f=&cid=' . $_GET['cid'] : '/?f=') . '&conv=1',
943 'sel' => $conv_active,
944 'title' => L10n::t('Posts that mention or involve you'),
945 'id' => 'personal-tab',
950 if (Feature::isEnabled(local_user(), 'new_tab')) {
952 'label' => L10n::t('New'),
953 'url' => 'network/new' . ((x($_GET,'cid')) ? '/?f=&cid=' . $_GET['cid'] : ''),
954 'sel' => $new_active,
955 'title' => L10n::t('Activity Stream - by date'),
956 'id' => 'activitiy-by-date-tab',
961 if (Feature::isEnabled(local_user(), 'link_tab')) {
963 'label' => L10n::t('Shared Links'),
964 'url' => str_replace('/new', '', $cmd) . ((x($_GET,'cid')) ? '/?f=&cid=' . $_GET['cid'] : '/?f=') . '&bmark=1',
965 'sel' => $bookmarked_active,
966 'title' => L10n::t('Interesting Links'),
967 'id' => 'shared-links-tab',
972 if (Feature::isEnabled(local_user(), 'star_posts')) {
974 'label' => L10n::t('Starred'),
975 'url' => str_replace('/new', '', $cmd) . ((x($_GET,'cid')) ? '/?f=&cid=' . $_GET['cid'] : '/?f=') . '&star=1',
976 'sel' => $starred_active,
977 'title' => L10n::t('Favourite Posts'),
978 'id' => 'starred-posts-tab',
983 // save selected tab, but only if not in file mode
984 if (!x($_GET, 'file')) {
985 PConfig::set(local_user(), 'network.view', 'tab.selected', [
986 $all_active, $postord_active, $conv_active, $new_active, $starred_active, $bookmarked_active, $spam_active
990 $arr = ['tabs' => $tabs];
991 Addon::callHooks('network_tabs', $arr);
993 $tpl = get_markup_template('common_tabs.tpl');
995 return replace_macros($tpl, ['$tabs' => $arr['tabs']]);
997 // --- end item filter tabs