3828a4954c63399e895c605a8c27dc1bdddbe84b
[friendica.git/.git] / src / Content / Nav.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2024, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
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.
11  *
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.
16  *
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/>.
19  *
20  */
21
22 namespace Friendica\Content;
23
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;
39
40 class Nav
41 {
42         private static $selected = [
43                 'global'        => null,
44                 'community'     => null,
45                 'channel'       => null,
46                 'network'       => null,
47                 'home'          => null,
48                 'profiles'      => null,
49                 'introductions' => null,
50                 'notifications' => null,
51                 'messages'      => null,
52                 'directory'     => null,
53                 'settings'      => null,
54                 'contacts'      => null,
55                 'delegation'    => null,
56                 'calendar'      => null,
57                 'register'      => null
58         ];
59
60         /**
61          * An array of HTML links provided by addons providing a module via the app_menu hook
62          *
63          * @var array|null
64          */
65         private $appMenu = null;
66
67         /** @var BaseURL */
68         private $baseUrl;
69         /** @var L10n */
70         private $l10n;
71         /** @var IHandleUserSessions */
72         private $session;
73         /** @var Database */
74         private $database;
75         /** @var IManageConfigValues */
76         private $config;
77         /** @var Router */
78         private $router;
79
80         public function __construct(BaseURL $baseUrl, L10n $l10n, IHandleUserSessions $session, Database $database, IManageConfigValues $config, Router $router)
81         {
82                 $this->baseUrl  = $baseUrl;
83                 $this->l10n     = $l10n;
84                 $this->session  = $session;
85                 $this->database = $database;
86                 $this->config   = $config;
87                 $this->router   = $router;
88         }
89
90         /**
91          * Set a menu item in navbar as selected
92          *
93          * @param string $item
94          */
95         public static function setSelected(string $item)
96         {
97                 self::$selected[$item] = 'selected';
98         }
99
100         /**
101          * Build page header and site navigation bars
102          *
103          * @return string
104          * @throws HTTPException\InternalServerErrorException
105          * @throws HTTPException\MethodNotAllowedException
106          * @throws HTTPException\ServiceUnavailableException
107          */
108         public function getHtml(): string
109         {
110                 // Placeholder div for popup panel
111                 $nav = '<div id="panel" style="display: none;"></div>';
112
113                 $nav_info = $this->getInfo();
114
115                 $tpl = Renderer::getMarkupTemplate('nav.tpl');
116
117                 $nav .= Renderer::replaceMacros($tpl, [
118                         '$sitelocation' => $nav_info['sitelocation'],
119                         '$nav'          => $nav_info['nav'],
120                         '$banner'       => $nav_info['banner'],
121                         '$emptynotifications' => $this->l10n->t('Nothing new here'),
122                         '$userinfo'     => $nav_info['userinfo'],
123                         '$sel'          => self::$selected,
124                         '$apps'         => $this->getAppMenu(),
125                         '$home'         => $this->l10n->t('Go back'),
126                         '$clear_notifs' => $this->l10n->t('Clear notifications'),
127                         '$search_hint'  => $this->l10n->t('@name, !group, #tags, content')
128                 ]);
129
130                 Hook::callAll('page_header', $nav);
131
132                 return $nav;
133         }
134
135         /**
136          * Returns the addon app menu
137          *
138          * @return array
139          * @throws HTTPException\InternalServerErrorException
140          */
141         public function getAppMenu(): array
142         {
143                 if (is_null($this->appMenu)) {
144                         $this->appMenu = $this->populateAppMenu();
145                 }
146
147                 return $this->appMenu;
148         }
149
150         /**
151          * Returns menus for apps that require one
152          *
153          * @return array
154          * @throws HTTPException\InternalServerErrorException
155          */
156         private function populateAppMenu(): array
157         {
158                 $appMenu = [];
159
160                 //Don't populate apps_menu if apps are private
161                 if (
162                         $this->session->getLocalUserId()
163                         || !$this->config->get('config', 'private_addons', false)
164                 ) {
165                         $arr = ['app_menu' => $appMenu];
166
167                         Hook::callAll('app_menu', $arr);
168
169                         $appMenu = $arr['app_menu'];
170                 }
171
172                 return $appMenu;
173         }
174
175         /**
176          * Prepares a list of navigation links
177          *
178          * @return array Navigation links
179          *    string 'sitelocation' => The webbie (username@site.com)
180          *    array 'nav' => Array of links used in the nav menu
181          *    string 'banner' => Formatted html link with banner image
182          *    array 'userinfo' => Array of user information (name, icon)
183          * @throws HTTPException\InternalServerErrorException
184          * @throws HTTPException\MethodNotAllowedException
185          */
186         private function getInfo(): array
187         {
188                 /*
189                  * Our network is distributed, and as you visit friends some
190                  * sites look exactly the same - it isn't always easy to know where you are.
191                  * Display the current site location as a navigation aid.
192                  */
193
194                 $myident = !empty($this->session->getLocalUserNickname()) ? $this->session->getLocalUserNickname() . '@' : '';
195
196                 $sitelocation = $myident . substr($this->baseUrl, strpos($this->baseUrl, '//') + 2);
197
198                 $nav = [
199                         'admin'         => null,
200                         'moderation'    => null,
201                         'apps'          => null,
202                         'community'     => null,
203                         'channel'       => null,
204                         'home'          => null,
205                         'calendar'      => null,
206                         'login'         => null,
207                         'logout'        => null,
208                         'langselector'  => null,
209                         'messages'      => null,
210                         'network'       => null,
211                         'notifications' => null,
212                         'remote'        => null,
213                         'search'        => null,
214                         'usermenu'      => [],
215                 ];
216
217                 // Display login or logout
218                 $userinfo = null;
219
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')];
223                 } else {
224                         $nav['login'] = ['login', $this->l10n->t('Login'), ($this->router->getModuleClass() == Login::class ? 'selected' : ''), $this->l10n->t('Sign in')];
225                 }
226
227                 if ($this->session->isAuthenticated()) {
228                         // user menu
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')];
235
236                         // user info
237                         $contact = $this->database->selectFirst('contact', ['id', 'url', 'avatar', 'micro', 'name', 'nick', 'baseurl', 'updated'], ['uid' => $this->session->getLocalUserId(), 'self' => true]);
238                         $userinfo = [
239                                 'icon' => Contact::getMicro($contact),
240                                 'name' => $contact['name'],
241                         ];
242                 }
243
244                 // "Home" should also take you home from an authenticated remote profile connection
245                 $homelink = $this->session->getMyUrl();
246                 if (!$homelink) {
247                         $homelink = $this->session->get('visitor_home', '');
248                 }
249
250                 if ($this->router->getModuleClass() != Home::class && !$this->session->getLocalUserId()) {
251                         $nav['home'] = [$homelink, $this->l10n->t('Home'), '', $this->l10n->t('Home Page')];
252                 }
253
254                 if (\Friendica\Module\Register::getPolicy() === \Friendica\Module\Register::OPEN && !$this->session->isAuthenticated()) {
255                         $nav['register'] = ['register', $this->l10n->t('Register'), '', $this->l10n->t('Create an account')];
256                 }
257
258                 $help_url = 'help';
259
260                 if (!$this->config->get('system', 'hide_help')) {
261                         $nav['help'] = [$help_url, $this->l10n->t('Help'), '', $this->l10n->t('Help and documentation')];
262                 }
263
264                 if (count($this->getAppMenu()) > 0) {
265                         $nav['apps'] = ['apps', $this->l10n->t('Apps'), '', $this->l10n->t('Addon applications, utilities, games')];
266                 }
267
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')];
270
271                         $nav['searchoption'] = [
272                                 $this->l10n->t('Full Text'),
273                                 $this->l10n->t('Tags'),
274                                 $this->l10n->t('Contacts')
275                         ];
276
277                         if ($this->config->get('system', 'poco_local_search')) {
278                                 $nav['searchoption'][] = $this->l10n->t('Groups');
279                         }
280                 }
281
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);
285                 }
286
287                 if (Feature::isEnabled($this->session->getLocalUserId(), Feature::COMMUNITY) && (($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')];
290                 }
291
292                 if ($this->session->getLocalUserId()) {
293                         $nav['calendar'] = ['calendar', $this->l10n->t('Calendar'), '', $this->l10n->t('Calendar')];
294                 }
295
296                 $nav['directory'] = [$gdirpath, $this->l10n->t('Directory'), '', $this->l10n->t('People directory')];
297
298                 $nav['about'] = ['friendica', $this->l10n->t('Information'), '', $this->l10n->t('Information about this friendica instance')];
299
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')];
302                 }
303
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')];
307
308                         $nav['home'] = ['profile/' . $this->session->getLocalUserNickname(), $this->l10n->t('Home'), '', $this->l10n->t('Your posts and conversations')];
309
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')];
316                         }
317
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')];
322
323                         if (User::hasIdentities($this->session->getSubManagedUserId() ?: $this->session->getLocalUserId())) {
324                                 $nav['delegation'] = ['delegation', $this->l10n->t('Accounts'), '', $this->l10n->t('Manage other pages')];
325                         }
326
327                         $nav['settings'] = ['settings', $this->l10n->t('Settings'), '', $this->l10n->t('Account settings')];
328
329                         $nav['contacts'] = ['contact', $this->l10n->t('Contacts'), '', $this->l10n->t('Manage/edit friends and contacts')];
330                 }
331
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')];
336                 }
337
338                 $nav['navigation'] = ['navigation/', $this->l10n->t('Navigation'), '', $this->l10n->t('Site map')];
339
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>';
344                 }
345
346                 $nav_info = [
347                         'banner'       => $banner,
348                         'nav'          => $nav,
349                         'sitelocation' => $sitelocation,
350                         'userinfo'     => $userinfo,
351                 ];
352
353                 Hook::callAll('nav_info', $nav_info);
354
355                 return $nav_info;
356         }
357 }