4 * @file mod/network.php
7 use Friendica\Content\Feature;
8 use Friendica\Content\ForumManager;
9 use Friendica\Content\Nav;
10 use Friendica\Content\Widget;
11 use Friendica\Core\Addon;
12 use Friendica\Core\L10n;
13 use Friendica\Core\Config;
14 use Friendica\Core\PConfig;
15 use Friendica\Core\System;
16 use Friendica\Database\DBM;
17 use Friendica\Model\Contact;
18 use Friendica\Model\Group;
19 use Friendica\Model\Profile;
20 use Friendica\Module\Login;
22 require_once 'include/conversation.php';
23 require_once 'include/items.php';
24 require_once 'include/acl_selectors.php';
26 function network_init(App $a)
29 notice(L10n::t('Permission denied.') . EOL);
33 $search = (x($_GET, 'search') ? escape_tags($_GET['search']) : '');
35 if (($search != '') && !empty($_GET['submit'])) {
36 goaway('search?search=' . urlencode($search));
39 if (x($_GET, 'save')) {
40 $exists = dba::exists('search', ['uid' => local_user(), 'term' => $search]);
42 dba::insert('search', ['uid' => local_user(), 'term' => $search]);
45 if (x($_GET, 'remove')) {
46 dba::delete('search', ['uid' => local_user(), 'term' => $search]);
49 $is_a_date_query = false;
51 $group_id = (($a->argc > 1 && is_numeric($a->argv[1])) ? intval($a->argv[1]) : 0);
54 if (x($_GET, 'cid') && intval($_GET['cid']) != 0) {
56 $_GET['nets'] = 'all';
61 for ($x = 1; $x < $a->argc; $x ++) {
62 if (is_a_date_arg($a->argv[$x])) {
63 $is_a_date_query = true;
69 // convert query string to array. remove friendica args
71 $query_string = str_replace($a->cmd . '?', '', $a->query_string);
72 parse_str($query_string, $query_array);
73 array_shift($query_array);
75 // fetch last used network view and redirect if needed
76 if (!$is_a_date_query) {
77 $sel_nets = defaults($_GET, 'nets', false);
78 $sel_tabs = network_query_get_sel_tab($a);
79 $sel_groups = network_query_get_sel_group($a);
80 $last_sel_tabs = PConfig::get(local_user(), 'network.view', 'tab.selected');
82 $remember_tab = ($sel_tabs[0] === 'active' && is_array($last_sel_tabs) && $last_sel_tabs[0] !== 'active');
84 $net_baseurl = '/network';
87 if ($sel_groups !== false) {
88 $net_baseurl .= '/' . $sel_groups;
92 // redirect if current selected tab is '/network' and
93 // last selected tab is _not_ '/network?f=&order=comment'.
94 // and this isn't a date query
106 'f=&order=comment', //all
107 'f=&order=post', //postord
110 'f=&star=1', //starred
111 'f=&bmark=1', //bookmarked
115 $k = array_search('active', $last_sel_tabs);
118 $net_baseurl .= $tab_baseurls[$k];
120 // parse out tab queries
122 $dest_qs = $tab_args[$k];
123 parse_str($dest_qs, $dest_qa);
124 $net_args = array_merge($net_args, $dest_qa);
126 $remember_tab = false;
130 if ($sel_nets !== false) {
131 $net_args['nets'] = $sel_nets;
135 $net_args = array_merge($query_array, $net_args);
136 $net_queries = build_querystring($net_args);
138 $redir_url = ($net_queries ? $net_baseurl . '?' . $net_queries : $net_baseurl);
140 goaway(System::baseUrl() . $redir_url);
144 // If nets is set to all, unset it
145 if (x($_GET, 'nets') && $_GET['nets'] === 'all') {
146 unset($_GET['nets']);
149 if (!x($a->page, 'aside')) {
150 $a->page['aside'] = '';
153 $a->page['aside'] .= (Feature::isEnabled(local_user(), 'groups') ?
154 Group::sidebarWidget('network/0', 'network', 'standard', $group_id) : '');
155 $a->page['aside'] .= (Feature::isEnabled(local_user(), 'forumlist_widget') ? ForumManager::widget(local_user(), $cid) : '');
156 $a->page['aside'] .= posted_date_widget('network', local_user(), false);
157 $a->page['aside'] .= Widget::networks('network', (x($_GET, 'nets') ? $_GET['nets'] : ''));
158 $a->page['aside'] .= saved_searches($search);
159 $a->page['aside'] .= Widget::fileAs('network', (x($_GET, 'file') ? $_GET['file'] : ''));
162 function saved_searches($search)
164 if (!Feature::isEnabled(local_user(), 'savedsearch')) {
170 $srchurl = '/network?f='
171 . ((x($_GET, 'cid')) ? '&cid=' . $_GET['cid'] : '')
172 . ((x($_GET, 'star')) ? '&star=' . $_GET['star'] : '')
173 . ((x($_GET, 'bmark')) ? '&bmark=' . $_GET['bmark'] : '')
174 . ((x($_GET, 'conv')) ? '&conv=' . $_GET['conv'] : '')
175 . ((x($_GET, 'nets')) ? '&nets=' . $_GET['nets'] : '')
176 . ((x($_GET, 'cmin')) ? '&cmin=' . $_GET['cmin'] : '')
177 . ((x($_GET, 'cmax')) ? '&cmax=' . $_GET['cmax'] : '')
178 . ((x($_GET, 'file')) ? '&file=' . $_GET['file'] : '');
183 $terms = dba::select('search', ['id', 'term'], ['uid' => local_user()]);
186 while ($rr = dba::fetch($terms)) {
189 'term' => $rr['term'],
190 'encodedterm' => urlencode($rr['term']),
191 'delete' => L10n::t('Remove term'),
192 'selected' => ($search == $rr['term']),
196 $tpl = get_markup_template('saved_searches_aside.tpl');
197 $o = replace_macros($tpl, [
198 '$title' => L10n::t('Saved Searches'),
199 '$add' => L10n::t('add'),
200 '$searchbox' => search($search, 'netsearch-box', $srchurl, true),
208 * Return selected tab from query
211 * '/network' => $no_active = 'active'
212 * '/network?f=&order=comment' => $comment_active = 'active'
213 * '/network?f=&order=post' => $postord_active = 'active'
214 * '/network?f=&conv=1', => $conv_active = 'active'
215 * '/network/new', => $new_active = 'active'
216 * '/network?f=&star=1', => $starred_active = 'active'
217 * '/network?f=&bmark=1', => $bookmarked_active = 'active'
218 * '/network?f=&spam=1', => $spam_active = 'active'
220 * @return Array ($no_active, $comment_active, $postord_active, $conv_active, $new_active, $starred_active, $bookmarked_active, $spam_active);
222 function network_query_get_sel_tab(App $a)
225 $starred_active = '';
227 $bookmarked_active = '';
231 $postord_active = '';
233 if (($a->argc > 1 && $a->argv[1] === 'new') || ($a->argc > 2 && $a->argv[2] === 'new')) {
234 $new_active = 'active';
237 if (x($_GET, 'star')) {
238 $starred_active = 'active';
241 if (x($_GET, 'bmark')) {
242 $bookmarked_active = 'active';
245 if (x($_GET, 'conv')) {
246 $conv_active = 'active';
249 if (x($_GET, 'spam')) {
250 $spam_active = 'active';
253 if (($new_active == '') && ($starred_active == '') && ($bookmarked_active == '') && ($conv_active == '') && ($spam_active == '')) {
254 $no_active = 'active';
257 if ($no_active == 'active' && x($_GET, 'order')) {
258 switch($_GET['order']) {
259 case 'post' : $postord_active = 'active'; $no_active=''; break;
260 case 'comment' : $all_active = 'active'; $no_active=''; break;
264 return [$no_active, $all_active, $postord_active, $conv_active, $new_active, $starred_active, $bookmarked_active, $spam_active];
267 function network_query_get_sel_group(App $a)
271 if ($a->argc >= 2 && is_numeric($a->argv[1])) {
272 $group = $a->argv[1];
279 * @brief Sets the pager data and returns SQL
281 * @param App $a The global App
282 * @param integer $update Used for the automatic reloading
283 * @return string SQL with the appropriate LIMIT clause
285 function networkPager($a, $update)
288 // only setup pagination on initial page view
292 // check if we serve a mobile device and get the user settings
295 $itemspage_network = PConfig::get(local_user(), 'system', 'itemspage_mobile_network');
296 $itemspage_network = ((intval($itemspage_network)) ? $itemspage_network : 20);
298 $itemspage_network = PConfig::get(local_user(), 'system', 'itemspage_network');
299 $itemspage_network = ((intval($itemspage_network)) ? $itemspage_network : 40);
302 // now that we have the user settings, see if the theme forces
303 // a maximum item number which is lower then the user choice
304 if (($a->force_max_items > 0) && ($a->force_max_items < $itemspage_network)) {
305 $itemspage_network = $a->force_max_items;
308 $a->set_pager_itemspage($itemspage_network);
310 return sprintf(" LIMIT %d, %d ", intval($a->pager['start']), intval($a->pager['itemspage']));
314 * @brief Sets items as seen
316 * @param array $condition The array with the SQL condition
318 function networkSetSeen($condition)
320 if (empty($condition)) {
324 $unseen = dba::exists('item', $condition);
327 $r = dba::update('item', ['unseen' => false], $condition);
332 * @brief Create the conversation HTML
334 * @param App $a The global App
335 * @param array $items Items of the conversation
336 * @param string $mode Display mode for the conversation
337 * @param integer $update Used for the automatic reloading
338 * @return string HTML of the conversation
340 function networkConversation($a, $items, $mode, $update)
342 // Set this so that the conversation function can find out contact info for our wall-wall items
343 $a->page_contact = $a->contact;
345 $o = conversation($a, $items, $mode, $update);
348 if (PConfig::get(local_user(), 'system', 'infinite_scroll')) {
349 $o .= scroll_loader();
351 $o .= alt_pager($a, count($items));
358 function network_content(App $a, $update = 0)
361 return Login::form();
364 /// @TODO Is this really necessary? $a is already available to hooks
365 $arr = ['query' => $a->query_string];
366 Addon::callHooks('network_content_init', $arr);
371 for ($x = 1; $x < $a->argc; $x ++) {
372 if ($a->argv[$x] === 'new') {
378 if (x($_GET, 'file')) {
383 $o = networkFlatView($a, $update);
385 $o = networkThreadedView($a, $update);
392 * @brief Get the network content in flat view
394 * @param App $a The global App
395 * @param integer $update Used for the automatic reloading
396 * @return string HTML of the network content in flat view
398 function networkFlatView(App $a, $update = 0)
400 // Rawmode is used for fetching new content at the end of the page
401 $rawmode = (isset($_GET['mode']) AND ( $_GET['mode'] == 'raw'));
403 if (isset($_GET['last_id'])) {
404 $last_id = intval($_GET['last_id']);
411 $file = ((x($_GET, 'file')) ? $_GET['file'] : '');
413 if (!$update && !$rawmode) {
414 $tabs = network_tabs($a);
417 Nav::setSelected('network');
421 'allow_location' => $a->user['allow_location'],
422 'default_location' => $a->user['default-location'],
423 'nickname' => $a->user['nickname'],
424 'lockstate' => (((is_array($a->user) &&
425 ((strlen($a->user['allow_cid'])) || (strlen($a->user['allow_gid'])) ||
426 (strlen($a->user['deny_cid'])) || (strlen($a->user['deny_gid']))))) ? 'lock' : 'unlock'),
427 'default_perms' => get_acl_permissions($a->user),
428 'acl' => populate_acl($a->user, true),
430 'visitor' => 'block',
431 'profile_uid' => local_user(),
435 $o .= status_editor($a, $x);
437 if (!Config::get('theme', 'hide_eventlist')) {
438 $o .= Profile::getBirthdays();
439 $o .= Profile::getEvents();
444 $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` ",
445 dbesc(protect_sprintf($file)), intval(TERM_OBJ_POST), intval(TERM_FILE), intval(local_user()));
447 $sql_post_table = " INNER JOIN `thread` ON `thread`.`iid` = `item`.`parent`";
450 $pager_sql = networkPager($a, $update);
452 // show all items unthreaded in reverse created date order
453 $items = q("SELECT %s FROM `item` $sql_post_table %s
454 WHERE %s AND `item`.`uid` = %d
455 ORDER BY `item`.`id` DESC $pager_sql ",
456 item_fieldlists(), item_joins(), item_condition(),
457 intval($_SESSION['uid'])
460 $condition = ['unseen' => true, 'uid' => local_user()];
461 networkSetSeen($condition);
463 $mode = 'network-new';
464 $o .= networkConversation($a, $items, $mode, $update);
470 * @brief Get the network content in threaded view
472 * @param App $a The global App
473 * @param integer $update Used for the automatic reloading
474 * @return string HTML of the network content in flat view
476 function networkThreadedView(App $a, $update = 0)
478 // Rawmode is used for fetching new content at the end of the page
479 $rawmode = (isset($_GET['mode']) AND ( $_GET['mode'] == 'raw'));
481 if (isset($_GET['last_received']) && isset($_GET['last_commented']) && isset($_GET['last_created']) && isset($_GET['last_id'])) {
482 $last_received = DBM::date($_GET['last_received']);
483 $last_commented = DBM::date($_GET['last_commented']);
484 $last_created = DBM::date($_GET['last_created']);
485 $last_id = intval($_GET['last_id']);
488 $last_commented = '';
493 $datequery = $datequery2 = '';
498 for ($x = 1; $x < $a->argc; $x ++) {
499 if (is_a_date_arg($a->argv[$x])) {
501 $datequery2 = escape_tags($a->argv[$x]);
503 $datequery = escape_tags($a->argv[$x]);
504 $_GET['order'] = 'post';
506 } elseif (intval($a->argv[$x])) {
507 $gid = intval($a->argv[$x]);
508 $def_acl = ['allow_gid' => '<' . $gid . '>'];
515 $cid = intval(defaults($_GET, 'cid' , 0));
516 $star = intval(defaults($_GET, 'star' , 0));
517 $bmark = intval(defaults($_GET, 'bmark', 0));
518 $conv = intval(defaults($_GET, 'conv' , 0));
519 $order = notags(defaults($_GET, 'order', 'comment'));
520 $nets = defaults($_GET, 'nets' , '');
523 $def_acl = ['allow_cid' => '<' . intval($cid) . '>'];
527 $r = dba::select('contact', ['id'], ['uid' => local_user(), 'network' => $nets], ['self' => false]);
530 while ($rr = dba::fetch($r)) {
531 $str .= '<' . $rr['id'] . '>';
534 $def_acl = ['allow_cid' => $str];
538 if (!$update && !$rawmode) {
539 $tabs = network_tabs($a);
543 if (($t = Contact::getOStatusCountByGroupId($gid)) && !PConfig::get(local_user(), 'system', 'nowarn_insecure')) {
544 notice(L10n::tt("Warning: This group contains %s member from a network that doesn't allow non public messages.",
545 "Warning: This group contains %s members from a network that doesn't allow non public messages.",
547 notice(L10n::t("Messages in this group won't be send to these receivers.").EOL);
551 Nav::setSelected('network');
556 // If $cid belongs to a communitity forum or a privat goup,.add a mention to the status editor
557 $condition = ["`id` = ? AND (`forum` OR `prv`)", $cid];
558 $contact = dba::selectFirst('contact', ['addr', 'nick'], $condition);
559 if (DBM::is_result($contact)) {
560 if ($contact['addr'] != '') {
561 $content = '!' . $contact['addr'];
563 $content = '!' . $contact['nick'] . '+' . $cid;
570 'allow_location' => $a->user['allow_location'],
571 'default_location' => $a->user['default-location'],
572 'nickname' => $a->user['nickname'],
573 'lockstate' => ((($gid) || ($cid) || ($nets) || (is_array($a->user) &&
574 ((strlen($a->user['allow_cid'])) || (strlen($a->user['allow_gid'])) ||
575 (strlen($a->user['deny_cid'])) || (strlen($a->user['deny_gid']))))) ? 'lock' : 'unlock'),
576 'default_perms' => get_acl_permissions($a->user),
577 'acl' => populate_acl((($gid || $cid || $nets) ? $def_acl : $a->user), true),
578 'bang' => (($gid || $cid || $nets) ? '!' : ''),
579 'visitor' => 'block',
580 'profile_uid' => local_user(),
581 'content' => $content,
584 $o .= status_editor($a, $x);
587 // We don't have to deal with ACLs on this page. You're looking at everything
588 // that belongs to you, hence you can see all of it. We will filter by group if
591 $sql_post_table = '';
592 $sql_options = (($star) ? " AND `thread`.`starred` " : '');
593 $sql_options .= (($bmark) ? " AND `thread`.`bookmark` " : '');
594 $sql_extra = $sql_options;
597 $sql_table = '`thread`';
598 $sql_parent = '`iid`';
601 $sql_table = '`item`';
602 $sql_parent = '`parent`';
603 $sql_post_table = " INNER JOIN `thread` ON `thread`.`iid` = `item`.`parent`";
606 $sql_nets = (($nets) ? sprintf(" AND $sql_table.`network` = '%s' ", dbesc($nets)) : '');
607 $sql_tag_nets = (($nets) ? sprintf(" AND `item`.`network` = '%s' ", dbesc($nets)) : '');
610 $group = dba::selectFirst('group', ['name'], ['id' => $gid, 'uid' => $_SESSION['uid']]);
611 if (!DBM::is_result($group)) {
615 notice(L10n::t('No such group') . EOL);
620 $contacts = Group::expand([$gid]);
622 if ((is_array($contacts)) && count($contacts)) {
623 $contact_str_self = '';
625 $contact_str = implode(',', $contacts);
626 $self = dba::selectFirst('contact', ['id'], ['uid' => $_SESSION['uid'], 'self' => true]);
627 if (DBM::is_result($self)) {
628 $contact_str_self = $self['id'];
631 $sql_post_table .= " INNER JOIN `item` AS `temp1` ON `temp1`.`id` = " . $sql_table . "." . $sql_parent;
632 $sql_extra3 .= " AND (`thread`.`contact-id` IN ($contact_str) ";
633 $sql_extra3 .= " OR (`thread`.`contact-id` = '$contact_str_self' AND `temp1`.`allow_gid` LIKE '" . protect_sprintf('%<' . intval($gid) . '>%') . "' AND `temp1`.`private`))";
635 $sql_extra3 .= " AND false ";
636 info(L10n::t('Group is empty'));
639 $o = replace_macros(get_markup_template('section_title.tpl'), [
640 '$title' => L10n::t('Group: %s', $group['name'])
643 $fields = ['id', 'name', 'network', 'writable', 'nurl',
644 'forum', 'prv', 'contact-type', 'addr', 'thumb', 'location'];
645 $condition = ["`id` = ? AND (NOT `blocked` OR `pending`)", $cid];
646 $contact = dba::selectFirst('contact', $fields, $condition);
647 if (DBM::is_result($contact)) {
648 $sql_extra = " AND " . $sql_table . ".`contact-id` = " . intval($cid);
652 'name' => htmlentities($contact['name']),
653 'itemurl' => defaults($contact, 'addr', $contact['nurl']),
654 'thumb' => proxy_url($contact['thumb'], false, PROXY_SIZE_THUMB),
655 'details' => $contact['location'],
658 $entries[0]['account_type'] = Contact::getAccountType($contact);
660 $o = replace_macros(get_markup_template('viewcontact_template.tpl'), [
661 'contacts' => $entries,
665 if ($contact['network'] === NETWORK_OSTATUS && $contact['writable'] && !PConfig::get(local_user(),'system','nowarn_insecure')) {
666 notice(L10n::t('Private messages to this person are at risk of public disclosure.') . EOL);
669 notice(L10n::t('Invalid contact.') . EOL);
675 if (!$gid && !$cid && !$update && !Config::get('theme', 'hide_eventlist')) {
676 $o .= Profile::getBirthdays();
677 $o .= Profile::getEvents();
681 $sql_extra3 .= protect_sprintf(sprintf(" AND $sql_table.created <= '%s' ",
682 dbesc(datetime_convert(date_default_timezone_get(), '', $datequery))));
685 $sql_extra3 .= protect_sprintf(sprintf(" AND $sql_table.created >= '%s' ",
686 dbesc(datetime_convert(date_default_timezone_get(), '', $datequery2))));
690 $order_mode = 'received';
693 $sql_extra3 .= " AND $sql_table.`mention`";
696 // Normal conversation view
697 if ($order === 'post') {
698 $ordering = '`created`';
699 if ($sql_order == '') {
700 $order_mode = 'created';
703 $ordering = '`commented`';
704 if ($sql_order == '') {
705 $order_mode = 'commented';
709 if ($sql_order == '') {
710 $sql_order = "$sql_table.$ordering";
713 if (x($_GET, 'offset')) {
714 $sql_range = sprintf(" AND $sql_order <= '%s'", dbesc($_GET['offset']));
719 $pager_sql = networkPager($a, $update);
723 switch ($order_mode) {
725 if ($last_received != '') {
726 $last_date = $last_received;
727 $sql_range .= sprintf(" AND $sql_table.`received` < '%s'", dbesc($last_received));
728 $a->set_pager_page(1);
729 $pager_sql = sprintf(" LIMIT %d, %d ", intval($a->pager['start']), intval($a->pager['itemspage']));
733 if ($last_commented != '') {
734 $last_date = $last_commented;
735 $sql_range .= sprintf(" AND $sql_table.`commented` < '%s'", dbesc($last_commented));
736 $a->set_pager_page(1);
737 $pager_sql = sprintf(" LIMIT %d, %d ", intval($a->pager['start']), intval($a->pager['itemspage']));
741 if ($last_created != '') {
742 $last_date = $last_created;
743 $sql_range .= sprintf(" AND $sql_table.`created` < '%s'", dbesc($last_created));
744 $a->set_pager_page(1);
745 $pager_sql = sprintf(" LIMIT %d, %d ", intval($a->pager['start']), intval($a->pager['itemspage']));
749 if (($last_id > 0) && ($sql_table == '`thread`')) {
750 $sql_range .= sprintf(" AND $sql_table.`iid` < '%s'", dbesc($last_id));
751 $a->set_pager_page(1);
752 $pager_sql = sprintf(" LIMIT %d, %d ", intval($a->pager['start']), intval($a->pager['itemspage']));
757 // Fetch a page full of parent items for this page
759 if (Config::get('system', 'like_no_comment')) {
760 $sql_extra4 = " AND `item`.`verb` = '" . ACTIVITY_POST . "'";
764 $r = q("SELECT `item`.`parent` AS `item_id`, `item`.`network` AS `item_network`, `contact`.`uid` AS `contact_uid`, $sql_order AS `order_date`
765 FROM $sql_table $sql_post_table INNER JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
766 AND (NOT `contact`.`blocked` OR `contact`.`pending`)
767 WHERE `item`.`uid` = %d AND `item`.`visible` AND NOT `item`.`deleted` $sql_extra4
768 AND NOT `item`.`moderated` AND `item`.`unseen`
769 $sql_extra3 $sql_extra $sql_range $sql_nets
770 ORDER BY `order_date` DESC LIMIT 100",
774 $r = q("SELECT `thread`.`iid` AS `item_id`, `thread`.`network` AS `item_network`, `contact`.`uid` AS `contact_uid`, $sql_order AS `order_date`
775 FROM $sql_table $sql_post_table STRAIGHT_JOIN `contact` ON `contact`.`id` = `thread`.`contact-id`
776 AND (NOT `contact`.`blocked` OR `contact`.`pending`)
777 WHERE `thread`.`uid` = %d AND `thread`.`visible` AND NOT `thread`.`deleted`
778 AND NOT `thread`.`moderated`
779 $sql_extra2 $sql_extra3 $sql_range $sql_extra $sql_nets
780 ORDER BY $sql_order DESC $pager_sql",
785 // Only show it when unfiltered (no groups, no networks, ...)
786 if (Config::get('system', 'comment_public') && in_array($nets, ['', NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_OSTATUS]) && (strlen($sql_extra . $sql_extra2 . $sql_extra3) == 0)) {
787 if (DBM::is_result($r)) {
788 $top_limit = current($r)['order_date'];
789 $bottom_limit = end($r)['order_date'];
791 $top_limit = datetime_convert();
792 $bottom_limit = datetime_convert();
795 // When checking for updates we need to fetch from the newest date to the newest date before
796 if ($update && !empty($_SESSION['network_last_date']) && ($bottom_limit > $_SESSION['network_last_date'])) {
797 $bottom_limit = $_SESSION['network_last_date'];
799 $_SESSION['network_last_date'] = $top_limit;
801 if ($last_date > $top_limit) {
802 $top_limit = $last_date;
803 } elseif ($a->pager['page'] == 1) {
804 // Highest possible top limit when we are on the first page
805 $top_limit = datetime_convert();
808 $items = dba::p("SELECT `item`.`id` AS `item_id`, `item`.`network` AS `item_network`, `contact`.`uid` AS `contact_uid` FROM `item`
809 INNER JOIN (SELECT `oid` FROM `term` WHERE `term` IN
810 (SELECT SUBSTR(`term`, 2) FROM `search` WHERE `uid` = ? AND `term` LIKE '#%') AND `otype` = ? AND `type` = ? AND `uid` = 0) AS `term`
811 ON `item`.`id` = `term`.`oid`
812 INNER JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
813 WHERE `item`.`uid` = 0 AND `item`.$ordering < ? AND `item`.$ordering > ?" . $sql_tag_nets,
814 local_user(), TERM_OBJ_POST, TERM_HASHTAG, $top_limit, $bottom_limit);
815 $data = dba::inArray($items);
817 if (count($data) > 0) {
818 logger('Tagged items: ' . count($data) . ' - ' . $bottom_limit . ' - ' . $top_limit . ' - ' . local_user()); //$last_date);
819 $r = array_merge($data, $r);
823 // Then fetch all the children of the parents that are on this page
830 if (DBM::is_result($r)) {
831 foreach ($r as $rr) {
832 if (!in_array($rr['item_id'], $parents_arr)) {
833 $parents_arr[] = $rr['item_id'];
837 $parents_str = implode(', ', $parents_arr);
839 // splitted into separate queries to avoid the problem with very long threads
840 // so always the last X comments are loaded
841 // This problem can occur expecially with imported facebook posts
842 $max_comments = Config::get('system', 'max_comments');
843 if ($max_comments == 0) {
847 foreach ($parents_arr AS $parents) {
848 $thread_items = dba::p(item_query() . " AND `item`.`parent` = ?
849 ORDER BY `item`.`commented` DESC LIMIT " . intval($max_comments + 1),
853 if (DBM::is_result($thread_items)) {
854 $items = array_merge($items, dba::inArray($thread_items));
857 $items = conv_sort($items, $ordering);
860 if (x($_GET, 'offset')) {
861 $date_offset = $_GET['offset'];
862 } elseif (count($items)) {
863 $date_offset = $items[0][$order_mode];
868 $a->page_offset = $date_offset;
870 // We aren't going to try and figure out at the item, group, and page
871 // level which items you've seen and which you haven't. If you're looking
872 // at the top level network page just mark everything seen.
874 if (!$gid && !$cid && !$star) {
875 $condition = ['unseen' => true, 'uid' => local_user()];
876 networkSetSeen($condition);
877 } elseif ($parents_str) {
878 $condition = ["`uid` = ? AND `unseen` AND `parent` IN (" . dbesc($parents_str) . ")", local_user()];
879 networkSetSeen($condition);
884 $o .= networkConversation($a, $items, $mode, $update);
890 * @brief Get the network tabs menu
892 * @param App $a The global App
893 * @return string Html of the networktab
895 function network_tabs(App $a)
898 /// @TODO fix this logic, reduce duplication
899 /// $a->page['content'] .= '<div class="tabs-wrapper">';
900 list($no_active, $all_active, $postord_active, $conv_active, $new_active, $starred_active, $bookmarked_active, $spam_active) = network_query_get_sel_tab($a);
902 // if no tabs are selected, defaults to comments
903 if ($no_active == 'active') {
904 $all_active = 'active';
912 'label' => L10n::t('Commented Order'),
913 'url' => str_replace('/new', '', $cmd) . '?f=&order=comment' . ((x($_GET,'cid')) ? '&cid=' . $_GET['cid'] : ''),
914 'sel' => $all_active,
915 'title' => L10n::t('Sort by Comment Date'),
916 'id' => 'commented-order-tab',
920 'label' => L10n::t('Posted Order'),
921 'url' => str_replace('/new', '', $cmd) . '?f=&order=post' . ((x($_GET,'cid')) ? '&cid=' . $_GET['cid'] : ''),
922 'sel' => $postord_active,
923 'title' => L10n::t('Sort by Post Date'),
924 'id' => 'posted-order-tab',
929 if (Feature::isEnabled(local_user(), 'personal_tab')) {
931 'label' => L10n::t('Personal'),
932 'url' => str_replace('/new', '', $cmd) . ((x($_GET,'cid')) ? '/?f=&cid=' . $_GET['cid'] : '/?f=') . '&conv=1',
933 'sel' => $conv_active,
934 'title' => L10n::t('Posts that mention or involve you'),
935 'id' => 'personal-tab',
940 if (Feature::isEnabled(local_user(), 'new_tab')) {
942 'label' => L10n::t('New'),
943 'url' => 'network/new' . ((x($_GET,'cid')) ? '/?f=&cid=' . $_GET['cid'] : ''),
944 'sel' => $new_active,
945 'title' => L10n::t('Activity Stream - by date'),
946 'id' => 'activitiy-by-date-tab',
951 if (Feature::isEnabled(local_user(), 'link_tab')) {
953 'label' => L10n::t('Shared Links'),
954 'url' => str_replace('/new', '', $cmd) . ((x($_GET,'cid')) ? '/?f=&cid=' . $_GET['cid'] : '/?f=') . '&bmark=1',
955 'sel' => $bookmarked_active,
956 'title' => L10n::t('Interesting Links'),
957 'id' => 'shared-links-tab',
962 if (Feature::isEnabled(local_user(), 'star_posts')) {
964 'label' => L10n::t('Starred'),
965 'url' => str_replace('/new', '', $cmd) . ((x($_GET,'cid')) ? '/?f=&cid=' . $_GET['cid'] : '/?f=') . '&star=1',
966 'sel' => $starred_active,
967 'title' => L10n::t('Favourite Posts'),
968 'id' => 'starred-posts-tab',
973 // save selected tab, but only if not in file mode
974 if (!x($_GET, 'file')) {
975 PConfig::set(local_user(), 'network.view', 'tab.selected', [
976 $all_active, $postord_active, $conv_active, $new_active, $starred_active, $bookmarked_active, $spam_active
980 $arr = ['tabs' => $tabs];
981 Addon::callHooks('network_tabs', $arr);
983 $tpl = get_markup_template('common_tabs.tpl');
985 return replace_macros($tpl, ['$tabs' => $arr['tabs']]);
987 // --- end item filter tabs