3 * @copyright Copyright (C) 2010-2023, the Friendica project
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Content;
24 use Friendica\App\BaseURL;
25 use Friendica\App\Router;
26 use Friendica\Core\Config\Capability\IManageConfigValues;
27 use Friendica\Core\Hook;
28 use Friendica\Core\L10n;
29 use Friendica\Core\Renderer;
30 use Friendica\Core\Session\Capability\IHandleUserSessions;
31 use Friendica\Database\Database;
32 use Friendica\Model\Contact;
33 use Friendica\Model\Profile;
34 use Friendica\Model\User;
35 use Friendica\Module\Conversation\Community;
36 use Friendica\Module\Home;
37 use Friendica\Module\Security\Login;
38 use Friendica\Network\HTTPException;
42 private static $selected = [
48 'introductions' => null,
49 'notifications' => null,
60 * An array of HTML links provided by addons providing a module via the app_menu hook
64 private $appMenu = null;
70 /** @var IHandleUserSessions */
74 /** @var IManageConfigValues */
79 public function __construct(BaseURL $baseUrl, L10n $l10n, IHandleUserSessions $session, Database $database, IManageConfigValues $config, Router $router)
81 $this->baseUrl = $baseUrl;
83 $this->session = $session;
84 $this->database = $database;
85 $this->config = $config;
86 $this->router = $router;
90 * Set a menu item in navbar as selected
94 public static function setSelected(string $item)
96 self::$selected[$item] = 'selected';
100 * Build page header and site navigation bars
103 * @throws HTTPException\InternalServerErrorException
104 * @throws HTTPException\MethodNotAllowedException
105 * @throws HTTPException\ServiceUnavailableException
107 public function getHtml(): string
109 // Placeholder div for popup panel
110 $nav = '<div id="panel" style="display: none;"></div>';
112 $nav_info = $this->getInfo();
114 $tpl = Renderer::getMarkupTemplate('nav.tpl');
116 $nav .= Renderer::replaceMacros($tpl, [
117 '$sitelocation' => $nav_info['sitelocation'],
118 '$nav' => $nav_info['nav'],
119 '$banner' => $nav_info['banner'],
120 '$emptynotifications' => $this->l10n->t('Nothing new here'),
121 '$userinfo' => $nav_info['userinfo'],
122 '$sel' => self::$selected,
123 '$apps' => $this->getAppMenu(),
124 '$home' => $this->l10n->t('Go back'),
125 '$clear_notifs' => $this->l10n->t('Clear notifications'),
126 '$search_hint' => $this->l10n->t('@name, !forum, #tags, content')
129 Hook::callAll('page_header', $nav);
135 * Returns the addon app menu
138 * @throws HTTPException\InternalServerErrorException
140 public function getAppMenu(): array
142 if (is_null($this->appMenu)) {
143 $this->appMenu = $this->populateAppMenu();
146 return $this->appMenu;
150 * Returns menus for apps that require one
153 * @throws HTTPException\InternalServerErrorException
155 private function populateAppMenu(): array
159 //Don't populate apps_menu if apps are private
161 $this->session->getLocalUserId()
162 || !$this->config->get('config', 'private_addons', false)
164 $arr = ['app_menu' => $appMenu];
166 Hook::callAll('app_menu', $arr);
168 $appMenu = $arr['app_menu'];
175 * Prepares a list of navigation links
177 * @return array Navigation links
178 * string 'sitelocation' => The webbie (username@site.com)
179 * array 'nav' => Array of links used in the nav menu
180 * string 'banner' => Formatted html link with banner image
181 * array 'userinfo' => Array of user information (name, icon)
182 * @throws HTTPException\InternalServerErrorException
183 * @throws HTTPException\MethodNotAllowedException
185 private function getInfo(): array
187 $ssl_state = (bool) $this->session->getLocalUserId();
190 * Our network is distributed, and as you visit friends some
191 * sites look exactly the same - it isn't always easy to know where you are.
192 * Display the current site location as a navigation aid.
195 $myident = !empty($this->session->getLocalUserNickname()) ? $this->session->getLocalUserNickname() . '@' : '';
197 $sitelocation = $myident . substr($this->baseUrl->get($ssl_state), strpos($this->baseUrl->get($ssl_state), '//') + 2);
201 'moderation' => null,
208 'langselector' => null,
211 'notifications' => null,
217 // Display login or logout
220 // nav links: array of array('href', 'text', 'extra css classes', 'title')
221 if ($this->session->isAuthenticated()) {
222 $nav['logout'] = ['logout', $this->l10n->t('Logout'), '', $this->l10n->t('End this session')];
224 $nav['login'] = ['login', $this->l10n->t('Login'), ($this->router->getModuleClass() == Login::class ? 'selected' : ''), $this->l10n->t('Sign in')];
227 if ($this->session->isAuthenticated()) {
229 $nav['usermenu'][] = ['profile/' . $this->session->getLocalUserNickname(), $this->l10n->t('Conversations'), '', $this->l10n->t('Conversations you started')];
230 $nav['usermenu'][] = ['profile/' . $this->session->getLocalUserNickname() . '/profile', $this->l10n->t('Profile'), '', $this->l10n->t('Your profile page')];
231 $nav['usermenu'][] = ['profile/' . $this->session->getLocalUserNickname() . '/photos', $this->l10n->t('Photos'), '', $this->l10n->t('Your photos')];
232 $nav['usermenu'][] = ['profile/' . $this->session->getLocalUserNickname() . '/media', $this->l10n->t('Media'), '', $this->l10n->t('Your postings with media')];
233 $nav['usermenu'][] = ['calendar/', $this->l10n->t('Calendar'), '', $this->l10n->t('Your calendar')];
234 $nav['usermenu'][] = ['notes/', $this->l10n->t('Personal notes'), '', $this->l10n->t('Your personal notes')];
237 $contact = $this->database->selectFirst('contact', ['id', 'url', 'avatar', 'micro', 'name', 'nick', 'baseurl', 'updated'], ['uid' => $this->session->getLocalUserId(), 'self' => true]);
239 'icon' => Contact::getMicro($contact),
240 'name' => $contact['name'],
244 // "Home" should also take you home from an authenticated remote profile connection
245 $homelink = $this->session->getMyUrl();
247 $homelink = $this->session->get('visitor_home', '');
250 if ($this->router->getModuleClass() != Home::class && !$this->session->getLocalUserId()) {
251 $nav['home'] = [$homelink, $this->l10n->t('Home'), '', $this->l10n->t('Home Page')];
254 if (intval($this->config->get('config', 'register_policy')) === \Friendica\Module\Register::OPEN && !$this->session->isAuthenticated()) {
255 $nav['register'] = ['register', $this->l10n->t('Register'), '', $this->l10n->t('Create an account')];
260 if (!$this->config->get('system', 'hide_help')) {
261 $nav['help'] = [$help_url, $this->l10n->t('Help'), '', $this->l10n->t('Help and documentation')];
264 if (count($this->getAppMenu()) > 0) {
265 $nav['apps'] = ['apps', $this->l10n->t('Apps'), '', $this->l10n->t('Addon applications, utilities, games')];
268 if ($this->session->getLocalUserId() || !$this->config->get('system', 'local_search')) {
269 $nav['search'] = ['search', $this->l10n->t('Search'), '', $this->l10n->t('Search site content')];
271 $nav['searchoption'] = [
272 $this->l10n->t('Full Text'),
273 $this->l10n->t('Tags'),
274 $this->l10n->t('Contacts')
277 if ($this->config->get('system', 'poco_local_search')) {
278 $nav['searchoption'][] = $this->l10n->t('Forums');
282 $gdirpath = 'directory';
283 if ($this->config->get('system', 'singleuser') && $this->config->get('system', 'directory')) {
284 $gdirpath = Profile::zrl($this->config->get('system', 'directory'), true);
287 if (($this->session->getLocalUserId() || $this->config->get('system', 'community_page_style') != Community::DISABLED_VISITOR) &&
288 !($this->config->get('system', 'community_page_style') == Community::DISABLED)) {
289 $nav['community'] = ['community', $this->l10n->t('Community'), '', $this->l10n->t('Conversations on this and other servers')];
292 if ($this->session->getLocalUserId()) {
293 $nav['calendar'] = ['calendar', $this->l10n->t('Calendar'), '', $this->l10n->t('Calendar')];
296 $nav['directory'] = [$gdirpath, $this->l10n->t('Directory'), '', $this->l10n->t('People directory')];
298 $nav['about'] = ['friendica', $this->l10n->t('Information'), '', $this->l10n->t('Information about this friendica instance')];
300 if ($this->config->get('system', 'tosdisplay')) {
301 $nav['tos'] = ['tos', $this->l10n->t('Terms of Service'), '', $this->l10n->t('Terms of Service of this Friendica instance')];
304 // The following nav links are only show to logged-in users
305 if ($this->session->getLocalUserNickname()) {
306 $nav['network'] = ['network', $this->l10n->t('Network'), '', $this->l10n->t('Conversations from your friends')];
308 $nav['home'] = ['profile/' . $this->session->getLocalUserNickname(), $this->l10n->t('Home'), '', $this->l10n->t('Your posts and conversations')];
310 // Don't show notifications for public communities
311 if ($this->session->get('page_flags', '') != User::PAGE_FLAGS_COMMUNITY) {
312 $nav['introductions'] = ['notifications/intros', $this->l10n->t('Introductions'), '', $this->l10n->t('Friend Requests')];
313 $nav['notifications'] = ['notifications', $this->l10n->t('Notifications'), '', $this->l10n->t('Notifications')];
314 $nav['notifications']['all'] = ['notifications/system', $this->l10n->t('See all notifications'), '', ''];
315 $nav['notifications']['mark'] = ['', $this->l10n->t('Mark as seen'), '', $this->l10n->t('Mark all system notifications as seen')];
318 $nav['messages'] = ['message', $this->l10n->t('Messages'), '', $this->l10n->t('Private mail')];
319 $nav['messages']['inbox'] = ['message', $this->l10n->t('Inbox'), '', $this->l10n->t('Inbox')];
320 $nav['messages']['outbox'] = ['message/sent', $this->l10n->t('Outbox'), '', $this->l10n->t('Outbox')];
321 $nav['messages']['new'] = ['message/new', $this->l10n->t('New Message'), '', $this->l10n->t('New Message')];
323 if (User::hasIdentities($this->session->getSubManagedUserId() ?: $this->session->getLocalUserId())) {
324 $nav['delegation'] = ['delegation', $this->l10n->t('Accounts'), '', $this->l10n->t('Manage other pages')];
327 $nav['settings'] = ['settings', $this->l10n->t('Settings'), '', $this->l10n->t('Account settings')];
329 $nav['contacts'] = ['contact', $this->l10n->t('Contacts'), '', $this->l10n->t('Manage/edit friends and contacts')];
332 // Show the link to the admin configuration page if user is admin
333 if ($this->session->isSiteAdmin()) {
334 $nav['admin'] = ['admin/', $this->l10n->t('Admin'), '', $this->l10n->t('Site setup and configuration')];
335 $nav['moderation'] = ['moderation/', $this->l10n->t('Moderation'), '', $this->l10n->t('Content and user moderation')];
338 $nav['navigation'] = ['navigation/', $this->l10n->t('Navigation'), '', $this->l10n->t('Site map')];
340 // Provide a banner/logo/whatever
341 $banner = $this->config->get('system', 'banner');
342 if (is_null($banner)) {
343 $banner = '<a href="https://friendi.ca"><img id="logo-img" width="32" height="32" src="images/friendica.svg" alt="logo" /></a><span id="logo-text"><a href="https://friendi.ca">Friendica</a></span>';
349 'sitelocation' => $sitelocation,
350 'userinfo' => $userinfo,
353 Hook::callAll('nav_info', $nav_info);