]> git.mxchange.org Git - friendica.git/blob - src/Module/Profile/Profile.php
553e9c523140b06a184138f3e3e1843a6a69b775
[friendica.git] / src / Module / Profile / Profile.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\Module\Profile;
23
24 use Friendica\Content\Feature;
25 use Friendica\Content\ForumManager;
26 use Friendica\Content\Nav;
27 use Friendica\Content\Text\BBCode;
28 use Friendica\Content\Text\HTML;
29 use Friendica\Core\Hook;
30 use Friendica\Core\Protocol;
31 use Friendica\Core\Renderer;
32 use Friendica\Core\System;
33 use Friendica\Database\DBA;
34 use Friendica\DI;
35 use Friendica\Model\Contact;
36 use Friendica\Model\Profile as ProfileModel;
37 use Friendica\Model\Tag;
38 use Friendica\Model\User;
39 use Friendica\Module\BaseProfile;
40 use Friendica\Module\Security\Login;
41 use Friendica\Network\HTTPException;
42 use Friendica\Protocol\ActivityPub;
43 use Friendica\Util\DateTimeFormat;
44 use Friendica\Util\Temporal;
45
46 class Profile extends BaseProfile
47 {
48         protected function rawContent(array $request = [])
49         {
50                 if (ActivityPub::isRequest()) {
51                         $user = DBA::selectFirst('user', ['uid'], ['nickname' => $this->parameters['nickname'], 'account_removed' => false]);
52                         if (DBA::isResult($user)) {
53                                 try {
54                                         $data = ActivityPub\Transmitter::getProfile($user['uid']);
55                                         header('Access-Control-Allow-Origin: *');
56                                         header('Cache-Control: max-age=23200, stale-while-revalidate=23200');
57                                         System::jsonExit($data, 'application/activity+json');
58                                 } catch (HTTPException\NotFoundException $e) {
59                                         System::jsonError(404, ['error' => 'Record not found']);
60                                 }
61                         }
62
63                         if (DBA::exists('userd', ['username' => $this->parameters['nickname']])) {
64                                 // Known deleted user
65                                 $data = ActivityPub\Transmitter::getDeletedUser($this->parameters['nickname']);
66
67                                 System::jsonError(410, $data);
68                         } else {
69                                 // Any other case (unknown, blocked, nverified, expired, no profile, no self contact)
70                                 System::jsonError(404, []);
71                         }
72                 }
73         }
74
75         protected function content(array $request = []): string
76         {
77                 $a = DI::app();
78
79                 $profile = ProfileModel::load($a, $this->parameters['nickname'] ?? '');
80                 if (!$profile) {
81                         throw new HTTPException\NotFoundException(DI::l10n()->t('Profile not found.'));
82                 }
83
84                 $remote_contact_id = DI::userSession()->getRemoteContactID($profile['uid']);
85
86                 if (DI::config()->get('system', 'block_public') && !DI::userSession()->isAuthenticated()) {
87                         return Login::form();
88                 }
89
90                 if (!empty($profile['hidewall']) && !DI::userSession()->isAuthenticated()) {
91                         $this->baseUrl->redirect('profile/' . $profile['nickname'] . '/restricted');
92                 }
93
94                 if (!empty($profile['page-flags']) && $profile['page-flags'] == User::PAGE_FLAGS_COMMUNITY) {
95                         DI::page()['htmlhead'] .= '<meta name="friendica.community" content="true" />' . "\n";
96                 }
97
98                 DI::page()['htmlhead'] .= self::buildHtmlHead($profile, $this->parameters['nickname'], $remote_contact_id);
99
100                 Nav::setSelected('home');
101
102                 $is_owner = DI::userSession()->getLocalUserId() == $profile['uid'];
103                 $o = self::getTabsHTML($a, 'profile', $is_owner, $profile['nickname'], $profile['hide-friends']);
104
105                 $view_as_contacts = [];
106                 $view_as_contact_id = 0;
107                 $view_as_contact_alert = '';
108                 if ($is_owner) {
109                         $view_as_contact_id = intval($_GET['viewas'] ?? 0);
110
111                         $view_as_contacts = Contact::selectToArray(['id', 'name'], [
112                                 'uid' => DI::userSession()->getLocalUserId(),
113                                 'rel' => [Contact::FOLLOWER, Contact::SHARING, Contact::FRIEND],
114                                 'network' => Protocol::DFRN,
115                                 'blocked' => false,
116                         ]);
117
118                         $view_as_contact_ids = array_column($view_as_contacts, 'id');
119
120                         // User manually provided a contact ID they aren't privy to, silently defaulting to their own view
121                         if (!in_array($view_as_contact_id, $view_as_contact_ids)) {
122                                 $view_as_contact_id = 0;
123                         }
124
125                         if (($key = array_search($view_as_contact_id, $view_as_contact_ids)) !== false) {
126                                 $view_as_contact_alert = DI::l10n()->t(
127                                         'You\'re currently viewing your profile as <b>%s</b> <a href="%s" class="btn btn-sm pull-right">Cancel</a>',
128                                         htmlentities($view_as_contacts[$key]['name'], ENT_COMPAT, 'UTF-8'),
129                                         'profile/' . $this->parameters['nickname'] . '/profile'
130                                 );
131                         }
132                 }
133
134                 $basic_fields = [];
135
136                 $basic_fields += self::buildField('fullname', DI::l10n()->t('Full Name:'), $profile['name']);
137
138                 if (Feature::isEnabled($profile['uid'], 'profile_membersince')) {
139                         $basic_fields += self::buildField(
140                                 'membersince',
141                                 DI::l10n()->t('Member since:'),
142                                 DateTimeFormat::local($profile['register_date'])
143                         );
144                 }
145
146                 if (!empty($profile['dob']) && $profile['dob'] > DBA::NULL_DATE) {
147                         $year_bd_format = DI::l10n()->t('j F, Y');
148                         $short_bd_format = DI::l10n()->t('j F');
149
150                         $dob = DI::l10n()->getDay(
151                                 intval($profile['dob']) ?
152                                         DateTimeFormat::utc($profile['dob'] . ' 00:00 +00:00', $year_bd_format)
153                                         : DateTimeFormat::utc('2001-' . substr($profile['dob'], 5) . ' 00:00 +00:00', $short_bd_format)
154                         );
155
156                         $basic_fields += self::buildField('dob', DI::l10n()->t('Birthday:'), $dob);
157
158                         if ($age = Temporal::getAgeByTimezone($profile['dob'], $profile['timezone'])) {
159                                 $basic_fields += self::buildField('age', DI::l10n()->t('Age: '), DI::l10n()->tt('%d year old', '%d years old', $age));
160                         }
161                 }
162
163                 if ($profile['about']) {
164                         $basic_fields += self::buildField('about', DI::l10n()->t('Description:'), BBCode::convertForUriId($profile['uri-id'], $profile['about']));
165                 }
166
167                 if ($profile['xmpp']) {
168                         $basic_fields += self::buildField('xmpp', DI::l10n()->t('XMPP:'), $profile['xmpp']);
169                 }
170
171                 if ($profile['matrix']) {
172                         $basic_fields += self::buildField('matrix', DI::l10n()->t('Matrix:'), $profile['matrix']);
173                 }
174
175                 if ($profile['homepage']) {
176                         $basic_fields += self::buildField('homepage', DI::l10n()->t('Homepage:'), HTML::toLink($profile['homepage']));
177                 }
178
179                 if (
180                         $profile['address']
181                         || $profile['locality']
182                         || $profile['postal-code']
183                         || $profile['region']
184                         || $profile['country-name']
185                 ) {
186                         $basic_fields += self::buildField('location', DI::l10n()->t('Location:'), ProfileModel::formatLocation($profile));
187                 }
188
189                 if ($profile['pub_keywords']) {
190                         $tags = [];
191                         // Separator is defined in Module\Settings\Profile\Index::cleanKeywords
192                         foreach (explode(', ', $profile['pub_keywords']) as $tag_label) {
193                                 $tags[] = [
194                                         'url' => '/search?tag=' . $tag_label,
195                                         'label' => Tag::TAG_CHARACTER[Tag::HASHTAG] . $tag_label,
196                                 ];
197                         }
198
199                         $basic_fields += self::buildField('pub_keywords', DI::l10n()->t('Tags:'), $tags);
200                 }
201
202                 $custom_fields = [];
203
204                 // Defaults to the current logged in user self contact id to show self-only fields
205                 $contact_id = $view_as_contact_id ?: $remote_contact_id ?: 0;
206
207                 if ($is_owner && $contact_id === 0) {
208                         $profile_fields = DI::profileField()->selectByUserId($profile['uid']);
209                 } else {
210                         $profile_fields = DI::profileField()->selectByContactId($contact_id, $profile['uid']);
211                 }
212
213                 foreach ($profile_fields as $profile_field) {
214                         $custom_fields += self::buildField(
215                                 'custom_' . $profile_field->order,
216                                 $profile_field->label,
217                                 BBCode::convertForUriId($profile['uri-id'], $profile_field->value),
218                                 'aprofile custom'
219                         );
220                 };
221
222                 //show subcribed forum if it is enabled in the usersettings
223                 if (Feature::isEnabled($profile['uid'], 'forumlist_profile')) {
224                         $custom_fields += self::buildField(
225                                 'forumlist',
226                                 DI::l10n()->t('Forums:'),
227                                 ForumManager::profileAdvanced($profile['uid'])
228                         );
229                 }
230
231                 $tpl = Renderer::getMarkupTemplate('profile/profile.tpl');
232                 $o .= Renderer::replaceMacros($tpl, [
233                         '$title' => DI::l10n()->t('Profile'),
234                         '$yourself' => DI::l10n()->t('Yourself'),
235                         '$view_as_contacts' => $view_as_contacts,
236                         '$view_as_contact_id' => $view_as_contact_id,
237                         '$view_as_contact_alert' => $view_as_contact_alert,
238                         '$view_as' => DI::l10n()->t('View profile as:'),
239                         '$submit' => DI::l10n()->t('Submit'),
240                         '$basic' => DI::l10n()->t('Basic'),
241                         '$advanced' => DI::l10n()->t('Advanced'),
242                         '$is_owner' => $profile['uid'] == DI::userSession()->getLocalUserId(),
243                         '$query_string' => DI::args()->getQueryString(),
244                         '$basic_fields' => $basic_fields,
245                         '$custom_fields' => $custom_fields,
246                         '$profile' => $profile,
247                         '$edit_link' => [
248                                 'url' => DI::baseUrl() . '/settings/profile', DI::l10n()->t('Edit profile'),
249                                 'title' => '',
250                                 'label' => DI::l10n()->t('Edit profile')
251                         ],
252                         '$viewas_link' => [
253                                 'url' =>  DI::args()->getQueryString() . '#viewas',
254                                 'title' => '',
255                                 'label' => DI::l10n()->t('View as')
256                         ],
257                 ]);
258
259                 Hook::callAll('profile_advanced', $o);
260
261                 return $o;
262         }
263
264         /**
265          * Creates a profile field structure to be used in the profile template
266          *
267          * @param string $name  Arbitrary name of the field
268          * @param string $label Display label of the field
269          * @param mixed  $value Display value of the field
270          * @param string $class Optional CSS class to apply to the field
271          * @return array
272          */
273         private static function buildField(string $name, string $label, $value, string $class = 'aprofile')
274         {
275                 return [$name => [
276                         'id' => 'aprofile-' . $name,
277                         'class' => $class,
278                         'label' => $label,
279                         'value' => $value,
280                 ]];
281         }
282
283         private static function buildHtmlHead(array $profile, string $nickname, int $remote_contact_id)
284         {
285                 $baseUrl = DI::baseUrl();
286
287                 $htmlhead = "\n";
288
289                 if (!empty($profile['page-flags']) && $profile['page-flags'] == User::PAGE_FLAGS_COMMUNITY) {
290                         $htmlhead .= '<meta name="friendica.community" content="true" />' . "\n";
291                 }
292
293                 if (!empty($profile['openidserver'])) {
294                         $htmlhead .= '<link rel="openid.server" href="' . $profile['openidserver'] . '" />' . "\n";
295                 }
296
297                 if (!empty($profile['openid'])) {
298                         $delegate = strstr($profile['openid'], '://') ? $profile['openid'] : 'https://' . $profile['openid'];
299                         $htmlhead .= '<link rel="openid.delegate" href="' . $delegate . '" />' . "\n";
300                 }
301
302                 // site block
303                 $blocked   = !DI::userSession()->isAuthenticated() && DI::config()->get('system', 'block_public');
304                 $userblock = !DI::userSession()->isAuthenticated() && $profile['hidewall'];
305                 if (!$blocked && !$userblock) {
306                         $keywords = str_replace(['#', ',', ' ', ',,'], ['', ' ', ',', ','], $profile['pub_keywords'] ?? '');
307                         if (strlen($keywords)) {
308                                 $htmlhead .= '<meta name="keywords" content="' . $keywords . '" />' . "\n";
309                         }
310                 }
311
312                 $htmlhead .= '<meta name="dfrn-global-visibility" content="' . ($profile['net-publish'] ? 'true' : 'false') . '" />' . "\n";
313
314                 if (!$profile['net-publish']) {
315                         $htmlhead .= '<meta content="noindex, noarchive" name="robots" />' . "\n";
316                 }
317
318                 $htmlhead .= '<link rel="alternate" type="application/atom+xml" href="' . $baseUrl . '/dfrn_poll/' . $nickname . '" title="DFRN: ' . DI::l10n()->t('%s\'s timeline', $profile['name']) . '"/>' . "\n";
319                 $htmlhead .= '<link rel="alternate" type="application/atom+xml" href="' . $baseUrl . '/feed/' . $nickname . '/" title="' . DI::l10n()->t('%s\'s posts', $profile['name']) . '"/>' . "\n";
320                 $htmlhead .= '<link rel="alternate" type="application/atom+xml" href="' . $baseUrl . '/feed/' . $nickname . '/comments" title="' . DI::l10n()->t('%s\'s comments', $profile['name']) . '"/>' . "\n";
321                 $htmlhead .= '<link rel="alternate" type="application/atom+xml" href="' . $baseUrl . '/feed/' . $nickname . '/activity" title="' . DI::l10n()->t('%s\'s timeline', $profile['name']) . '"/>' . "\n";
322                 $uri = urlencode('acct:' . $profile['nickname'] . '@' . $baseUrl->getHostname() . ($baseUrl->getUrlPath() ? '/' . $baseUrl->getUrlPath() : ''));
323                 $htmlhead .= '<link rel="lrdd" type="application/xrd+xml" href="' . $baseUrl . '/xrd/?uri=' . $uri . '" />' . "\n";
324                 header('Link: <' . $baseUrl . '/xrd/?uri=' . $uri . '>; rel="lrdd"; type="application/xrd+xml"', false);
325
326                 $dfrn_pages = ['request', 'confirm', 'notify', 'poll'];
327                 foreach ($dfrn_pages as $dfrn) {
328                         $htmlhead .= '<link rel="dfrn-' . $dfrn . '" href="' . $baseUrl . '/dfrn_' . $dfrn . '/' . $nickname . '" />' . "\n";
329                 }
330
331                 return $htmlhead;
332         }
333 }