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\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;
31 require_once 'include/conversation.php';
32 require_once 'include/items.php';
34 function network_init(App $a)
37 notice(L10n::t('Permission denied.') . EOL);
41 Hook::add('head', __FILE__, 'network_infinite_scroll_head');
43 $search = (x($_GET, 'search') ? escape_tags($_GET['search']) : '');
45 if (($search != '') && !empty($_GET['submit'])) {
46 $a->internalRedirect('search?search=' . urlencode($search));
49 if (x($_GET, 'save')) {
50 $exists = DBA::exists('search', ['uid' => local_user(), 'term' => $search]);
52 DBA::insert('search', ['uid' => local_user(), 'term' => $search]);
55 if (x($_GET, 'remove')) {
56 DBA::delete('search', ['uid' => local_user(), 'term' => $search]);
59 $is_a_date_query = false;
61 $group_id = (($a->argc > 1 && is_numeric($a->argv[1])) ? intval($a->argv[1]) : 0);
64 if (x($_GET, 'cid') && intval($_GET['cid']) != 0) {
66 $_GET['nets'] = 'all';
71 for ($x = 1; $x < $a->argc; $x ++) {
72 if (is_a_date_arg($a->argv[$x])) {
73 $is_a_date_query = true;
79 // convert query string to array. remove friendica args
81 $query_string = str_replace($a->cmd . '?', '', $a->query_string);
82 parse_str($query_string, $query_array);
83 array_shift($query_array);
85 // fetch last used network view and redirect if needed
86 if (!$is_a_date_query) {
87 $sel_nets = defaults($_GET, 'nets', false);
88 $sel_tabs = network_query_get_sel_tab($a);
89 $sel_groups = network_query_get_sel_group($a);
90 $last_sel_tabs = PConfig::get(local_user(), 'network.view', 'tab.selected');
92 $remember_tab = ($sel_tabs[0] === 'active' && is_array($last_sel_tabs) && $last_sel_tabs[0] !== 'active');
94 $net_baseurl = '/network';
97 if ($sel_groups !== false) {
98 $net_baseurl .= '/' . $sel_groups;
102 // redirect if current selected tab is '/network' and
103 // last selected tab is _not_ '/network?f=&order=comment'.
104 // and this isn't a date query
115 'f=&order=comment', //all
116 'f=&order=post', //postord
119 'f=&star=1', //starred
120 'f=&bmark=1', //bookmarked
123 $k = array_search('active', $last_sel_tabs);
126 $net_baseurl .= $tab_baseurls[$k];
128 // parse out tab queries
130 $dest_qs = $tab_args[$k];
131 parse_str($dest_qs, $dest_qa);
132 $net_args = array_merge($net_args, $dest_qa);
134 $remember_tab = false;
138 if ($sel_nets !== false) {
139 $net_args['nets'] = $sel_nets;
143 $net_args = array_merge($query_array, $net_args);
144 $net_queries = build_querystring($net_args);
146 $redir_url = ($net_queries ? $net_baseurl . '?' . $net_queries : $net_baseurl);
148 $a->internalRedirect($redir_url);
152 // If nets is set to all, unset it
153 if (x($_GET, 'nets') && $_GET['nets'] === 'all') {
154 unset($_GET['nets']);
157 if (!x($a->page, 'aside')) {
158 $a->page['aside'] = '';
161 $a->page['aside'] .= (Feature::isEnabled(local_user(), 'groups') ?
162 Group::sidebarWidget('network/0', 'network', 'standard', $group_id) : '');
163 $a->page['aside'] .= (Feature::isEnabled(local_user(), 'forumlist_widget') ? ForumManager::widget(local_user(), $cid) : '');
164 $a->page['aside'] .= posted_date_widget('network', local_user(), false);
165 $a->page['aside'] .= Widget::networks('network', (x($_GET, 'nets') ? $_GET['nets'] : ''));
166 $a->page['aside'] .= saved_searches($search);
167 $a->page['aside'] .= Widget::fileAs('network', (x($_GET, 'file') ? $_GET['file'] : ''));
170 function saved_searches($search)
172 if (!Feature::isEnabled(local_user(), 'savedsearch')) {
178 $srchurl = '/network?f='
179 . ((x($_GET, 'cid')) ? '&cid=' . $_GET['cid'] : '')
180 . ((x($_GET, 'star')) ? '&star=' . $_GET['star'] : '')
181 . ((x($_GET, 'bmark')) ? '&bmark=' . $_GET['bmark'] : '')
182 . ((x($_GET, 'conv')) ? '&conv=' . $_GET['conv'] : '')
183 . ((x($_GET, 'nets')) ? '&nets=' . $_GET['nets'] : '')
184 . ((x($_GET, 'cmin')) ? '&cmin=' . $_GET['cmin'] : '')
185 . ((x($_GET, 'cmax')) ? '&cmax=' . $_GET['cmax'] : '')
186 . ((x($_GET, 'file')) ? '&file=' . $_GET['file'] : '');
191 $terms = DBA::select('search', ['id', 'term'], ['uid' => local_user()]);
194 while ($rr = DBA::fetch($terms)) {
197 'term' => $rr['term'],
198 'encodedterm' => urlencode($rr['term']),
199 'delete' => L10n::t('Remove term'),
200 'selected' => ($search == $rr['term']),
204 $tpl = Renderer::getMarkupTemplate('saved_searches_aside.tpl');
205 $o = Renderer::replaceMacros($tpl, [
206 '$title' => L10n::t('Saved Searches'),
207 '$add' => L10n::t('add'),
208 '$searchbox' => search($search, 'netsearch-box', $srchurl, true),
216 * Return selected tab from query
219 * '/network' => $no_active = 'active'
220 * '/network?f=&order=comment' => $comment_active = 'active'
221 * '/network?f=&order=post' => $postord_active = 'active'
222 * '/network?f=&conv=1', => $conv_active = 'active'
223 * '/network/new', => $new_active = 'active'
224 * '/network?f=&star=1', => $starred_active = 'active'
225 * '/network?f=&bmark=1', => $bookmarked_active = 'active'
227 * @return Array ($no_active, $comment_active, $postord_active, $conv_active, $new_active, $starred_active, $bookmarked_active);
229 function network_query_get_sel_tab(App $a)
232 $starred_active = '';
234 $bookmarked_active = '';
237 $postord_active = '';
239 if (($a->argc > 1 && $a->argv[1] === 'new') || ($a->argc > 2 && $a->argv[2] === 'new')) {
240 $new_active = 'active';
243 if (x($_GET, 'star')) {
244 $starred_active = 'active';
247 if (x($_GET, 'bmark')) {
248 $bookmarked_active = 'active';
251 if (x($_GET, 'conv')) {
252 $conv_active = 'active';
255 if (($new_active == '') && ($starred_active == '') && ($bookmarked_active == '') && ($conv_active == '')) {
256 $no_active = 'active';
259 if ($no_active == 'active' && x($_GET, 'order')) {
260 switch($_GET['order']) {
261 case 'post' : $postord_active = 'active'; $no_active=''; break;
262 case 'comment' : $all_active = 'active'; $no_active=''; break;
266 return [$no_active, $all_active, $postord_active, $conv_active, $new_active, $starred_active, $bookmarked_active];
269 function network_query_get_sel_group(App $a)
273 if ($a->argc >= 2 && is_numeric($a->argv[1])) {
274 $group = $a->argv[1];
281 * @brief Sets the pager data and returns SQL
283 * @param App $a The global App
284 * @param integer $update Used for the automatic reloading
285 * @return string SQL with the appropriate LIMIT clause
287 function networkPager(App $a, Pager $pager, $update)
290 // only setup pagination on initial page view
294 // check if we serve a mobile device and get the user settings
297 $itemspage_network = PConfig::get(local_user(), 'system', 'itemspage_mobile_network');
298 $itemspage_network = ((intval($itemspage_network)) ? $itemspage_network : 20);
300 $itemspage_network = PConfig::get(local_user(), 'system', 'itemspage_network');
301 $itemspage_network = ((intval($itemspage_network)) ? $itemspage_network : 40);
304 // now that we have the user settings, see if the theme forces
305 // a maximum item number which is lower then the user choice
306 if (($a->force_max_items > 0) && ($a->force_max_items < $itemspage_network)) {
307 $itemspage_network = $a->force_max_items;
310 $pager->setItemsPerPage($itemspage_network);
312 return sprintf(" LIMIT %d, %d ", $pager->getStart(), $pager->getItemsPerPage());
316 * @brief Sets items as seen
318 * @param array $condition The array with the SQL condition
320 function networkSetSeen($condition)
322 if (empty($condition)) {
326 $unseen = Item::exists($condition);
329 $r = Item::update(['unseen' => false], $condition);
334 * @brief Create the conversation HTML
336 * @param App $a The global App
337 * @param array $items Items of the conversation
338 * @param string $mode Display mode for the conversation
339 * @param integer $update Used for the automatic reloading
340 * @return string HTML of the conversation
342 function networkConversation(App $a, $items, Pager $pager, $mode, $update, $ordering = '')
344 // Set this so that the conversation function can find out contact info for our wall-wall items
345 $a->page_contact = $a->contact;
347 $o = conversation($a, $items, $pager, $mode, $update, false, $ordering, local_user());
350 if (PConfig::get(local_user(), 'system', 'infinite_scroll')) {
351 $o .= scroll_loader();
353 $o .= $pager->renderMinimal(count($items));
360 function network_content(App $a, $update = 0, $parent = 0)
363 return Login::form();
366 /// @TODO Is this really necessary? $a is already available to hooks
367 $arr = ['query' => $a->query_string];
368 Addon::callHooks('network_content_init', $arr);
373 for ($x = 1; $x < $a->argc; $x ++) {
374 if ($a->argv[$x] === 'new') {
380 if (!empty($_GET['file'])) {
385 $o = networkFlatView($a, $update);
387 $o = networkThreadedView($a, $update, $parent);
394 * @brief Get the network content in flat view
396 * @param Pager $pager
397 * @param App $a The global App
398 * @param integer $update Used for the automatic reloading
399 * @return string HTML of the network content in flat view
401 function networkFlatView(App $a, $update = 0)
404 // Rawmode is used for fetching new content at the end of the page
405 $rawmode = (isset($_GET['mode']) && ($_GET['mode'] == 'raw'));
407 if (isset($_GET['last_id'])) {
408 $last_id = intval($_GET['last_id']);
415 $file = defaults($_GET, 'file', '');
417 if (!$update && !$rawmode) {
418 $tabs = network_tabs($a);
421 Nav::setSelected('network');
425 'allow_location' => $a->user['allow_location'],
426 'default_location' => $a->user['default-location'],
427 'nickname' => $a->user['nickname'],
428 'lockstate' => (is_array($a->user) &&
429 (strlen($a->user['allow_cid']) || strlen($a->user['allow_gid']) ||
430 strlen($a->user['deny_cid']) || strlen($a->user['deny_gid'])) ? 'lock' : 'unlock'),
431 'default_perms' => ACL::getDefaultUserPermissions($a->user),
432 'acl' => ACL::getFullSelectorHTML($a->user, true),
434 'visitor' => 'block',
435 'profile_uid' => local_user(),
439 $o .= status_editor($a, $x);
441 if (!Config::get('theme', 'hide_eventlist')) {
442 $o .= Profile::getBirthdays();
443 $o .= Profile::getEventsReminderHTML();
447 $pager = new Pager($a->query_string);
449 /// @TODO Figure out why this variable is unused
450 $pager_sql = networkPager($a, $pager, $update);
453 $condition = ["`term` = ? AND `otype` = ? AND `type` = ? AND `uid` = ?",
454 $file, TERM_OBJ_POST, TERM_FILE, local_user()];
455 $params = ['order' => ['tid' => true], 'limit' => [$pager->getStart(), $pager->getItemsPerPage()]];
456 $result = DBA::select('term', ['oid'], $condition);
459 while ($term = DBA::fetch($result)) {
460 $posts[] = $term['oid'];
464 $condition = ['uid' => local_user(), 'id' => $posts];
466 $condition = ['uid' => local_user()];
469 $params = ['order' => ['id' => true], 'limit' => [$pager->getStart(), $pager->getItemsPerPage()]];
470 $result = Item::selectForUser(local_user(), [], $condition, $params);
471 $items = Item::inArray($result);
473 $condition = ['unseen' => true, 'uid' => local_user()];
474 networkSetSeen($condition);
476 $o .= networkConversation($a, $items, $pager, 'network-new', $update);
482 * @brief Get the network content in threaded view
484 * @global Pager $pager
485 * @param App $a The global App
486 * @param integer $update Used for the automatic reloading
487 * @param integer $parent
488 * @return string HTML of the network content in flat view
490 function networkThreadedView(App $a, $update, $parent)
492 /// @TODO this will have to be converted to a static property of the converted Module\Network class
495 // Rawmode is used for fetching new content at the end of the page
496 $rawmode = (isset($_GET['mode']) AND ( $_GET['mode'] == 'raw'));
498 if (isset($_GET['last_received']) && isset($_GET['last_commented']) && isset($_GET['last_created']) && isset($_GET['last_id'])) {
499 $last_received = DateTimeFormat::utc($_GET['last_received']);
500 $last_commented = DateTimeFormat::utc($_GET['last_commented']);
501 $last_created = DateTimeFormat::utc($_GET['last_created']);
502 $last_id = intval($_GET['last_id']);
505 $last_commented = '';
510 $datequery = $datequery2 = '';
514 $default_permissions = [];
517 for ($x = 1; $x < $a->argc; $x ++) {
518 if (is_a_date_arg($a->argv[$x])) {
520 $datequery2 = escape_tags($a->argv[$x]);
522 $datequery = escape_tags($a->argv[$x]);
523 $_GET['order'] = 'post';
525 } elseif (intval($a->argv[$x])) {
526 $gid = intval($a->argv[$x]);
527 $default_permissions = ['allow_gid' => '<' . $gid . '>'];
534 $cid = intval(defaults($_GET, 'cid' , 0));
535 $star = intval(defaults($_GET, 'star' , 0));
536 $bmark = intval(defaults($_GET, 'bmark', 0));
537 $conv = intval(defaults($_GET, 'conv' , 0));
538 $order = notags(defaults($_GET, 'order', 'comment'));
539 $nets = defaults($_GET, 'nets' , '');
542 $default_permissions = ['allow_cid' => '<' . intval($cid) . '>'];
546 $r = DBA::select('contact', ['id'], ['uid' => local_user(), 'network' => $nets], ['self' => false]);
549 while ($rr = DBA::fetch($r)) {
550 $str .= '<' . $rr['id'] . '>';
553 $default_permissions = ['allow_cid' => $str];
557 if (!$update && !$rawmode) {
558 $tabs = network_tabs($a);
561 if ($gid && ($t = Contact::getOStatusCountByGroupId($gid)) && !PConfig::get(local_user(), 'system', 'nowarn_insecure')) {
562 notice(L10n::tt("Warning: This group contains %s member from a network that doesn't allow non public messages.",
563 "Warning: This group contains %s members from a network that doesn't allow non public messages.",
565 notice(L10n::t("Messages in this group won't be send to these receivers.").EOL);
568 Nav::setSelected('network');
573 // If $cid belongs to a communitity forum or a privat goup,.add a mention to the status editor
574 $condition = ["`id` = ? AND (`forum` OR `prv`)", $cid];
575 $contact = DBA::selectFirst('contact', ['addr', 'nick'], $condition);
576 if (DBA::isResult($contact)) {
577 if ($contact['addr'] != '') {
578 $content = '!' . $contact['addr'];
580 $content = '!' . $contact['nick'] . '+' . $cid;
587 'allow_location' => $a->user['allow_location'],
588 'default_location' => $a->user['default-location'],
589 'nickname' => $a->user['nickname'],
590 'lockstate' => ($gid || $cid || $nets || (is_array($a->user) &&
591 (strlen($a->user['allow_cid']) || strlen($a->user['allow_gid']) ||
592 strlen($a->user['deny_cid']) || strlen($a->user['deny_gid']))) ? 'lock' : 'unlock'),
593 'default_perms' => ACL::getDefaultUserPermissions($a->user),
594 'acl' => ACL::getFullSelectorHTML($a->user, true, $default_permissions),
595 'bang' => (($gid || $cid || $nets) ? '!' : ''),
596 'visitor' => 'block',
597 'profile_uid' => local_user(),
598 'content' => $content,
601 $o .= status_editor($a, $x);
604 // We don't have to deal with ACLs on this page. You're looking at everything
605 // that belongs to you, hence you can see all of it. We will filter by group if
608 $sql_post_table = '';
609 $sql_options = ($star ? " AND `thread`.`starred` " : '');
610 $sql_options .= ($bmark ? sprintf(" AND `thread`.`post-type` = %d ", Item::PT_PAGE) : '');
611 $sql_extra = $sql_options;
614 $sql_table = '`thread`';
615 $sql_parent = '`iid`';
619 $sql_table = '`item`';
620 $sql_parent = '`parent`';
621 $sql_post_table = " INNER JOIN `thread` ON `thread`.`iid` = `item`.`parent`";
624 $sql_nets = (($nets) ? sprintf(" AND $sql_table.`network` = '%s' ", DBA::escape($nets)) : '');
625 $sql_tag_nets = (($nets) ? sprintf(" AND `item`.`network` = '%s' ", DBA::escape($nets)) : '');
628 $group = DBA::selectFirst('group', ['name'], ['id' => $gid, 'uid' => local_user()]);
629 if (!DBA::isResult($group)) {
633 notice(L10n::t('No such group') . EOL);
634 $a->internalRedirect('network/0');
638 $contacts = Group::expand([$gid]);
640 if ((is_array($contacts)) && count($contacts)) {
641 $contact_str_self = '';
643 $contact_str = implode(',', $contacts);
644 $self = DBA::selectFirst('contact', ['id'], ['uid' => local_user(), 'self' => true]);
645 if (DBA::isResult($self)) {
646 $contact_str_self = $self['id'];
649 $sql_post_table .= " INNER JOIN `item` AS `temp1` ON `temp1`.`id` = " . $sql_table . "." . $sql_parent;
650 $sql_extra3 .= " AND (`thread`.`contact-id` IN ($contact_str) ";
651 $sql_extra3 .= " OR (`thread`.`contact-id` = '$contact_str_self' AND `temp1`.`allow_gid` LIKE '" . protect_sprintf('%<' . intval($gid) . '>%') . "' AND `temp1`.`private`))";
653 $sql_extra3 .= " AND false ";
654 info(L10n::t('Group is empty'));
657 $o = Renderer::replaceMacros(Renderer::getMarkupTemplate('section_title.tpl'), [
658 '$title' => L10n::t('Group: %s', $group['name'])
661 $fields = ['id', 'name', 'network', 'writable', 'nurl',
662 'forum', 'prv', 'contact-type', 'addr', 'thumb', 'location'];
663 $condition = ["`id` = ? AND (NOT `blocked` OR `pending`)", $cid];
664 $contact = DBA::selectFirst('contact', $fields, $condition);
665 if (DBA::isResult($contact)) {
666 $sql_extra = " AND " . $sql_table . ".`contact-id` = " . intval($cid);
670 'name' => htmlentities($contact['name']),
671 'itemurl' => defaults($contact, 'addr', $contact['nurl']),
672 'thumb' => ProxyUtils::proxifyUrl($contact['thumb'], false, ProxyUtils::SIZE_THUMB),
673 'details' => $contact['location'],
676 $entries[0]['account_type'] = Contact::getAccountType($contact);
678 $o = Renderer::replaceMacros(Renderer::getMarkupTemplate('viewcontact_template.tpl'), [
679 'contacts' => $entries,
683 if ($contact['network'] === Protocol::OSTATUS && $contact['writable'] && !PConfig::get(local_user(),'system','nowarn_insecure')) {
684 notice(L10n::t('Private messages to this person are at risk of public disclosure.') . EOL);
687 notice(L10n::t('Invalid contact.') . EOL);
688 $a->internalRedirect('network');
693 if (!$gid && !$cid && !$update && !Config::get('theme', 'hide_eventlist')) {
694 $o .= Profile::getBirthdays();
695 $o .= Profile::getEventsReminderHTML();
699 $sql_extra3 .= protect_sprintf(sprintf(" AND $sql_table.created <= '%s' ",
700 DBA::escape(DateTimeFormat::convert($datequery, 'UTC', date_default_timezone_get()))));
703 $sql_extra3 .= protect_sprintf(sprintf(" AND $sql_table.created >= '%s' ",
704 DBA::escape(DateTimeFormat::convert($datequery2, 'UTC', date_default_timezone_get()))));
708 $sql_extra3 .= " AND $sql_table.`mention`";
711 // Normal conversation view
712 if ($order === 'post') {
713 $ordering = '`created`';
714 $order_mode = 'created';
716 $ordering = '`commented`';
717 $order_mode = 'commented';
720 $sql_order = "$sql_table.$ordering";
722 if (!empty($_GET['offset'])) {
723 $sql_range = sprintf(" AND $sql_order <= '%s'", DBA::escape($_GET['offset']));
728 $pager = new Pager($a->query_string);
730 $pager_sql = networkPager($a, $pager, $update);
734 switch ($order_mode) {
736 if ($last_received != '') {
737 $last_date = $last_received;
738 $sql_range .= sprintf(" AND $sql_table.`received` < '%s'", DBA::escape($last_received));
740 $pager_sql = sprintf(" LIMIT %d, %d ", $pager->getStart(), $pager->getItemsPerPage());
744 if ($last_commented != '') {
745 $last_date = $last_commented;
746 $sql_range .= sprintf(" AND $sql_table.`commented` < '%s'", DBA::escape($last_commented));
748 $pager_sql = sprintf(" LIMIT %d, %d ", $pager->getStart(), $pager->getItemsPerPage());
752 if ($last_created != '') {
753 $last_date = $last_created;
754 $sql_range .= sprintf(" AND $sql_table.`created` < '%s'", DBA::escape($last_created));
756 $pager_sql = sprintf(" LIMIT %d, %d ", $pager->getStart(), $pager->getItemsPerPage());
760 if (($last_id > 0) && ($sql_table == '`thread`')) {
761 $sql_range .= sprintf(" AND $sql_table.`iid` < '%s'", DBA::escape($last_id));
763 $pager_sql = sprintf(" LIMIT %d, %d ", $pager->getStart(), $pager->getItemsPerPage());
768 // Fetch a page full of parent items for this page
770 if (!empty($parent)) {
771 // Load only a single thread
772 $sql_extra4 = "`item`.`id` = ".intval($parent);
774 // Load all unseen items
775 $sql_extra4 = "`item`.`unseen`";
776 if (Config::get("system", "like_no_comment")) {
777 $sql_extra4 .= " AND `item`.`gravity` IN (" . GRAVITY_PARENT . "," . GRAVITY_COMMENT . ")";
779 if ($order === 'post') {
780 // Only show toplevel posts when updating posts in this order mode
781 $sql_extra4 .= " AND `item`.`id` = `item`.`parent`";
785 $r = q("SELECT `item`.`parent-uri` AS `uri`, `item`.`parent` AS `item_id`, $sql_order AS `order_date`
786 FROM `item` $sql_post_table
787 STRAIGHT_JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
788 AND (NOT `contact`.`blocked` OR `contact`.`pending`)
789 AND (`item`.`gravity` != %d
790 OR `contact`.`uid` = `item`.`uid` AND `contact`.`self`
791 OR `contact`.`rel` IN (%d, %d) AND NOT `contact`.`readonly`)
792 LEFT JOIN `user-item` ON `user-item`.`iid` = `item`.`id` AND `user-item`.`uid` = %d
793 WHERE `item`.`uid` = %d AND `item`.`visible` AND NOT `item`.`deleted`
794 AND (`user-item`.`hidden` IS NULL OR NOT `user-item`.`hidden`)
795 AND NOT `item`.`moderated` AND $sql_extra4
796 $sql_extra3 $sql_extra $sql_range $sql_nets
797 ORDER BY `order_date` DESC LIMIT 100",
798 intval(GRAVITY_PARENT),
799 intval(Contact::SHARING),
800 intval(Contact::FRIEND),
801 intval(local_user()),
805 $r = q("SELECT `item`.`uri`, `thread`.`iid` AS `item_id`, $sql_order AS `order_date`
806 FROM `thread` $sql_post_table
807 STRAIGHT_JOIN `contact` ON `contact`.`id` = `thread`.`contact-id`
808 AND (NOT `contact`.`blocked` OR `contact`.`pending`)
809 STRAIGHT_JOIN `item` ON `item`.`id` = `thread`.`iid`
810 AND (`item`.`gravity` != %d
811 OR `contact`.`uid` = `item`.`uid` AND `contact`.`self`
812 OR `contact`.`rel` IN (%d, %d) AND NOT `contact`.`readonly`)
813 LEFT JOIN `user-item` ON `user-item`.`iid` = `item`.`id` AND `user-item`.`uid` = %d
814 WHERE `thread`.`uid` = %d AND `thread`.`visible` AND NOT `thread`.`deleted`
815 AND NOT `thread`.`moderated`
816 AND (`user-item`.`hidden` IS NULL OR NOT `user-item`.`hidden`)
817 $sql_extra2 $sql_extra3 $sql_range $sql_extra $sql_nets
818 ORDER BY `order_date` DESC $pager_sql",
819 intval(GRAVITY_PARENT),
820 intval(Contact::SHARING),
821 intval(Contact::FRIEND),
822 intval(local_user()),
827 // Only show it when unfiltered (no groups, no networks, ...)
828 if (in_array($nets, ['', Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::DIASPORA, Protocol::OSTATUS]) && (strlen($sql_extra . $sql_extra2 . $sql_extra3) == 0)) {
829 if (DBA::isResult($r)) {
830 $top_limit = current($r)['order_date'];
831 $bottom_limit = end($r)['order_date'];
832 if (empty($_SESSION['network_last_top_limit']) || ($_SESSION['network_last_top_limit'] < $top_limit)) {
833 $_SESSION['network_last_top_limit'] = $top_limit;
836 $top_limit = $bottom_limit = DateTimeFormat::utcNow();
839 // When checking for updates we need to fetch from the newest date to the newest date before
840 // Only do this, when the last stored date isn't too long ago (10 times the update interval)
841 $browser_update = PConfig::get(local_user(), 'system', 'update_interval', 40000) / 1000;
843 if (($browser_update > 0) && $update && !empty($_SESSION['network_last_date']) &&
844 (($bottom_limit < $_SESSION['network_last_date']) || ($top_limit == $bottom_limit)) &&
845 ((time() - $_SESSION['network_last_date_timestamp']) < ($browser_update * 10))) {
846 $bottom_limit = $_SESSION['network_last_date'];
848 $_SESSION['network_last_date'] = defaults($_SESSION, 'network_last_top_limit', $top_limit);
849 $_SESSION['network_last_date_timestamp'] = time();
851 if ($last_date > $top_limit) {
852 $top_limit = $last_date;
853 } elseif ($pager->getPage() == 1) {
854 // Highest possible top limit when we are on the first page
855 $top_limit = DateTimeFormat::utcNow();
858 $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`
859 STRAIGHT_JOIN (SELECT `oid` FROM `term` WHERE `term` IN
860 (SELECT SUBSTR(`term`, 2) FROM `search` WHERE `uid` = ? AND `term` LIKE '#%') AND `otype` = ? AND `type` = ? AND `uid` = 0) AS `term`
861 ON `item`.`id` = `term`.`oid`
862 STRAIGHT_JOIN `contact` AS `author` ON `author`.`id` = `item`.`author-id`
863 WHERE `item`.`uid` = 0 AND `item`.$ordering < ? AND `item`.$ordering > ?
864 AND NOT `author`.`hidden` AND NOT `author`.`blocked`" . $sql_tag_nets,
865 local_user(), TERM_OBJ_POST, TERM_HASHTAG,
866 $top_limit, $bottom_limit);
868 $data = DBA::toArray($items);
870 if (count($data) > 0) {
871 $tag_top_limit = current($data)['order_date'];
872 if ($_SESSION['network_last_date'] < $tag_top_limit) {
873 $_SESSION['network_last_date'] = $tag_top_limit;
876 Logger::log('Tagged items: ' . count($data) . ' - ' . $bottom_limit . ' - ' . $top_limit . ' - ' . local_user().' - '.(int)$update);
878 foreach ($r as $item) {
879 $s[$item['uri']] = $item;
881 foreach ($data as $item) {
882 // Don't show hash tag posts from blocked or ignored contacts
883 $condition = ["`nurl` = ? AND `uid` = ? AND (`blocked` OR `readonly`)",
884 normalise_link($item['author-link']), local_user()];
885 if (!DBA::exists('contact', $condition)) {
886 $s[$item['uri']] = $item;
898 if (DBA::isResult($items)) {
901 foreach ($items as $item) {
902 if ($date_offset < $item['order_date']) {
903 $date_offset = $item['order_date'];
905 if (!in_array($item['item_id'], $parents_arr) && ($item['item_id'] > 0)) {
906 $parents_arr[] = $item['item_id'];
909 $parents_str = implode(', ', $parents_arr);
912 if (x($_GET, 'offset')) {
913 $date_offset = $_GET['offset'];
916 $query_string = $a->query_string;
917 if ($date_offset && !preg_match('/[?&].offset=/', $query_string)) {
918 $query_string .= '&offset=' . urlencode($date_offset);
921 $pager->setQueryString($query_string);
923 // We aren't going to try and figure out at the item, group, and page
924 // level which items you've seen and which you haven't. If you're looking
925 // at the top level network page just mark everything seen.
927 if (!$gid && !$cid && !$star) {
928 $condition = ['unseen' => true, 'uid' => local_user()];
929 networkSetSeen($condition);
930 } elseif ($parents_str) {
931 $condition = ["`uid` = ? AND `unseen` AND `parent` IN (" . DBA::escape($parents_str) . ")", local_user()];
932 networkSetSeen($condition);
937 $o .= networkConversation($a, $items, $pager, $mode, $update, $ordering);
943 * @brief Get the network tabs menu
945 * @param App $a The global App
946 * @return string Html of the networktab
948 function network_tabs(App $a)
951 /// @TODO fix this logic, reduce duplication
952 /// $a->page['content'] .= '<div class="tabs-wrapper">';
953 list($no_active, $all_active, $postord_active, $conv_active, $new_active, $starred_active, $bookmarked_active) = network_query_get_sel_tab($a);
955 // if no tabs are selected, defaults to comments
956 if ($no_active == 'active') {
957 $all_active = 'active';
965 'label' => L10n::t('Commented Order'),
966 'url' => str_replace('/new', '', $cmd) . '?f=&order=comment' . ((x($_GET,'cid')) ? '&cid=' . $_GET['cid'] : ''),
967 'sel' => $all_active,
968 'title' => L10n::t('Sort by Comment Date'),
969 'id' => 'commented-order-tab',
973 'label' => L10n::t('Posted Order'),
974 'url' => str_replace('/new', '', $cmd) . '?f=&order=post' . ((x($_GET,'cid')) ? '&cid=' . $_GET['cid'] : ''),
975 'sel' => $postord_active,
976 'title' => L10n::t('Sort by Post Date'),
977 'id' => 'posted-order-tab',
982 if (Feature::isEnabled(local_user(), 'personal_tab')) {
984 'label' => L10n::t('Personal'),
985 'url' => str_replace('/new', '', $cmd) . ((x($_GET,'cid')) ? '/?f=&cid=' . $_GET['cid'] : '/?f=') . '&conv=1',
986 'sel' => $conv_active,
987 'title' => L10n::t('Posts that mention or involve you'),
988 'id' => 'personal-tab',
993 if (Feature::isEnabled(local_user(), 'new_tab')) {
995 'label' => L10n::t('New'),
996 'url' => 'network/new' . ((x($_GET,'cid')) ? '/?f=&cid=' . $_GET['cid'] : ''),
997 'sel' => $new_active,
998 'title' => L10n::t('Activity Stream - by date'),
999 'id' => 'activitiy-by-date-tab',
1004 if (Feature::isEnabled(local_user(), 'link_tab')) {
1006 'label' => L10n::t('Shared Links'),
1007 'url' => str_replace('/new', '', $cmd) . ((x($_GET,'cid')) ? '/?f=&cid=' . $_GET['cid'] : '/?f=') . '&bmark=1',
1008 'sel' => $bookmarked_active,
1009 'title' => L10n::t('Interesting Links'),
1010 'id' => 'shared-links-tab',
1015 if (Feature::isEnabled(local_user(), 'star_posts')) {
1017 'label' => L10n::t('Starred'),
1018 'url' => str_replace('/new', '', $cmd) . ((x($_GET,'cid')) ? '/?f=&cid=' . $_GET['cid'] : '/?f=') . '&star=1',
1019 'sel' => $starred_active,
1020 'title' => L10n::t('Favourite Posts'),
1021 'id' => 'starred-posts-tab',
1026 // save selected tab, but only if not in file mode
1027 if (!x($_GET, 'file')) {
1028 PConfig::set(local_user(), 'network.view', 'tab.selected', [
1029 $all_active, $postord_active, $conv_active, $new_active, $starred_active, $bookmarked_active
1033 $arr = ['tabs' => $tabs];
1034 Addon::callHooks('network_tabs', $arr);
1036 $tpl = Renderer::getMarkupTemplate('common_tabs.tpl');
1038 return Renderer::replaceMacros($tpl, ['$tabs' => $arr['tabs']]);
1040 // --- end item filter tabs
1044 * Network hook into the HTML head to enable infinite scroll.
1046 * Since the HTML head is built after the module content has been generated, we need to retrieve the base query string
1047 * of the page to make the correct asynchronous call. This is obtained through the Pager that was instantiated in
1048 * networkThreadedView or networkFlatView.
1050 * @global Pager $pager
1052 * @param string $htmlhead The head tag HTML string
1054 function network_infinite_scroll_head(App $a, &$htmlhead)
1056 /// @TODO this will have to be converted to a static property of the converted Module\Network class
1059 if (PConfig::get(local_user(), 'system', 'infinite_scroll')
1060 && defaults($_GET, 'mode', '') != 'minimal'
1062 $tpl = Renderer::getMarkupTemplate('infinite_scroll_head.tpl');
1063 $htmlhead .= Renderer::replaceMacros($tpl, [
1064 '$pageno' => $pager->getPage(),
1065 '$reload_uri' => $pager->getBaseQueryString()