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\Addon;
16 use Friendica\Core\Config;
17 use Friendica\Core\Hook;
18 use Friendica\Core\L10n;
19 use Friendica\Core\Logger;
20 use Friendica\Core\PConfig;
21 use Friendica\Core\Protocol;
22 use Friendica\Core\Renderer;
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\Module\Login;
29 use Friendica\Util\DateTimeFormat;
30 use Friendica\Util\Proxy as ProxyUtils;
31 use Friendica\Util\Strings;
33 require_once 'include/conversation.php';
34 require_once 'include/items.php';
36 function network_init(App $a)
39 notice(L10n::t('Permission denied.') . EOL);
43 Hook::add('head', __FILE__, 'network_infinite_scroll_head');
45 $search = (x($_GET, 'search') ? Strings::escapeHtml($_GET['search']) : '');
47 if (($search != '') && !empty($_GET['submit'])) {
48 $a->internalRedirect('search?search=' . urlencode($search));
51 if (x($_GET, 'save')) {
52 $exists = DBA::exists('search', ['uid' => local_user(), 'term' => $search]);
54 DBA::insert('search', ['uid' => local_user(), 'term' => $search]);
57 if (x($_GET, 'remove')) {
58 DBA::delete('search', ['uid' => local_user(), 'term' => $search]);
61 $is_a_date_query = false;
63 $group_id = (($a->argc > 1 && is_numeric($a->argv[1])) ? intval($a->argv[1]) : 0);
66 if (x($_GET, 'cid') && intval($_GET['cid']) != 0) {
68 $_GET['nets'] = 'all';
73 for ($x = 1; $x < $a->argc; $x ++) {
74 if (is_a_date_arg($a->argv[$x])) {
75 $is_a_date_query = true;
81 // convert query string to array. remove friendica args
83 $query_string = str_replace($a->cmd . '?', '', $a->query_string);
84 parse_str($query_string, $query_array);
85 array_shift($query_array);
87 // fetch last used network view and redirect if needed
88 if (!$is_a_date_query) {
89 $sel_nets = defaults($_GET, 'nets', false);
90 $sel_tabs = network_query_get_sel_tab($a);
91 $sel_groups = network_query_get_sel_group($a);
92 $last_sel_tabs = PConfig::get(local_user(), 'network.view', 'tab.selected');
94 $remember_tab = ($sel_tabs[0] === 'active' && is_array($last_sel_tabs) && $last_sel_tabs[0] !== 'active');
96 $net_baseurl = '/network';
99 if ($sel_groups !== false) {
100 $net_baseurl .= '/' . $sel_groups;
104 // redirect if current selected tab is '/network' and
105 // last selected tab is _not_ '/network?f=&order=comment'.
106 // and this isn't a date query
117 'f=&order=comment', //all
118 'f=&order=post', //postord
121 'f=&star=1', //starred
122 'f=&bmark=1', //bookmarked
125 $k = array_search('active', $last_sel_tabs);
128 $net_baseurl .= $tab_baseurls[$k];
130 // parse out tab queries
132 $dest_qs = $tab_args[$k];
133 parse_str($dest_qs, $dest_qa);
134 $net_args = array_merge($net_args, $dest_qa);
136 $remember_tab = false;
140 if ($sel_nets !== false) {
141 $net_args['nets'] = $sel_nets;
145 $net_args = array_merge($query_array, $net_args);
146 $net_queries = build_querystring($net_args);
148 $redir_url = ($net_queries ? $net_baseurl . '?' . $net_queries : $net_baseurl);
150 $a->internalRedirect($redir_url);
154 // If nets is set to all, unset it
155 if (x($_GET, 'nets') && $_GET['nets'] === 'all') {
156 unset($_GET['nets']);
159 if (!x($a->page, 'aside')) {
160 $a->page['aside'] = '';
163 $a->page['aside'] .= (Feature::isEnabled(local_user(), 'groups') ?
164 Group::sidebarWidget('network/0', 'network', 'standard', $group_id) : '');
165 $a->page['aside'] .= (Feature::isEnabled(local_user(), 'forumlist_widget') ? ForumManager::widget(local_user(), $cid) : '');
166 $a->page['aside'] .= posted_date_widget('network', local_user(), false);
167 $a->page['aside'] .= Widget::networks('network', (x($_GET, 'nets') ? $_GET['nets'] : ''));
168 $a->page['aside'] .= saved_searches($search);
169 $a->page['aside'] .= Widget::fileAs('network', (x($_GET, 'file') ? $_GET['file'] : ''));
172 function saved_searches($search)
174 if (!Feature::isEnabled(local_user(), 'savedsearch')) {
180 $srchurl = '/network?f='
181 . ((x($_GET, 'cid')) ? '&cid=' . $_GET['cid'] : '')
182 . ((x($_GET, 'star')) ? '&star=' . $_GET['star'] : '')
183 . ((x($_GET, 'bmark')) ? '&bmark=' . $_GET['bmark'] : '')
184 . ((x($_GET, 'conv')) ? '&conv=' . $_GET['conv'] : '')
185 . ((x($_GET, 'nets')) ? '&nets=' . $_GET['nets'] : '')
186 . ((x($_GET, 'cmin')) ? '&cmin=' . $_GET['cmin'] : '')
187 . ((x($_GET, 'cmax')) ? '&cmax=' . $_GET['cmax'] : '')
188 . ((x($_GET, 'file')) ? '&file=' . $_GET['file'] : '');
193 $terms = DBA::select('search', ['id', 'term'], ['uid' => local_user()]);
196 while ($rr = DBA::fetch($terms)) {
199 'term' => $rr['term'],
200 'encodedterm' => urlencode($rr['term']),
201 'delete' => L10n::t('Remove term'),
202 'selected' => ($search == $rr['term']),
206 $tpl = Renderer::getMarkupTemplate('saved_searches_aside.tpl');
207 $o = Renderer::replaceMacros($tpl, [
208 '$title' => L10n::t('Saved Searches'),
209 '$add' => L10n::t('add'),
210 '$searchbox' => HTML::search($search, 'netsearch-box', $srchurl, true),
218 * Return selected tab from query
221 * '/network' => $no_active = 'active'
222 * '/network?f=&order=comment' => $comment_active = 'active'
223 * '/network?f=&order=post' => $postord_active = 'active'
224 * '/network?f=&conv=1', => $conv_active = 'active'
225 * '/network/new', => $new_active = 'active'
226 * '/network?f=&star=1', => $starred_active = 'active'
227 * '/network?f=&bmark=1', => $bookmarked_active = 'active'
229 * @return Array ($no_active, $comment_active, $postord_active, $conv_active, $new_active, $starred_active, $bookmarked_active);
231 function network_query_get_sel_tab(App $a)
234 $starred_active = '';
236 $bookmarked_active = '';
239 $postord_active = '';
241 if (($a->argc > 1 && $a->argv[1] === 'new') || ($a->argc > 2 && $a->argv[2] === 'new')) {
242 $new_active = 'active';
245 if (x($_GET, 'star')) {
246 $starred_active = 'active';
249 if (x($_GET, 'bmark')) {
250 $bookmarked_active = 'active';
253 if (x($_GET, 'conv')) {
254 $conv_active = 'active';
257 if (($new_active == '') && ($starred_active == '') && ($bookmarked_active == '') && ($conv_active == '')) {
258 $no_active = 'active';
261 if ($no_active == 'active' && x($_GET, 'order')) {
262 switch($_GET['order']) {
263 case 'post' : $postord_active = 'active'; $no_active=''; break;
264 case 'comment' : $all_active = 'active'; $no_active=''; break;
268 return [$no_active, $all_active, $postord_active, $conv_active, $new_active, $starred_active, $bookmarked_active];
271 function network_query_get_sel_group(App $a)
275 if ($a->argc >= 2 && is_numeric($a->argv[1])) {
276 $group = $a->argv[1];
283 * @brief Sets the pager data and returns SQL
285 * @param App $a The global App
286 * @param integer $update Used for the automatic reloading
287 * @return string SQL with the appropriate LIMIT clause
289 function networkPager(App $a, Pager $pager, $update)
292 // only setup pagination on initial page view
296 // check if we serve a mobile device and get the user settings
299 $itemspage_network = PConfig::get(local_user(), 'system', 'itemspage_mobile_network');
300 $itemspage_network = ((intval($itemspage_network)) ? $itemspage_network : 20);
302 $itemspage_network = PConfig::get(local_user(), 'system', 'itemspage_network');
303 $itemspage_network = ((intval($itemspage_network)) ? $itemspage_network : 40);
306 // now that we have the user settings, see if the theme forces
307 // a maximum item number which is lower then the user choice
308 if (($a->force_max_items > 0) && ($a->force_max_items < $itemspage_network)) {
309 $itemspage_network = $a->force_max_items;
312 $pager->setItemsPerPage($itemspage_network);
314 return sprintf(" LIMIT %d, %d ", $pager->getStart(), $pager->getItemsPerPage());
318 * @brief Sets items as seen
320 * @param array $condition The array with the SQL condition
322 function networkSetSeen($condition)
324 if (empty($condition)) {
328 $unseen = Item::exists($condition);
331 $r = Item::update(['unseen' => false], $condition);
336 * @brief Create the conversation HTML
338 * @param App $a The global App
339 * @param array $items Items of the conversation
340 * @param string $mode Display mode for the conversation
341 * @param integer $update Used for the automatic reloading
342 * @return string HTML of the conversation
344 function networkConversation(App $a, $items, Pager $pager, $mode, $update, $ordering = '')
346 // Set this so that the conversation function can find out contact info for our wall-wall items
347 $a->page_contact = $a->contact;
349 $items = (empty($items) ? [] : $items);
350 $o = conversation($a, $items, $pager, $mode, $update, false, $ordering, local_user());
353 if (PConfig::get(local_user(), 'system', 'infinite_scroll')) {
354 $o .= HTML::scrollLoader();
356 $o .= $pager->renderMinimal(count($items));
363 function network_content(App $a, $update = 0, $parent = 0)
366 return Login::form();
369 /// @TODO Is this really necessary? $a is already available to hooks
370 $arr = ['query' => $a->query_string];
371 Addon::callHooks('network_content_init', $arr);
376 for ($x = 1; $x < $a->argc; $x ++) {
377 if ($a->argv[$x] === 'new') {
383 if (!empty($_GET['file'])) {
388 $o = networkFlatView($a, $update);
390 $o = networkThreadedView($a, $update, $parent);
397 * @brief Get the network content in flat view
399 * @param Pager $pager
400 * @param App $a The global App
401 * @param integer $update Used for the automatic reloading
402 * @return string HTML of the network content in flat view
404 function networkFlatView(App $a, $update = 0)
407 // Rawmode is used for fetching new content at the end of the page
408 $rawmode = (isset($_GET['mode']) && ($_GET['mode'] == 'raw'));
410 if (isset($_GET['last_id'])) {
411 $last_id = intval($_GET['last_id']);
418 $file = defaults($_GET, 'file', '');
420 if (!$update && !$rawmode) {
421 $tabs = network_tabs($a);
424 Nav::setSelected('network');
428 'allow_location' => $a->user['allow_location'],
429 'default_location' => $a->user['default-location'],
430 'nickname' => $a->user['nickname'],
431 'lockstate' => (is_array($a->user) &&
432 (strlen($a->user['allow_cid']) || strlen($a->user['allow_gid']) ||
433 strlen($a->user['deny_cid']) || strlen($a->user['deny_gid'])) ? 'lock' : 'unlock'),
434 'default_perms' => ACL::getDefaultUserPermissions($a->user),
435 'acl' => ACL::getFullSelectorHTML($a->user, true),
437 'visitor' => 'block',
438 'profile_uid' => local_user(),
442 $o .= status_editor($a, $x);
444 if (!Config::get('theme', 'hide_eventlist')) {
445 $o .= Profile::getBirthdays();
446 $o .= Profile::getEventsReminderHTML();
450 $pager = new Pager($a->query_string);
452 /// @TODO Figure out why this variable is unused
453 $pager_sql = networkPager($a, $pager, $update);
456 $condition = ["`term` = ? AND `otype` = ? AND `type` = ? AND `uid` = ?",
457 $file, TERM_OBJ_POST, TERM_FILE, local_user()];
458 $params = ['order' => ['tid' => true], 'limit' => [$pager->getStart(), $pager->getItemsPerPage()]];
459 $result = DBA::select('term', ['oid'], $condition);
462 while ($term = DBA::fetch($result)) {
463 $posts[] = $term['oid'];
467 $condition = ['uid' => local_user(), 'id' => $posts];
469 $condition = ['uid' => local_user()];
472 $params = ['order' => ['id' => true], 'limit' => [$pager->getStart(), $pager->getItemsPerPage()]];
473 $result = Item::selectForUser(local_user(), [], $condition, $params);
474 $items = Item::inArray($result);
476 $condition = ['unseen' => true, 'uid' => local_user()];
477 networkSetSeen($condition);
479 $o .= networkConversation($a, $items, $pager, 'network-new', $update);
485 * @brief Get the network content in threaded view
487 * @global Pager $pager
488 * @param App $a The global App
489 * @param integer $update Used for the automatic reloading
490 * @param integer $parent
491 * @return string HTML of the network content in flat view
493 function networkThreadedView(App $a, $update, $parent)
495 /// @TODO this will have to be converted to a static property of the converted Module\Network class
498 // Rawmode is used for fetching new content at the end of the page
499 $rawmode = (isset($_GET['mode']) AND ( $_GET['mode'] == 'raw'));
501 if (isset($_GET['last_received']) && isset($_GET['last_commented']) && isset($_GET['last_created']) && isset($_GET['last_id'])) {
502 $last_received = DateTimeFormat::utc($_GET['last_received']);
503 $last_commented = DateTimeFormat::utc($_GET['last_commented']);
504 $last_created = DateTimeFormat::utc($_GET['last_created']);
505 $last_id = intval($_GET['last_id']);
508 $last_commented = '';
513 $datequery = $datequery2 = '';
517 $default_permissions = [];
520 for ($x = 1; $x < $a->argc; $x ++) {
521 if (is_a_date_arg($a->argv[$x])) {
523 $datequery2 = Strings::escapeHtml($a->argv[$x]);
525 $datequery = Strings::escapeHtml($a->argv[$x]);
526 $_GET['order'] = 'post';
528 } elseif (intval($a->argv[$x])) {
529 $gid = intval($a->argv[$x]);
530 $default_permissions = ['allow_gid' => '<' . $gid . '>'];
537 $cid = intval(defaults($_GET, 'cid' , 0));
538 $star = intval(defaults($_GET, 'star' , 0));
539 $bmark = intval(defaults($_GET, 'bmark', 0));
540 $conv = intval(defaults($_GET, 'conv' , 0));
541 $order = Strings::escapeTags(defaults($_GET, 'order', 'comment'));
542 $nets = defaults($_GET, 'nets' , '');
545 $default_permissions = ['allow_cid' => '<' . intval($cid) . '>'];
549 $r = DBA::select('contact', ['id'], ['uid' => local_user(), 'network' => $nets], ['self' => false]);
552 while ($rr = DBA::fetch($r)) {
553 $str .= '<' . $rr['id'] . '>';
556 $default_permissions = ['allow_cid' => $str];
560 if (!$update && !$rawmode) {
561 $tabs = network_tabs($a);
564 if ($gid && ($t = Contact::getOStatusCountByGroupId($gid)) && !PConfig::get(local_user(), 'system', 'nowarn_insecure')) {
565 notice(L10n::tt("Warning: This group contains %s member from a network that doesn't allow non public messages.",
566 "Warning: This group contains %s members from a network that doesn't allow non public messages.",
568 notice(L10n::t("Messages in this group won't be send to these receivers.").EOL);
571 Nav::setSelected('network');
576 // If $cid belongs to a communitity forum or a privat goup,.add a mention to the status editor
577 $condition = ["`id` = ? AND (`forum` OR `prv`)", $cid];
578 $contact = DBA::selectFirst('contact', ['addr', 'nick'], $condition);
579 if (DBA::isResult($contact)) {
580 if ($contact['addr'] != '') {
581 $content = '!' . $contact['addr'];
583 $content = '!' . $contact['nick'] . '+' . $cid;
590 'allow_location' => $a->user['allow_location'],
591 'default_location' => $a->user['default-location'],
592 'nickname' => $a->user['nickname'],
593 'lockstate' => ($gid || $cid || $nets || (is_array($a->user) &&
594 (strlen($a->user['allow_cid']) || strlen($a->user['allow_gid']) ||
595 strlen($a->user['deny_cid']) || strlen($a->user['deny_gid']))) ? 'lock' : 'unlock'),
596 'default_perms' => ACL::getDefaultUserPermissions($a->user),
597 'acl' => ACL::getFullSelectorHTML($a->user, true, $default_permissions),
598 'bang' => (($gid || $cid || $nets) ? '!' : ''),
599 'visitor' => 'block',
600 'profile_uid' => local_user(),
601 'content' => $content,
604 $o .= status_editor($a, $x);
607 // We don't have to deal with ACLs on this page. You're looking at everything
608 // that belongs to you, hence you can see all of it. We will filter by group if
611 $sql_post_table = '';
612 $sql_options = ($star ? " AND `thread`.`starred` " : '');
613 $sql_options .= ($bmark ? sprintf(" AND `thread`.`post-type` = %d ", Item::PT_PAGE) : '');
614 $sql_extra = $sql_options;
617 $sql_table = '`thread`';
618 $sql_parent = '`iid`';
622 $sql_table = '`item`';
623 $sql_parent = '`parent`';
624 $sql_post_table = " INNER JOIN `thread` ON `thread`.`iid` = `item`.`parent`";
627 $sql_nets = (($nets) ? sprintf(" AND $sql_table.`network` = '%s' ", DBA::escape($nets)) : '');
628 $sql_tag_nets = (($nets) ? sprintf(" AND `item`.`network` = '%s' ", DBA::escape($nets)) : '');
631 $group = DBA::selectFirst('group', ['name'], ['id' => $gid, 'uid' => local_user()]);
632 if (!DBA::isResult($group)) {
636 notice(L10n::t('No such group') . EOL);
637 $a->internalRedirect('network/0');
641 $contacts = Group::expand([$gid]);
643 if ((is_array($contacts)) && count($contacts)) {
644 $contact_str_self = '';
646 $contact_str = implode(',', $contacts);
647 $self = DBA::selectFirst('contact', ['id'], ['uid' => local_user(), 'self' => true]);
648 if (DBA::isResult($self)) {
649 $contact_str_self = $self['id'];
652 $sql_post_table .= " INNER JOIN `item` AS `temp1` ON `temp1`.`id` = " . $sql_table . "." . $sql_parent;
653 $sql_extra3 .= " AND (`thread`.`contact-id` IN ($contact_str) ";
654 $sql_extra3 .= " OR (`thread`.`contact-id` = '$contact_str_self' AND `temp1`.`allow_gid` LIKE '" . Strings::protectSprintf('%<' . intval($gid) . '>%') . "' AND `temp1`.`private`))";
656 $sql_extra3 .= " AND false ";
657 info(L10n::t('Group is empty'));
660 $o = Renderer::replaceMacros(Renderer::getMarkupTemplate('section_title.tpl'), [
661 '$title' => L10n::t('Group: %s', $group['name'])
664 $fields = ['id', 'name', 'network', 'writable', 'nurl',
665 'forum', 'prv', 'contact-type', 'addr', 'thumb', 'location'];
666 $condition = ["`id` = ? AND (NOT `blocked` OR `pending`)", $cid];
667 $contact = DBA::selectFirst('contact', $fields, $condition);
668 if (DBA::isResult($contact)) {
669 $sql_extra = " AND " . $sql_table . ".`contact-id` = " . intval($cid);
673 'name' => htmlentities($contact['name']),
674 'itemurl' => defaults($contact, 'addr', $contact['nurl']),
675 'thumb' => ProxyUtils::proxifyUrl($contact['thumb'], false, ProxyUtils::SIZE_THUMB),
676 'details' => $contact['location'],
679 $entries[0]['account_type'] = Contact::getAccountType($contact);
681 $o = Renderer::replaceMacros(Renderer::getMarkupTemplate('viewcontact_template.tpl'), [
682 'contacts' => $entries,
686 if ($contact['network'] === Protocol::OSTATUS && $contact['writable'] && !PConfig::get(local_user(),'system','nowarn_insecure')) {
687 notice(L10n::t('Private messages to this person are at risk of public disclosure.') . EOL);
690 notice(L10n::t('Invalid contact.') . EOL);
691 $a->internalRedirect('network');
696 if (!$gid && !$cid && !$update && !Config::get('theme', 'hide_eventlist')) {
697 $o .= Profile::getBirthdays();
698 $o .= Profile::getEventsReminderHTML();
702 $sql_extra3 .= Strings::protectSprintf(sprintf(" AND $sql_table.created <= '%s' ",
703 DBA::escape(DateTimeFormat::convert($datequery, 'UTC', date_default_timezone_get()))));
706 $sql_extra3 .= Strings::protectSprintf(sprintf(" AND $sql_table.created >= '%s' ",
707 DBA::escape(DateTimeFormat::convert($datequery2, 'UTC', date_default_timezone_get()))));
711 $sql_extra3 .= " AND $sql_table.`mention`";
714 // Normal conversation view
715 if ($order === 'post') {
716 $ordering = '`created`';
717 $order_mode = 'created';
719 $ordering = '`commented`';
720 $order_mode = 'commented';
723 $sql_order = "$sql_table.$ordering";
725 if (!empty($_GET['offset'])) {
726 $sql_range = sprintf(" AND $sql_order <= '%s'", DBA::escape($_GET['offset']));
731 $pager = new Pager($a->query_string);
733 $pager_sql = networkPager($a, $pager, $update);
737 switch ($order_mode) {
739 if ($last_received != '') {
740 $last_date = $last_received;
741 $sql_range .= sprintf(" AND $sql_table.`received` < '%s'", DBA::escape($last_received));
743 $pager_sql = sprintf(" LIMIT %d, %d ", $pager->getStart(), $pager->getItemsPerPage());
747 if ($last_commented != '') {
748 $last_date = $last_commented;
749 $sql_range .= sprintf(" AND $sql_table.`commented` < '%s'", DBA::escape($last_commented));
751 $pager_sql = sprintf(" LIMIT %d, %d ", $pager->getStart(), $pager->getItemsPerPage());
755 if ($last_created != '') {
756 $last_date = $last_created;
757 $sql_range .= sprintf(" AND $sql_table.`created` < '%s'", DBA::escape($last_created));
759 $pager_sql = sprintf(" LIMIT %d, %d ", $pager->getStart(), $pager->getItemsPerPage());
763 if (($last_id > 0) && ($sql_table == '`thread`')) {
764 $sql_range .= sprintf(" AND $sql_table.`iid` < '%s'", DBA::escape($last_id));
766 $pager_sql = sprintf(" LIMIT %d, %d ", $pager->getStart(), $pager->getItemsPerPage());
771 // Fetch a page full of parent items for this page
773 if (!empty($parent)) {
774 // Load only a single thread
775 $sql_extra4 = "`item`.`id` = ".intval($parent);
777 // Load all unseen items
778 $sql_extra4 = "`item`.`unseen`";
779 if (Config::get("system", "like_no_comment")) {
780 $sql_extra4 .= " AND `item`.`gravity` IN (" . GRAVITY_PARENT . "," . GRAVITY_COMMENT . ")";
782 if ($order === 'post') {
783 // Only show toplevel posts when updating posts in this order mode
784 $sql_extra4 .= " AND `item`.`id` = `item`.`parent`";
788 $r = q("SELECT `item`.`parent-uri` AS `uri`, `item`.`parent` AS `item_id`, $sql_order AS `order_date`
789 FROM `item` $sql_post_table
790 STRAIGHT_JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
791 AND (NOT `contact`.`blocked` OR `contact`.`pending`)
792 AND (`item`.`gravity` != %d
793 OR `contact`.`uid` = `item`.`uid` AND `contact`.`self`
794 OR `contact`.`rel` IN (%d, %d) AND NOT `contact`.`readonly`)
795 LEFT JOIN `user-item` ON `user-item`.`iid` = `item`.`id` AND `user-item`.`uid` = %d
796 WHERE `item`.`uid` = %d AND `item`.`visible` AND NOT `item`.`deleted`
797 AND (`user-item`.`hidden` IS NULL OR NOT `user-item`.`hidden`)
798 AND NOT `item`.`moderated` AND $sql_extra4
799 $sql_extra3 $sql_extra $sql_range $sql_nets
800 ORDER BY `order_date` DESC LIMIT 100",
801 intval(GRAVITY_PARENT),
802 intval(Contact::SHARING),
803 intval(Contact::FRIEND),
804 intval(local_user()),
808 $r = q("SELECT `item`.`uri`, `thread`.`iid` AS `item_id`, $sql_order AS `order_date`
809 FROM `thread` $sql_post_table
810 STRAIGHT_JOIN `contact` ON `contact`.`id` = `thread`.`contact-id`
811 AND (NOT `contact`.`blocked` OR `contact`.`pending`)
812 STRAIGHT_JOIN `item` ON `item`.`id` = `thread`.`iid`
813 AND (`item`.`gravity` != %d
814 OR `contact`.`uid` = `item`.`uid` AND `contact`.`self`
815 OR `contact`.`rel` IN (%d, %d) AND NOT `contact`.`readonly`)
816 LEFT JOIN `user-item` ON `user-item`.`iid` = `item`.`id` AND `user-item`.`uid` = %d
817 WHERE `thread`.`uid` = %d AND `thread`.`visible` AND NOT `thread`.`deleted`
818 AND NOT `thread`.`moderated`
819 AND (`user-item`.`hidden` IS NULL OR NOT `user-item`.`hidden`)
820 $sql_extra2 $sql_extra3 $sql_range $sql_extra $sql_nets
821 ORDER BY `order_date` DESC $pager_sql",
822 intval(GRAVITY_PARENT),
823 intval(Contact::SHARING),
824 intval(Contact::FRIEND),
825 intval(local_user()),
830 // Only show it when unfiltered (no groups, no networks, ...)
831 if (in_array($nets, ['', Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::DIASPORA, Protocol::OSTATUS]) && (strlen($sql_extra . $sql_extra2 . $sql_extra3) == 0)) {
832 if (DBA::isResult($r)) {
833 $top_limit = current($r)['order_date'];
834 $bottom_limit = end($r)['order_date'];
835 if (empty($_SESSION['network_last_top_limit']) || ($_SESSION['network_last_top_limit'] < $top_limit)) {
836 $_SESSION['network_last_top_limit'] = $top_limit;
839 $top_limit = $bottom_limit = DateTimeFormat::utcNow();
842 // When checking for updates we need to fetch from the newest date to the newest date before
843 // Only do this, when the last stored date isn't too long ago (10 times the update interval)
844 $browser_update = PConfig::get(local_user(), 'system', 'update_interval', 40000) / 1000;
846 if (($browser_update > 0) && $update && !empty($_SESSION['network_last_date']) &&
847 (($bottom_limit < $_SESSION['network_last_date']) || ($top_limit == $bottom_limit)) &&
848 ((time() - $_SESSION['network_last_date_timestamp']) < ($browser_update * 10))) {
849 $bottom_limit = $_SESSION['network_last_date'];
851 $_SESSION['network_last_date'] = defaults($_SESSION, 'network_last_top_limit', $top_limit);
852 $_SESSION['network_last_date_timestamp'] = time();
854 if ($last_date > $top_limit) {
855 $top_limit = $last_date;
856 } elseif ($pager->getPage() == 1) {
857 // Highest possible top limit when we are on the first page
858 $top_limit = DateTimeFormat::utcNow();
861 $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`
862 STRAIGHT_JOIN (SELECT `oid` FROM `term` WHERE `term` IN
863 (SELECT SUBSTR(`term`, 2) FROM `search` WHERE `uid` = ? AND `term` LIKE '#%') AND `otype` = ? AND `type` = ? AND `uid` = 0) AS `term`
864 ON `item`.`id` = `term`.`oid`
865 STRAIGHT_JOIN `contact` AS `author` ON `author`.`id` = `item`.`author-id`
866 WHERE `item`.`uid` = 0 AND `item`.$ordering < ? AND `item`.$ordering > ?
867 AND NOT `author`.`hidden` AND NOT `author`.`blocked`" . $sql_tag_nets,
868 local_user(), TERM_OBJ_POST, TERM_HASHTAG,
869 $top_limit, $bottom_limit);
871 $data = DBA::toArray($items);
873 if (count($data) > 0) {
874 $tag_top_limit = current($data)['order_date'];
875 if ($_SESSION['network_last_date'] < $tag_top_limit) {
876 $_SESSION['network_last_date'] = $tag_top_limit;
879 Logger::log('Tagged items: ' . count($data) . ' - ' . $bottom_limit . ' - ' . $top_limit . ' - ' . local_user().' - '.(int)$update);
881 foreach ($r as $item) {
882 $s[$item['uri']] = $item;
884 foreach ($data as $item) {
885 // Don't show hash tag posts from blocked or ignored contacts
886 $condition = ["`nurl` = ? AND `uid` = ? AND (`blocked` OR `readonly`)",
887 Strings::normaliseLink($item['author-link']), local_user()];
888 if (!DBA::exists('contact', $condition)) {
889 $s[$item['uri']] = $item;
901 if (DBA::isResult($items)) {
904 foreach ($items as $item) {
905 if ($date_offset < $item['order_date']) {
906 $date_offset = $item['order_date'];
908 if (!in_array($item['item_id'], $parents_arr) && ($item['item_id'] > 0)) {
909 $parents_arr[] = $item['item_id'];
912 $parents_str = implode(', ', $parents_arr);
915 if (x($_GET, 'offset')) {
916 $date_offset = $_GET['offset'];
919 $query_string = $a->query_string;
920 if ($date_offset && !preg_match('/[?&].offset=/', $query_string)) {
921 $query_string .= '&offset=' . urlencode($date_offset);
924 $pager->setQueryString($query_string);
926 // We aren't going to try and figure out at the item, group, and page
927 // level which items you've seen and which you haven't. If you're looking
928 // at the top level network page just mark everything seen.
930 if (!$gid && !$cid && !$star) {
931 $condition = ['unseen' => true, 'uid' => local_user()];
932 networkSetSeen($condition);
933 } elseif ($parents_str) {
934 $condition = ["`uid` = ? AND `unseen` AND `parent` IN (" . DBA::escape($parents_str) . ")", local_user()];
935 networkSetSeen($condition);
940 $o .= networkConversation($a, $items, $pager, $mode, $update, $ordering);
946 * @brief Get the network tabs menu
948 * @param App $a The global App
949 * @return string Html of the networktab
951 function network_tabs(App $a)
954 /// @TODO fix this logic, reduce duplication
955 /// $a->page['content'] .= '<div class="tabs-wrapper">';
956 list($no_active, $all_active, $postord_active, $conv_active, $new_active, $starred_active, $bookmarked_active) = network_query_get_sel_tab($a);
958 // if no tabs are selected, defaults to comments
959 if ($no_active == 'active') {
960 $all_active = 'active';
968 'label' => L10n::t('Commented Order'),
969 'url' => str_replace('/new', '', $cmd) . '?f=&order=comment' . ((x($_GET,'cid')) ? '&cid=' . $_GET['cid'] : ''),
970 'sel' => $all_active,
971 'title' => L10n::t('Sort by Comment Date'),
972 'id' => 'commented-order-tab',
976 'label' => L10n::t('Posted Order'),
977 'url' => str_replace('/new', '', $cmd) . '?f=&order=post' . ((x($_GET,'cid')) ? '&cid=' . $_GET['cid'] : ''),
978 'sel' => $postord_active,
979 'title' => L10n::t('Sort by Post Date'),
980 'id' => 'posted-order-tab',
985 if (Feature::isEnabled(local_user(), 'personal_tab')) {
987 'label' => L10n::t('Personal'),
988 'url' => str_replace('/new', '', $cmd) . ((x($_GET,'cid')) ? '/?f=&cid=' . $_GET['cid'] : '/?f=') . '&conv=1',
989 'sel' => $conv_active,
990 'title' => L10n::t('Posts that mention or involve you'),
991 'id' => 'personal-tab',
996 if (Feature::isEnabled(local_user(), 'new_tab')) {
998 'label' => L10n::t('New'),
999 'url' => 'network/new' . ((x($_GET,'cid')) ? '/?f=&cid=' . $_GET['cid'] : ''),
1000 'sel' => $new_active,
1001 'title' => L10n::t('Activity Stream - by date'),
1002 'id' => 'activitiy-by-date-tab',
1007 if (Feature::isEnabled(local_user(), 'link_tab')) {
1009 'label' => L10n::t('Shared Links'),
1010 'url' => str_replace('/new', '', $cmd) . ((x($_GET,'cid')) ? '/?f=&cid=' . $_GET['cid'] : '/?f=') . '&bmark=1',
1011 'sel' => $bookmarked_active,
1012 'title' => L10n::t('Interesting Links'),
1013 'id' => 'shared-links-tab',
1018 if (Feature::isEnabled(local_user(), 'star_posts')) {
1020 'label' => L10n::t('Starred'),
1021 'url' => str_replace('/new', '', $cmd) . ((x($_GET,'cid')) ? '/?f=&cid=' . $_GET['cid'] : '/?f=') . '&star=1',
1022 'sel' => $starred_active,
1023 'title' => L10n::t('Favourite Posts'),
1024 'id' => 'starred-posts-tab',
1029 // save selected tab, but only if not in file mode
1030 if (!x($_GET, 'file')) {
1031 PConfig::set(local_user(), 'network.view', 'tab.selected', [
1032 $all_active, $postord_active, $conv_active, $new_active, $starred_active, $bookmarked_active
1036 $arr = ['tabs' => $tabs];
1037 Addon::callHooks('network_tabs', $arr);
1039 $tpl = Renderer::getMarkupTemplate('common_tabs.tpl');
1041 return Renderer::replaceMacros($tpl, ['$tabs' => $arr['tabs']]);
1043 // --- end item filter tabs
1047 * Network hook into the HTML head to enable infinite scroll.
1049 * Since the HTML head is built after the module content has been generated, we need to retrieve the base query string
1050 * of the page to make the correct asynchronous call. This is obtained through the Pager that was instantiated in
1051 * networkThreadedView or networkFlatView.
1053 * @global Pager $pager
1055 * @param string $htmlhead The head tag HTML string
1057 function network_infinite_scroll_head(App $a, &$htmlhead)
1059 /// @TODO this will have to be converted to a static property of the converted Module\Network class
1062 if (PConfig::get(local_user(), 'system', 'infinite_scroll')
1063 && defaults($_GET, 'mode', '') != 'minimal'
1065 $tpl = Renderer::getMarkupTemplate('infinite_scroll_head.tpl');
1066 $htmlhead .= Renderer::replaceMacros($tpl, [
1067 '$pageno' => $pager->getPage(),
1068 '$reload_uri' => $pager->getBaseQueryString()