3 * @copyright Copyright (C) 2010-2021, the Friendica project
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\Tag;
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 function rawContent()
51 if (ActivityPub::isRequest()) {
52 $user = DBA::selectFirst('user', ['uid'], ['nickname' => $this->parameters['nickname']]);
53 if (DBA::isResult($user)) {
55 $data = ActivityPub\Transmitter::getProfile($user['uid']);
56 header('Access-Control-Allow-Origin: *');
57 header('Cache-Control: max-age=23200, stale-while-revalidate=23200');
58 System::jsonExit($data, 'application/activity+json');
59 } catch (HTTPException\NotFoundException $e) {
60 System::jsonError(404, ['error' => 'Record not found']);
64 if (DBA::exists('userd', ['username' => $this->parameters['nickname']])) {
66 $data = ActivityPub\Transmitter::getDeletedUser($this->parameters['nickname']);
68 System::jsonError(410, $data);
70 // Any other case (unknown, blocked, nverified, expired, no profile, no self contact)
71 System::jsonError(404, []);
76 public function content(): string
80 $profile = ProfileModel::load($a, $this->parameters['nickname']);
82 throw new HTTPException\NotFoundException(DI::l10n()->t('Profile not found.'));
85 $remote_contact_id = Session::getRemoteContactID($profile['uid']);
87 if (DI::config()->get('system', 'block_public') && !local_user() && !$remote_contact_id) {
91 $is_owner = local_user() == $profile['uid'];
93 if (!empty($profile['hidewall']) && !$is_owner && !$remote_contact_id) {
94 throw new HTTPException\ForbiddenException(DI::l10n()->t('Access to this profile has been restricted.'));
97 if (!empty($profile['page-flags']) && $profile['page-flags'] == User::PAGE_FLAGS_COMMUNITY) {
98 DI::page()['htmlhead'] .= '<meta name="friendica.community" content="true" />' . "\n";
101 DI::page()['htmlhead'] .= self::buildHtmlHead($profile, $this->parameters['nickname'], $remote_contact_id);
103 Nav::setSelected('home');
105 $is_owner = local_user() == $profile['uid'];
106 $o = self::getTabsHTML($a, 'profile', $is_owner, $profile['nickname'], $profile['hide-friends']);
108 if (!empty($profile['hidewall']) && !$is_owner && !$remote_contact_id) {
109 notice(DI::l10n()->t('Access to this profile has been restricted.'));
113 $view_as_contacts = [];
114 $view_as_contact_id = 0;
115 $view_as_contact_alert = '';
117 $view_as_contact_id = intval($_GET['viewas'] ?? 0);
119 $view_as_contacts = Contact::selectToArray(['id', 'name'], [
120 'uid' => local_user(),
121 'rel' => [Contact::FOLLOWER, Contact::SHARING, Contact::FRIEND],
122 'network' => Protocol::DFRN,
126 $view_as_contact_ids = array_column($view_as_contacts, 'id');
128 // User manually provided a contact ID they aren't privy to, silently defaulting to their own view
129 if (!in_array($view_as_contact_id, $view_as_contact_ids)) {
130 $view_as_contact_id = 0;
133 if (($key = array_search($view_as_contact_id, $view_as_contact_ids)) !== false) {
134 $view_as_contact_alert = DI::l10n()->t(
135 'You\'re currently viewing your profile as <b>%s</b> <a href="%s" class="btn btn-sm pull-right">Cancel</a>',
136 htmlentities($view_as_contacts[$key]['name'], ENT_COMPAT, 'UTF-8'),
137 'profile/' . $this->parameters['nickname'] . '/profile'
144 $basic_fields += self::buildField('fullname', DI::l10n()->t('Full Name:'), $profile['name']);
146 if (Feature::isEnabled($profile['uid'], 'profile_membersince')) {
147 $basic_fields += self::buildField(
149 DI::l10n()->t('Member since:'),
150 DateTimeFormat::local($profile['register_date'])
154 if (!empty($profile['dob']) && $profile['dob'] > DBA::NULL_DATE) {
155 $year_bd_format = DI::l10n()->t('j F, Y');
156 $short_bd_format = DI::l10n()->t('j F');
158 $dob = DI::l10n()->getDay(
159 intval($profile['dob']) ?
160 DateTimeFormat::utc($profile['dob'] . ' 00:00 +00:00', $year_bd_format)
161 : DateTimeFormat::utc('2001-' . substr($profile['dob'], 5) . ' 00:00 +00:00', $short_bd_format)
164 $basic_fields += self::buildField('dob', DI::l10n()->t('Birthday:'), $dob);
166 if ($age = Temporal::getAgeByTimezone($profile['dob'], $profile['timezone'])) {
167 $basic_fields += self::buildField('age', DI::l10n()->t('Age: '), DI::l10n()->tt('%d year old', '%d years old', $age));
171 if ($profile['about']) {
172 $basic_fields += self::buildField('about', DI::l10n()->t('Description:'), BBCode::convertForUriId($profile['uri-id'], $profile['about']));
175 if ($profile['xmpp']) {
176 $basic_fields += self::buildField('xmpp', DI::l10n()->t('XMPP:'), $profile['xmpp']);
179 if ($profile['matrix']) {
180 $basic_fields += self::buildField('matrix', DI::l10n()->t('Matrix:'), $profile['matrix']);
183 if ($profile['homepage']) {
184 $basic_fields += self::buildField('homepage', DI::l10n()->t('Homepage:'), HTML::toLink($profile['homepage']));
189 || $profile['locality']
190 || $profile['postal-code']
191 || $profile['region']
192 || $profile['country-name']
194 $basic_fields += self::buildField('location', DI::l10n()->t('Location:'), ProfileModel::formatLocation($profile));
197 if ($profile['pub_keywords']) {
199 // Separator is defined in Module\Settings\Profile\Index::cleanKeywords
200 foreach (explode(', ', $profile['pub_keywords']) as $tag_label) {
202 'url' => '/search?tag=' . $tag_label,
203 'label' => Tag::TAG_CHARACTER[Tag::HASHTAG] . $tag_label,
207 $basic_fields += self::buildField('pub_keywords', DI::l10n()->t('Tags:'), $tags);
212 // Defaults to the current logged in user self contact id to show self-only fields
213 $contact_id = $view_as_contact_id ?: $remote_contact_id ?: 0;
215 if ($is_owner && $contact_id === 0) {
216 $profile_fields = DI::profileField()->selectByUserId($profile['uid']);
218 $profile_fields = DI::profileField()->selectByContactId($contact_id, $profile['uid']);
221 foreach ($profile_fields as $profile_field) {
222 $custom_fields += self::buildField(
223 'custom_' . $profile_field->order,
224 $profile_field->label,
225 BBCode::convertForUriId($profile['uri-id'], $profile_field->value),
230 //show subcribed forum if it is enabled in the usersettings
231 if (Feature::isEnabled($profile['uid'], 'forumlist_profile')) {
232 $custom_fields += self::buildField(
234 DI::l10n()->t('Forums:'),
235 ForumManager::profileAdvanced($profile['uid'])
239 $tpl = Renderer::getMarkupTemplate('profile/profile.tpl');
240 $o .= Renderer::replaceMacros($tpl, [
241 '$title' => DI::l10n()->t('Profile'),
242 '$yourself' => DI::l10n()->t('Yourself'),
243 '$view_as_contacts' => $view_as_contacts,
244 '$view_as_contact_id' => $view_as_contact_id,
245 '$view_as_contact_alert' => $view_as_contact_alert,
246 '$view_as' => DI::l10n()->t('View profile as:'),
247 '$submit' => DI::l10n()->t('Submit'),
248 '$basic' => DI::l10n()->t('Basic'),
249 '$advanced' => DI::l10n()->t('Advanced'),
250 '$is_owner' => $profile['uid'] == local_user(),
251 '$query_string' => DI::args()->getQueryString(),
252 '$basic_fields' => $basic_fields,
253 '$custom_fields' => $custom_fields,
254 '$profile' => $profile,
256 'url' => DI::baseUrl() . '/settings/profile', DI::l10n()->t('Edit profile'),
258 'label' => DI::l10n()->t('Edit profile')
261 'url' => DI::args()->getQueryString() . '#viewas',
263 'label' => DI::l10n()->t('View as')
267 Hook::callAll('profile_advanced', $o);
273 * Creates a profile field structure to be used in the profile template
275 * @param string $name Arbitrary name of the field
276 * @param string $label Display label of the field
277 * @param mixed $value Display value of the field
278 * @param string $class Optional CSS class to apply to the field
281 private static function buildField(string $name, string $label, $value, string $class = 'aprofile')
284 'id' => 'aprofile-' . $name,
291 private static function buildHtmlHead(array $profile, string $nickname, int $remote_contact_id)
293 $baseUrl = DI::baseUrl();
297 if (!empty($profile['page-flags']) && $profile['page-flags'] == User::PAGE_FLAGS_COMMUNITY) {
298 $htmlhead .= '<meta name="friendica.community" content="true" />' . "\n";
301 if (!empty($profile['openidserver'])) {
302 $htmlhead .= '<link rel="openid.server" href="' . $profile['openidserver'] . '" />' . "\n";
305 if (!empty($profile['openid'])) {
306 $delegate = strstr($profile['openid'], '://') ? $profile['openid'] : 'https://' . $profile['openid'];
307 $htmlhead .= '<link rel="openid.delegate" href="' . $delegate . '" />' . "\n";
311 $blocked = !local_user() && !$remote_contact_id && DI::config()->get('system', 'block_public');
312 $userblock = !local_user() && !$remote_contact_id && $profile['hidewall'];
313 if (!$blocked && !$userblock) {
314 $keywords = str_replace(['#', ',', ' ', ',,'], ['', ' ', ',', ','], $profile['pub_keywords'] ?? '');
315 if (strlen($keywords)) {
316 $htmlhead .= '<meta name="keywords" content="' . $keywords . '" />' . "\n";
320 $htmlhead .= '<meta name="dfrn-global-visibility" content="' . ($profile['net-publish'] ? 'true' : 'false') . '" />' . "\n";
322 if (!$profile['net-publish']) {
323 $htmlhead .= '<meta content="noindex, noarchive" name="robots" />' . "\n";
326 $htmlhead .= '<link rel="alternate" type="application/atom+xml" href="' . $baseUrl . '/dfrn_poll/' . $nickname . '" title="DFRN: ' . DI::l10n()->t('%s\'s timeline', $profile['name']) . '"/>' . "\n";
327 $htmlhead .= '<link rel="alternate" type="application/atom+xml" href="' . $baseUrl . '/feed/' . $nickname . '/" title="' . DI::l10n()->t('%s\'s posts', $profile['name']) . '"/>' . "\n";
328 $htmlhead .= '<link rel="alternate" type="application/atom+xml" href="' . $baseUrl . '/feed/' . $nickname . '/comments" title="' . DI::l10n()->t('%s\'s comments', $profile['name']) . '"/>' . "\n";
329 $htmlhead .= '<link rel="alternate" type="application/atom+xml" href="' . $baseUrl . '/feed/' . $nickname . '/activity" title="' . DI::l10n()->t('%s\'s timeline', $profile['name']) . '"/>' . "\n";
330 $uri = urlencode('acct:' . $profile['nickname'] . '@' . $baseUrl->getHostname() . ($baseUrl->getUrlPath() ? '/' . $baseUrl->getUrlPath() : ''));
331 $htmlhead .= '<link rel="lrdd" type="application/xrd+xml" href="' . $baseUrl . '/xrd/?uri=' . $uri . '" />' . "\n";
332 header('Link: <' . $baseUrl . '/xrd/?uri=' . $uri . '>; rel="lrdd"; type="application/xrd+xml"', false);
334 $dfrn_pages = ['request', 'confirm', 'notify', 'poll'];
335 foreach ($dfrn_pages as $dfrn) {
336 $htmlhead .= '<link rel="dfrn-' . $dfrn . '" href="' . $baseUrl . '/dfrn_' . $dfrn . '/' . $nickname . '" />' . "\n";
338 $htmlhead .= '<link rel="dfrn-poco" href="' . $baseUrl . '/poco/' . $nickname . '" />' . "\n";