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\Core\ACL;
14 use Friendica\Core\Addon;
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\Database\DBA;
22 use Friendica\Model\Contact;
23 use Friendica\Model\Group;
24 use Friendica\Model\Item;
25 use Friendica\Model\Profile;
26 use Friendica\Module\Login;
27 use Friendica\Util\DateTimeFormat;
28 use Friendica\Util\Proxy as ProxyUtils;
30 require_once 'include/conversation.php';
31 require_once 'include/items.php';
33 function network_init(App $a)
36 notice(L10n::t('Permission denied.') . EOL);
40 Hook::add('head', __FILE__, 'network_infinite_scroll_head');
42 $search = (x($_GET, 'search') ? escape_tags($_GET['search']) : '');
44 if (($search != '') && !empty($_GET['submit'])) {
45 $a->internalRedirect('search?search=' . urlencode($search));
48 if (x($_GET, 'save')) {
49 $exists = DBA::exists('search', ['uid' => local_user(), 'term' => $search]);
51 DBA::insert('search', ['uid' => local_user(), 'term' => $search]);
54 if (x($_GET, 'remove')) {
55 DBA::delete('search', ['uid' => local_user(), 'term' => $search]);
58 $is_a_date_query = false;
60 $group_id = (($a->argc > 1 && is_numeric($a->argv[1])) ? intval($a->argv[1]) : 0);
63 if (x($_GET, 'cid') && intval($_GET['cid']) != 0) {
65 $_GET['nets'] = 'all';
70 for ($x = 1; $x < $a->argc; $x ++) {
71 if (is_a_date_arg($a->argv[$x])) {
72 $is_a_date_query = true;
78 // convert query string to array. remove friendica args
80 $query_string = str_replace($a->cmd . '?', '', $a->query_string);
81 parse_str($query_string, $query_array);
82 array_shift($query_array);
84 // fetch last used network view and redirect if needed
85 if (!$is_a_date_query) {
86 $sel_nets = defaults($_GET, 'nets', false);
87 $sel_tabs = network_query_get_sel_tab($a);
88 $sel_groups = network_query_get_sel_group($a);
89 $last_sel_tabs = PConfig::get(local_user(), 'network.view', 'tab.selected');
91 $remember_tab = ($sel_tabs[0] === 'active' && is_array($last_sel_tabs) && $last_sel_tabs[0] !== 'active');
93 $net_baseurl = '/network';
96 if ($sel_groups !== false) {
97 $net_baseurl .= '/' . $sel_groups;
101 // redirect if current selected tab is '/network' and
102 // last selected tab is _not_ '/network?f=&order=comment'.
103 // and this isn't a date query
114 'f=&order=comment', //all
115 'f=&order=post', //postord
118 'f=&star=1', //starred
119 'f=&bmark=1', //bookmarked
122 $k = array_search('active', $last_sel_tabs);
125 $net_baseurl .= $tab_baseurls[$k];
127 // parse out tab queries
129 $dest_qs = $tab_args[$k];
130 parse_str($dest_qs, $dest_qa);
131 $net_args = array_merge($net_args, $dest_qa);
133 $remember_tab = false;
137 if ($sel_nets !== false) {
138 $net_args['nets'] = $sel_nets;
142 $net_args = array_merge($query_array, $net_args);
143 $net_queries = build_querystring($net_args);
145 $redir_url = ($net_queries ? $net_baseurl . '?' . $net_queries : $net_baseurl);
147 $a->internalRedirect($redir_url);
151 // If nets is set to all, unset it
152 if (x($_GET, 'nets') && $_GET['nets'] === 'all') {
153 unset($_GET['nets']);
156 if (!x($a->page, 'aside')) {
157 $a->page['aside'] = '';
160 $a->page['aside'] .= (Feature::isEnabled(local_user(), 'groups') ?
161 Group::sidebarWidget('network/0', 'network', 'standard', $group_id) : '');
162 $a->page['aside'] .= (Feature::isEnabled(local_user(), 'forumlist_widget') ? ForumManager::widget(local_user(), $cid) : '');
163 $a->page['aside'] .= posted_date_widget('network', local_user(), false);
164 $a->page['aside'] .= Widget::networks('network', (x($_GET, 'nets') ? $_GET['nets'] : ''));
165 $a->page['aside'] .= saved_searches($search);
166 $a->page['aside'] .= Widget::fileAs('network', (x($_GET, 'file') ? $_GET['file'] : ''));
169 function saved_searches($search)
171 if (!Feature::isEnabled(local_user(), 'savedsearch')) {
177 $srchurl = '/network?f='
178 . ((x($_GET, 'cid')) ? '&cid=' . $_GET['cid'] : '')
179 . ((x($_GET, 'star')) ? '&star=' . $_GET['star'] : '')
180 . ((x($_GET, 'bmark')) ? '&bmark=' . $_GET['bmark'] : '')
181 . ((x($_GET, 'conv')) ? '&conv=' . $_GET['conv'] : '')
182 . ((x($_GET, 'nets')) ? '&nets=' . $_GET['nets'] : '')
183 . ((x($_GET, 'cmin')) ? '&cmin=' . $_GET['cmin'] : '')
184 . ((x($_GET, 'cmax')) ? '&cmax=' . $_GET['cmax'] : '')
185 . ((x($_GET, 'file')) ? '&file=' . $_GET['file'] : '');
190 $terms = DBA::select('search', ['id', 'term'], ['uid' => local_user()]);
193 while ($rr = DBA::fetch($terms)) {
196 'term' => $rr['term'],
197 'encodedterm' => urlencode($rr['term']),
198 'delete' => L10n::t('Remove term'),
199 'selected' => ($search == $rr['term']),
203 $tpl = get_markup_template('saved_searches_aside.tpl');
204 $o = replace_macros($tpl, [
205 '$title' => L10n::t('Saved Searches'),
206 '$add' => L10n::t('add'),
207 '$searchbox' => search($search, 'netsearch-box', $srchurl, true),
215 * Return selected tab from query
218 * '/network' => $no_active = 'active'
219 * '/network?f=&order=comment' => $comment_active = 'active'
220 * '/network?f=&order=post' => $postord_active = 'active'
221 * '/network?f=&conv=1', => $conv_active = 'active'
222 * '/network/new', => $new_active = 'active'
223 * '/network?f=&star=1', => $starred_active = 'active'
224 * '/network?f=&bmark=1', => $bookmarked_active = 'active'
226 * @return Array ($no_active, $comment_active, $postord_active, $conv_active, $new_active, $starred_active, $bookmarked_active);
228 function network_query_get_sel_tab(App $a)
231 $starred_active = '';
233 $bookmarked_active = '';
236 $postord_active = '';
238 if (($a->argc > 1 && $a->argv[1] === 'new') || ($a->argc > 2 && $a->argv[2] === 'new')) {
239 $new_active = 'active';
242 if (x($_GET, 'star')) {
243 $starred_active = 'active';
246 if (x($_GET, 'bmark')) {
247 $bookmarked_active = 'active';
250 if (x($_GET, 'conv')) {
251 $conv_active = 'active';
254 if (($new_active == '') && ($starred_active == '') && ($bookmarked_active == '') && ($conv_active == '')) {
255 $no_active = 'active';
258 if ($no_active == 'active' && x($_GET, 'order')) {
259 switch($_GET['order']) {
260 case 'post' : $postord_active = 'active'; $no_active=''; break;
261 case 'comment' : $all_active = 'active'; $no_active=''; break;
265 return [$no_active, $all_active, $postord_active, $conv_active, $new_active, $starred_active, $bookmarked_active];
268 function network_query_get_sel_group(App $a)
272 if ($a->argc >= 2 && is_numeric($a->argv[1])) {
273 $group = $a->argv[1];
280 * @brief Sets the pager data and returns SQL
282 * @param App $a The global App
283 * @param integer $update Used for the automatic reloading
284 * @return string SQL with the appropriate LIMIT clause
286 function networkPager(App $a, Pager $pager, $update)
289 // only setup pagination on initial page view
293 // check if we serve a mobile device and get the user settings
296 $itemspage_network = PConfig::get(local_user(), 'system', 'itemspage_mobile_network');
297 $itemspage_network = ((intval($itemspage_network)) ? $itemspage_network : 20);
299 $itemspage_network = PConfig::get(local_user(), 'system', 'itemspage_network');
300 $itemspage_network = ((intval($itemspage_network)) ? $itemspage_network : 40);
303 // now that we have the user settings, see if the theme forces
304 // a maximum item number which is lower then the user choice
305 if (($a->force_max_items > 0) && ($a->force_max_items < $itemspage_network)) {
306 $itemspage_network = $a->force_max_items;
309 $pager->setItemsPerPage($itemspage_network);
311 return sprintf(" LIMIT %d, %d ", $pager->getStart(), $pager->getItemsPerPage());
315 * @brief Sets items as seen
317 * @param array $condition The array with the SQL condition
319 function networkSetSeen($condition)
321 if (empty($condition)) {
325 $unseen = Item::exists($condition);
328 $r = Item::update(['unseen' => false], $condition);
333 * @brief Create the conversation HTML
335 * @param App $a The global App
336 * @param array $items Items of the conversation
337 * @param string $mode Display mode for the conversation
338 * @param integer $update Used for the automatic reloading
339 * @return string HTML of the conversation
341 function networkConversation(App $a, $items, Pager $pager, $mode, $update, $ordering = '')
343 // Set this so that the conversation function can find out contact info for our wall-wall items
344 $a->page_contact = $a->contact;
346 $o = conversation($a, $items, $pager, $mode, $update, false, $ordering, local_user());
349 if (PConfig::get(local_user(), 'system', 'infinite_scroll')) {
350 $o .= scroll_loader();
352 $o .= $pager->renderMinimal(count($items));
359 function network_content(App $a, $update = 0, $parent = 0)
362 return Login::form();
365 /// @TODO Is this really necessary? $a is already available to hooks
366 $arr = ['query' => $a->query_string];
367 Addon::callHooks('network_content_init', $arr);
372 for ($x = 1; $x < $a->argc; $x ++) {
373 if ($a->argv[$x] === 'new') {
379 if (!empty($_GET['file'])) {
384 $o = networkFlatView($a, $update);
386 $o = networkThreadedView($a, $update, $parent);
393 * @brief Get the network content in flat view
395 * @param Pager $pager
396 * @param App $a The global App
397 * @param integer $update Used for the automatic reloading
398 * @return string HTML of the network content in flat view
400 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']) && ($_GET['mode'] == 'raw'));
406 if (isset($_GET['last_id'])) {
407 $last_id = intval($_GET['last_id']);
414 $file = defaults($_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();
446 $pager = new Pager($a->query_string);
448 /// @TODO Figure out why this variable is unused
449 $pager_sql = networkPager($a, $pager, $update);
452 $condition = ["`term` = ? AND `otype` = ? AND `type` = ? AND `uid` = ?",
453 $file, TERM_OBJ_POST, TERM_FILE, local_user()];
454 $params = ['order' => ['tid' => true], 'limit' => [$pager->getStart(), $pager->getItemsPerPage()]];
455 $result = DBA::select('term', ['oid'], $condition);
458 while ($term = DBA::fetch($result)) {
459 $posts[] = $term['oid'];
463 $condition = ['uid' => local_user(), 'id' => $posts];
465 $condition = ['uid' => local_user()];
468 $params = ['order' => ['id' => true], 'limit' => [$pager->getStart(), $pager->getItemsPerPage()]];
469 $result = Item::selectForUser(local_user(), [], $condition, $params);
470 $items = Item::inArray($result);
472 $condition = ['unseen' => true, 'uid' => local_user()];
473 networkSetSeen($condition);
475 $o .= networkConversation($a, $items, $pager, 'network-new', $update);
481 * @brief Get the network content in threaded view
483 * @global Pager $pager
484 * @param App $a The global App
485 * @param integer $update Used for the automatic reloading
486 * @param integer $parent
487 * @return string HTML of the network content in flat view
489 function networkThreadedView(App $a, $update, $parent)
491 /// @TODO this will have to be converted to a static property of the converted Module\Network class
494 // Rawmode is used for fetching new content at the end of the page
495 $rawmode = (isset($_GET['mode']) AND ( $_GET['mode'] == 'raw'));
497 if (isset($_GET['last_received']) && isset($_GET['last_commented']) && isset($_GET['last_created']) && isset($_GET['last_id'])) {
498 $last_received = DateTimeFormat::utc($_GET['last_received']);
499 $last_commented = DateTimeFormat::utc($_GET['last_commented']);
500 $last_created = DateTimeFormat::utc($_GET['last_created']);
501 $last_id = intval($_GET['last_id']);
504 $last_commented = '';
509 $datequery = $datequery2 = '';
513 $default_permissions = [];
516 for ($x = 1; $x < $a->argc; $x ++) {
517 if (is_a_date_arg($a->argv[$x])) {
519 $datequery2 = escape_tags($a->argv[$x]);
521 $datequery = escape_tags($a->argv[$x]);
522 $_GET['order'] = 'post';
524 } elseif (intval($a->argv[$x])) {
525 $gid = intval($a->argv[$x]);
526 $default_permissions = ['allow_gid' => '<' . $gid . '>'];
533 $cid = intval(defaults($_GET, 'cid' , 0));
534 $star = intval(defaults($_GET, 'star' , 0));
535 $bmark = intval(defaults($_GET, 'bmark', 0));
536 $conv = intval(defaults($_GET, 'conv' , 0));
537 $order = notags(defaults($_GET, 'order', 'comment'));
538 $nets = defaults($_GET, 'nets' , '');
541 $default_permissions = ['allow_cid' => '<' . intval($cid) . '>'];
545 $r = DBA::select('contact', ['id'], ['uid' => local_user(), 'network' => $nets], ['self' => false]);
548 while ($rr = DBA::fetch($r)) {
549 $str .= '<' . $rr['id'] . '>';
552 $default_permissions = ['allow_cid' => $str];
556 if (!$update && !$rawmode) {
557 $tabs = network_tabs($a);
560 if ($gid && ($t = Contact::getOStatusCountByGroupId($gid)) && !PConfig::get(local_user(), 'system', 'nowarn_insecure')) {
561 notice(L10n::tt("Warning: This group contains %s member from a network that doesn't allow non public messages.",
562 "Warning: This group contains %s members from a network that doesn't allow non public messages.",
564 notice(L10n::t("Messages in this group won't be send to these receivers.").EOL);
567 Nav::setSelected('network');
572 // If $cid belongs to a communitity forum or a privat goup,.add a mention to the status editor
573 $condition = ["`id` = ? AND (`forum` OR `prv`)", $cid];
574 $contact = DBA::selectFirst('contact', ['addr', 'nick'], $condition);
575 if (DBA::isResult($contact)) {
576 if ($contact['addr'] != '') {
577 $content = '!' . $contact['addr'];
579 $content = '!' . $contact['nick'] . '+' . $cid;
586 'allow_location' => $a->user['allow_location'],
587 'default_location' => $a->user['default-location'],
588 'nickname' => $a->user['nickname'],
589 'lockstate' => ($gid || $cid || $nets || (is_array($a->user) &&
590 (strlen($a->user['allow_cid']) || strlen($a->user['allow_gid']) ||
591 strlen($a->user['deny_cid']) || strlen($a->user['deny_gid']))) ? 'lock' : 'unlock'),
592 'default_perms' => ACL::getDefaultUserPermissions($a->user),
593 'acl' => ACL::getFullSelectorHTML($a->user, true, $default_permissions),
594 'bang' => (($gid || $cid || $nets) ? '!' : ''),
595 'visitor' => 'block',
596 'profile_uid' => local_user(),
597 'content' => $content,
600 $o .= status_editor($a, $x);
603 // We don't have to deal with ACLs on this page. You're looking at everything
604 // that belongs to you, hence you can see all of it. We will filter by group if
607 $sql_post_table = '';
608 $sql_options = ($star ? " AND `thread`.`starred` " : '');
609 $sql_options .= ($bmark ? sprintf(" AND `thread`.`post-type` = %d ", Item::PT_PAGE) : '');
610 $sql_extra = $sql_options;
613 $sql_table = '`thread`';
614 $sql_parent = '`iid`';
618 $sql_table = '`item`';
619 $sql_parent = '`parent`';
620 $sql_post_table = " INNER JOIN `thread` ON `thread`.`iid` = `item`.`parent`";
623 $sql_nets = (($nets) ? sprintf(" AND $sql_table.`network` = '%s' ", DBA::escape($nets)) : '');
624 $sql_tag_nets = (($nets) ? sprintf(" AND `item`.`network` = '%s' ", DBA::escape($nets)) : '');
627 $group = DBA::selectFirst('group', ['name'], ['id' => $gid, 'uid' => local_user()]);
628 if (!DBA::isResult($group)) {
632 notice(L10n::t('No such group') . EOL);
633 $a->internalRedirect('network/0');
637 $contacts = Group::expand([$gid]);
639 if ((is_array($contacts)) && count($contacts)) {
640 $contact_str_self = '';
642 $contact_str = implode(',', $contacts);
643 $self = DBA::selectFirst('contact', ['id'], ['uid' => local_user(), 'self' => true]);
644 if (DBA::isResult($self)) {
645 $contact_str_self = $self['id'];
648 $sql_post_table .= " INNER JOIN `item` AS `temp1` ON `temp1`.`id` = " . $sql_table . "." . $sql_parent;
649 $sql_extra3 .= " AND (`thread`.`contact-id` IN ($contact_str) ";
650 $sql_extra3 .= " OR (`thread`.`contact-id` = '$contact_str_self' AND `temp1`.`allow_gid` LIKE '" . protect_sprintf('%<' . intval($gid) . '>%') . "' AND `temp1`.`private`))";
652 $sql_extra3 .= " AND false ";
653 info(L10n::t('Group is empty'));
656 $o = replace_macros(get_markup_template('section_title.tpl'), [
657 '$title' => L10n::t('Group: %s', $group['name'])
660 $fields = ['id', 'name', 'network', 'writable', 'nurl',
661 'forum', 'prv', 'contact-type', 'addr', 'thumb', 'location'];
662 $condition = ["`id` = ? AND (NOT `blocked` OR `pending`)", $cid];
663 $contact = DBA::selectFirst('contact', $fields, $condition);
664 if (DBA::isResult($contact)) {
665 $sql_extra = " AND " . $sql_table . ".`contact-id` = " . intval($cid);
669 'name' => htmlentities($contact['name']),
670 'itemurl' => defaults($contact, 'addr', $contact['nurl']),
671 'thumb' => ProxyUtils::proxifyUrl($contact['thumb'], false, ProxyUtils::SIZE_THUMB),
672 'details' => $contact['location'],
675 $entries[0]['account_type'] = Contact::getAccountType($contact);
677 $o = replace_macros(get_markup_template('viewcontact_template.tpl'), [
678 'contacts' => $entries,
682 if ($contact['network'] === Protocol::OSTATUS && $contact['writable'] && !PConfig::get(local_user(),'system','nowarn_insecure')) {
683 notice(L10n::t('Private messages to this person are at risk of public disclosure.') . EOL);
686 notice(L10n::t('Invalid contact.') . EOL);
687 $a->internalRedirect('network');
692 if (!$gid && !$cid && !$update && !Config::get('theme', 'hide_eventlist')) {
693 $o .= Profile::getBirthdays();
694 $o .= Profile::getEventsReminderHTML();
698 $sql_extra3 .= protect_sprintf(sprintf(" AND $sql_table.created <= '%s' ",
699 DBA::escape(DateTimeFormat::convert($datequery, 'UTC', date_default_timezone_get()))));
702 $sql_extra3 .= protect_sprintf(sprintf(" AND $sql_table.created >= '%s' ",
703 DBA::escape(DateTimeFormat::convert($datequery2, 'UTC', date_default_timezone_get()))));
707 $sql_extra3 .= " AND $sql_table.`mention`";
710 // Normal conversation view
711 if ($order === 'post') {
712 $ordering = '`created`';
713 $order_mode = 'created';
715 $ordering = '`commented`';
716 $order_mode = 'commented';
719 $sql_order = "$sql_table.$ordering";
721 if (!empty($_GET['offset'])) {
722 $sql_range = sprintf(" AND $sql_order <= '%s'", DBA::escape($_GET['offset']));
727 $pager = new Pager($a->query_string);
729 $pager_sql = networkPager($a, $pager, $update);
733 switch ($order_mode) {
735 if ($last_received != '') {
736 $last_date = $last_received;
737 $sql_range .= sprintf(" AND $sql_table.`received` < '%s'", DBA::escape($last_received));
739 $pager_sql = sprintf(" LIMIT %d, %d ", $pager->getStart(), $pager->getItemsPerPage());
743 if ($last_commented != '') {
744 $last_date = $last_commented;
745 $sql_range .= sprintf(" AND $sql_table.`commented` < '%s'", DBA::escape($last_commented));
747 $pager_sql = sprintf(" LIMIT %d, %d ", $pager->getStart(), $pager->getItemsPerPage());
751 if ($last_created != '') {
752 $last_date = $last_created;
753 $sql_range .= sprintf(" AND $sql_table.`created` < '%s'", DBA::escape($last_created));
755 $pager_sql = sprintf(" LIMIT %d, %d ", $pager->getStart(), $pager->getItemsPerPage());
759 if (($last_id > 0) && ($sql_table == '`thread`')) {
760 $sql_range .= sprintf(" AND $sql_table.`iid` < '%s'", DBA::escape($last_id));
762 $pager_sql = sprintf(" LIMIT %d, %d ", $pager->getStart(), $pager->getItemsPerPage());
767 // Fetch a page full of parent items for this page
769 if (!empty($parent)) {
770 // Load only a single thread
771 $sql_extra4 = "`item`.`id` = ".intval($parent);
773 // Load all unseen items
774 $sql_extra4 = "`item`.`unseen`";
775 if (Config::get("system", "like_no_comment")) {
776 $sql_extra4 .= " AND `item`.`gravity` IN (" . GRAVITY_PARENT . "," . GRAVITY_COMMENT . ")";
778 if ($order === 'post') {
779 // Only show toplevel posts when updating posts in this order mode
780 $sql_extra4 .= " AND `item`.`id` = `item`.`parent`";
784 $r = q("SELECT `item`.`parent-uri` AS `uri`, `item`.`parent` AS `item_id`, $sql_order AS `order_date`
785 FROM `item` $sql_post_table
786 STRAIGHT_JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
787 AND (NOT `contact`.`blocked` OR `contact`.`pending`)
788 AND (`item`.`gravity` != %d
789 OR `contact`.`uid` = `item`.`uid` AND `contact`.`self`
790 OR `contact`.`rel` IN (%d, %d) AND NOT `contact`.`readonly`)
791 LEFT JOIN `user-item` ON `user-item`.`iid` = `item`.`id` AND `user-item`.`uid` = %d
792 WHERE `item`.`uid` = %d AND `item`.`visible` AND NOT `item`.`deleted`
793 AND (`user-item`.`hidden` IS NULL OR NOT `user-item`.`hidden`)
794 AND NOT `item`.`moderated` AND $sql_extra4
795 $sql_extra3 $sql_extra $sql_range $sql_nets
796 ORDER BY `order_date` DESC LIMIT 100",
797 intval(GRAVITY_PARENT),
798 intval(Contact::SHARING),
799 intval(Contact::FRIEND),
800 intval(local_user()),
804 $r = q("SELECT `item`.`uri`, `thread`.`iid` AS `item_id`, $sql_order AS `order_date`
805 FROM `thread` $sql_post_table
806 STRAIGHT_JOIN `contact` ON `contact`.`id` = `thread`.`contact-id`
807 AND (NOT `contact`.`blocked` OR `contact`.`pending`)
808 STRAIGHT_JOIN `item` ON `item`.`id` = `thread`.`iid`
809 AND (`item`.`gravity` != %d
810 OR `contact`.`uid` = `item`.`uid` AND `contact`.`self`
811 OR `contact`.`rel` IN (%d, %d) AND NOT `contact`.`readonly`)
812 LEFT JOIN `user-item` ON `user-item`.`iid` = `item`.`id` AND `user-item`.`uid` = %d
813 WHERE `thread`.`uid` = %d AND `thread`.`visible` AND NOT `thread`.`deleted`
814 AND NOT `thread`.`moderated`
815 AND (`user-item`.`hidden` IS NULL OR NOT `user-item`.`hidden`)
816 $sql_extra2 $sql_extra3 $sql_range $sql_extra $sql_nets
817 ORDER BY `order_date` DESC $pager_sql",
818 intval(GRAVITY_PARENT),
819 intval(Contact::SHARING),
820 intval(Contact::FRIEND),
821 intval(local_user()),
826 // Only show it when unfiltered (no groups, no networks, ...)
827 if (in_array($nets, ['', Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::DIASPORA, Protocol::OSTATUS]) && (strlen($sql_extra . $sql_extra2 . $sql_extra3) == 0)) {
828 if (DBA::isResult($r)) {
829 $top_limit = current($r)['order_date'];
830 $bottom_limit = end($r)['order_date'];
831 if (empty($_SESSION['network_last_top_limit']) || ($_SESSION['network_last_top_limit'] < $top_limit)) {
832 $_SESSION['network_last_top_limit'] = $top_limit;
835 $top_limit = $bottom_limit = DateTimeFormat::utcNow();
838 // When checking for updates we need to fetch from the newest date to the newest date before
839 // Only do this, when the last stored date isn't too long ago (10 times the update interval)
840 $browser_update = PConfig::get(local_user(), 'system', 'update_interval', 40000) / 1000;
842 if (($browser_update > 0) && $update && !empty($_SESSION['network_last_date']) &&
843 (($bottom_limit < $_SESSION['network_last_date']) || ($top_limit == $bottom_limit)) &&
844 ((time() - $_SESSION['network_last_date_timestamp']) < ($browser_update * 10))) {
845 $bottom_limit = $_SESSION['network_last_date'];
847 $_SESSION['network_last_date'] = defaults($_SESSION, 'network_last_top_limit', $top_limit);
848 $_SESSION['network_last_date_timestamp'] = time();
850 if ($last_date > $top_limit) {
851 $top_limit = $last_date;
852 } elseif ($pager->getPage() == 1) {
853 // Highest possible top limit when we are on the first page
854 $top_limit = DateTimeFormat::utcNow();
857 $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`
858 STRAIGHT_JOIN (SELECT `oid` FROM `term` WHERE `term` IN
859 (SELECT SUBSTR(`term`, 2) FROM `search` WHERE `uid` = ? AND `term` LIKE '#%') AND `otype` = ? AND `type` = ? AND `uid` = 0) AS `term`
860 ON `item`.`id` = `term`.`oid`
861 STRAIGHT_JOIN `contact` AS `author` ON `author`.`id` = `item`.`author-id`
862 WHERE `item`.`uid` = 0 AND `item`.$ordering < ? AND `item`.$ordering > ?
863 AND NOT `author`.`hidden` AND NOT `author`.`blocked`" . $sql_tag_nets,
864 local_user(), TERM_OBJ_POST, TERM_HASHTAG,
865 $top_limit, $bottom_limit);
867 $data = DBA::toArray($items);
869 if (count($data) > 0) {
870 $tag_top_limit = current($data)['order_date'];
871 if ($_SESSION['network_last_date'] < $tag_top_limit) {
872 $_SESSION['network_last_date'] = $tag_top_limit;
875 Logger::log('Tagged items: ' . count($data) . ' - ' . $bottom_limit . ' - ' . $top_limit . ' - ' . local_user().' - '.(int)$update);
877 foreach ($r as $item) {
878 $s[$item['uri']] = $item;
880 foreach ($data as $item) {
881 // Don't show hash tag posts from blocked or ignored contacts
882 $condition = ["`nurl` = ? AND `uid` = ? AND (`blocked` OR `readonly`)",
883 normalise_link($item['author-link']), local_user()];
884 if (!DBA::exists('contact', $condition)) {
885 $s[$item['uri']] = $item;
897 if (DBA::isResult($items)) {
900 foreach ($items as $item) {
901 if ($date_offset < $item['order_date']) {
902 $date_offset = $item['order_date'];
904 if (!in_array($item['item_id'], $parents_arr) && ($item['item_id'] > 0)) {
905 $parents_arr[] = $item['item_id'];
908 $parents_str = implode(', ', $parents_arr);
911 if (x($_GET, 'offset')) {
912 $date_offset = $_GET['offset'];
915 $query_string = $a->query_string;
916 if ($date_offset && !preg_match('/[?&].offset=/', $query_string)) {
917 $query_string .= '&offset=' . urlencode($date_offset);
920 $pager->setQueryString($query_string);
922 // We aren't going to try and figure out at the item, group, and page
923 // level which items you've seen and which you haven't. If you're looking
924 // at the top level network page just mark everything seen.
926 if (!$gid && !$cid && !$star) {
927 $condition = ['unseen' => true, 'uid' => local_user()];
928 networkSetSeen($condition);
929 } elseif ($parents_str) {
930 $condition = ["`uid` = ? AND `unseen` AND `parent` IN (" . DBA::escape($parents_str) . ")", local_user()];
931 networkSetSeen($condition);
936 $o .= networkConversation($a, $items, $pager, $mode, $update, $ordering);
942 * @brief Get the network tabs menu
944 * @param App $a The global App
945 * @return string Html of the networktab
947 function network_tabs(App $a)
950 /// @TODO fix this logic, reduce duplication
951 /// $a->page['content'] .= '<div class="tabs-wrapper">';
952 list($no_active, $all_active, $postord_active, $conv_active, $new_active, $starred_active, $bookmarked_active) = network_query_get_sel_tab($a);
954 // if no tabs are selected, defaults to comments
955 if ($no_active == 'active') {
956 $all_active = 'active';
964 'label' => L10n::t('Commented Order'),
965 'url' => str_replace('/new', '', $cmd) . '?f=&order=comment' . ((x($_GET,'cid')) ? '&cid=' . $_GET['cid'] : ''),
966 'sel' => $all_active,
967 'title' => L10n::t('Sort by Comment Date'),
968 'id' => 'commented-order-tab',
972 'label' => L10n::t('Posted Order'),
973 'url' => str_replace('/new', '', $cmd) . '?f=&order=post' . ((x($_GET,'cid')) ? '&cid=' . $_GET['cid'] : ''),
974 'sel' => $postord_active,
975 'title' => L10n::t('Sort by Post Date'),
976 'id' => 'posted-order-tab',
981 if (Feature::isEnabled(local_user(), 'personal_tab')) {
983 'label' => L10n::t('Personal'),
984 'url' => str_replace('/new', '', $cmd) . ((x($_GET,'cid')) ? '/?f=&cid=' . $_GET['cid'] : '/?f=') . '&conv=1',
985 'sel' => $conv_active,
986 'title' => L10n::t('Posts that mention or involve you'),
987 'id' => 'personal-tab',
992 if (Feature::isEnabled(local_user(), 'new_tab')) {
994 'label' => L10n::t('New'),
995 'url' => 'network/new' . ((x($_GET,'cid')) ? '/?f=&cid=' . $_GET['cid'] : ''),
996 'sel' => $new_active,
997 'title' => L10n::t('Activity Stream - by date'),
998 'id' => 'activitiy-by-date-tab',
1003 if (Feature::isEnabled(local_user(), 'link_tab')) {
1005 'label' => L10n::t('Shared Links'),
1006 'url' => str_replace('/new', '', $cmd) . ((x($_GET,'cid')) ? '/?f=&cid=' . $_GET['cid'] : '/?f=') . '&bmark=1',
1007 'sel' => $bookmarked_active,
1008 'title' => L10n::t('Interesting Links'),
1009 'id' => 'shared-links-tab',
1014 if (Feature::isEnabled(local_user(), 'star_posts')) {
1016 'label' => L10n::t('Starred'),
1017 'url' => str_replace('/new', '', $cmd) . ((x($_GET,'cid')) ? '/?f=&cid=' . $_GET['cid'] : '/?f=') . '&star=1',
1018 'sel' => $starred_active,
1019 'title' => L10n::t('Favourite Posts'),
1020 'id' => 'starred-posts-tab',
1025 // save selected tab, but only if not in file mode
1026 if (!x($_GET, 'file')) {
1027 PConfig::set(local_user(), 'network.view', 'tab.selected', [
1028 $all_active, $postord_active, $conv_active, $new_active, $starred_active, $bookmarked_active
1032 $arr = ['tabs' => $tabs];
1033 Addon::callHooks('network_tabs', $arr);
1035 $tpl = get_markup_template('common_tabs.tpl');
1037 return replace_macros($tpl, ['$tabs' => $arr['tabs']]);
1039 // --- end item filter tabs
1043 * Network hook into the HTML head to enable infinite scroll.
1045 * Since the HTML head is built after the module content has been generated, we need to retrieve the base query string
1046 * of the page to make the correct asynchronous call. This is obtained through the Pager that was instantiated in
1047 * networkThreadedView or networkFlatView.
1049 * @global Pager $pager
1051 * @param string $htmlhead The head tag HTML string
1053 function network_infinite_scroll_head(App $a, &$htmlhead)
1055 /// @TODO this will have to be converted to a static property of the converted Module\Network class
1058 if (PConfig::get(local_user(), 'system', 'infinite_scroll')
1059 && defaults($_GET, 'mode', '') != 'minimal'
1061 $tpl = get_markup_template('infinite_scroll_head.tpl');
1062 $htmlhead .= replace_macros($tpl, [
1063 '$pageno' => $pager->getPage(),
1064 '$reload_uri' => $pager->getBaseQueryString()