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';
29 function network_init(App $a)
32 notice(L10n::t('Permission denied.') . EOL);
36 $search = (x($_GET, 'search') ? escape_tags($_GET['search']) : '');
38 if (($search != '') && !empty($_GET['submit'])) {
39 goaway('search?search=' . urlencode($search));
42 if (x($_GET, 'save')) {
43 $exists = dba::exists('search', ['uid' => local_user(), 'term' => $search]);
45 dba::insert('search', ['uid' => local_user(), 'term' => $search]);
48 if (x($_GET, 'remove')) {
49 dba::delete('search', ['uid' => local_user(), 'term' => $search]);
52 $is_a_date_query = false;
54 $group_id = (($a->argc > 1 && is_numeric($a->argv[1])) ? intval($a->argv[1]) : 0);
57 if (x($_GET, 'cid') && intval($_GET['cid']) != 0) {
59 $_GET['nets'] = 'all';
64 for ($x = 1; $x < $a->argc; $x ++) {
65 if (is_a_date_arg($a->argv[$x])) {
66 $is_a_date_query = true;
72 // convert query string to array. remove friendica args
74 $query_string = str_replace($a->cmd . '?', '', $a->query_string);
75 parse_str($query_string, $query_array);
76 array_shift($query_array);
78 // fetch last used network view and redirect if needed
79 if (!$is_a_date_query) {
80 $sel_nets = defaults($_GET, 'nets', false);
81 $sel_tabs = network_query_get_sel_tab($a);
82 $sel_groups = network_query_get_sel_group($a);
83 $last_sel_tabs = PConfig::get(local_user(), 'network.view', 'tab.selected');
85 $remember_tab = ($sel_tabs[0] === 'active' && is_array($last_sel_tabs) && $last_sel_tabs[0] !== 'active');
87 $net_baseurl = '/network';
90 if ($sel_groups !== false) {
91 $net_baseurl .= '/' . $sel_groups;
95 // redirect if current selected tab is '/network' and
96 // last selected tab is _not_ '/network?f=&order=comment'.
97 // and this isn't a date query
109 'f=&order=comment', //all
110 'f=&order=post', //postord
113 'f=&star=1', //starred
114 'f=&bmark=1', //bookmarked
118 $k = array_search('active', $last_sel_tabs);
121 $net_baseurl .= $tab_baseurls[$k];
123 // parse out tab queries
125 $dest_qs = $tab_args[$k];
126 parse_str($dest_qs, $dest_qa);
127 $net_args = array_merge($net_args, $dest_qa);
129 $remember_tab = false;
133 if ($sel_nets !== false) {
134 $net_args['nets'] = $sel_nets;
138 $net_args = array_merge($query_array, $net_args);
139 $net_queries = build_querystring($net_args);
141 $redir_url = ($net_queries ? $net_baseurl . '?' . $net_queries : $net_baseurl);
143 goaway(System::baseUrl() . $redir_url);
147 // If nets is set to all, unset it
148 if (x($_GET, 'nets') && $_GET['nets'] === 'all') {
149 unset($_GET['nets']);
152 if (!x($a->page, 'aside')) {
153 $a->page['aside'] = '';
156 $a->page['aside'] .= (Feature::isEnabled(local_user(), 'groups') ?
157 Group::sidebarWidget('network/0', 'network', 'standard', $group_id) : '');
158 $a->page['aside'] .= (Feature::isEnabled(local_user(), 'forumlist_widget') ? ForumManager::widget(local_user(), $cid) : '');
159 $a->page['aside'] .= posted_date_widget('network', local_user(), false);
160 $a->page['aside'] .= Widget::networks('network', (x($_GET, 'nets') ? $_GET['nets'] : ''));
161 $a->page['aside'] .= saved_searches($search);
162 $a->page['aside'] .= Widget::fileAs('network', (x($_GET, 'file') ? $_GET['file'] : ''));
165 function saved_searches($search)
167 if (!Feature::isEnabled(local_user(), 'savedsearch')) {
173 $srchurl = '/network?f='
174 . ((x($_GET, 'cid')) ? '&cid=' . $_GET['cid'] : '')
175 . ((x($_GET, 'star')) ? '&star=' . $_GET['star'] : '')
176 . ((x($_GET, 'bmark')) ? '&bmark=' . $_GET['bmark'] : '')
177 . ((x($_GET, 'conv')) ? '&conv=' . $_GET['conv'] : '')
178 . ((x($_GET, 'nets')) ? '&nets=' . $_GET['nets'] : '')
179 . ((x($_GET, 'cmin')) ? '&cmin=' . $_GET['cmin'] : '')
180 . ((x($_GET, 'cmax')) ? '&cmax=' . $_GET['cmax'] : '')
181 . ((x($_GET, 'file')) ? '&file=' . $_GET['file'] : '');
186 $terms = dba::select('search', ['id', 'term'], ['uid' => local_user()]);
189 while ($rr = dba::fetch($terms)) {
192 'term' => $rr['term'],
193 'encodedterm' => urlencode($rr['term']),
194 'delete' => L10n::t('Remove term'),
195 'selected' => ($search == $rr['term']),
199 $tpl = get_markup_template('saved_searches_aside.tpl');
200 $o = replace_macros($tpl, [
201 '$title' => L10n::t('Saved Searches'),
202 '$add' => L10n::t('add'),
203 '$searchbox' => search($search, 'netsearch-box', $srchurl, true),
211 * Return selected tab from query
214 * '/network' => $no_active = 'active'
215 * '/network?f=&order=comment' => $comment_active = 'active'
216 * '/network?f=&order=post' => $postord_active = 'active'
217 * '/network?f=&conv=1', => $conv_active = 'active'
218 * '/network/new', => $new_active = 'active'
219 * '/network?f=&star=1', => $starred_active = 'active'
220 * '/network?f=&bmark=1', => $bookmarked_active = 'active'
221 * '/network?f=&spam=1', => $spam_active = 'active'
223 * @return Array ($no_active, $comment_active, $postord_active, $conv_active, $new_active, $starred_active, $bookmarked_active, $spam_active);
225 function network_query_get_sel_tab(App $a)
228 $starred_active = '';
230 $bookmarked_active = '';
234 $postord_active = '';
236 if (($a->argc > 1 && $a->argv[1] === 'new') || ($a->argc > 2 && $a->argv[2] === 'new')) {
237 $new_active = 'active';
240 if (x($_GET, 'star')) {
241 $starred_active = 'active';
244 if (x($_GET, 'bmark')) {
245 $bookmarked_active = 'active';
248 if (x($_GET, 'conv')) {
249 $conv_active = 'active';
252 if (x($_GET, 'spam')) {
253 $spam_active = 'active';
256 if (($new_active == '') && ($starred_active == '') && ($bookmarked_active == '') && ($conv_active == '') && ($spam_active == '')) {
257 $no_active = 'active';
260 if ($no_active == 'active' && x($_GET, 'order')) {
261 switch($_GET['order']) {
262 case 'post' : $postord_active = 'active'; $no_active=''; break;
263 case 'comment' : $all_active = 'active'; $no_active=''; break;
267 return [$no_active, $all_active, $postord_active, $conv_active, $new_active, $starred_active, $bookmarked_active, $spam_active];
270 function network_query_get_sel_group(App $a)
274 if ($a->argc >= 2 && is_numeric($a->argv[1])) {
275 $group = $a->argv[1];
282 * @brief Sets the pager data and returns SQL
284 * @param App $a The global App
285 * @param integer $update Used for the automatic reloading
286 * @return string SQL with the appropriate LIMIT clause
288 function networkPager($a, $update)
291 // only setup pagination on initial page view
295 // check if we serve a mobile device and get the user settings
298 $itemspage_network = PConfig::get(local_user(), 'system', 'itemspage_mobile_network');
299 $itemspage_network = ((intval($itemspage_network)) ? $itemspage_network : 20);
301 $itemspage_network = PConfig::get(local_user(), 'system', 'itemspage_network');
302 $itemspage_network = ((intval($itemspage_network)) ? $itemspage_network : 40);
305 // now that we have the user settings, see if the theme forces
306 // a maximum item number which is lower then the user choice
307 if (($a->force_max_items > 0) && ($a->force_max_items < $itemspage_network)) {
308 $itemspage_network = $a->force_max_items;
311 $a->set_pager_itemspage($itemspage_network);
313 return sprintf(" LIMIT %d, %d ", intval($a->pager['start']), intval($a->pager['itemspage']));
317 * @brief Sets items as seen
319 * @param array $condition The array with the SQL condition
321 function networkSetSeen($condition)
323 if (empty($condition)) {
327 $unseen = dba::exists('item', $condition);
330 $r = Item::update(['unseen' => false], $condition);
335 * @brief Create the conversation HTML
337 * @param App $a The global App
338 * @param array $items Items of the conversation
339 * @param string $mode Display mode for the conversation
340 * @param integer $update Used for the automatic reloading
341 * @return string HTML of the conversation
343 function networkConversation($a, $items, $mode, $update, $ordering = '')
345 // Set this so that the conversation function can find out contact info for our wall-wall items
346 $a->page_contact = $a->contact;
348 $o = conversation($a, $items, $mode, $update, false, $ordering);
351 if (PConfig::get(local_user(), 'system', 'infinite_scroll')) {
352 $o .= scroll_loader();
354 $o .= alt_pager($a, count($items));
361 function network_content(App $a, $update = 0, $parent = 0)
364 return Login::form();
367 /// @TODO Is this really necessary? $a is already available to hooks
368 $arr = ['query' => $a->query_string];
369 Addon::callHooks('network_content_init', $arr);
374 for ($x = 1; $x < $a->argc; $x ++) {
375 if ($a->argv[$x] === 'new') {
381 if (x($_GET, 'file')) {
386 $o = networkFlatView($a, $update);
388 $o = networkThreadedView($a, $update, $parent);
395 * @brief Get the network content in flat view
397 * @param App $a The global App
398 * @param integer $update Used for the automatic reloading
399 * @return string HTML of the network content in flat view
401 function networkFlatView(App $a, $update = 0)
403 // Rawmode is used for fetching new content at the end of the page
404 $rawmode = (isset($_GET['mode']) AND ( $_GET['mode'] == 'raw'));
406 if (isset($_GET['last_id'])) {
407 $last_id = intval($_GET['last_id']);
414 $file = ((x($_GET, 'file')) ? $_GET['file'] : '');
416 if (!$update && !$rawmode) {
417 $tabs = network_tabs($a);
420 Nav::setSelected('network');
424 'allow_location' => $a->user['allow_location'],
425 'default_location' => $a->user['default-location'],
426 'nickname' => $a->user['nickname'],
427 'lockstate' => (((is_array($a->user) &&
428 ((strlen($a->user['allow_cid'])) || (strlen($a->user['allow_gid'])) ||
429 (strlen($a->user['deny_cid'])) || (strlen($a->user['deny_gid']))))) ? 'lock' : 'unlock'),
430 'default_perms' => ACL::getDefaultUserPermissions($a->user),
431 'acl' => ACL::getFullSelectorHTML($a->user, true),
433 'visitor' => 'block',
434 'profile_uid' => local_user(),
438 $o .= status_editor($a, $x);
440 if (!Config::get('theme', 'hide_eventlist')) {
441 $o .= Profile::getBirthdays();
442 $o .= Profile::getEventsReminderHTML();
447 $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` ",
448 dbesc(protect_sprintf($file)), intval(TERM_OBJ_POST), intval(TERM_FILE), intval(local_user()));
450 $sql_post_table = " INNER JOIN `thread` ON `thread`.`iid` = `item`.`parent`";
453 $pager_sql = networkPager($a, $update);
455 // show all items unthreaded in reverse created date order
456 $items = q("SELECT %s FROM `item` $sql_post_table %s
457 WHERE %s AND `item`.`uid` = %d
458 ORDER BY `item`.`id` DESC $pager_sql ",
459 item_fieldlists(), item_joins(), item_condition(),
460 intval($_SESSION['uid'])
463 $condition = ['unseen' => true, 'uid' => local_user()];
464 networkSetSeen($condition);
466 $mode = 'network-new';
467 $o .= networkConversation($a, $items, $mode, $update);
473 * @brief Get the network content in threaded view
475 * @param App $a The global App
476 * @param integer $update Used for the automatic reloading
477 * @return string HTML of the network content in flat view
479 function networkThreadedView(App $a, $update, $parent)
481 // Rawmode is used for fetching new content at the end of the page
482 $rawmode = (isset($_GET['mode']) AND ( $_GET['mode'] == 'raw'));
484 if (isset($_GET['last_received']) && isset($_GET['last_commented']) && isset($_GET['last_created']) && isset($_GET['last_id'])) {
485 $last_received = DBM::date($_GET['last_received']);
486 $last_commented = DBM::date($_GET['last_commented']);
487 $last_created = DBM::date($_GET['last_created']);
488 $last_id = intval($_GET['last_id']);
491 $last_commented = '';
496 $datequery = $datequery2 = '';
501 for ($x = 1; $x < $a->argc; $x ++) {
502 if (is_a_date_arg($a->argv[$x])) {
504 $datequery2 = escape_tags($a->argv[$x]);
506 $datequery = escape_tags($a->argv[$x]);
507 $_GET['order'] = 'post';
509 } elseif (intval($a->argv[$x])) {
510 $gid = intval($a->argv[$x]);
511 $def_acl = ['allow_gid' => '<' . $gid . '>'];
518 $cid = intval(defaults($_GET, 'cid' , 0));
519 $star = intval(defaults($_GET, 'star' , 0));
520 $bmark = intval(defaults($_GET, 'bmark', 0));
521 $conv = intval(defaults($_GET, 'conv' , 0));
522 $order = notags(defaults($_GET, 'order', 'comment'));
523 $nets = defaults($_GET, 'nets' , '');
526 $def_acl = ['allow_cid' => '<' . intval($cid) . '>'];
530 $r = dba::select('contact', ['id'], ['uid' => local_user(), 'network' => $nets], ['self' => false]);
533 while ($rr = dba::fetch($r)) {
534 $str .= '<' . $rr['id'] . '>';
537 $def_acl = ['allow_cid' => $str];
541 if (!$update && !$rawmode) {
542 $tabs = network_tabs($a);
546 if (($t = Contact::getOStatusCountByGroupId($gid)) && !PConfig::get(local_user(), 'system', 'nowarn_insecure')) {
547 notice(L10n::tt("Warning: This group contains %s member from a network that doesn't allow non public messages.",
548 "Warning: This group contains %s members from a network that doesn't allow non public messages.",
550 notice(L10n::t("Messages in this group won't be send to these receivers.").EOL);
554 Nav::setSelected('network');
559 // If $cid belongs to a communitity forum or a privat goup,.add a mention to the status editor
560 $condition = ["`id` = ? AND (`forum` OR `prv`)", $cid];
561 $contact = dba::selectFirst('contact', ['addr', 'nick'], $condition);
562 if (DBM::is_result($contact)) {
563 if ($contact['addr'] != '') {
564 $content = '!' . $contact['addr'];
566 $content = '!' . $contact['nick'] . '+' . $cid;
573 'allow_location' => $a->user['allow_location'],
574 'default_location' => $a->user['default-location'],
575 'nickname' => $a->user['nickname'],
576 'lockstate' => ((($gid) || ($cid) || ($nets) || (is_array($a->user) &&
577 ((strlen($a->user['allow_cid'])) || (strlen($a->user['allow_gid'])) ||
578 (strlen($a->user['deny_cid'])) || (strlen($a->user['deny_gid']))))) ? 'lock' : 'unlock'),
579 'default_perms' => ACL::getDefaultUserPermissions($a->user),
580 'acl' => ACL::getFullSelectorHTML((($gid || $cid || $nets) ? $def_acl : $a->user), true),
581 'bang' => (($gid || $cid || $nets) ? '!' : ''),
582 'visitor' => 'block',
583 'profile_uid' => local_user(),
584 'content' => $content,
587 $o .= status_editor($a, $x);
590 // We don't have to deal with ACLs on this page. You're looking at everything
591 // that belongs to you, hence you can see all of it. We will filter by group if
594 $sql_post_table = '';
595 $sql_options = (($star) ? " AND `thread`.`starred` " : '');
596 $sql_options .= (($bmark) ? " AND `thread`.`bookmark` " : '');
597 $sql_extra = $sql_options;
600 $sql_table = '`thread`';
601 $sql_parent = '`iid`';
604 $sql_table = '`item`';
605 $sql_parent = '`parent`';
606 $sql_post_table = " INNER JOIN `thread` ON `thread`.`iid` = `item`.`parent`";
609 $sql_nets = (($nets) ? sprintf(" AND $sql_table.`network` = '%s' ", dbesc($nets)) : '');
610 $sql_tag_nets = (($nets) ? sprintf(" AND `item`.`network` = '%s' ", dbesc($nets)) : '');
613 $group = dba::selectFirst('group', ['name'], ['id' => $gid, 'uid' => $_SESSION['uid']]);
614 if (!DBM::is_result($group)) {
618 notice(L10n::t('No such group') . EOL);
623 $contacts = Group::expand([$gid]);
625 if ((is_array($contacts)) && count($contacts)) {
626 $contact_str_self = '';
628 $contact_str = implode(',', $contacts);
629 $self = dba::selectFirst('contact', ['id'], ['uid' => $_SESSION['uid'], 'self' => true]);
630 if (DBM::is_result($self)) {
631 $contact_str_self = $self['id'];
634 $sql_post_table .= " INNER JOIN `item` AS `temp1` ON `temp1`.`id` = " . $sql_table . "." . $sql_parent;
635 $sql_extra3 .= " AND (`thread`.`contact-id` IN ($contact_str) ";
636 $sql_extra3 .= " OR (`thread`.`contact-id` = '$contact_str_self' AND `temp1`.`allow_gid` LIKE '" . protect_sprintf('%<' . intval($gid) . '>%') . "' AND `temp1`.`private`))";
638 $sql_extra3 .= " AND false ";
639 info(L10n::t('Group is empty'));
642 $o = replace_macros(get_markup_template('section_title.tpl'), [
643 '$title' => L10n::t('Group: %s', $group['name'])
646 $fields = ['id', 'name', 'network', 'writable', 'nurl',
647 'forum', 'prv', 'contact-type', 'addr', 'thumb', 'location'];
648 $condition = ["`id` = ? AND (NOT `blocked` OR `pending`)", $cid];
649 $contact = dba::selectFirst('contact', $fields, $condition);
650 if (DBM::is_result($contact)) {
651 $sql_extra = " AND " . $sql_table . ".`contact-id` = " . intval($cid);
655 'name' => htmlentities($contact['name']),
656 'itemurl' => defaults($contact, 'addr', $contact['nurl']),
657 'thumb' => proxy_url($contact['thumb'], false, PROXY_SIZE_THUMB),
658 'details' => $contact['location'],
661 $entries[0]['account_type'] = Contact::getAccountType($contact);
663 $o = replace_macros(get_markup_template('viewcontact_template.tpl'), [
664 'contacts' => $entries,
668 if ($contact['network'] === NETWORK_OSTATUS && $contact['writable'] && !PConfig::get(local_user(),'system','nowarn_insecure')) {
669 notice(L10n::t('Private messages to this person are at risk of public disclosure.') . EOL);
672 notice(L10n::t('Invalid contact.') . EOL);
678 if (!$gid && !$cid && !$update && !Config::get('theme', 'hide_eventlist')) {
679 $o .= Profile::getBirthdays();
680 $o .= Profile::getEventsReminderHTML();
684 $sql_extra3 .= protect_sprintf(sprintf(" AND $sql_table.created <= '%s' ",
685 dbesc(DateTimeFormat::convert($datequery, 'UTC', date_default_timezone_get()))));
688 $sql_extra3 .= protect_sprintf(sprintf(" AND $sql_table.created >= '%s' ",
689 dbesc(DateTimeFormat::convert($datequery2, 'UTC', date_default_timezone_get()))));
693 $sql_extra3 .= " AND $sql_table.`mention`";
696 // Normal conversation view
697 if ($order === 'post') {
698 $ordering = '`created`';
699 $order_mode = 'created';
701 $ordering = '`commented`';
702 $order_mode = 'commented';
705 if ($sql_order == '') {
706 $sql_order = "$sql_table.$ordering";
709 if (x($_GET, 'offset')) {
710 $sql_range = sprintf(" AND $sql_order <= '%s'", dbesc($_GET['offset']));
715 $pager_sql = networkPager($a, $update);
719 switch ($order_mode) {
721 if ($last_received != '') {
722 $last_date = $last_received;
723 $sql_range .= sprintf(" AND $sql_table.`received` < '%s'", dbesc($last_received));
724 $a->set_pager_page(1);
725 $pager_sql = sprintf(" LIMIT %d, %d ", intval($a->pager['start']), intval($a->pager['itemspage']));
729 if ($last_commented != '') {
730 $last_date = $last_commented;
731 $sql_range .= sprintf(" AND $sql_table.`commented` < '%s'", dbesc($last_commented));
732 $a->set_pager_page(1);
733 $pager_sql = sprintf(" LIMIT %d, %d ", intval($a->pager['start']), intval($a->pager['itemspage']));
737 if ($last_created != '') {
738 $last_date = $last_created;
739 $sql_range .= sprintf(" AND $sql_table.`created` < '%s'", dbesc($last_created));
740 $a->set_pager_page(1);
741 $pager_sql = sprintf(" LIMIT %d, %d ", intval($a->pager['start']), intval($a->pager['itemspage']));
745 if (($last_id > 0) && ($sql_table == '`thread`')) {
746 $sql_range .= sprintf(" AND $sql_table.`iid` < '%s'", dbesc($last_id));
747 $a->set_pager_page(1);
748 $pager_sql = sprintf(" LIMIT %d, %d ", intval($a->pager['start']), intval($a->pager['itemspage']));
753 // Fetch a page full of parent items for this page
755 if (!empty($parent)) {
756 // Load only a single thread
757 $sql_extra4 = "`item`.`id` = ".intval($parent);
759 // Load all unseen items
760 $sql_extra4 = "`item`.`unseen`";
761 if (Config::get("system", "like_no_comment")) {
762 $sql_extra4 .= " AND `item`.`verb` = '".ACTIVITY_POST."'";
764 if ($order === 'post') {
765 // Only show toplevel posts when updating posts in this order mode
766 $sql_extra4 .= " AND `item`.`id` = `item`.`parent`";
770 $r = q("SELECT `item`.`parent-uri` AS `uri`, `item`.`parent` AS `item_id`, $sql_order AS `order_date`
771 FROM `item` $sql_post_table
772 STRAIGHT_JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
773 AND (NOT `contact`.`blocked` OR `contact`.`pending`)
774 WHERE `item`.`uid` = %d AND `item`.`visible` AND NOT `item`.`deleted`
775 AND NOT `item`.`moderated` AND $sql_extra4
776 $sql_extra3 $sql_extra $sql_range $sql_nets
777 ORDER BY `order_date` DESC LIMIT 100",
781 $r = q("SELECT `item`.`uri`, `thread`.`iid` AS `item_id`, $sql_order AS `order_date`
782 FROM `thread` $sql_post_table
783 STRAIGHT_JOIN `contact` ON `contact`.`id` = `thread`.`contact-id`
784 AND (NOT `contact`.`blocked` OR `contact`.`pending`)
785 STRAIGHT_JOIN `item` ON `item`.`id` = `thread`.`iid`
786 WHERE `thread`.`uid` = %d AND `thread`.`visible` AND NOT `thread`.`deleted`
787 AND NOT `thread`.`moderated`
788 $sql_extra2 $sql_extra3 $sql_range $sql_extra $sql_nets
789 ORDER BY `order_date` DESC $pager_sql",
794 // Only show it when unfiltered (no groups, no networks, ...)
795 if (in_array($nets, ['', NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_OSTATUS]) && (strlen($sql_extra . $sql_extra2 . $sql_extra3) == 0)) {
796 if (DBM::is_result($r)) {
797 $top_limit = current($r)['order_date'];
798 $bottom_limit = end($r)['order_date'];
799 if (empty($_SESSION['network_last_top_limit']) || ($_SESSION['network_last_top_limit'] < $top_limit)) {
800 $_SESSION['network_last_top_limit'] = $top_limit;
803 $top_limit = $bottom_limit = DateTimeFormat::utcNow();
806 // When checking for updates we need to fetch from the newest date to the newest date before
807 // Only do this, when the last stored date isn't too long ago (10 times the update interval)
808 $browser_update = PConfig::get(local_user(), 'system', 'update_interval', 40000) / 1000;
810 if (($browser_update > 0) && $update && !empty($_SESSION['network_last_date']) &&
811 (($bottom_limit < $_SESSION['network_last_date']) || ($top_limit == $bottom_limit)) &&
812 ((time() - $_SESSION['network_last_date_timestamp']) < ($browser_update * 10))) {
813 $bottom_limit = $_SESSION['network_last_date'];
815 $_SESSION['network_last_date'] = defaults($_SESSION, 'network_last_top_limit', $top_limit);
816 $_SESSION['network_last_date_timestamp'] = time();
818 if ($last_date > $top_limit) {
819 $top_limit = $last_date;
820 } elseif ($a->pager['page'] == 1) {
821 // Highest possible top limit when we are on the first page
822 $top_limit = DateTimeFormat::utcNow();
825 $items = dba::p("SELECT `item`.`parent-uri` AS `uri`, 0 AS `item_id`, `item`.$ordering AS `order_date` FROM `item`
826 STRAIGHT_JOIN (SELECT `oid` FROM `term` WHERE `term` IN
827 (SELECT SUBSTR(`term`, 2) FROM `search` WHERE `uid` = ? AND `term` LIKE '#%') AND `otype` = ? AND `type` = ? AND `uid` = 0) AS `term`
828 ON `item`.`id` = `term`.`oid`
829 STRAIGHT_JOIN `contact` ON `contact`.`id` = `item`.`author-id`
830 WHERE `item`.`uid` = 0 AND `item`.$ordering < ? AND `item`.$ordering > ?
831 AND NOT `contact`.`hidden` AND NOT `contact`.`blocked`" . $sql_tag_nets,
832 local_user(), TERM_OBJ_POST, TERM_HASHTAG, $top_limit, $bottom_limit);
834 $data = dba::inArray($items);
836 if (count($data) > 0) {
837 $tag_top_limit = current($data)['order_date'];
838 if ($_SESSION['network_last_date'] < $tag_top_limit) {
839 $_SESSION['network_last_date'] = $tag_top_limit;
842 logger('Tagged items: ' . count($data) . ' - ' . $bottom_limit . ' - ' . $top_limit . ' - ' . local_user().' - '.(int)$update);
844 foreach ($r as $item) {
845 $s[$item['uri']] = $item;
847 foreach ($data as $item) {
848 $s[$item['uri']] = $item;
859 if (DBM::is_result($items)) {
862 foreach ($items as $item) {
863 if ($date_offset < $item['order_date']) {
864 $date_offset = $item['order_date'];
866 if (!in_array($item['item_id'], $parents_arr) && ($item['item_id'] > 0)) {
867 $parents_arr[] = $item['item_id'];
870 $parents_str = implode(', ', $parents_arr);
873 if (x($_GET, 'offset')) {
874 $date_offset = $_GET['offset'];
877 $a->page_offset = $date_offset;
879 // We aren't going to try and figure out at the item, group, and page
880 // level which items you've seen and which you haven't. If you're looking
881 // at the top level network page just mark everything seen.
883 if (!$gid && !$cid && !$star) {
884 $condition = ['unseen' => true, 'uid' => local_user()];
885 networkSetSeen($condition);
886 } elseif ($parents_str) {
887 $condition = ["`uid` = ? AND `unseen` AND `parent` IN (" . dbesc($parents_str) . ")", local_user()];
888 networkSetSeen($condition);
893 $o .= networkConversation($a, $items, $mode, $update, $ordering);
899 * @brief Get the network tabs menu
901 * @param App $a The global App
902 * @return string Html of the networktab
904 function network_tabs(App $a)
907 /// @TODO fix this logic, reduce duplication
908 /// $a->page['content'] .= '<div class="tabs-wrapper">';
909 list($no_active, $all_active, $postord_active, $conv_active, $new_active, $starred_active, $bookmarked_active, $spam_active) = network_query_get_sel_tab($a);
911 // if no tabs are selected, defaults to comments
912 if ($no_active == 'active') {
913 $all_active = 'active';
921 'label' => L10n::t('Commented Order'),
922 'url' => str_replace('/new', '', $cmd) . '?f=&order=comment' . ((x($_GET,'cid')) ? '&cid=' . $_GET['cid'] : ''),
923 'sel' => $all_active,
924 'title' => L10n::t('Sort by Comment Date'),
925 'id' => 'commented-order-tab',
929 'label' => L10n::t('Posted Order'),
930 'url' => str_replace('/new', '', $cmd) . '?f=&order=post' . ((x($_GET,'cid')) ? '&cid=' . $_GET['cid'] : ''),
931 'sel' => $postord_active,
932 'title' => L10n::t('Sort by Post Date'),
933 'id' => 'posted-order-tab',
938 if (Feature::isEnabled(local_user(), 'personal_tab')) {
940 'label' => L10n::t('Personal'),
941 'url' => str_replace('/new', '', $cmd) . ((x($_GET,'cid')) ? '/?f=&cid=' . $_GET['cid'] : '/?f=') . '&conv=1',
942 'sel' => $conv_active,
943 'title' => L10n::t('Posts that mention or involve you'),
944 'id' => 'personal-tab',
949 if (Feature::isEnabled(local_user(), 'new_tab')) {
951 'label' => L10n::t('New'),
952 'url' => 'network/new' . ((x($_GET,'cid')) ? '/?f=&cid=' . $_GET['cid'] : ''),
953 'sel' => $new_active,
954 'title' => L10n::t('Activity Stream - by date'),
955 'id' => 'activitiy-by-date-tab',
960 if (Feature::isEnabled(local_user(), 'link_tab')) {
962 'label' => L10n::t('Shared Links'),
963 'url' => str_replace('/new', '', $cmd) . ((x($_GET,'cid')) ? '/?f=&cid=' . $_GET['cid'] : '/?f=') . '&bmark=1',
964 'sel' => $bookmarked_active,
965 'title' => L10n::t('Interesting Links'),
966 'id' => 'shared-links-tab',
971 if (Feature::isEnabled(local_user(), 'star_posts')) {
973 'label' => L10n::t('Starred'),
974 'url' => str_replace('/new', '', $cmd) . ((x($_GET,'cid')) ? '/?f=&cid=' . $_GET['cid'] : '/?f=') . '&star=1',
975 'sel' => $starred_active,
976 'title' => L10n::t('Favourite Posts'),
977 'id' => 'starred-posts-tab',
982 // save selected tab, but only if not in file mode
983 if (!x($_GET, 'file')) {
984 PConfig::set(local_user(), 'network.view', 'tab.selected', [
985 $all_active, $postord_active, $conv_active, $new_active, $starred_active, $bookmarked_active, $spam_active
989 $arr = ['tabs' => $tabs];
990 Addon::callHooks('network_tabs', $arr);
992 $tpl = get_markup_template('common_tabs.tpl');
994 return replace_macros($tpl, ['$tabs' => $arr['tabs']]);
996 // --- end item filter tabs