]> git.mxchange.org Git - friendica.git/blob - src/Content/Nav.php
Merge pull request #12018 from nupplaphil/feat/session_util
[friendica.git] / src / Content / Nav.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, 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;
25 use Friendica\Core\Hook;
26 use Friendica\Core\Renderer;
27 use Friendica\Core\Session;
28 use Friendica\Database\DBA;
29 use Friendica\DI;
30 use Friendica\Model\Contact;
31 use Friendica\Model\Profile;
32 use Friendica\Model\User;
33 use Friendica\Module\Conversation\Community;
34
35 class Nav
36 {
37         private static $selected = [
38                 'global'    => null,
39                 'community' => null,
40                 'network'   => null,
41                 'home'      => null,
42                 'profiles'  => null,
43                 'introductions' => null,
44                 'notifications' => null,
45                 'messages'  => null,
46                 'directory' => null,
47                 'settings'  => null,
48                 'contacts'  => null,
49                 'delegation'=> null,
50                 'events'    => null,
51                 'register'  => null
52         ];
53
54         /**
55          * An array of HTML links provided by addons providing a module via the app_menu hook
56          *
57          * @var array
58          */
59         private static $app_menu = null;
60
61         /**
62          * Set a menu item in navbar as selected
63          *
64          * @param string $item
65          */
66         public static function setSelected(string $item)
67         {
68                 self::$selected[$item] = 'selected';
69         }
70
71         /**
72          * Build page header and site navigation bars
73          *
74          * @param  App    $a
75          * @return string
76          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
77          */
78         public static function build(App $a): string
79         {
80                 // Placeholder div for popup panel
81                 $nav = '<div id="panel" style="display: none;"></div>';
82
83                 $nav_info = self::getInfo($a);
84
85                 $tpl = Renderer::getMarkupTemplate('nav.tpl');
86
87                 $nav .= Renderer::replaceMacros($tpl, [
88                         '$sitelocation' => $nav_info['sitelocation'],
89                         '$nav'          => $nav_info['nav'],
90                         '$banner'       => $nav_info['banner'],
91                         '$emptynotifications' => DI::l10n()->t('Nothing new here'),
92                         '$userinfo'     => $nav_info['userinfo'],
93                         '$sel'          => self::$selected,
94                         '$apps'         => self::getAppMenu(),
95                         '$home'         => DI::l10n()->t('Go back'),
96                         '$clear_notifs' => DI::l10n()->t('Clear notifications'),
97                         '$search_hint'  => DI::l10n()->t('@name, !forum, #tags, content')
98                 ]);
99
100                 Hook::callAll('page_header', $nav);
101
102                 return $nav;
103         }
104
105         /**
106          * Returns the addon app menu
107          *
108          * @return array
109          */
110         public static function getAppMenu(): array
111         {
112                 if (is_null(self::$app_menu)) {
113                         self::populateAppMenu();
114                 }
115
116                 return self::$app_menu;
117         }
118
119         /**
120          * Fills the apps static variable with apps that require a menu
121          *
122          * @return void
123          */
124         private static function populateAppMenu()
125         {
126                 self::$app_menu = [];
127
128                 //Don't populate apps_menu if apps are private
129                 $privateapps = DI::config()->get('config', 'private_addons', false);
130                 if (local_user() || !$privateapps) {
131                         $arr = ['app_menu' => self::$app_menu];
132
133                         Hook::callAll('app_menu', $arr);
134
135                         self::$app_menu = $arr['app_menu'];
136                 }
137         }
138
139         /**
140          * Prepares a list of navigation links
141          *
142          * @param  App   $a
143          * @return array Navigation links
144          *    string 'sitelocation' => The webbie (username@site.com)
145          *    array 'nav' => Array of links used in the nav menu
146          *    string 'banner' => Formatted html link with banner image
147          *    array 'userinfo' => Array of user information (name, icon)
148          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
149          */
150         private static function getInfo(App $a): array
151         {
152                 $ssl_state = (bool) local_user();
153
154                 /*
155                  * Our network is distributed, and as you visit friends some of the
156                  * sites look exactly the same - it isn't always easy to know where you are.
157                  * Display the current site location as a navigation aid.
158                  */
159
160                 $myident = !empty($a->getLoggedInUserNickname()) ? $a->getLoggedInUserNickname() . '@' : '';
161
162                 $sitelocation = $myident . substr(DI::baseUrl()->get($ssl_state), strpos(DI::baseUrl()->get($ssl_state), '//') + 2);
163
164                 $nav = [
165                         'admin'         => null,
166                         'apps'          => null,
167                         'community'     => null,
168                         'home'          => null,
169                         'events'        => null,
170                         'login'         => null,
171                         'logout'        => null,
172                         'langselector'  => null,
173                         'messages'      => null,
174                         'network'       => null,
175                         'notifications' => null,
176                         'remote'        => null,
177                         'search'        => null,
178                         'usermenu'      => [],
179                 ];
180
181                 // Display login or logout
182                 $userinfo = null;
183
184                 // nav links: array of array('href', 'text', 'extra css classes', 'title')
185                 if (Session::isAuthenticated()) {
186                         $nav['logout'] = ['logout', DI::l10n()->t('Logout'), '', DI::l10n()->t('End this session')];
187                 } else {
188                         $nav['login'] = ['login', DI::l10n()->t('Login'), (DI::args()->getModuleName() == 'login' ? 'selected' : ''), DI::l10n()->t('Sign in')];
189                 }
190
191                 if ($a->isLoggedIn()) {
192                         // user menu
193                         $nav['usermenu'][] = ['profile/' . $a->getLoggedInUserNickname(), DI::l10n()->t('Status'), '', DI::l10n()->t('Your posts and conversations')];
194                         $nav['usermenu'][] = ['profile/' . $a->getLoggedInUserNickname() . '/profile', DI::l10n()->t('Profile'), '', DI::l10n()->t('Your profile page')];
195                         $nav['usermenu'][] = ['photos/' . $a->getLoggedInUserNickname(), DI::l10n()->t('Photos'), '', DI::l10n()->t('Your photos')];
196                         $nav['usermenu'][] = ['profile/' . $a->getLoggedInUserNickname() . '/media', DI::l10n()->t('Media'), '', DI::l10n()->t('Your postings with media')];
197                         $nav['usermenu'][] = ['events/', DI::l10n()->t('Events'), '', DI::l10n()->t('Your events')];
198                         $nav['usermenu'][] = ['notes/', DI::l10n()->t('Personal notes'), '', DI::l10n()->t('Your personal notes')];
199
200                         // user info
201                         $contact = DBA::selectFirst('contact', ['id', 'url', 'avatar', 'micro', 'name', 'nick', 'baseurl', 'updated'], ['uid' => $a->getLoggedInUserId(), 'self' => true]);
202                         $userinfo = [
203                                 'icon' => Contact::getMicro($contact),
204                                 'name' => $contact['name'],
205                         ];
206                 }
207
208                 // "Home" should also take you home from an authenticated remote profile connection
209                 $homelink = Profile::getMyURL();
210                 if (! $homelink) {
211                         $homelink = DI::session()->get('visitor_home', '');
212                 }
213
214                 if ((DI::args()->getModuleName() != 'home') && (! (local_user()))) {
215                         $nav['home'] = [$homelink, DI::l10n()->t('Home'), '', DI::l10n()->t('Home Page')];
216                 }
217
218                 if (intval(DI::config()->get('config', 'register_policy')) === \Friendica\Module\Register::OPEN && !Session::isAuthenticated()) {
219                         $nav['register'] = ['register', DI::l10n()->t('Register'), '', DI::l10n()->t('Create an account')];
220                 }
221
222                 $help_url = 'help';
223
224                 if (!DI::config()->get('system', 'hide_help')) {
225                         $nav['help'] = [$help_url, DI::l10n()->t('Help'), '', DI::l10n()->t('Help and documentation')];
226                 }
227
228                 if (count(self::getAppMenu()) > 0) {
229                         $nav['apps'] = ['apps', DI::l10n()->t('Apps'), '', DI::l10n()->t('Addon applications, utilities, games')];
230                 }
231
232                 if (local_user() || !DI::config()->get('system', 'local_search')) {
233                         $nav['search'] = ['search', DI::l10n()->t('Search'), '', DI::l10n()->t('Search site content')];
234
235                         $nav['searchoption'] = [
236                                 DI::l10n()->t('Full Text'),
237                                 DI::l10n()->t('Tags'),
238                                 DI::l10n()->t('Contacts')
239                         ];
240
241                         if (DI::config()->get('system', 'poco_local_search')) {
242                                 $nav['searchoption'][] = DI::l10n()->t('Forums');
243                         }
244                 }
245
246                 $gdirpath = 'directory';
247
248                 if (strlen(DI::config()->get('system', 'singleuser'))) {
249                         $gdir = DI::config()->get('system', 'directory');
250                         if (strlen($gdir)) {
251                                 $gdirpath = Profile::zrl($gdir, true);
252                         }
253                 }
254
255                 if ((local_user() || DI::config()->get('system', 'community_page_style') != Community::DISABLED_VISITOR) &&
256                         !(DI::config()->get('system', 'community_page_style') == Community::DISABLED)) {
257                         $nav['community'] = ['community', DI::l10n()->t('Community'), '', DI::l10n()->t('Conversations on this and other servers')];
258                 }
259
260                 if (local_user()) {
261                         $nav['events'] = ['events', DI::l10n()->t('Events'), '', DI::l10n()->t('Events and Calendar')];
262                 }
263
264                 $nav['directory'] = [$gdirpath, DI::l10n()->t('Directory'), '', DI::l10n()->t('People directory')];
265
266                 $nav['about'] = ['friendica', DI::l10n()->t('Information'), '', DI::l10n()->t('Information about this friendica instance')];
267
268                 if (DI::config()->get('system', 'tosdisplay')) {
269                         $nav['tos'] = ['tos', DI::l10n()->t('Terms of Service'), '', DI::l10n()->t('Terms of Service of this Friendica instance')];
270                 }
271
272                 // The following nav links are only show to logged in users
273                 if (local_user() && !empty($a->getLoggedInUserNickname())) {
274                         $nav['network'] = ['network', DI::l10n()->t('Network'), '', DI::l10n()->t('Conversations from your friends')];
275
276                         $nav['home'] = ['profile/' . $a->getLoggedInUserNickname(), DI::l10n()->t('Home'), '', DI::l10n()->t('Your posts and conversations')];
277
278                         // Don't show notifications for public communities
279                         if (DI::session()->get('page_flags', '') != User::PAGE_FLAGS_COMMUNITY) {
280                                 $nav['introductions'] = ['notifications/intros', DI::l10n()->t('Introductions'), '', DI::l10n()->t('Friend Requests')];
281                                 $nav['notifications'] = ['notifications',       DI::l10n()->t('Notifications'), '', DI::l10n()->t('Notifications')];
282                                 $nav['notifications']['all'] = ['notifications/system', DI::l10n()->t('See all notifications'), '', ''];
283                                 $nav['notifications']['mark'] = ['', DI::l10n()->t('Mark as seen'), '', DI::l10n()->t('Mark all system notifications as seen')];
284                         }
285
286                         $nav['messages'] = ['message', DI::l10n()->t('Messages'), '', DI::l10n()->t('Private mail')];
287                         $nav['messages']['inbox'] = ['message', DI::l10n()->t('Inbox'), '', DI::l10n()->t('Inbox')];
288                         $nav['messages']['outbox'] = ['message/sent', DI::l10n()->t('Outbox'), '', DI::l10n()->t('Outbox')];
289                         $nav['messages']['new'] = ['message/new', DI::l10n()->t('New Message'), '', DI::l10n()->t('New Message')];
290
291                         if (User::hasIdentities(DI::session()->get('submanage') ?: local_user())) {
292                                 $nav['delegation'] = ['delegation', DI::l10n()->t('Accounts'), '', DI::l10n()->t('Manage other pages')];
293                         }
294
295                         $nav['settings'] = ['settings', DI::l10n()->t('Settings'), '', DI::l10n()->t('Account settings')];
296
297                         $nav['contacts'] = ['contact', DI::l10n()->t('Contacts'), '', DI::l10n()->t('Manage/edit friends and contacts')];
298                 }
299
300                 // Show the link to the admin configuration page if user is admin
301                 if ($a->isSiteAdmin()) {
302                         $nav['admin'] = ['admin/', DI::l10n()->t('Admin'), '', DI::l10n()->t('Site setup and configuration')];
303                 }
304
305                 $nav['navigation'] = ['navigation/', DI::l10n()->t('Navigation'), '', DI::l10n()->t('Site map')];
306
307                 // Provide a banner/logo/whatever
308                 $banner = DI::config()->get('system', 'banner');
309                 if (is_null($banner)) {
310                         $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>';
311                 }
312
313                 $nav_info = [
314                         'banner'       => $banner,
315                         'nav'          => $nav,
316                         'sitelocation' => $sitelocation,
317                         'userinfo'     => $userinfo,
318                 ];
319
320                 Hook::callAll('nav_info', $nav_info);
321
322                 return $nav_info;
323         }
324 }