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'
212 * @return array ($no_active, $comment_active, $postord_active, $conv_active, $new_active, $starred_active, $bookmarked_active);
214 function network_query_get_sel_tab(App $a)
217 $starred_active = '';
219 $bookmarked_active = '';
222 $postord_active = '';
224 if (($a->argc > 1 && $a->argv[1] === 'new') || ($a->argc > 2 && $a->argv[2] === 'new')) {
225 $new_active = 'active';
228 if (!empty($_GET['star'])) {
229 $starred_active = 'active';
232 if (!empty($_GET['bmark'])) {
233 $bookmarked_active = 'active';
236 if (!empty($_GET['conv'])) {
237 $conv_active = 'active';
240 if (($new_active == '') && ($starred_active == '') && ($bookmarked_active == '') && ($conv_active == '')) {
241 $no_active = 'active';
244 if ($no_active == 'active' && !empty($_GET['order'])) {
245 switch($_GET['order']) {
246 case 'post' : $postord_active = 'active'; $no_active=''; break;
247 case 'comment' : $all_active = 'active'; $no_active=''; break;
251 return [$no_active, $all_active, $postord_active, $conv_active, $new_active, $starred_active, $bookmarked_active];
254 function network_query_get_sel_group(App $a)
258 if ($a->argc >= 2 && is_numeric($a->argv[1])) {
259 $group = $a->argv[1];
266 * @brief Sets the pager data and returns SQL
268 * @param App $a The global App
269 * @param Pager $pager
270 * @param integer $update Used for the automatic reloading
271 * @return string SQL with the appropriate LIMIT clause
272 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
274 function networkPager(App $a, Pager $pager, $update)
277 // only setup pagination on initial page view
281 // check if we serve a mobile device and get the user settings
284 $itemspage_network = PConfig::get(local_user(), 'system', 'itemspage_mobile_network');
285 $itemspage_network = ((intval($itemspage_network)) ? $itemspage_network : 20);
287 $itemspage_network = PConfig::get(local_user(), 'system', 'itemspage_network');
288 $itemspage_network = ((intval($itemspage_network)) ? $itemspage_network : 40);
291 // now that we have the user settings, see if the theme forces
292 // a maximum item number which is lower then the user choice
293 if (($a->force_max_items > 0) && ($a->force_max_items < $itemspage_network)) {
294 $itemspage_network = $a->force_max_items;
297 $pager->setItemsPerPage($itemspage_network);
299 return sprintf(" LIMIT %d, %d ", $pager->getStart(), $pager->getItemsPerPage());
303 * @brief Sets items as seen
305 * @param array $condition The array with the SQL condition
306 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
308 function networkSetSeen($condition)
310 if (empty($condition)) {
314 $unseen = Item::exists($condition);
317 Item::update(['unseen' => false], $condition);
322 * @brief Create the conversation HTML
324 * @param App $a The global App
325 * @param array $items Items of the conversation
326 * @param Pager $pager
327 * @param string $mode Display mode for the conversation
328 * @param integer $update Used for the automatic reloading
329 * @param string $ordering
330 * @return string HTML of the conversation
331 * @throws ImagickException
332 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
334 function networkConversation(App $a, $items, Pager $pager, $mode, $update, $ordering = '')
336 // Set this so that the conversation function can find out contact info for our wall-wall items
337 $a->page_contact = $a->contact;
339 if (!is_array($items)) {
340 Logger::log("Expecting items to be an array. Got " . print_r($items, true));
344 $o = conversation($a, $items, $pager, $mode, $update, false, $ordering, local_user());
347 if (PConfig::get(local_user(), 'system', 'infinite_scroll')) {
348 $o .= HTML::scrollLoader();
350 $o .= $pager->renderMinimal(count($items));
357 function network_content(App $a, $update = 0, $parent = 0)
360 return Login::form();
363 /// @TODO Is this really necessary? $a is already available to hooks
364 $arr = ['query' => $a->query_string];
365 Hook::callAll('network_content_init', $arr);
370 for ($x = 1; $x < $a->argc; $x ++) {
371 if ($a->argv[$x] === 'new') {
377 if (!empty($_GET['file'])) {
382 $o = networkFlatView($a, $update);
384 $o = networkThreadedView($a, $update, $parent);
388 info("No items found");
395 * @brief Get the network content in flat view
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
400 * @throws ImagickException
401 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
402 * @global Pager $pager
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'));
412 $file = defaults($_GET, 'file', '');
414 if (!$update && !$rawmode) {
415 $tabs = network_tabs($a);
418 Nav::setSelected('network');
422 'allow_location' => $a->user['allow_location'],
423 'default_location' => $a->user['default-location'],
424 'nickname' => $a->user['nickname'],
425 'lockstate' => (is_array($a->user) &&
426 (strlen($a->user['allow_cid']) || strlen($a->user['allow_gid']) ||
427 strlen($a->user['deny_cid']) || strlen($a->user['deny_gid'])) ? 'lock' : 'unlock'),
428 'default_perms' => ACL::getDefaultUserPermissions($a->user),
429 'acl' => ACL::getFullSelectorHTML($a->user, true),
431 'visitor' => 'block',
432 'profile_uid' => local_user(),
436 $o .= status_editor($a, $x);
438 if (!Config::get('theme', 'hide_eventlist')) {
439 $o .= Profile::getBirthdays();
440 $o .= Profile::getEventsReminderHTML();
444 $pager = new Pager($a->query_string);
446 networkPager($a, $pager, $update);
449 $condition = ["`term` = ? AND `otype` = ? AND `type` = ? AND `uid` = ?",
450 $file, TERM_OBJ_POST, TERM_FILE, local_user()];
451 $params = ['order' => ['tid' => true], 'limit' => [$pager->getStart(), $pager->getItemsPerPage()]];
452 $result = DBA::select('term', ['oid'], $condition, $params);
455 while ($term = DBA::fetch($result)) {
456 $posts[] = $term['oid'];
460 if (count($posts) == 0) {
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 * @param App $a The global App
484 * @param integer $update Used for the automatic reloading
485 * @param integer $parent
486 * @return string HTML of the network content in flat view
487 * @throws ImagickException
488 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
489 * @global Pager $pager
491 function networkThreadedView(App $a, $update, $parent)
493 /// @TODO this will have to be converted to a static property of the converted Module\Network class
496 // Rawmode is used for fetching new content at the end of the page
497 $rawmode = (isset($_GET['mode']) AND ( $_GET['mode'] == 'raw'));
499 if (isset($_GET['last_received']) && isset($_GET['last_commented']) && isset($_GET['last_created']) && isset($_GET['last_id'])) {
500 $last_received = DateTimeFormat::utc($_GET['last_received']);
501 $last_commented = DateTimeFormat::utc($_GET['last_commented']);
502 $last_created = DateTimeFormat::utc($_GET['last_created']);
503 $last_id = intval($_GET['last_id']);
506 $last_commented = '';
511 $datequery = $datequery2 = '';
515 $default_permissions = [];
518 for ($x = 1; $x < $a->argc; $x ++) {
519 if (is_a_date_arg($a->argv[$x])) {
521 $datequery2 = Strings::escapeHtml($a->argv[$x]);
523 $datequery = Strings::escapeHtml($a->argv[$x]);
524 $_GET['order'] = 'post';
526 } elseif (intval($a->argv[$x])) {
527 $gid = intval($a->argv[$x]);
528 $default_permissions = ['allow_gid' => '<' . $gid . '>'];
535 $cid = intval(defaults($_GET, 'cid' , 0));
536 $star = intval(defaults($_GET, 'star' , 0));
537 $bmark = intval(defaults($_GET, 'bmark', 0));
538 $conv = intval(defaults($_GET, 'conv' , 0));
539 $order = Strings::escapeTags(defaults($_GET, 'order', 'comment'));
540 $nets = defaults($_GET, 'nets' , '');
543 $default_permissions = ['allow_cid' => '<' . intval($cid) . '>'];
547 $r = DBA::select('contact', ['id'], ['uid' => local_user(), 'network' => $nets], ['self' => false]);
550 while ($rr = DBA::fetch($r)) {
551 $str .= '<' . $rr['id'] . '>';
554 $default_permissions = ['allow_cid' => $str];
558 if (!$update && !$rawmode) {
559 $tabs = network_tabs($a);
562 if ($gid && ($t = Contact::getOStatusCountByGroupId($gid)) && !PConfig::get(local_user(), 'system', 'nowarn_insecure')) {
563 notice(L10n::tt("Warning: This group contains %s member from a network that doesn't allow non public messages.",
564 "Warning: This group contains %s members from a network that doesn't allow non public messages.",
566 notice(L10n::t("Messages in this group won't be send to these receivers.").EOL);
569 Nav::setSelected('network');
574 // If $cid belongs to a communitity forum or a privat goup,.add a mention to the status editor
575 $condition = ["`id` = ? AND (`forum` OR `prv`)", $cid];
576 $contact = DBA::selectFirst('contact', ['addr', 'nick'], $condition);
577 if (DBA::isResult($contact)) {
578 if ($contact['addr'] != '') {
579 $content = '!' . $contact['addr'];
581 $content = '!' . $contact['nick'] . '+' . $cid;
588 'allow_location' => $a->user['allow_location'],
589 'default_location' => $a->user['default-location'],
590 'nickname' => $a->user['nickname'],
591 'lockstate' => ($gid || $cid || $nets || (is_array($a->user) &&
592 (strlen($a->user['allow_cid']) || strlen($a->user['allow_gid']) ||
593 strlen($a->user['deny_cid']) || strlen($a->user['deny_gid']))) ? 'lock' : 'unlock'),
594 'default_perms' => ACL::getDefaultUserPermissions($a->user),
595 'acl' => ACL::getFullSelectorHTML($a->user, true, $default_permissions),
596 'bang' => (($gid || $cid || $nets) ? '!' : ''),
597 'visitor' => 'block',
598 'profile_uid' => local_user(),
599 'content' => $content,
602 $o .= status_editor($a, $x);
605 // We don't have to deal with ACLs on this page. You're looking at everything
606 // that belongs to you, hence you can see all of it. We will filter by group if
609 $sql_post_table = '';
610 $sql_options = ($star ? " AND `thread`.`starred` " : '');
611 $sql_options .= ($bmark ? sprintf(" AND `thread`.`post-type` = %d ", Item::PT_PAGE) : '');
612 $sql_extra = $sql_options;
615 $sql_table = '`thread`';
616 $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 '" . Strings::protectSprintf('%<' . 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' => $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 .= Strings::protectSprintf(sprintf(" AND $sql_table.created <= '%s' ",
700 DBA::escape(DateTimeFormat::convert($datequery, 'UTC', date_default_timezone_get()))));
703 $sql_extra3 .= Strings::protectSprintf(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 Strings::normaliseLink($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 (!empty($_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
947 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
949 function network_tabs(App $a)
952 /// @TODO fix this logic, reduce duplication
953 /// $a->page['content'] .= '<div class="tabs-wrapper">';
954 list($no_active, $all_active, $postord_active, $conv_active, $new_active, $starred_active, $bookmarked_active) = network_query_get_sel_tab($a);
956 // if no tabs are selected, defaults to comments
957 if ($no_active == 'active') {
958 $all_active = 'active';
966 'label' => L10n::t('Commented Order'),
967 'url' => str_replace('/new', '', $cmd) . '?f=&order=comment' . (!empty($_GET['cid']) ? '&cid=' . $_GET['cid'] : ''),
968 'sel' => $all_active,
969 'title' => L10n::t('Sort by Comment Date'),
970 'id' => 'commented-order-tab',
974 'label' => L10n::t('Posted Order'),
975 'url' => str_replace('/new', '', $cmd) . '?f=&order=post' . (!empty($_GET['cid']) ? '&cid=' . $_GET['cid'] : ''),
976 'sel' => $postord_active,
977 'title' => L10n::t('Sort by Post Date'),
978 'id' => 'posted-order-tab',
984 'label' => L10n::t('Personal'),
985 'url' => str_replace('/new', '', $cmd) . (!empty($_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',
992 if (Feature::isEnabled(local_user(), 'new_tab')) {
994 'label' => L10n::t('New'),
995 'url' => 'network/new' . (!empty($_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) . (!empty($_GET['cid']) ? '/?f=&cid=' . $_GET['cid'] : '/?f=') . '&bmark=1',
1007 'sel' => $bookmarked_active,
1008 'title' => L10n::t('Interesting Links'),
1009 'id' => 'shared-links-tab',
1015 'label' => L10n::t('Starred'),
1016 'url' => str_replace('/new', '', $cmd) . (!empty($_GET['cid']) ? '/?f=&cid=' . $_GET['cid'] : '/?f=') . '&star=1',
1017 'sel' => $starred_active,
1018 'title' => L10n::t('Favourite Posts'),
1019 'id' => 'starred-posts-tab',
1023 // save selected tab, but only if not in file mode
1024 if (empty($_GET['file'])) {
1025 PConfig::set(local_user(), 'network.view', 'tab.selected', [
1026 $all_active, $postord_active, $conv_active, $new_active, $starred_active, $bookmarked_active
1030 $arr = ['tabs' => $tabs];
1031 Hook::callAll('network_tabs', $arr);
1033 $tpl = Renderer::getMarkupTemplate('common_tabs.tpl');
1035 return Renderer::replaceMacros($tpl, ['$tabs' => $arr['tabs']]);
1037 // --- end item filter tabs
1041 * Network hook into the HTML head to enable infinite scroll.
1043 * Since the HTML head is built after the module content has been generated, we need to retrieve the base query string
1044 * of the page to make the correct asynchronous call. This is obtained through the Pager that was instantiated in
1045 * networkThreadedView or networkFlatView.
1048 * @param string $htmlhead The head tag HTML string
1049 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1050 * @global Pager $pager
1052 function network_infinite_scroll_head(App $a, &$htmlhead)
1054 /// @TODO this will have to be converted to a static property of the converted Module\Network class
1060 if (PConfig::get(local_user(), 'system', 'infinite_scroll')
1061 && defaults($_GET, 'mode', '') != 'minimal'
1063 $tpl = Renderer::getMarkupTemplate('infinite_scroll_head.tpl');
1064 $htmlhead .= Renderer::replaceMacros($tpl, [
1065 '$pageno' => $pager->getPage(),
1066 '$reload_uri' => $pager->getBaseQueryString()