4 * @file mod/network.php
8 use Friendica\Content\Feature;
9 use Friendica\Content\ForumManager;
10 use Friendica\Content\Nav;
11 use Friendica\Content\Pager;
12 use Friendica\Content\Widget;
13 use Friendica\Content\Text\HTML;
14 use Friendica\Core\ACL;
15 use Friendica\Core\Config;
16 use Friendica\Core\Hook;
17 use Friendica\Core\L10n;
18 use Friendica\Core\Logger;
19 use Friendica\Core\PConfig;
20 use Friendica\Core\Protocol;
21 use Friendica\Core\Renderer;
22 use Friendica\Core\Session;
23 use Friendica\Database\DBA;
24 use Friendica\Model\Contact;
25 use Friendica\Model\Group;
26 use Friendica\Model\Item;
27 use Friendica\Model\Profile;
28 use Friendica\Model\Term;
29 use Friendica\Module\Login;
30 use Friendica\Util\DateTimeFormat;
31 use Friendica\Util\Proxy as ProxyUtils;
32 use Friendica\Util\Strings;
34 function network_init(App $a)
37 notice(L10n::t('Permission denied.') . EOL);
41 Hook::add('head', __FILE__, 'network_infinite_scroll_head');
43 $is_a_date_query = false;
45 $group_id = (($a->argc > 1 && is_numeric($a->argv[1])) ? intval($a->argv[1]) : 0);
48 if (!empty($_GET['cid'])) {
55 for ($x = 1; $x < $a->argc; $x ++) {
56 if (is_a_date_arg($a->argv[$x])) {
57 $is_a_date_query = true;
63 // convert query string to array. remove friendica args
65 parse_str(parse_url($a->query_string, PHP_URL_QUERY), $query_array);
67 // fetch last used network view and redirect if needed
68 if (!$is_a_date_query) {
69 $sel_nets = defaults($_GET, 'nets', '');
70 $sel_tabs = network_query_get_sel_tab($a);
71 $sel_groups = network_query_get_sel_group($a);
72 $last_sel_tabs = PConfig::get(local_user(), 'network.view', 'tab.selected');
74 $remember_tab = ($sel_tabs[0] === 'active' && is_array($last_sel_tabs) && $last_sel_tabs[0] !== 'active');
76 $net_baseurl = '/network';
79 if ($sel_groups !== false) {
80 $net_baseurl .= '/' . $sel_groups;
84 // redirect if current selected tab is '/network' and
85 // last selected tab is _not_ '/network?order=comment'.
86 // and this isn't a date query
97 'order=comment', //all
98 'order=post', //postord
102 'bmark=1', //bookmarked
105 $k = array_search('active', $last_sel_tabs);
108 $net_baseurl .= $tab_baseurls[$k];
110 // parse out tab queries
112 $dest_qs = $tab_args[$k];
113 parse_str($dest_qs, $dest_qa);
114 $net_args = array_merge($net_args, $dest_qa);
116 $remember_tab = false;
121 $net_args['nets'] = $sel_nets;
125 $net_args = array_merge($query_array, $net_args);
126 $net_queries = http_build_query($net_args);
128 $redir_url = ($net_queries ? $net_baseurl . '?' . $net_queries : $net_baseurl);
130 $a->internalRedirect($redir_url);
134 if (empty($a->page['aside'])) {
135 $a->page['aside'] = '';
138 $a->page['aside'] .= Group::sidebarWidget('network/0', 'network', 'standard', $group_id);
139 $a->page['aside'] .= ForumManager::widget(local_user(), $cid);
140 $a->page['aside'] .= Widget::postedByYear('network', local_user(), false);
141 $a->page['aside'] .= Widget::networks('network', defaults($_GET, 'nets', '') );
142 $a->page['aside'] .= Widget\SavedSearches::getHTML($a->query_string);
143 $a->page['aside'] .= Widget::fileAs('network', defaults($_GET, 'file', '') );
147 * Return selected tab from query
150 * '/network' => $no_active = 'active'
151 * '/network?order=comment' => $comment_active = 'active'
152 * '/network?order=post' => $postord_active = 'active'
153 * '/network?conv=1', => $conv_active = 'active'
154 * '/network/new', => $new_active = 'active'
155 * '/network?star=1', => $starred_active = 'active'
156 * '/network?bmark=1', => $bookmarked_active = 'active'
159 * @return array ($no_active, $comment_active, $postord_active, $conv_active, $new_active, $starred_active, $bookmarked_active);
161 function network_query_get_sel_tab(App $a)
164 $starred_active = '';
166 $bookmarked_active = '';
169 $postord_active = '';
171 if (($a->argc > 1 && $a->argv[1] === 'new') || ($a->argc > 2 && $a->argv[2] === 'new')) {
172 $new_active = 'active';
175 if (!empty($_GET['star'])) {
176 $starred_active = 'active';
179 if (!empty($_GET['bmark'])) {
180 $bookmarked_active = 'active';
183 if (!empty($_GET['conv'])) {
184 $conv_active = 'active';
187 if (($new_active == '') && ($starred_active == '') && ($bookmarked_active == '') && ($conv_active == '')) {
188 $no_active = 'active';
191 if ($no_active == 'active' && !empty($_GET['order'])) {
192 switch($_GET['order']) {
193 case 'post' : $postord_active = 'active'; $no_active=''; break;
194 case 'comment' : $all_active = 'active'; $no_active=''; break;
198 return [$no_active, $all_active, $postord_active, $conv_active, $new_active, $starred_active, $bookmarked_active];
201 function network_query_get_sel_group(App $a)
205 if ($a->argc >= 2 && is_numeric($a->argv[1])) {
206 $group = $a->argv[1];
213 * @brief Sets the pager data and returns SQL
215 * @param App $a The global App
216 * @param Pager $pager
217 * @param integer $update Used for the automatic reloading
218 * @return string SQL with the appropriate LIMIT clause
219 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
221 function networkPager(App $a, Pager $pager, $update)
224 // only setup pagination on initial page view
228 // check if we serve a mobile device and get the user settings
231 $itemspage_network = PConfig::get(local_user(), 'system', 'itemspage_mobile_network');
232 $itemspage_network = ((intval($itemspage_network)) ? $itemspage_network : 20);
234 $itemspage_network = PConfig::get(local_user(), 'system', 'itemspage_network');
235 $itemspage_network = ((intval($itemspage_network)) ? $itemspage_network : 40);
238 // now that we have the user settings, see if the theme forces
239 // a maximum item number which is lower then the user choice
240 if (($a->force_max_items > 0) && ($a->force_max_items < $itemspage_network)) {
241 $itemspage_network = $a->force_max_items;
244 $pager->setItemsPerPage($itemspage_network);
246 return sprintf(" LIMIT %d, %d ", $pager->getStart(), $pager->getItemsPerPage());
250 * @brief Sets items as seen
252 * @param array $condition The array with the SQL condition
253 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
255 function networkSetSeen($condition)
257 if (empty($condition)) {
261 $unseen = Item::exists($condition);
264 Item::update(['unseen' => false], $condition);
269 * @brief Create the conversation HTML
271 * @param App $a The global App
272 * @param array $items Items of the conversation
273 * @param Pager $pager
274 * @param string $mode Display mode for the conversation
275 * @param integer $update Used for the automatic reloading
276 * @param string $ordering
277 * @return string HTML of the conversation
278 * @throws ImagickException
279 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
281 function networkConversation(App $a, $items, Pager $pager, $mode, $update, $ordering = '')
283 // Set this so that the conversation function can find out contact info for our wall-wall items
284 $a->page_contact = $a->contact;
286 if (!is_array($items)) {
287 Logger::log("Expecting items to be an array. Got " . print_r($items, true));
291 $o = conversation($a, $items, $pager, $mode, $update, false, $ordering, local_user());
294 if (PConfig::get(local_user(), 'system', 'infinite_scroll')) {
295 $o .= HTML::scrollLoader();
297 $o .= $pager->renderMinimal(count($items));
304 function network_content(App $a, $update = 0, $parent = 0)
307 return Login::form();
310 /// @TODO Is this really necessary? $a is already available to hooks
311 $arr = ['query' => $a->query_string];
312 Hook::callAll('network_content_init', $arr);
317 for ($x = 1; $x < $a->argc; $x ++) {
318 if ($a->argv[$x] === 'new') {
324 if (!empty($_GET['file'])) {
329 $o = networkFlatView($a, $update);
331 $o = networkThreadedView($a, $update, $parent);
335 info("No items found");
342 * @brief Get the network content in flat view
344 * @param App $a The global App
345 * @param integer $update Used for the automatic reloading
346 * @return string HTML of the network content in flat view
347 * @throws ImagickException
348 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
349 * @global Pager $pager
351 function networkFlatView(App $a, $update = 0)
354 // Rawmode is used for fetching new content at the end of the page
355 $rawmode = (isset($_GET['mode']) && ($_GET['mode'] == 'raw'));
359 $file = defaults($_GET, 'file', '');
361 if (!$update && !$rawmode) {
362 $tabs = network_tabs($a);
365 Nav::setSelected('network');
369 'allow_location' => $a->user['allow_location'],
370 'default_location' => $a->user['default-location'],
371 'nickname' => $a->user['nickname'],
372 'lockstate' => (is_array($a->user) &&
373 (strlen($a->user['allow_cid']) || strlen($a->user['allow_gid']) ||
374 strlen($a->user['deny_cid']) || strlen($a->user['deny_gid'])) ? 'lock' : 'unlock'),
375 'default_perms' => ACL::getDefaultUserPermissions($a->user),
376 'acl' => ACL::getFullSelectorHTML($a->user, true),
378 'visitor' => 'block',
379 'profile_uid' => local_user(),
383 $o .= status_editor($a, $x);
385 if (!Config::get('theme', 'hide_eventlist')) {
386 $o .= Profile::getBirthdays();
387 $o .= Profile::getEventsReminderHTML();
391 $pager = new Pager($a->query_string);
393 networkPager($a, $pager, $update);
395 $item_params = ['order' => ['id' => true]];
398 $term_condition = ["`term` = ? AND `otype` = ? AND `type` = ? AND `uid` = ?",
399 $file, Term::OBJECT_TYPE_POST, Term::FILE, local_user()];
400 $term_params = ['order' => ['tid' => true], 'limit' => [$pager->getStart(), $pager->getItemsPerPage()]];
401 $result = DBA::select('term', ['oid'], $term_condition, $term_params);
404 while ($term = DBA::fetch($result)) {
405 $posts[] = $term['oid'];
409 if (count($posts) == 0) {
412 $item_condition = ['uid' => local_user(), 'id' => $posts];
414 $item_condition = ['uid' => local_user()];
415 $item_params['limit'] = [$pager->getStart(), $pager->getItemsPerPage()];
417 networkSetSeen(['unseen' => true, 'uid' => local_user()]);
420 $result = Item::selectForUser(local_user(), [], $item_condition, $item_params);
421 $items = Item::inArray($result);
422 $o .= networkConversation($a, $items, $pager, 'network-new', $update);
428 * @brief Get the network content in threaded view
430 * @param App $a The global App
431 * @param integer $update Used for the automatic reloading
432 * @param integer $parent
433 * @return string HTML of the network content in flat view
434 * @throws ImagickException
435 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
436 * @global Pager $pager
438 function networkThreadedView(App $a, $update, $parent)
440 /// @TODO this will have to be converted to a static property of the converted Module\Network class
443 // Rawmode is used for fetching new content at the end of the page
444 $rawmode = (isset($_GET['mode']) AND ( $_GET['mode'] == 'raw'));
446 if (isset($_GET['last_received']) && isset($_GET['last_commented']) && isset($_GET['last_created']) && isset($_GET['last_id'])) {
447 $last_received = DateTimeFormat::utc($_GET['last_received']);
448 $last_commented = DateTimeFormat::utc($_GET['last_commented']);
449 $last_created = DateTimeFormat::utc($_GET['last_created']);
450 $last_id = intval($_GET['last_id']);
453 $last_commented = '';
458 $datequery = $datequery2 = '';
462 $default_permissions = [];
465 for ($x = 1; $x < $a->argc; $x ++) {
466 if (is_a_date_arg($a->argv[$x])) {
468 $datequery2 = Strings::escapeHtml($a->argv[$x]);
470 $datequery = Strings::escapeHtml($a->argv[$x]);
471 $_GET['order'] = 'post';
473 } elseif (intval($a->argv[$x])) {
474 $gid = intval($a->argv[$x]);
475 $default_permissions['allow_gid'] = [$gid];
482 $cid = intval(defaults($_GET, 'cid' , 0));
483 $star = intval(defaults($_GET, 'star' , 0));
484 $bmark = intval(defaults($_GET, 'bmark', 0));
485 $conv = intval(defaults($_GET, 'conv' , 0));
486 $order = Strings::escapeTags(defaults($_GET, 'order', 'comment'));
487 $nets = defaults($_GET, 'nets' , '');
491 $allowedCids[] = (int) $cid;
494 'uid' => local_user(),
500 'rel' => [Contact::SHARING, Contact::FRIEND],
502 $contactStmt = DBA::select('contact', ['id'], $condition);
503 while ($contact = DBA::fetch($contactStmt)) {
504 $allowedCids[] = (int) $contact['id'];
506 DBA::close($contactStmt);
509 if (count($allowedCids)) {
510 $default_permissions['allow_cid'] = $allowedCids;
513 if (!$update && !$rawmode) {
514 $tabs = network_tabs($a);
517 if ($gid && ($t = Contact::getOStatusCountByGroupId($gid)) && !PConfig::get(local_user(), 'system', 'nowarn_insecure')) {
518 notice(L10n::tt("Warning: This group contains %s member from a network that doesn't allow non public messages.",
519 "Warning: This group contains %s members from a network that doesn't allow non public messages.",
521 notice(L10n::t("Messages in this group won't be send to these receivers.").EOL);
524 Nav::setSelected('network');
529 // If $cid belongs to a communitity forum or a privat goup,.add a mention to the status editor
530 $condition = ["`id` = ? AND (`forum` OR `prv`)", $cid];
531 $contact = DBA::selectFirst('contact', ['addr', 'nick'], $condition);
532 if (DBA::isResult($contact)) {
533 if ($contact['addr'] != '') {
534 $content = '!' . $contact['addr'];
536 $content = '!' . $contact['nick'] . '+' . $cid;
543 'allow_location' => $a->user['allow_location'],
544 'default_location' => $a->user['default-location'],
545 'nickname' => $a->user['nickname'],
546 'lockstate' => ($gid || $cid || $nets || (is_array($a->user) &&
547 (strlen($a->user['allow_cid']) || strlen($a->user['allow_gid']) ||
548 strlen($a->user['deny_cid']) || strlen($a->user['deny_gid']))) ? 'lock' : 'unlock'),
549 'default_perms' => ACL::getDefaultUserPermissions($a->user),
550 'acl' => ACL::getFullSelectorHTML($a->user, true, $default_permissions),
551 'bang' => (($gid || $cid || $nets) ? '!' : ''),
552 'visitor' => 'block',
553 'profile_uid' => local_user(),
554 'content' => $content,
557 $o .= status_editor($a, $x);
560 // We don't have to deal with ACLs on this page. You're looking at everything
561 // that belongs to you, hence you can see all of it. We will filter by group if
564 $sql_post_table = '';
565 $sql_options = ($star ? " AND `thread`.`starred` " : '');
566 $sql_options .= ($bmark ? sprintf(" AND `thread`.`post-type` = %d ", Item::PT_PAGE) : '');
567 $sql_extra = $sql_options;
570 $sql_table = '`thread`';
571 $sql_parent = '`iid`';
574 $sql_table = '`item`';
575 $sql_parent = '`parent`';
576 $sql_post_table = " INNER JOIN `thread` ON `thread`.`iid` = `item`.`parent`";
579 $sql_nets = (($nets) ? sprintf(" AND $sql_table.`network` = '%s' ", DBA::escape($nets)) : '');
580 $sql_tag_nets = (($nets) ? sprintf(" AND `item`.`network` = '%s' ", DBA::escape($nets)) : '');
583 $group = DBA::selectFirst('group', ['name'], ['id' => $gid, 'uid' => local_user()]);
584 if (!DBA::isResult($group)) {
588 notice(L10n::t('No such group') . EOL);
589 $a->internalRedirect('network/0');
593 $contacts = Group::expand(local_user(), [$gid]);
595 if ((is_array($contacts)) && count($contacts)) {
596 $contact_str_self = '';
598 $contact_str = implode(',', $contacts);
599 $self = DBA::selectFirst('contact', ['id'], ['uid' => local_user(), 'self' => true]);
600 if (DBA::isResult($self)) {
601 $contact_str_self = $self['id'];
604 $sql_post_table .= " INNER JOIN `item` AS `temp1` ON `temp1`.`id` = " . $sql_table . "." . $sql_parent;
605 $sql_extra3 .= " AND (`thread`.`contact-id` IN ($contact_str) ";
606 $sql_extra3 .= " OR (`thread`.`contact-id` = '$contact_str_self' AND `temp1`.`allow_gid` LIKE '" . Strings::protectSprintf('%<' . intval($gid) . '>%') . "' AND `temp1`.`private`))";
608 $sql_extra3 .= " AND false ";
609 info(L10n::t('Group is empty'));
612 $o = Renderer::replaceMacros(Renderer::getMarkupTemplate('section_title.tpl'), [
613 '$title' => L10n::t('Group: %s', $group['name'])
616 $fields = ['id', 'name', 'network', 'writable', 'nurl',
617 'forum', 'prv', 'contact-type', 'addr', 'thumb', 'location'];
618 $condition = ["`id` = ? AND (NOT `blocked` OR `pending`)", $cid];
619 $contact = DBA::selectFirst('contact', $fields, $condition);
620 if (DBA::isResult($contact)) {
621 $sql_extra = " AND " . $sql_table . ".`contact-id` = " . intval($cid);
625 'name' => $contact['name'],
626 'itemurl' => defaults($contact, 'addr', $contact['nurl']),
627 'thumb' => ProxyUtils::proxifyUrl($contact['thumb'], false, ProxyUtils::SIZE_THUMB),
628 'details' => $contact['location'],
631 $entries[0]['account_type'] = Contact::getAccountType($contact);
633 $o = Renderer::replaceMacros(Renderer::getMarkupTemplate('viewcontact_template.tpl'), [
634 'contacts' => $entries,
638 if ($contact['network'] === Protocol::OSTATUS && $contact['writable'] && !PConfig::get(local_user(),'system','nowarn_insecure')) {
639 notice(L10n::t('Private messages to this person are at risk of public disclosure.') . EOL);
642 notice(L10n::t('Invalid contact.') . EOL);
643 $a->internalRedirect('network');
648 if (!$gid && !$cid && !$update && !Config::get('theme', 'hide_eventlist')) {
649 $o .= Profile::getBirthdays();
650 $o .= Profile::getEventsReminderHTML();
654 $sql_extra3 .= Strings::protectSprintf(sprintf(" AND $sql_table.received <= '%s' ",
655 DBA::escape(DateTimeFormat::convert($datequery, 'UTC', date_default_timezone_get()))));
658 $sql_extra3 .= Strings::protectSprintf(sprintf(" AND $sql_table.received >= '%s' ",
659 DBA::escape(DateTimeFormat::convert($datequery2, 'UTC', date_default_timezone_get()))));
663 $sql_extra3 .= " AND $sql_table.`mention`";
666 // Normal conversation view
667 if ($order === 'post') {
668 $ordering = '`received`';
669 $order_mode = 'received';
671 $ordering = '`commented`';
672 $order_mode = 'commented';
675 $sql_order = "$sql_table.$ordering";
677 if (!empty($_GET['offset'])) {
678 $sql_range = sprintf(" AND $sql_order <= '%s'", DBA::escape($_GET['offset']));
683 $pager = new Pager($a->query_string);
685 $pager_sql = networkPager($a, $pager, $update);
689 switch ($order_mode) {
691 if ($last_received != '') {
692 $last_date = $last_received;
693 $sql_range .= sprintf(" AND $sql_table.`received` < '%s'", DBA::escape($last_received));
695 $pager_sql = sprintf(" LIMIT %d, %d ", $pager->getStart(), $pager->getItemsPerPage());
699 if ($last_commented != '') {
700 $last_date = $last_commented;
701 $sql_range .= sprintf(" AND $sql_table.`commented` < '%s'", DBA::escape($last_commented));
703 $pager_sql = sprintf(" LIMIT %d, %d ", $pager->getStart(), $pager->getItemsPerPage());
707 if ($last_created != '') {
708 $last_date = $last_created;
709 $sql_range .= sprintf(" AND $sql_table.`created` < '%s'", DBA::escape($last_created));
711 $pager_sql = sprintf(" LIMIT %d, %d ", $pager->getStart(), $pager->getItemsPerPage());
715 if (($last_id > 0) && ($sql_table == '`thread`')) {
716 $sql_range .= sprintf(" AND $sql_table.`iid` < '%s'", DBA::escape($last_id));
718 $pager_sql = sprintf(" LIMIT %d, %d ", $pager->getStart(), $pager->getItemsPerPage());
723 // Fetch a page full of parent items for this page
725 if (!empty($parent)) {
726 // Load only a single thread
727 $sql_extra4 = "`item`.`id` = ".intval($parent);
729 // Load all unseen items
730 $sql_extra4 = "`item`.`unseen`";
731 if (Config::get("system", "like_no_comment")) {
732 $sql_extra4 .= " AND `item`.`gravity` IN (" . GRAVITY_PARENT . "," . GRAVITY_COMMENT . ")";
734 if ($order === 'post') {
735 // Only show toplevel posts when updating posts in this order mode
736 $sql_extra4 .= " AND `item`.`id` = `item`.`parent`";
740 $r = q("SELECT `item`.`parent-uri` AS `uri`, `item`.`parent` AS `item_id`, $sql_order AS `order_date`
741 FROM `item` $sql_post_table
742 STRAIGHT_JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
743 AND (NOT `contact`.`blocked` OR `contact`.`pending`)
744 AND (`item`.`gravity` != %d
745 OR `contact`.`uid` = `item`.`uid` AND `contact`.`self`
746 OR `contact`.`rel` IN (%d, %d) AND NOT `contact`.`readonly`)
747 LEFT JOIN `user-item` ON `user-item`.`iid` = `item`.`id` AND `user-item`.`uid` = %d
748 WHERE `item`.`uid` = %d AND `item`.`visible` AND NOT `item`.`deleted`
749 AND (`user-item`.`hidden` IS NULL OR NOT `user-item`.`hidden`)
750 AND NOT `item`.`moderated` AND $sql_extra4
751 $sql_extra3 $sql_extra $sql_range $sql_nets
752 ORDER BY `order_date` DESC LIMIT 100",
753 intval(GRAVITY_PARENT),
754 intval(Contact::SHARING),
755 intval(Contact::FRIEND),
756 intval(local_user()),
760 $r = q("SELECT `item`.`uri`, `thread`.`iid` AS `item_id`, $sql_order AS `order_date`
761 FROM `thread` $sql_post_table
762 STRAIGHT_JOIN `contact` ON `contact`.`id` = `thread`.`contact-id`
763 AND (NOT `contact`.`blocked` OR `contact`.`pending`)
764 STRAIGHT_JOIN `item` ON `item`.`id` = `thread`.`iid`
765 AND (`item`.`gravity` != %d
766 OR `contact`.`uid` = `item`.`uid` AND `contact`.`self`
767 OR `contact`.`rel` IN (%d, %d) AND NOT `contact`.`readonly`)
768 LEFT JOIN `user-item` ON `user-item`.`iid` = `item`.`id` AND `user-item`.`uid` = %d
769 WHERE `thread`.`uid` = %d AND `thread`.`visible` AND NOT `thread`.`deleted`
770 AND NOT `thread`.`moderated`
771 AND (`user-item`.`hidden` IS NULL OR NOT `user-item`.`hidden`)
772 $sql_extra2 $sql_extra3 $sql_range $sql_extra $sql_nets
773 ORDER BY `order_date` DESC $pager_sql",
774 intval(GRAVITY_PARENT),
775 intval(Contact::SHARING),
776 intval(Contact::FRIEND),
777 intval(local_user()),
782 // Only show it when unfiltered (no groups, no networks, ...)
783 if (in_array($nets, ['', Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::DIASPORA, Protocol::OSTATUS]) && (strlen($sql_extra . $sql_extra2 . $sql_extra3) == 0)) {
784 if (DBA::isResult($r)) {
785 $top_limit = current($r)['order_date'];
786 $bottom_limit = end($r)['order_date'];
787 if (empty($_SESSION['network_last_top_limit']) || ($_SESSION['network_last_top_limit'] < $top_limit)) {
788 $_SESSION['network_last_top_limit'] = $top_limit;
791 $top_limit = $bottom_limit = DateTimeFormat::utcNow();
794 // When checking for updates we need to fetch from the newest date to the newest date before
795 // Only do this, when the last stored date isn't too long ago (10 times the update interval)
796 $browser_update = PConfig::get(local_user(), 'system', 'update_interval', 40000) / 1000;
798 if (($browser_update > 0) && $update && !empty($_SESSION['network_last_date']) &&
799 (($bottom_limit < $_SESSION['network_last_date']) || ($top_limit == $bottom_limit)) &&
800 ((time() - $_SESSION['network_last_date_timestamp']) < ($browser_update * 10))) {
801 $bottom_limit = $_SESSION['network_last_date'];
803 $_SESSION['network_last_date'] = Session::get('network_last_top_limit', $top_limit);
804 $_SESSION['network_last_date_timestamp'] = time();
806 if ($last_date > $top_limit) {
807 $top_limit = $last_date;
808 } elseif ($pager->getPage() == 1) {
809 // Highest possible top limit when we are on the first page
810 $top_limit = DateTimeFormat::utcNow();
813 $items = DBA::p("SELECT `item`.`parent-uri` AS `uri`, 0 AS `item_id`, `item`.$ordering AS `order_date`, `author`.`url` AS `author-link` FROM `item`
814 STRAIGHT_JOIN (SELECT `oid` FROM `term` WHERE `term` IN
815 (SELECT SUBSTR(`term`, 2) FROM `search` WHERE `uid` = ? AND `term` LIKE '#%') AND `otype` = ? AND `type` = ? AND `uid` = 0) AS `term`
816 ON `item`.`id` = `term`.`oid`
817 STRAIGHT_JOIN `contact` AS `author` ON `author`.`id` = `item`.`author-id`
818 WHERE `item`.`uid` = 0 AND `item`.$ordering < ? AND `item`.$ordering > ? AND `item`.`gravity` = ?
819 AND NOT `author`.`hidden` AND NOT `author`.`blocked`" . $sql_tag_nets,
820 local_user(), TERM_OBJ_POST, TERM_HASHTAG,
821 $top_limit, $bottom_limit, GRAVITY_PARENT);
823 $data = DBA::toArray($items);
825 if (count($data) > 0) {
826 $tag_top_limit = current($data)['order_date'];
827 if ($_SESSION['network_last_date'] < $tag_top_limit) {
828 $_SESSION['network_last_date'] = $tag_top_limit;
831 Logger::log('Tagged items: ' . count($data) . ' - ' . $bottom_limit . ' - ' . $top_limit . ' - ' . local_user().' - '.(int)$update);
833 foreach ($r as $item) {
834 $s[$item['uri']] = $item;
836 foreach ($data as $item) {
837 // Don't show hash tag posts from blocked or ignored contacts
838 $condition = ["`nurl` = ? AND `uid` = ? AND (`blocked` OR `readonly`)",
839 Strings::normaliseLink($item['author-link']), local_user()];
840 if (!DBA::exists('contact', $condition)) {
841 $s[$item['uri']] = $item;
853 if (DBA::isResult($items)) {
856 foreach ($items as $item) {
857 if ($date_offset < $item['order_date']) {
858 $date_offset = $item['order_date'];
860 if (!in_array($item['item_id'], $parents_arr) && ($item['item_id'] > 0)) {
861 $parents_arr[] = $item['item_id'];
864 $parents_str = implode(', ', $parents_arr);
867 if (!empty($_GET['offset'])) {
868 $date_offset = $_GET['offset'];
871 $query_string = $a->query_string;
872 if ($date_offset && !preg_match('/[?&].offset=/', $query_string)) {
873 $query_string .= '&offset=' . urlencode($date_offset);
876 $pager->setQueryString($query_string);
878 // We aren't going to try and figure out at the item, group, and page
879 // level which items you've seen and which you haven't. If you're looking
880 // at the top level network page just mark everything seen.
882 if (!$gid && !$cid && !$star) {
883 $condition = ['unseen' => true, 'uid' => local_user()];
884 networkSetSeen($condition);
885 } elseif ($parents_str) {
886 $condition = ["`uid` = ? AND `unseen` AND `parent` IN (" . DBA::escape($parents_str) . ")", local_user()];
887 networkSetSeen($condition);
892 $o .= networkConversation($a, $items, $pager, $mode, $update, $ordering);
898 * @brief Get the network tabs menu
900 * @param App $a The global App
901 * @return string Html of the networktab
902 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
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) = 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) . '?order=comment' . (!empty($_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) . '?order=post' . (!empty($_GET['cid']) ? '&cid=' . $_GET['cid'] : ''),
931 'sel' => $postord_active,
932 'title' => L10n::t('Sort by Post Date'),
933 'id' => 'posted-order-tab',
939 'label' => L10n::t('Personal'),
940 'url' => str_replace('/new', '', $cmd) . (!empty($_GET['cid']) ? '/?cid=' . $_GET['cid'] : '/?f=') . '&conv=1',
941 'sel' => $conv_active,
942 'title' => L10n::t('Posts that mention or involve you'),
943 'id' => 'personal-tab',
947 if (Feature::isEnabled(local_user(), 'new_tab')) {
949 'label' => L10n::t('New'),
950 'url' => 'network/new' . (!empty($_GET['cid']) ? '/?cid=' . $_GET['cid'] : ''),
951 'sel' => $new_active,
952 'title' => L10n::t('Activity Stream - by date'),
953 'id' => 'activitiy-by-date-tab',
958 if (Feature::isEnabled(local_user(), 'link_tab')) {
960 'label' => L10n::t('Shared Links'),
961 'url' => str_replace('/new', '', $cmd) . (!empty($_GET['cid']) ? '/?cid=' . $_GET['cid'] : '/?f=') . '&bmark=1',
962 'sel' => $bookmarked_active,
963 'title' => L10n::t('Interesting Links'),
964 'id' => 'shared-links-tab',
970 'label' => L10n::t('Starred'),
971 'url' => str_replace('/new', '', $cmd) . (!empty($_GET['cid']) ? '/?cid=' . $_GET['cid'] : '/?f=') . '&star=1',
972 'sel' => $starred_active,
973 'title' => L10n::t('Favourite Posts'),
974 'id' => 'starred-posts-tab',
978 // save selected tab, but only if not in file mode
979 if (empty($_GET['file'])) {
980 PConfig::set(local_user(), 'network.view', 'tab.selected', [
981 $all_active, $postord_active, $conv_active, $new_active, $starred_active, $bookmarked_active
985 $arr = ['tabs' => $tabs];
986 Hook::callAll('network_tabs', $arr);
988 $tpl = Renderer::getMarkupTemplate('common_tabs.tpl');
990 return Renderer::replaceMacros($tpl, ['$tabs' => $arr['tabs']]);
992 // --- end item filter tabs
996 * Network hook into the HTML head to enable infinite scroll.
998 * Since the HTML head is built after the module content has been generated, we need to retrieve the base query string
999 * of the page to make the correct asynchronous call. This is obtained through the Pager that was instantiated in
1000 * networkThreadedView or networkFlatView.
1003 * @param string $htmlhead The head tag HTML string
1004 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1005 * @global Pager $pager
1007 function network_infinite_scroll_head(App $a, &$htmlhead)
1009 /// @TODO this will have to be converted to a static property of the converted Module\Network class
1015 if (PConfig::get(local_user(), 'system', 'infinite_scroll')
1016 && defaults($_GET, 'mode', '') != 'minimal'
1018 $tpl = Renderer::getMarkupTemplate('infinite_scroll_head.tpl');
1019 $htmlhead .= Renderer::replaceMacros($tpl, [
1020 '$pageno' => $pager->getPage(),
1021 '$reload_uri' => $pager->getBaseQueryString()