3 * @copyright Copyright (C) 2020, Friendica
5 * @license GNU AGPL version 3 or any later version
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.
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.
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/>.
22 namespace Friendica\Module\Profile;
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;
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;
47 class Profile extends BaseProfile
49 public static function rawContent(array $parameters = [])
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']);
57 System::jsonExit($data, 'application/activity+json');
61 if (DBA::exists('userd', ['username' => $parameters['nickname']])) {
63 $data = ActivityPub\Transmitter::getDeletedUser($parameters['nickname']);
65 System::jsonError(410, $data);
67 // Any other case (unknown, blocked, nverified, expired, no profile, no self contact)
68 System::jsonError(404, []);
73 public static function content(array $parameters = [])
77 ProfileModel::load($a, $parameters['nickname']);
80 throw new HTTPException\NotFoundException(DI::l10n()->t('Profile not found.'));
83 $remote_contact_id = Session::getRemoteContactID($a->profile_uid);
85 if (DI::config()->get('system', 'block_public') && !local_user() && !$remote_contact_id) {
89 $is_owner = local_user() == $a->profile_uid;
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.'));
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";
99 DI::page()['htmlhead'] .= self::buildHtmlHead($a->profile, $parameters['nickname'], $remote_contact_id);
101 Nav::setSelected('home');
103 $is_owner = local_user() == $a->profile['uid'];
104 $o = self::getTabsHTML($a, 'profile', $is_owner, $a->profile['nickname']);
106 if (!empty($a->profile['hidewall']) && !$is_owner && !$remote_contact_id) {
107 notice(DI::l10n()->t('Access to this profile has been restricted.'));
111 $view_as_contacts = [];
112 $view_as_contact_id = 0;
114 $view_as_contact_id = intval($_GET['viewas'] ?? 0);
116 $view_as_contacts = Contact::selectToArray(['id', 'name'], [
117 'uid' => local_user(),
118 'rel' => [Contact::FOLLOWER, Contact::SHARING, Contact::FRIEND],
119 'network' => Protocol::DFRN,
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;
131 $basic_fields += self::buildField('fullname', DI::l10n()->t('Full Name:'), $a->profile['name']);
133 if (Feature::isEnabled($a->profile_uid, 'profile_membersince')) {
134 $basic_fields += self::buildField(
136 DI::l10n()->t('Member since:'),
137 DateTimeFormat::local($a->profile['register_date'])
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');
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)
151 $basic_fields += self::buildField('dob', DI::l10n()->t('Birthday:'), $dob);
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));
158 if ($a->profile['about']) {
159 $basic_fields += self::buildField('about', DI::l10n()->t('Description:'), BBCode::convert($a->profile['about']));
162 if ($a->profile['xmpp']) {
163 $basic_fields += self::buildField('xmpp', DI::l10n()->t('XMPP:'), $a->profile['xmpp']);
166 if ($a->profile['homepage']) {
167 $basic_fields += self::buildField('homepage', DI::l10n()->t('Homepage:'), HTML::toLink($a->profile['homepage']));
171 $a->profile['address']
172 || $a->profile['locality']
173 || $a->profile['postal-code']
174 || $a->profile['region']
175 || $a->profile['country-name']
177 $basic_fields += self::buildField('location', DI::l10n()->t('Location:'), ProfileModel::formatLocation($a->profile));
180 if ($a->profile['pub_keywords']) {
182 foreach (explode(',', $a->profile['pub_keywords']) as $tag_label) {
184 'url' => '/search?tag=' . $tag_label,
185 'label' => Term::TAG_CHARACTER[Term::HASHTAG] . $tag_label,
189 $basic_fields += self::buildField('pub_keywords', DI::l10n()->t('Tags:'), $tags);
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;
197 if ($is_owner && $contact_id === 0) {
198 $profile_fields = DI::profileField()->selectByUserId($a->profile_uid);
200 $profile_fields = DI::profileField()->selectByContactId($contact_id, $a->profile_uid);
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),
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(
216 DI::l10n()->t('Forums:'),
217 ForumManager::profileAdvanced($a->profile_uid)
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,
235 'url' => DI::baseUrl() . '/settings/profile', DI::l10n()->t('Edit profile'),
237 'label' => DI::l10n()->t('Edit profile')
241 Hook::callAll('profile_advanced', $o);
247 * Creates a profile field structure to be used in the profile template
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
255 private static function buildField(string $name, string $label, $value, string $class = 'aprofile')
258 'id' => 'aprofile-' . $name,
265 private static function buildHtmlHead(array $profile, string $nickname, int $remote_contact_id)
267 $baseUrl = DI::baseUrl();
271 if (!empty($profile['page-flags']) && $profile['page-flags'] == User::PAGE_FLAGS_COMMUNITY) {
272 $htmlhead .= '<meta name="friendica.community" content="true" />' . "\n";
275 if (!empty($profile['openidserver'])) {
276 $htmlhead .= '<link rel="openid.server" href="' . $profile['openidserver'] . '" />' . "\n";
279 if (!empty($profile['openid'])) {
280 $delegate = strstr($profile['openid'], '://') ? $profile['openid'] : 'https://' . $profile['openid'];
281 $htmlhead .= '<link rel="openid.delegate" href="' . $delegate . '" />' . "\n";
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";
294 $htmlhead .= '<meta name="dfrn-global-visibility" content="' . ($profile['net-publish'] ? 'true' : 'false') . '" />' . "\n";
296 if (!$profile['net-publish'] || $profile['hidewall']) {
297 $htmlhead .= '<meta content="noindex, noarchive" name="robots" />' . "\n";
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);
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";
312 $htmlhead .= '<link rel="dfrn-poco" href="' . $baseUrl . '/poco/' . $nickname . '" />' . "\n";