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\Database\DBA;
23 use Friendica\Model\Contact;
24 use Friendica\Model\Group;
25 use Friendica\Model\Item;
26 use Friendica\Model\Profile;
27 use Friendica\Module\Login;
28 use Friendica\Util\DateTimeFormat;
29 use Friendica\Util\Proxy as ProxyUtils;
30 use Friendica\Util\Strings;
32 function network_init(App $a)
35 notice(L10n::t('Permission denied.') . EOL);
39 Hook::add('head', __FILE__, 'network_infinite_scroll_head');
41 $search = (!empty($_GET['search']) ? Strings::escapeHtml($_GET['search']) : '');
43 if (($search != '') && !empty($_GET['submit'])) {
44 $a->internalRedirect('search?search=' . urlencode($search));
47 if (!empty($_GET['save'])) {
48 $exists = DBA::exists('search', ['uid' => local_user(), 'term' => $search]);
50 DBA::insert('search', ['uid' => local_user(), 'term' => $search]);
53 if (!empty($_GET['remove'])) {
54 DBA::delete('search', ['uid' => local_user(), 'term' => $search]);
57 $is_a_date_query = false;
59 $group_id = (($a->argc > 1 && is_numeric($a->argv[1])) ? intval($a->argv[1]) : 0);
62 if (!empty($_GET['cid'])) {
69 for ($x = 1; $x < $a->argc; $x ++) {
70 if (is_a_date_arg($a->argv[$x])) {
71 $is_a_date_query = true;
77 // convert query string to array. remove friendica args
79 $query_string = str_replace($a->cmd . '?', '', $a->query_string);
80 parse_str($query_string, $query_array);
81 array_shift($query_array);
83 // fetch last used network view and redirect if needed
84 if (!$is_a_date_query) {
85 $sel_nets = defaults($_GET, 'nets', '');
86 $sel_tabs = network_query_get_sel_tab($a);
87 $sel_groups = network_query_get_sel_group($a);
88 $last_sel_tabs = PConfig::get(local_user(), 'network.view', 'tab.selected');
90 $remember_tab = ($sel_tabs[0] === 'active' && is_array($last_sel_tabs) && $last_sel_tabs[0] !== 'active');
92 $net_baseurl = '/network';
95 if ($sel_groups !== false) {
96 $net_baseurl .= '/' . $sel_groups;
100 // redirect if current selected tab is '/network' and
101 // last selected tab is _not_ '/network?f=&order=comment'.
102 // and this isn't a date query
113 'f=&order=comment', //all
114 'f=&order=post', //postord
117 'f=&star=1', //starred
118 'f=&bmark=1', //bookmarked
121 $k = array_search('active', $last_sel_tabs);
124 $net_baseurl .= $tab_baseurls[$k];
126 // parse out tab queries
128 $dest_qs = $tab_args[$k];
129 parse_str($dest_qs, $dest_qa);
130 $net_args = array_merge($net_args, $dest_qa);
132 $remember_tab = false;
137 $net_args['nets'] = $sel_nets;
141 $net_args = array_merge($query_array, $net_args);
142 $net_queries = build_querystring($net_args);
144 $redir_url = ($net_queries ? $net_baseurl . '?' . $net_queries : $net_baseurl);
146 $a->internalRedirect($redir_url);
150 if (empty($a->page['aside'])) {
151 $a->page['aside'] = '';
154 $a->page['aside'] .= Group::sidebarWidget('network/0', 'network', 'standard', $group_id);
155 $a->page['aside'] .= ForumManager::widget(local_user(), $cid);
156 $a->page['aside'] .= posted_date_widget('network', local_user(), false);
157 $a->page['aside'] .= Widget::networks('network', defaults($_GET, 'nets', '') );
158 $a->page['aside'] .= saved_searches($search);
159 $a->page['aside'] .= Widget::fileAs('network', defaults($_GET, 'file', '') );
162 function saved_searches($search)
164 $srchurl = '/network?f='
165 . (!empty($_GET['cid']) ? '&cid=' . rawurlencode($_GET['cid']) : '')
166 . (!empty($_GET['star']) ? '&star=' . rawurlencode($_GET['star']) : '')
167 . (!empty($_GET['bmark']) ? '&bmark=' . rawurlencode($_GET['bmark']) : '')
168 . (!empty($_GET['conv']) ? '&conv=' . rawurlencode($_GET['conv']) : '')
169 . (!empty($_GET['nets']) ? '&nets=' . rawurlencode($_GET['nets']) : '')
170 . (!empty($_GET['cmin']) ? '&cmin=' . rawurlencode($_GET['cmin']) : '')
171 . (!empty($_GET['cmax']) ? '&cmax=' . rawurlencode($_GET['cmax']) : '')
172 . (!empty($_GET['file']) ? '&file=' . rawurlencode($_GET['file']) : '');
175 $terms = DBA::select('search', ['id', 'term'], ['uid' => local_user()]);
178 while ($rr = DBA::fetch($terms)) {
181 'term' => $rr['term'],
182 'encodedterm' => urlencode($rr['term']),
183 'delete' => L10n::t('Remove term'),
184 'selected' => ($search == $rr['term']),
188 $tpl = Renderer::getMarkupTemplate('saved_searches_aside.tpl');
189 $o = Renderer::replaceMacros($tpl, [
190 '$title' => L10n::t('Saved Searches'),
191 '$add' => L10n::t('add'),
192 '$searchbox' => HTML::search($search, 'netsearch-box', $srchurl),
200 * Return selected tab from query
203 * '/network' => $no_active = 'active'
204 * '/network?f=&order=comment' => $comment_active = 'active'
205 * '/network?f=&order=post' => $postord_active = 'active'
206 * '/network?f=&conv=1', => $conv_active = 'active'
207 * '/network/new', => $new_active = 'active'
208 * '/network?f=&star=1', => $starred_active = 'active'
209 * '/network?f=&bmark=1', => $bookmarked_active = 'active'
211 * @return Array ($no_active, $comment_active, $postord_active, $conv_active, $new_active, $starred_active, $bookmarked_active);
213 function network_query_get_sel_tab(App $a)
216 $starred_active = '';
218 $bookmarked_active = '';
221 $postord_active = '';
223 if (($a->argc > 1 && $a->argv[1] === 'new') || ($a->argc > 2 && $a->argv[2] === 'new')) {
224 $new_active = 'active';
227 if (!empty($_GET['star'])) {
228 $starred_active = 'active';
231 if (!empty($_GET['bmark'])) {
232 $bookmarked_active = 'active';
235 if (!empty($_GET['conv'])) {
236 $conv_active = 'active';
239 if (($new_active == '') && ($starred_active == '') && ($bookmarked_active == '') && ($conv_active == '')) {
240 $no_active = 'active';
243 if ($no_active == 'active' && !empty($_GET['order'])) {
244 switch($_GET['order']) {
245 case 'post' : $postord_active = 'active'; $no_active=''; break;
246 case 'comment' : $all_active = 'active'; $no_active=''; break;
250 return [$no_active, $all_active, $postord_active, $conv_active, $new_active, $starred_active, $bookmarked_active];
253 function network_query_get_sel_group(App $a)
257 if ($a->argc >= 2 && is_numeric($a->argv[1])) {
258 $group = $a->argv[1];
265 * @brief Sets the pager data and returns SQL
267 * @param App $a The global App
268 * @param integer $update Used for the automatic reloading
269 * @return string SQL with the appropriate LIMIT clause
271 function networkPager(App $a, Pager $pager, $update)
274 // only setup pagination on initial page view
278 // check if we serve a mobile device and get the user settings
281 $itemspage_network = PConfig::get(local_user(), 'system', 'itemspage_mobile_network');
282 $itemspage_network = ((intval($itemspage_network)) ? $itemspage_network : 20);
284 $itemspage_network = PConfig::get(local_user(), 'system', 'itemspage_network');
285 $itemspage_network = ((intval($itemspage_network)) ? $itemspage_network : 40);
288 // now that we have the user settings, see if the theme forces
289 // a maximum item number which is lower then the user choice
290 if (($a->force_max_items > 0) && ($a->force_max_items < $itemspage_network)) {
291 $itemspage_network = $a->force_max_items;
294 $pager->setItemsPerPage($itemspage_network);
296 return sprintf(" LIMIT %d, %d ", $pager->getStart(), $pager->getItemsPerPage());
300 * @brief Sets items as seen
302 * @param array $condition The array with the SQL condition
304 function networkSetSeen($condition)
306 if (empty($condition)) {
310 $unseen = Item::exists($condition);
313 $r = Item::update(['unseen' => false], $condition);
318 * @brief Create the conversation HTML
320 * @param App $a The global App
321 * @param array $items Items of the conversation
322 * @param string $mode Display mode for the conversation
323 * @param integer $update Used for the automatic reloading
324 * @return string HTML of the conversation
326 function networkConversation(App $a, $items, Pager $pager, $mode, $update, $ordering = '')
328 // Set this so that the conversation function can find out contact info for our wall-wall items
329 $a->page_contact = $a->contact;
331 if (!is_array($items)) {
332 Logger::log("Expecting items to be an array. Got " . print_r($items, true));
336 $o = conversation($a, $items, $pager, $mode, $update, false, $ordering, local_user());
339 if (PConfig::get(local_user(), 'system', 'infinite_scroll')) {
340 $o .= HTML::scrollLoader();
342 $o .= $pager->renderMinimal(count($items));
349 function network_content(App $a, $update = 0, $parent = 0)
352 return Login::form();
355 /// @TODO Is this really necessary? $a is already available to hooks
356 $arr = ['query' => $a->query_string];
357 Hook::callAll('network_content_init', $arr);
362 for ($x = 1; $x < $a->argc; $x ++) {
363 if ($a->argv[$x] === 'new') {
369 if (!empty($_GET['file'])) {
374 $o = networkFlatView($a, $update);
376 $o = networkThreadedView($a, $update, $parent);
380 info("No items found");
387 * @brief Get the network content in flat view
389 * @param Pager $pager
390 * @param App $a The global App
391 * @param integer $update Used for the automatic reloading
392 * @return string HTML of the network content in flat view
394 function networkFlatView(App $a, $update = 0)
397 // Rawmode is used for fetching new content at the end of the page
398 $rawmode = (isset($_GET['mode']) && ($_GET['mode'] == 'raw'));
400 if (isset($_GET['last_id'])) {
401 $last_id = intval($_GET['last_id']);
408 $file = defaults($_GET, 'file', '');
410 if (!$update && !$rawmode) {
411 $tabs = network_tabs($a);
414 Nav::setSelected('network');
418 'allow_location' => $a->user['allow_location'],
419 'default_location' => $a->user['default-location'],
420 'nickname' => $a->user['nickname'],
421 'lockstate' => (is_array($a->user) &&
422 (strlen($a->user['allow_cid']) || strlen($a->user['allow_gid']) ||
423 strlen($a->user['deny_cid']) || strlen($a->user['deny_gid'])) ? 'lock' : 'unlock'),
424 'default_perms' => ACL::getDefaultUserPermissions($a->user),
425 'acl' => ACL::getFullSelectorHTML($a->user, true),
427 'visitor' => 'block',
428 'profile_uid' => local_user(),
432 $o .= status_editor($a, $x);
434 if (!Config::get('theme', 'hide_eventlist')) {
435 $o .= Profile::getBirthdays();
436 $o .= Profile::getEventsReminderHTML();
440 $pager = new Pager($a->query_string);
442 /// @TODO Figure out why this variable is unused
443 $pager_sql = networkPager($a, $pager, $update);
446 $condition = ["`term` = ? AND `otype` = ? AND `type` = ? AND `uid` = ?",
447 $file, TERM_OBJ_POST, TERM_FILE, local_user()];
448 $params = ['order' => ['tid' => true], 'limit' => [$pager->getStart(), $pager->getItemsPerPage()]];
449 $result = DBA::select('term', ['oid'], $condition);
452 while ($term = DBA::fetch($result)) {
453 $posts[] = $term['oid'];
457 if (count($posts) == 0) {
460 $condition = ['uid' => local_user(), 'id' => $posts];
462 $condition = ['uid' => local_user()];
465 $params = ['order' => ['id' => true], 'limit' => [$pager->getStart(), $pager->getItemsPerPage()]];
466 $result = Item::selectForUser(local_user(), [], $condition, $params);
467 $items = Item::inArray($result);
469 $condition = ['unseen' => true, 'uid' => local_user()];
470 networkSetSeen($condition);
472 $o .= networkConversation($a, $items, $pager, 'network-new', $update);
478 * @brief Get the network content in threaded view
480 * @global Pager $pager
481 * @param App $a The global App
482 * @param integer $update Used for the automatic reloading
483 * @param integer $parent
484 * @return string HTML of the network content in flat view
486 function networkThreadedView(App $a, $update, $parent)
488 /// @TODO this will have to be converted to a static property of the converted Module\Network class
491 // Rawmode is used for fetching new content at the end of the page
492 $rawmode = (isset($_GET['mode']) AND ( $_GET['mode'] == 'raw'));
494 if (isset($_GET['last_received']) && isset($_GET['last_commented']) && isset($_GET['last_created']) && isset($_GET['last_id'])) {
495 $last_received = DateTimeFormat::utc($_GET['last_received']);
496 $last_commented = DateTimeFormat::utc($_GET['last_commented']);
497 $last_created = DateTimeFormat::utc($_GET['last_created']);
498 $last_id = intval($_GET['last_id']);
501 $last_commented = '';
506 $datequery = $datequery2 = '';
510 $default_permissions = [];
513 for ($x = 1; $x < $a->argc; $x ++) {
514 if (is_a_date_arg($a->argv[$x])) {
516 $datequery2 = Strings::escapeHtml($a->argv[$x]);
518 $datequery = Strings::escapeHtml($a->argv[$x]);
519 $_GET['order'] = 'post';
521 } elseif (intval($a->argv[$x])) {
522 $gid = intval($a->argv[$x]);
523 $default_permissions = ['allow_gid' => '<' . $gid . '>'];
530 $cid = intval(defaults($_GET, 'cid' , 0));
531 $star = intval(defaults($_GET, 'star' , 0));
532 $bmark = intval(defaults($_GET, 'bmark', 0));
533 $conv = intval(defaults($_GET, 'conv' , 0));
534 $order = Strings::escapeTags(defaults($_GET, 'order', 'comment'));
535 $nets = defaults($_GET, 'nets' , '');
538 $default_permissions = ['allow_cid' => '<' . intval($cid) . '>'];
542 $r = DBA::select('contact', ['id'], ['uid' => local_user(), 'network' => $nets], ['self' => false]);
545 while ($rr = DBA::fetch($r)) {
546 $str .= '<' . $rr['id'] . '>';
549 $default_permissions = ['allow_cid' => $str];
553 if (!$update && !$rawmode) {
554 $tabs = network_tabs($a);
557 if ($gid && ($t = Contact::getOStatusCountByGroupId($gid)) && !PConfig::get(local_user(), 'system', 'nowarn_insecure')) {
558 notice(L10n::tt("Warning: This group contains %s member from a network that doesn't allow non public messages.",
559 "Warning: This group contains %s members from a network that doesn't allow non public messages.",
561 notice(L10n::t("Messages in this group won't be send to these receivers.").EOL);
564 Nav::setSelected('network');
569 // If $cid belongs to a communitity forum or a privat goup,.add a mention to the status editor
570 $condition = ["`id` = ? AND (`forum` OR `prv`)", $cid];
571 $contact = DBA::selectFirst('contact', ['addr', 'nick'], $condition);
572 if (DBA::isResult($contact)) {
573 if ($contact['addr'] != '') {
574 $content = '!' . $contact['addr'];
576 $content = '!' . $contact['nick'] . '+' . $cid;
583 'allow_location' => $a->user['allow_location'],
584 'default_location' => $a->user['default-location'],
585 'nickname' => $a->user['nickname'],
586 'lockstate' => ($gid || $cid || $nets || (is_array($a->user) &&
587 (strlen($a->user['allow_cid']) || strlen($a->user['allow_gid']) ||
588 strlen($a->user['deny_cid']) || strlen($a->user['deny_gid']))) ? 'lock' : 'unlock'),
589 'default_perms' => ACL::getDefaultUserPermissions($a->user),
590 'acl' => ACL::getFullSelectorHTML($a->user, true, $default_permissions),
591 'bang' => (($gid || $cid || $nets) ? '!' : ''),
592 'visitor' => 'block',
593 'profile_uid' => local_user(),
594 'content' => $content,
597 $o .= status_editor($a, $x);
600 // We don't have to deal with ACLs on this page. You're looking at everything
601 // that belongs to you, hence you can see all of it. We will filter by group if
604 $sql_post_table = '';
605 $sql_options = ($star ? " AND `thread`.`starred` " : '');
606 $sql_options .= ($bmark ? sprintf(" AND `thread`.`post-type` = %d ", Item::PT_PAGE) : '');
607 $sql_extra = $sql_options;
610 $sql_table = '`thread`';
611 $sql_parent = '`iid`';
615 $sql_table = '`item`';
616 $sql_parent = '`parent`';
617 $sql_post_table = " INNER JOIN `thread` ON `thread`.`iid` = `item`.`parent`";
620 $sql_nets = (($nets) ? sprintf(" AND $sql_table.`network` = '%s' ", DBA::escape($nets)) : '');
621 $sql_tag_nets = (($nets) ? sprintf(" AND `item`.`network` = '%s' ", DBA::escape($nets)) : '');
624 $group = DBA::selectFirst('group', ['name'], ['id' => $gid, 'uid' => local_user()]);
625 if (!DBA::isResult($group)) {
629 notice(L10n::t('No such group') . EOL);
630 $a->internalRedirect('network/0');
634 $contacts = Group::expand([$gid]);
636 if ((is_array($contacts)) && count($contacts)) {
637 $contact_str_self = '';
639 $contact_str = implode(',', $contacts);
640 $self = DBA::selectFirst('contact', ['id'], ['uid' => local_user(), 'self' => true]);
641 if (DBA::isResult($self)) {
642 $contact_str_self = $self['id'];
645 $sql_post_table .= " INNER JOIN `item` AS `temp1` ON `temp1`.`id` = " . $sql_table . "." . $sql_parent;
646 $sql_extra3 .= " AND (`thread`.`contact-id` IN ($contact_str) ";
647 $sql_extra3 .= " OR (`thread`.`contact-id` = '$contact_str_self' AND `temp1`.`allow_gid` LIKE '" . Strings::protectSprintf('%<' . intval($gid) . '>%') . "' AND `temp1`.`private`))";
649 $sql_extra3 .= " AND false ";
650 info(L10n::t('Group is empty'));
653 $o = Renderer::replaceMacros(Renderer::getMarkupTemplate('section_title.tpl'), [
654 '$title' => L10n::t('Group: %s', $group['name'])
657 $fields = ['id', 'name', 'network', 'writable', 'nurl',
658 'forum', 'prv', 'contact-type', 'addr', 'thumb', 'location'];
659 $condition = ["`id` = ? AND (NOT `blocked` OR `pending`)", $cid];
660 $contact = DBA::selectFirst('contact', $fields, $condition);
661 if (DBA::isResult($contact)) {
662 $sql_extra = " AND " . $sql_table . ".`contact-id` = " . intval($cid);
666 'name' => $contact['name'],
667 'itemurl' => defaults($contact, 'addr', $contact['nurl']),
668 'thumb' => ProxyUtils::proxifyUrl($contact['thumb'], false, ProxyUtils::SIZE_THUMB),
669 'details' => $contact['location'],
672 $entries[0]['account_type'] = Contact::getAccountType($contact);
674 $o = Renderer::replaceMacros(Renderer::getMarkupTemplate('viewcontact_template.tpl'), [
675 'contacts' => $entries,
679 if ($contact['network'] === Protocol::OSTATUS && $contact['writable'] && !PConfig::get(local_user(),'system','nowarn_insecure')) {
680 notice(L10n::t('Private messages to this person are at risk of public disclosure.') . EOL);
683 notice(L10n::t('Invalid contact.') . EOL);
684 $a->internalRedirect('network');
689 if (!$gid && !$cid && !$update && !Config::get('theme', 'hide_eventlist')) {
690 $o .= Profile::getBirthdays();
691 $o .= Profile::getEventsReminderHTML();
695 $sql_extra3 .= Strings::protectSprintf(sprintf(" AND $sql_table.created <= '%s' ",
696 DBA::escape(DateTimeFormat::convert($datequery, 'UTC', date_default_timezone_get()))));
699 $sql_extra3 .= Strings::protectSprintf(sprintf(" AND $sql_table.created >= '%s' ",
700 DBA::escape(DateTimeFormat::convert($datequery2, 'UTC', date_default_timezone_get()))));
704 $sql_extra3 .= " AND $sql_table.`mention`";
707 // Normal conversation view
708 if ($order === 'post') {
709 $ordering = '`created`';
710 $order_mode = 'created';
712 $ordering = '`commented`';
713 $order_mode = 'commented';
716 $sql_order = "$sql_table.$ordering";
718 if (!empty($_GET['offset'])) {
719 $sql_range = sprintf(" AND $sql_order <= '%s'", DBA::escape($_GET['offset']));
724 $pager = new Pager($a->query_string);
726 $pager_sql = networkPager($a, $pager, $update);
730 switch ($order_mode) {
732 if ($last_received != '') {
733 $last_date = $last_received;
734 $sql_range .= sprintf(" AND $sql_table.`received` < '%s'", DBA::escape($last_received));
736 $pager_sql = sprintf(" LIMIT %d, %d ", $pager->getStart(), $pager->getItemsPerPage());
740 if ($last_commented != '') {
741 $last_date = $last_commented;
742 $sql_range .= sprintf(" AND $sql_table.`commented` < '%s'", DBA::escape($last_commented));
744 $pager_sql = sprintf(" LIMIT %d, %d ", $pager->getStart(), $pager->getItemsPerPage());
748 if ($last_created != '') {
749 $last_date = $last_created;
750 $sql_range .= sprintf(" AND $sql_table.`created` < '%s'", DBA::escape($last_created));
752 $pager_sql = sprintf(" LIMIT %d, %d ", $pager->getStart(), $pager->getItemsPerPage());
756 if (($last_id > 0) && ($sql_table == '`thread`')) {
757 $sql_range .= sprintf(" AND $sql_table.`iid` < '%s'", DBA::escape($last_id));
759 $pager_sql = sprintf(" LIMIT %d, %d ", $pager->getStart(), $pager->getItemsPerPage());
764 // Fetch a page full of parent items for this page
766 if (!empty($parent)) {
767 // Load only a single thread
768 $sql_extra4 = "`item`.`id` = ".intval($parent);
770 // Load all unseen items
771 $sql_extra4 = "`item`.`unseen`";
772 if (Config::get("system", "like_no_comment")) {
773 $sql_extra4 .= " AND `item`.`gravity` IN (" . GRAVITY_PARENT . "," . GRAVITY_COMMENT . ")";
775 if ($order === 'post') {
776 // Only show toplevel posts when updating posts in this order mode
777 $sql_extra4 .= " AND `item`.`id` = `item`.`parent`";
781 $r = q("SELECT `item`.`parent-uri` AS `uri`, `item`.`parent` AS `item_id`, $sql_order AS `order_date`
782 FROM `item` $sql_post_table
783 STRAIGHT_JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
784 AND (NOT `contact`.`blocked` OR `contact`.`pending`)
785 AND (`item`.`gravity` != %d
786 OR `contact`.`uid` = `item`.`uid` AND `contact`.`self`
787 OR `contact`.`rel` IN (%d, %d) AND NOT `contact`.`readonly`)
788 LEFT JOIN `user-item` ON `user-item`.`iid` = `item`.`id` AND `user-item`.`uid` = %d
789 WHERE `item`.`uid` = %d AND `item`.`visible` AND NOT `item`.`deleted`
790 AND (`user-item`.`hidden` IS NULL OR NOT `user-item`.`hidden`)
791 AND NOT `item`.`moderated` AND $sql_extra4
792 $sql_extra3 $sql_extra $sql_range $sql_nets
793 ORDER BY `order_date` DESC LIMIT 100",
794 intval(GRAVITY_PARENT),
795 intval(Contact::SHARING),
796 intval(Contact::FRIEND),
797 intval(local_user()),
801 $r = q("SELECT `item`.`uri`, `thread`.`iid` AS `item_id`, $sql_order AS `order_date`
802 FROM `thread` $sql_post_table
803 STRAIGHT_JOIN `contact` ON `contact`.`id` = `thread`.`contact-id`
804 AND (NOT `contact`.`blocked` OR `contact`.`pending`)
805 STRAIGHT_JOIN `item` ON `item`.`id` = `thread`.`iid`
806 AND (`item`.`gravity` != %d
807 OR `contact`.`uid` = `item`.`uid` AND `contact`.`self`
808 OR `contact`.`rel` IN (%d, %d) AND NOT `contact`.`readonly`)
809 LEFT JOIN `user-item` ON `user-item`.`iid` = `item`.`id` AND `user-item`.`uid` = %d
810 WHERE `thread`.`uid` = %d AND `thread`.`visible` AND NOT `thread`.`deleted`
811 AND NOT `thread`.`moderated`
812 AND (`user-item`.`hidden` IS NULL OR NOT `user-item`.`hidden`)
813 $sql_extra2 $sql_extra3 $sql_range $sql_extra $sql_nets
814 ORDER BY `order_date` DESC $pager_sql",
815 intval(GRAVITY_PARENT),
816 intval(Contact::SHARING),
817 intval(Contact::FRIEND),
818 intval(local_user()),
823 // Only show it when unfiltered (no groups, no networks, ...)
824 if (in_array($nets, ['', Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::DIASPORA, Protocol::OSTATUS]) && (strlen($sql_extra . $sql_extra2 . $sql_extra3) == 0)) {
825 if (DBA::isResult($r)) {
826 $top_limit = current($r)['order_date'];
827 $bottom_limit = end($r)['order_date'];
828 if (empty($_SESSION['network_last_top_limit']) || ($_SESSION['network_last_top_limit'] < $top_limit)) {
829 $_SESSION['network_last_top_limit'] = $top_limit;
832 $top_limit = $bottom_limit = DateTimeFormat::utcNow();
835 // When checking for updates we need to fetch from the newest date to the newest date before
836 // Only do this, when the last stored date isn't too long ago (10 times the update interval)
837 $browser_update = PConfig::get(local_user(), 'system', 'update_interval', 40000) / 1000;
839 if (($browser_update > 0) && $update && !empty($_SESSION['network_last_date']) &&
840 (($bottom_limit < $_SESSION['network_last_date']) || ($top_limit == $bottom_limit)) &&
841 ((time() - $_SESSION['network_last_date_timestamp']) < ($browser_update * 10))) {
842 $bottom_limit = $_SESSION['network_last_date'];
844 $_SESSION['network_last_date'] = defaults($_SESSION, 'network_last_top_limit', $top_limit);
845 $_SESSION['network_last_date_timestamp'] = time();
847 if ($last_date > $top_limit) {
848 $top_limit = $last_date;
849 } elseif ($pager->getPage() == 1) {
850 // Highest possible top limit when we are on the first page
851 $top_limit = DateTimeFormat::utcNow();
854 $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`
855 STRAIGHT_JOIN (SELECT `oid` FROM `term` WHERE `term` IN
856 (SELECT SUBSTR(`term`, 2) FROM `search` WHERE `uid` = ? AND `term` LIKE '#%') AND `otype` = ? AND `type` = ? AND `uid` = 0) AS `term`
857 ON `item`.`id` = `term`.`oid`
858 STRAIGHT_JOIN `contact` AS `author` ON `author`.`id` = `item`.`author-id`
859 WHERE `item`.`uid` = 0 AND `item`.$ordering < ? AND `item`.$ordering > ?
860 AND NOT `author`.`hidden` AND NOT `author`.`blocked`" . $sql_tag_nets,
861 local_user(), TERM_OBJ_POST, TERM_HASHTAG,
862 $top_limit, $bottom_limit);
864 $data = DBA::toArray($items);
866 if (count($data) > 0) {
867 $tag_top_limit = current($data)['order_date'];
868 if ($_SESSION['network_last_date'] < $tag_top_limit) {
869 $_SESSION['network_last_date'] = $tag_top_limit;
872 Logger::log('Tagged items: ' . count($data) . ' - ' . $bottom_limit . ' - ' . $top_limit . ' - ' . local_user().' - '.(int)$update);
874 foreach ($r as $item) {
875 $s[$item['uri']] = $item;
877 foreach ($data as $item) {
878 // Don't show hash tag posts from blocked or ignored contacts
879 $condition = ["`nurl` = ? AND `uid` = ? AND (`blocked` OR `readonly`)",
880 Strings::normaliseLink($item['author-link']), local_user()];
881 if (!DBA::exists('contact', $condition)) {
882 $s[$item['uri']] = $item;
894 if (DBA::isResult($items)) {
897 foreach ($items as $item) {
898 if ($date_offset < $item['order_date']) {
899 $date_offset = $item['order_date'];
901 if (!in_array($item['item_id'], $parents_arr) && ($item['item_id'] > 0)) {
902 $parents_arr[] = $item['item_id'];
905 $parents_str = implode(', ', $parents_arr);
908 if (!empty($_GET['offset'])) {
909 $date_offset = $_GET['offset'];
912 $query_string = $a->query_string;
913 if ($date_offset && !preg_match('/[?&].offset=/', $query_string)) {
914 $query_string .= '&offset=' . urlencode($date_offset);
917 $pager->setQueryString($query_string);
919 // We aren't going to try and figure out at the item, group, and page
920 // level which items you've seen and which you haven't. If you're looking
921 // at the top level network page just mark everything seen.
923 if (!$gid && !$cid && !$star) {
924 $condition = ['unseen' => true, 'uid' => local_user()];
925 networkSetSeen($condition);
926 } elseif ($parents_str) {
927 $condition = ["`uid` = ? AND `unseen` AND `parent` IN (" . DBA::escape($parents_str) . ")", local_user()];
928 networkSetSeen($condition);
933 $o .= networkConversation($a, $items, $pager, $mode, $update, $ordering);
939 * @brief Get the network tabs menu
941 * @param App $a The global App
942 * @return string Html of the networktab
944 function network_tabs(App $a)
947 /// @TODO fix this logic, reduce duplication
948 /// $a->page['content'] .= '<div class="tabs-wrapper">';
949 list($no_active, $all_active, $postord_active, $conv_active, $new_active, $starred_active, $bookmarked_active) = network_query_get_sel_tab($a);
951 // if no tabs are selected, defaults to comments
952 if ($no_active == 'active') {
953 $all_active = 'active';
961 'label' => L10n::t('Commented Order'),
962 'url' => str_replace('/new', '', $cmd) . '?f=&order=comment' . (!empty($_GET['cid']) ? '&cid=' . $_GET['cid'] : ''),
963 'sel' => $all_active,
964 'title' => L10n::t('Sort by Comment Date'),
965 'id' => 'commented-order-tab',
969 'label' => L10n::t('Posted Order'),
970 'url' => str_replace('/new', '', $cmd) . '?f=&order=post' . (!empty($_GET['cid']) ? '&cid=' . $_GET['cid'] : ''),
971 'sel' => $postord_active,
972 'title' => L10n::t('Sort by Post Date'),
973 'id' => 'posted-order-tab',
979 'label' => L10n::t('Personal'),
980 'url' => str_replace('/new', '', $cmd) . (!empty($_GET['cid']) ? '/?f=&cid=' . $_GET['cid'] : '/?f=') . '&conv=1',
981 'sel' => $conv_active,
982 'title' => L10n::t('Posts that mention or involve you'),
983 'id' => 'personal-tab',
987 if (Feature::isEnabled(local_user(), 'new_tab')) {
989 'label' => L10n::t('New'),
990 'url' => 'network/new' . (!empty($_GET['cid']) ? '/?f=&cid=' . $_GET['cid'] : ''),
991 'sel' => $new_active,
992 'title' => L10n::t('Activity Stream - by date'),
993 'id' => 'activitiy-by-date-tab',
998 if (Feature::isEnabled(local_user(), 'link_tab')) {
1000 'label' => L10n::t('Shared Links'),
1001 'url' => str_replace('/new', '', $cmd) . (!empty($_GET['cid']) ? '/?f=&cid=' . $_GET['cid'] : '/?f=') . '&bmark=1',
1002 'sel' => $bookmarked_active,
1003 'title' => L10n::t('Interesting Links'),
1004 'id' => 'shared-links-tab',
1010 'label' => L10n::t('Starred'),
1011 'url' => str_replace('/new', '', $cmd) . (!empty($_GET['cid']) ? '/?f=&cid=' . $_GET['cid'] : '/?f=') . '&star=1',
1012 'sel' => $starred_active,
1013 'title' => L10n::t('Favourite Posts'),
1014 'id' => 'starred-posts-tab',
1018 // save selected tab, but only if not in file mode
1019 if (empty($_GET['file'])) {
1020 PConfig::set(local_user(), 'network.view', 'tab.selected', [
1021 $all_active, $postord_active, $conv_active, $new_active, $starred_active, $bookmarked_active
1025 $arr = ['tabs' => $tabs];
1026 Hook::callAll('network_tabs', $arr);
1028 $tpl = Renderer::getMarkupTemplate('common_tabs.tpl');
1030 return Renderer::replaceMacros($tpl, ['$tabs' => $arr['tabs']]);
1032 // --- end item filter tabs
1036 * Network hook into the HTML head to enable infinite scroll.
1038 * Since the HTML head is built after the module content has been generated, we need to retrieve the base query string
1039 * of the page to make the correct asynchronous call. This is obtained through the Pager that was instantiated in
1040 * networkThreadedView or networkFlatView.
1042 * @global Pager $pager
1044 * @param string $htmlhead The head tag HTML string
1046 function network_infinite_scroll_head(App $a, &$htmlhead)
1048 /// @TODO this will have to be converted to a static property of the converted Module\Network class
1051 if (PConfig::get(local_user(), 'system', 'infinite_scroll')
1052 && defaults($_GET, 'mode', '') != 'minimal'
1054 $tpl = Renderer::getMarkupTemplate('infinite_scroll_head.tpl');
1055 $htmlhead .= Renderer::replaceMacros($tpl, [
1056 '$pageno' => $pager->getPage(),
1057 '$reload_uri' => $pager->getBaseQueryString()