]> git.mxchange.org Git - friendica.git/blob - src/Content/Nav.php
Adapt BaseURL calls to new UriInterface
[friendica.git] / src / Content / Nav.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2023, 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                 'network'   => null,
46                 'home'      => null,
47                 'profiles'  => null,
48                 'introductions' => null,
49                 'notifications' => null,
50                 'messages'  => null,
51                 'directory' => null,
52                 'settings'  => null,
53                 'contacts'  => null,
54                 'delegation'=> null,
55                 'calendar'  => null,
56                 'register'  => null
57         ];
58
59         /**
60          * An array of HTML links provided by addons providing a module via the app_menu hook
61          *
62          * @var array|null
63          */
64         private $appMenu = null;
65
66         /** @var BaseURL */
67         private $baseUrl;
68         /** @var L10n */
69         private $l10n;
70         /** @var IHandleUserSessions */
71         private $session;
72         /** @var Database */
73         private $database;
74         /** @var IManageConfigValues */
75         private $config;
76         /** @var Router */
77         private $router;
78
79         public function __construct(BaseURL $baseUrl, L10n $l10n, IHandleUserSessions $session, Database $database, IManageConfigValues $config, Router $router)
80         {
81                 $this->baseUrl  = $baseUrl;
82                 $this->l10n     = $l10n;
83                 $this->session  = $session;
84                 $this->database = $database;
85                 $this->config   = $config;
86                 $this->router   = $router;
87         }
88
89         /**
90          * Set a menu item in navbar as selected
91          *
92          * @param string $item
93          */
94         public static function setSelected(string $item)
95         {
96                 self::$selected[$item] = 'selected';
97         }
98
99         /**
100          * Build page header and site navigation bars
101          *
102          * @return string
103          * @throws HTTPException\InternalServerErrorException
104          * @throws HTTPException\MethodNotAllowedException
105          * @throws HTTPException\ServiceUnavailableException
106          */
107         public function getHtml(): string
108         {
109                 // Placeholder div for popup panel
110                 $nav = '<div id="panel" style="display: none;"></div>';
111
112                 $nav_info = $this->getInfo();
113
114                 $tpl = Renderer::getMarkupTemplate('nav.tpl');
115
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')
127                 ]);
128
129                 Hook::callAll('page_header', $nav);
130
131                 return $nav;
132         }
133
134         /**
135          * Returns the addon app menu
136          *
137          * @return array
138          * @throws HTTPException\InternalServerErrorException
139          */
140         public function getAppMenu(): array
141         {
142                 if (is_null($this->appMenu)) {
143                         $this->appMenu = $this->populateAppMenu();
144                 }
145
146                 return $this->appMenu;
147         }
148
149         /**
150          * Returns menus for apps that require one
151          *
152          * @return array
153          * @throws HTTPException\InternalServerErrorException
154          */
155         private function populateAppMenu(): array
156         {
157                 $appMenu = [];
158
159                 //Don't populate apps_menu if apps are private
160                 if (
161                         $this->session->getLocalUserId()
162                         || !$this->config->get('config', 'private_addons', false)
163                 ) {
164                         $arr = ['app_menu' => $appMenu];
165
166                         Hook::callAll('app_menu', $arr);
167
168                         $appMenu = $arr['app_menu'];
169                 }
170
171                 return $appMenu;
172         }
173
174         /**
175          * Prepares a list of navigation links
176          *
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
184          */
185         private function getInfo(): array
186         {
187                 $ssl_state = (bool) $this->session->getLocalUserId();
188
189                 /*
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.
193                  */
194
195                 $myident = !empty($this->session->getLocalUserNickname()) ? $this->session->getLocalUserNickname() . '@' : '';
196
197                 $sitelocation = $myident . substr($this->baseUrl, strpos($this->baseUrl, '//') + 2);
198
199                 $nav = [
200                         'admin'         => null,
201                         'moderation'    => null,
202                         'apps'          => null,
203                         'community'     => 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 (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')];
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('Forums');
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 (($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 }