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