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\Core\Session;
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\Model\Term;
29 use Friendica\Module\Login;
30 use Friendica\Util\DateTimeFormat;
31 use Friendica\Util\Proxy as ProxyUtils;
32 use Friendica\Util\Strings;
34 function network_init(App $a)
37 notice(L10n::t('Permission denied.') . EOL);
41 Hook::add('head', __FILE__, 'network_infinite_scroll_head');
43 $search = (!empty($_GET['search']) ? Strings::escapeHtml($_GET['search']) : '');
45 if (($search != '') && !empty($_GET['submit'])) {
46 $a->internalRedirect('search?search=' . urlencode($search));
49 if (!empty($_GET['save'])) {
50 $exists = DBA::exists('search', ['uid' => local_user(), 'term' => $search]);
52 DBA::insert('search', ['uid' => local_user(), 'term' => $search]);
55 if (!empty($_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 (!empty($_GET['cid'])) {
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 parse_str(parse_url($a->query_string, PHP_URL_QUERY), $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?order=comment'.
102 // and this isn't a date query
113 'order=comment', //all
114 'order=post', //postord
118 '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 = http_build_query($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'] .= Widget::postedByYear('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?order=comment' => $comment_active = 'active'
205 * '/network?order=post' => $postord_active = 'active'
206 * '/network?conv=1', => $conv_active = 'active'
207 * '/network/new', => $new_active = 'active'
208 * '/network?star=1', => $starred_active = 'active'
209 * '/network?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);
448 $item_params = ['order' => ['id' => true]];
451 $term_condition = ["`term` = ? AND `otype` = ? AND `type` = ? AND `uid` = ?",
452 $file, Term::OBJECT_TYPE_POST, Term::FILE, local_user()];
453 $term_params = ['order' => ['tid' => true], 'limit' => [$pager->getStart(), $pager->getItemsPerPage()]];
454 $result = DBA::select('term', ['oid'], $term_condition, $term_params);
457 while ($term = DBA::fetch($result)) {
458 $posts[] = $term['oid'];
462 if (count($posts) == 0) {
465 $item_condition = ['uid' => local_user(), 'id' => $posts];
467 $item_condition = ['uid' => local_user()];
468 $item_params['limit'] = [$pager->getStart(), $pager->getItemsPerPage()];
470 networkSetSeen(['unseen' => true, 'uid' => local_user()]);
473 $result = Item::selectForUser(local_user(), [], $item_condition, $item_params);
474 $items = Item::inArray($result);
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' , '');
544 $allowedCids[] = (int) $cid;
547 'uid' => local_user(),
553 'rel' => [Contact::SHARING, Contact::FRIEND],
555 $contactStmt = DBA::select('contact', ['id'], $condition);
556 while ($contact = DBA::fetch($contactStmt)) {
557 $allowedCids[] = (int) $contact['id'];
559 DBA::close($contactStmt);
562 if (count($allowedCids)) {
563 $default_permissions['allow_cid'] = $allowedCids;
566 if (!$update && !$rawmode) {
567 $tabs = network_tabs($a);
570 if ($gid && ($t = Contact::getOStatusCountByGroupId($gid)) && !PConfig::get(local_user(), 'system', 'nowarn_insecure')) {
571 notice(L10n::tt("Warning: This group contains %s member from a network that doesn't allow non public messages.",
572 "Warning: This group contains %s members from a network that doesn't allow non public messages.",
574 notice(L10n::t("Messages in this group won't be send to these receivers.").EOL);
577 Nav::setSelected('network');
582 // If $cid belongs to a communitity forum or a privat goup,.add a mention to the status editor
583 $condition = ["`id` = ? AND (`forum` OR `prv`)", $cid];
584 $contact = DBA::selectFirst('contact', ['addr', 'nick'], $condition);
585 if (DBA::isResult($contact)) {
586 if ($contact['addr'] != '') {
587 $content = '!' . $contact['addr'];
589 $content = '!' . $contact['nick'] . '+' . $cid;
596 'allow_location' => $a->user['allow_location'],
597 'default_location' => $a->user['default-location'],
598 'nickname' => $a->user['nickname'],
599 'lockstate' => ($gid || $cid || $nets || (is_array($a->user) &&
600 (strlen($a->user['allow_cid']) || strlen($a->user['allow_gid']) ||
601 strlen($a->user['deny_cid']) || strlen($a->user['deny_gid']))) ? 'lock' : 'unlock'),
602 'default_perms' => ACL::getDefaultUserPermissions($a->user),
603 'acl' => ACL::getFullSelectorHTML($a->user, true, $default_permissions),
604 'bang' => (($gid || $cid || $nets) ? '!' : ''),
605 'visitor' => 'block',
606 'profile_uid' => local_user(),
607 'content' => $content,
610 $o .= status_editor($a, $x);
613 // We don't have to deal with ACLs on this page. You're looking at everything
614 // that belongs to you, hence you can see all of it. We will filter by group if
617 $sql_post_table = '';
618 $sql_options = ($star ? " AND `thread`.`starred` " : '');
619 $sql_options .= ($bmark ? sprintf(" AND `thread`.`post-type` = %d ", Item::PT_PAGE) : '');
620 $sql_extra = $sql_options;
623 $sql_table = '`thread`';
624 $sql_parent = '`iid`';
627 $sql_table = '`item`';
628 $sql_parent = '`parent`';
629 $sql_post_table = " INNER JOIN `thread` ON `thread`.`iid` = `item`.`parent`";
632 $sql_nets = (($nets) ? sprintf(" AND $sql_table.`network` = '%s' ", DBA::escape($nets)) : '');
633 $sql_tag_nets = (($nets) ? sprintf(" AND `item`.`network` = '%s' ", DBA::escape($nets)) : '');
636 $group = DBA::selectFirst('group', ['name'], ['id' => $gid, 'uid' => local_user()]);
637 if (!DBA::isResult($group)) {
641 notice(L10n::t('No such group') . EOL);
642 $a->internalRedirect('network/0');
646 $contacts = Group::expand([$gid]);
648 if ((is_array($contacts)) && count($contacts)) {
649 $contact_str_self = '';
651 $contact_str = implode(',', $contacts);
652 $self = DBA::selectFirst('contact', ['id'], ['uid' => local_user(), 'self' => true]);
653 if (DBA::isResult($self)) {
654 $contact_str_self = $self['id'];
657 $sql_post_table .= " INNER JOIN `item` AS `temp1` ON `temp1`.`id` = " . $sql_table . "." . $sql_parent;
658 $sql_extra3 .= " AND (`thread`.`contact-id` IN ($contact_str) ";
659 $sql_extra3 .= " OR (`thread`.`contact-id` = '$contact_str_self' AND `temp1`.`allow_gid` LIKE '" . Strings::protectSprintf('%<' . intval($gid) . '>%') . "' AND `temp1`.`private`))";
661 $sql_extra3 .= " AND false ";
662 info(L10n::t('Group is empty'));
665 $o = Renderer::replaceMacros(Renderer::getMarkupTemplate('section_title.tpl'), [
666 '$title' => L10n::t('Group: %s', $group['name'])
669 $fields = ['id', 'name', 'network', 'writable', 'nurl',
670 'forum', 'prv', 'contact-type', 'addr', 'thumb', 'location'];
671 $condition = ["`id` = ? AND (NOT `blocked` OR `pending`)", $cid];
672 $contact = DBA::selectFirst('contact', $fields, $condition);
673 if (DBA::isResult($contact)) {
674 $sql_extra = " AND " . $sql_table . ".`contact-id` = " . intval($cid);
678 'name' => $contact['name'],
679 'itemurl' => defaults($contact, 'addr', $contact['nurl']),
680 'thumb' => ProxyUtils::proxifyUrl($contact['thumb'], false, ProxyUtils::SIZE_THUMB),
681 'details' => $contact['location'],
684 $entries[0]['account_type'] = Contact::getAccountType($contact);
686 $o = Renderer::replaceMacros(Renderer::getMarkupTemplate('viewcontact_template.tpl'), [
687 'contacts' => $entries,
691 if ($contact['network'] === Protocol::OSTATUS && $contact['writable'] && !PConfig::get(local_user(),'system','nowarn_insecure')) {
692 notice(L10n::t('Private messages to this person are at risk of public disclosure.') . EOL);
695 notice(L10n::t('Invalid contact.') . EOL);
696 $a->internalRedirect('network');
701 if (!$gid && !$cid && !$update && !Config::get('theme', 'hide_eventlist')) {
702 $o .= Profile::getBirthdays();
703 $o .= Profile::getEventsReminderHTML();
707 $sql_extra3 .= Strings::protectSprintf(sprintf(" AND $sql_table.received <= '%s' ",
708 DBA::escape(DateTimeFormat::convert($datequery, 'UTC', date_default_timezone_get()))));
711 $sql_extra3 .= Strings::protectSprintf(sprintf(" AND $sql_table.received >= '%s' ",
712 DBA::escape(DateTimeFormat::convert($datequery2, 'UTC', date_default_timezone_get()))));
716 $sql_extra3 .= " AND $sql_table.`mention`";
719 // Normal conversation view
720 if ($order === 'post') {
721 $ordering = '`received`';
722 $order_mode = 'received';
724 $ordering = '`commented`';
725 $order_mode = 'commented';
728 $sql_order = "$sql_table.$ordering";
730 if (!empty($_GET['offset'])) {
731 $sql_range = sprintf(" AND $sql_order <= '%s'", DBA::escape($_GET['offset']));
736 $pager = new Pager($a->query_string);
738 $pager_sql = networkPager($a, $pager, $update);
742 switch ($order_mode) {
744 if ($last_received != '') {
745 $last_date = $last_received;
746 $sql_range .= sprintf(" AND $sql_table.`received` < '%s'", DBA::escape($last_received));
748 $pager_sql = sprintf(" LIMIT %d, %d ", $pager->getStart(), $pager->getItemsPerPage());
752 if ($last_commented != '') {
753 $last_date = $last_commented;
754 $sql_range .= sprintf(" AND $sql_table.`commented` < '%s'", DBA::escape($last_commented));
756 $pager_sql = sprintf(" LIMIT %d, %d ", $pager->getStart(), $pager->getItemsPerPage());
760 if ($last_created != '') {
761 $last_date = $last_created;
762 $sql_range .= sprintf(" AND $sql_table.`created` < '%s'", DBA::escape($last_created));
764 $pager_sql = sprintf(" LIMIT %d, %d ", $pager->getStart(), $pager->getItemsPerPage());
768 if (($last_id > 0) && ($sql_table == '`thread`')) {
769 $sql_range .= sprintf(" AND $sql_table.`iid` < '%s'", DBA::escape($last_id));
771 $pager_sql = sprintf(" LIMIT %d, %d ", $pager->getStart(), $pager->getItemsPerPage());
776 // Fetch a page full of parent items for this page
778 if (!empty($parent)) {
779 // Load only a single thread
780 $sql_extra4 = "`item`.`id` = ".intval($parent);
782 // Load all unseen items
783 $sql_extra4 = "`item`.`unseen`";
784 if (Config::get("system", "like_no_comment")) {
785 $sql_extra4 .= " AND `item`.`gravity` IN (" . GRAVITY_PARENT . "," . GRAVITY_COMMENT . ")";
787 if ($order === 'post') {
788 // Only show toplevel posts when updating posts in this order mode
789 $sql_extra4 .= " AND `item`.`id` = `item`.`parent`";
793 $r = q("SELECT `item`.`parent-uri` AS `uri`, `item`.`parent` AS `item_id`, $sql_order AS `order_date`
794 FROM `item` $sql_post_table
795 STRAIGHT_JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
796 AND (NOT `contact`.`blocked` OR `contact`.`pending`)
797 AND (`item`.`gravity` != %d
798 OR `contact`.`uid` = `item`.`uid` AND `contact`.`self`
799 OR `contact`.`rel` IN (%d, %d) AND NOT `contact`.`readonly`)
800 LEFT JOIN `user-item` ON `user-item`.`iid` = `item`.`id` AND `user-item`.`uid` = %d
801 WHERE `item`.`uid` = %d AND `item`.`visible` AND NOT `item`.`deleted`
802 AND (`user-item`.`hidden` IS NULL OR NOT `user-item`.`hidden`)
803 AND NOT `item`.`moderated` AND $sql_extra4
804 $sql_extra3 $sql_extra $sql_range $sql_nets
805 ORDER BY `order_date` DESC LIMIT 100",
806 intval(GRAVITY_PARENT),
807 intval(Contact::SHARING),
808 intval(Contact::FRIEND),
809 intval(local_user()),
813 $r = q("SELECT `item`.`uri`, `thread`.`iid` AS `item_id`, $sql_order AS `order_date`
814 FROM `thread` $sql_post_table
815 STRAIGHT_JOIN `contact` ON `contact`.`id` = `thread`.`contact-id`
816 AND (NOT `contact`.`blocked` OR `contact`.`pending`)
817 STRAIGHT_JOIN `item` ON `item`.`id` = `thread`.`iid`
818 AND (`item`.`gravity` != %d
819 OR `contact`.`uid` = `item`.`uid` AND `contact`.`self`
820 OR `contact`.`rel` IN (%d, %d) AND NOT `contact`.`readonly`)
821 LEFT JOIN `user-item` ON `user-item`.`iid` = `item`.`id` AND `user-item`.`uid` = %d
822 WHERE `thread`.`uid` = %d AND `thread`.`visible` AND NOT `thread`.`deleted`
823 AND NOT `thread`.`moderated`
824 AND (`user-item`.`hidden` IS NULL OR NOT `user-item`.`hidden`)
825 $sql_extra2 $sql_extra3 $sql_range $sql_extra $sql_nets
826 ORDER BY `order_date` DESC $pager_sql",
827 intval(GRAVITY_PARENT),
828 intval(Contact::SHARING),
829 intval(Contact::FRIEND),
830 intval(local_user()),
835 // Only show it when unfiltered (no groups, no networks, ...)
836 if (in_array($nets, ['', Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::DIASPORA, Protocol::OSTATUS]) && (strlen($sql_extra . $sql_extra2 . $sql_extra3) == 0)) {
837 if (DBA::isResult($r)) {
838 $top_limit = current($r)['order_date'];
839 $bottom_limit = end($r)['order_date'];
840 if (empty($_SESSION['network_last_top_limit']) || ($_SESSION['network_last_top_limit'] < $top_limit)) {
841 $_SESSION['network_last_top_limit'] = $top_limit;
844 $top_limit = $bottom_limit = DateTimeFormat::utcNow();
847 // When checking for updates we need to fetch from the newest date to the newest date before
848 // Only do this, when the last stored date isn't too long ago (10 times the update interval)
849 $browser_update = PConfig::get(local_user(), 'system', 'update_interval', 40000) / 1000;
851 if (($browser_update > 0) && $update && !empty($_SESSION['network_last_date']) &&
852 (($bottom_limit < $_SESSION['network_last_date']) || ($top_limit == $bottom_limit)) &&
853 ((time() - $_SESSION['network_last_date_timestamp']) < ($browser_update * 10))) {
854 $bottom_limit = $_SESSION['network_last_date'];
856 $_SESSION['network_last_date'] = Session::get('network_last_top_limit', $top_limit);
857 $_SESSION['network_last_date_timestamp'] = time();
859 if ($last_date > $top_limit) {
860 $top_limit = $last_date;
861 } elseif ($pager->getPage() == 1) {
862 // Highest possible top limit when we are on the first page
863 $top_limit = DateTimeFormat::utcNow();
866 $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`
867 STRAIGHT_JOIN (SELECT `oid` FROM `term` WHERE `term` IN
868 (SELECT SUBSTR(`term`, 2) FROM `search` WHERE `uid` = ? AND `term` LIKE '#%') AND `otype` = ? AND `type` = ? AND `uid` = 0) AS `term`
869 ON `item`.`id` = `term`.`oid`
870 STRAIGHT_JOIN `contact` AS `author` ON `author`.`id` = `item`.`author-id`
871 WHERE `item`.`uid` = 0 AND `item`.$ordering < ? AND `item`.$ordering > ? AND `item`.`gravity` = ?
872 AND NOT `author`.`hidden` AND NOT `author`.`blocked`" . $sql_tag_nets,
873 local_user(), TERM_OBJ_POST, TERM_HASHTAG,
874 $top_limit, $bottom_limit, GRAVITY_PARENT);
876 $data = DBA::toArray($items);
878 if (count($data) > 0) {
879 $tag_top_limit = current($data)['order_date'];
880 if ($_SESSION['network_last_date'] < $tag_top_limit) {
881 $_SESSION['network_last_date'] = $tag_top_limit;
884 Logger::log('Tagged items: ' . count($data) . ' - ' . $bottom_limit . ' - ' . $top_limit . ' - ' . local_user().' - '.(int)$update);
886 foreach ($r as $item) {
887 $s[$item['uri']] = $item;
889 foreach ($data as $item) {
890 // Don't show hash tag posts from blocked or ignored contacts
891 $condition = ["`nurl` = ? AND `uid` = ? AND (`blocked` OR `readonly`)",
892 Strings::normaliseLink($item['author-link']), local_user()];
893 if (!DBA::exists('contact', $condition)) {
894 $s[$item['uri']] = $item;
906 if (DBA::isResult($items)) {
909 foreach ($items as $item) {
910 if ($date_offset < $item['order_date']) {
911 $date_offset = $item['order_date'];
913 if (!in_array($item['item_id'], $parents_arr) && ($item['item_id'] > 0)) {
914 $parents_arr[] = $item['item_id'];
917 $parents_str = implode(', ', $parents_arr);
920 if (!empty($_GET['offset'])) {
921 $date_offset = $_GET['offset'];
924 $query_string = $a->query_string;
925 if ($date_offset && !preg_match('/[?&].offset=/', $query_string)) {
926 $query_string .= '&offset=' . urlencode($date_offset);
929 $pager->setQueryString($query_string);
931 // We aren't going to try and figure out at the item, group, and page
932 // level which items you've seen and which you haven't. If you're looking
933 // at the top level network page just mark everything seen.
935 if (!$gid && !$cid && !$star) {
936 $condition = ['unseen' => true, 'uid' => local_user()];
937 networkSetSeen($condition);
938 } elseif ($parents_str) {
939 $condition = ["`uid` = ? AND `unseen` AND `parent` IN (" . DBA::escape($parents_str) . ")", local_user()];
940 networkSetSeen($condition);
945 $o .= networkConversation($a, $items, $pager, $mode, $update, $ordering);
951 * @brief Get the network tabs menu
953 * @param App $a The global App
954 * @return string Html of the networktab
955 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
957 function network_tabs(App $a)
960 /// @TODO fix this logic, reduce duplication
961 /// $a->page['content'] .= '<div class="tabs-wrapper">';
962 list($no_active, $all_active, $postord_active, $conv_active, $new_active, $starred_active, $bookmarked_active) = network_query_get_sel_tab($a);
964 // if no tabs are selected, defaults to comments
965 if ($no_active == 'active') {
966 $all_active = 'active';
974 'label' => L10n::t('Commented Order'),
975 'url' => str_replace('/new', '', $cmd) . '?order=comment' . (!empty($_GET['cid']) ? '&cid=' . $_GET['cid'] : ''),
976 'sel' => $all_active,
977 'title' => L10n::t('Sort by Comment Date'),
978 'id' => 'commented-order-tab',
982 'label' => L10n::t('Posted Order'),
983 'url' => str_replace('/new', '', $cmd) . '?order=post' . (!empty($_GET['cid']) ? '&cid=' . $_GET['cid'] : ''),
984 'sel' => $postord_active,
985 'title' => L10n::t('Sort by Post Date'),
986 'id' => 'posted-order-tab',
992 'label' => L10n::t('Personal'),
993 'url' => str_replace('/new', '', $cmd) . (!empty($_GET['cid']) ? '/?cid=' . $_GET['cid'] : '/?f=') . '&conv=1',
994 'sel' => $conv_active,
995 'title' => L10n::t('Posts that mention or involve you'),
996 'id' => 'personal-tab',
1000 if (Feature::isEnabled(local_user(), 'new_tab')) {
1002 'label' => L10n::t('New'),
1003 'url' => 'network/new' . (!empty($_GET['cid']) ? '/?cid=' . $_GET['cid'] : ''),
1004 'sel' => $new_active,
1005 'title' => L10n::t('Activity Stream - by date'),
1006 'id' => 'activitiy-by-date-tab',
1011 if (Feature::isEnabled(local_user(), 'link_tab')) {
1013 'label' => L10n::t('Shared Links'),
1014 'url' => str_replace('/new', '', $cmd) . (!empty($_GET['cid']) ? '/?cid=' . $_GET['cid'] : '/?f=') . '&bmark=1',
1015 'sel' => $bookmarked_active,
1016 'title' => L10n::t('Interesting Links'),
1017 'id' => 'shared-links-tab',
1023 'label' => L10n::t('Starred'),
1024 'url' => str_replace('/new', '', $cmd) . (!empty($_GET['cid']) ? '/?cid=' . $_GET['cid'] : '/?f=') . '&star=1',
1025 'sel' => $starred_active,
1026 'title' => L10n::t('Favourite Posts'),
1027 'id' => 'starred-posts-tab',
1031 // save selected tab, but only if not in file mode
1032 if (empty($_GET['file'])) {
1033 PConfig::set(local_user(), 'network.view', 'tab.selected', [
1034 $all_active, $postord_active, $conv_active, $new_active, $starred_active, $bookmarked_active
1038 $arr = ['tabs' => $tabs];
1039 Hook::callAll('network_tabs', $arr);
1041 $tpl = Renderer::getMarkupTemplate('common_tabs.tpl');
1043 return Renderer::replaceMacros($tpl, ['$tabs' => $arr['tabs']]);
1045 // --- end item filter tabs
1049 * Network hook into the HTML head to enable infinite scroll.
1051 * Since the HTML head is built after the module content has been generated, we need to retrieve the base query string
1052 * of the page to make the correct asynchronous call. This is obtained through the Pager that was instantiated in
1053 * networkThreadedView or networkFlatView.
1056 * @param string $htmlhead The head tag HTML string
1057 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1058 * @global Pager $pager
1060 function network_infinite_scroll_head(App $a, &$htmlhead)
1062 /// @TODO this will have to be converted to a static property of the converted Module\Network class
1068 if (PConfig::get(local_user(), 'system', 'infinite_scroll')
1069 && defaults($_GET, 'mode', '') != 'minimal'
1071 $tpl = Renderer::getMarkupTemplate('infinite_scroll_head.tpl');
1072 $htmlhead .= Renderer::replaceMacros($tpl, [
1073 '$pageno' => $pager->getPage(),
1074 '$reload_uri' => $pager->getBaseQueryString()