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