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