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