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\Content\Widget;
30 use Friendica\Core\Hook;
31 use Friendica\Core\Protocol;
32 use Friendica\Core\Renderer;
33 use Friendica\Core\Session;
34 use Friendica\Core\System;
35 use Friendica\Database\DBA;
37 use Friendica\Model\Contact;
38 use Friendica\Model\Profile as ProfileModel;
39 use Friendica\Model\Tag;
40 use Friendica\Model\User;
41 use Friendica\Module\BaseProfile;
42 use Friendica\Module\Security\Login;
43 use Friendica\Network\HTTPException;
44 use Friendica\Protocol\ActivityPub;
45 use Friendica\Util\DateTimeFormat;
46 use Friendica\Util\Temporal;
48 class Profile extends BaseProfile
50 public static function rawContent(array $parameters = [])
52 if (ActivityPub::isRequest()) {
53 $user = DBA::selectFirst('user', ['uid'], ['nickname' => $parameters['nickname']]);
54 if (DBA::isResult($user)) {
56 $data = ActivityPub\Transmitter::getProfile($user['uid']);
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 } catch (HTTPException\NotFoundException $e) {
61 System::jsonError(404, ['error' => 'Record not found']);
65 if (DBA::exists('userd', ['username' => $parameters['nickname']])) {
67 $data = ActivityPub\Transmitter::getDeletedUser($parameters['nickname']);
69 System::jsonError(410, $data);
71 // Any other case (unknown, blocked, nverified, expired, no profile, no self contact)
72 System::jsonError(404, []);
77 public static function content(array $parameters = [])
81 $profile = ProfileModel::load($a, $parameters['nickname']);
83 throw new HTTPException\NotFoundException(DI::l10n()->t('Profile not found.'));
86 $remote_contact_id = Session::getRemoteContactID($profile['uid']);
88 if (DI::config()->get('system', 'block_public') && !local_user() && !$remote_contact_id) {
92 $is_owner = local_user() == $profile['uid'];
94 if (!empty($profile['hidewall']) && !$is_owner && !$remote_contact_id) {
95 throw new HTTPException\ForbiddenException(DI::l10n()->t('Access to this profile has been restricted.'));
98 if (!empty($profile['page-flags']) && $profile['page-flags'] == User::PAGE_FLAGS_COMMUNITY) {
99 DI::page()['htmlhead'] .= '<meta name="friendica.community" content="true" />' . "\n";
102 DI::page()['htmlhead'] .= self::buildHtmlHead($profile, $parameters['nickname'], $remote_contact_id);
104 Nav::setSelected('home');
106 $is_owner = local_user() == $profile['uid'];
107 $o = self::getTabsHTML($a, 'profile', $is_owner, $profile);
109 if (!empty($profile['hidewall']) && !$is_owner && !$remote_contact_id) {
110 notice(DI::l10n()->t('Access to this profile has been restricted.'));
114 $view_as_contacts = [];
115 $view_as_contact_id = 0;
116 $view_as_contact_alert = '';
118 $view_as_contact_id = intval($_GET['viewas'] ?? 0);
120 $view_as_contacts = Contact::selectToArray(['id', 'name'], [
121 'uid' => local_user(),
122 'rel' => [Contact::FOLLOWER, Contact::SHARING, Contact::FRIEND],
123 'network' => Protocol::DFRN,
127 $view_as_contact_ids = array_column($view_as_contacts, 'id');
129 // User manually provided a contact ID they aren't privy to, silently defaulting to their own view
130 if (!in_array($view_as_contact_id, $view_as_contact_ids)) {
131 $view_as_contact_id = 0;
134 if (($key = array_search($view_as_contact_id, $view_as_contact_ids)) !== false) {
135 $view_as_contact_alert = DI::l10n()->t(
136 'You\'re currently viewing your profile as <b>%s</b> <a href="%s" class="btn btn-sm pull-right">Cancel</a>',
137 htmlentities($view_as_contacts[$key]['name'], ENT_COMPAT, 'UTF-8'),
138 'profile/' . $parameters['nickname'] . '/profile'
145 $basic_fields += self::buildField('fullname', DI::l10n()->t('Full Name:'), $profile['name']);
147 if (Feature::isEnabled($profile['uid'], 'profile_membersince')) {
148 $basic_fields += self::buildField(
150 DI::l10n()->t('Member since:'),
151 DateTimeFormat::local($profile['register_date'])
155 if (!empty($profile['dob']) && $profile['dob'] > DBA::NULL_DATE) {
156 $year_bd_format = DI::l10n()->t('j F, Y');
157 $short_bd_format = DI::l10n()->t('j F');
159 $dob = DI::l10n()->getDay(
160 intval($profile['dob']) ?
161 DateTimeFormat::utc($profile['dob'] . ' 00:00 +00:00', $year_bd_format)
162 : DateTimeFormat::utc('2001-' . substr($profile['dob'], 5) . ' 00:00 +00:00', $short_bd_format)
165 $basic_fields += self::buildField('dob', DI::l10n()->t('Birthday:'), $dob);
167 if ($age = Temporal::getAgeByTimezone($profile['dob'], $profile['timezone'])) {
168 $basic_fields += self::buildField('age', DI::l10n()->t('Age: '), DI::l10n()->tt('%d year old', '%d years old', $age));
172 if ($profile['about']) {
173 $basic_fields += self::buildField('about', DI::l10n()->t('Description:'), BBCode::convertForUriId($profile['uri-id'], $profile['about']));
176 if ($profile['xmpp']) {
177 $basic_fields += self::buildField('xmpp', DI::l10n()->t('XMPP:'), $profile['xmpp']);
180 if ($profile['matrix']) {
181 $basic_fields += self::buildField('matrix', DI::l10n()->t('Matrix:'), $profile['matrix']);
184 if ($profile['homepage']) {
185 $basic_fields += self::buildField('homepage', DI::l10n()->t('Homepage:'), HTML::toLink($profile['homepage']));
190 || $profile['locality']
191 || $profile['postal-code']
192 || $profile['region']
193 || $profile['country-name']
195 $basic_fields += self::buildField('location', DI::l10n()->t('Location:'), ProfileModel::formatLocation($profile));
198 if ($profile['pub_keywords']) {
200 // Separator is defined in Module\Settings\Profile\Index::cleanKeywords
201 foreach (explode(', ', $profile['pub_keywords']) as $tag_label) {
203 'url' => '/search?tag=' . $tag_label,
204 'label' => Tag::TAG_CHARACTER[Tag::HASHTAG] . $tag_label,
208 $basic_fields += self::buildField('pub_keywords', DI::l10n()->t('Tags:'), $tags);
213 // Defaults to the current logged in user self contact id to show self-only fields
214 $contact_id = $view_as_contact_id ?: $remote_contact_id ?: 0;
216 if ($is_owner && $contact_id === 0) {
217 $profile_fields = DI::profileField()->selectByUserId($profile['uid']);
219 $profile_fields = DI::profileField()->selectByContactId($contact_id, $profile['uid']);
222 foreach ($profile_fields as $profile_field) {
223 $custom_fields += self::buildField(
224 'custom_' . $profile_field->order,
225 $profile_field->label,
226 BBCode::convertForUriId($profile['uri-id'], $profile_field->value),
231 //show subcribed forum if it is enabled in the usersettings
232 if (Feature::isEnabled($profile['uid'], 'forumlist_profile')) {
233 $custom_fields += self::buildField(
235 DI::l10n()->t('Forums:'),
236 ForumManager::profileAdvanced($profile['uid'])
240 $tpl = Renderer::getMarkupTemplate('profile/profile.tpl');
241 $o .= Renderer::replaceMacros($tpl, [
242 '$title' => DI::l10n()->t('Profile'),
243 '$yourself' => DI::l10n()->t('Yourself'),
244 '$view_as_contacts' => $view_as_contacts,
245 '$view_as_contact_id' => $view_as_contact_id,
246 '$view_as_contact_alert' => $view_as_contact_alert,
247 '$view_as' => DI::l10n()->t('View profile as:'),
248 '$submit' => DI::l10n()->t('Submit'),
249 '$basic' => DI::l10n()->t('Basic'),
250 '$advanced' => DI::l10n()->t('Advanced'),
251 '$is_owner' => $profile['uid'] == local_user(),
252 '$query_string' => DI::args()->getQueryString(),
253 '$basic_fields' => $basic_fields,
254 '$custom_fields' => $custom_fields,
255 '$profile' => $profile,
257 'url' => DI::baseUrl() . '/settings/profile', DI::l10n()->t('Edit profile'),
259 'label' => DI::l10n()->t('Edit profile')
262 'url' => DI::args()->getQueryString() . '#viewas',
264 'label' => DI::l10n()->t('View as')
268 Hook::callAll('profile_advanced', $o);
274 * Creates a profile field structure to be used in the profile template
276 * @param string $name Arbitrary name of the field
277 * @param string $label Display label of the field
278 * @param mixed $value Display value of the field
279 * @param string $class Optional CSS class to apply to the field
282 private static function buildField(string $name, string $label, $value, string $class = 'aprofile')
285 'id' => 'aprofile-' . $name,
292 private static function buildHtmlHead(array $profile, string $nickname, int $remote_contact_id)
294 $baseUrl = DI::baseUrl();
298 if (!empty($profile['page-flags']) && $profile['page-flags'] == User::PAGE_FLAGS_COMMUNITY) {
299 $htmlhead .= '<meta name="friendica.community" content="true" />' . "\n";
302 if (!empty($profile['openidserver'])) {
303 $htmlhead .= '<link rel="openid.server" href="' . $profile['openidserver'] . '" />' . "\n";
306 if (!empty($profile['openid'])) {
307 $delegate = strstr($profile['openid'], '://') ? $profile['openid'] : 'https://' . $profile['openid'];
308 $htmlhead .= '<link rel="openid.delegate" href="' . $delegate . '" />' . "\n";
312 $blocked = !local_user() && !$remote_contact_id && DI::config()->get('system', 'block_public');
313 $userblock = !local_user() && !$remote_contact_id && $profile['hidewall'];
314 if (!$blocked && !$userblock) {
315 $keywords = str_replace(['#', ',', ' ', ',,'], ['', ' ', ',', ','], $profile['pub_keywords'] ?? '');
316 if (strlen($keywords)) {
317 $htmlhead .= '<meta name="keywords" content="' . $keywords . '" />' . "\n";
321 $htmlhead .= '<meta name="dfrn-global-visibility" content="' . ($profile['net-publish'] ? 'true' : 'false') . '" />' . "\n";
323 if (!$profile['net-publish']) {
324 $htmlhead .= '<meta content="noindex, noarchive" name="robots" />' . "\n";
327 $htmlhead .= '<link rel="alternate" type="application/atom+xml" href="' . $baseUrl . '/dfrn_poll/' . $nickname . '" title="DFRN: ' . DI::l10n()->t('%s\'s timeline', $profile['name']) . '"/>' . "\n";
328 $htmlhead .= '<link rel="alternate" type="application/atom+xml" href="' . $baseUrl . '/feed/' . $nickname . '/" title="' . DI::l10n()->t('%s\'s posts', $profile['name']) . '"/>' . "\n";
329 $htmlhead .= '<link rel="alternate" type="application/atom+xml" href="' . $baseUrl . '/feed/' . $nickname . '/comments" title="' . DI::l10n()->t('%s\'s comments', $profile['name']) . '"/>' . "\n";
330 $htmlhead .= '<link rel="alternate" type="application/atom+xml" href="' . $baseUrl . '/feed/' . $nickname . '/activity" title="' . DI::l10n()->t('%s\'s timeline', $profile['name']) . '"/>' . "\n";
331 $uri = urlencode('acct:' . $profile['nickname'] . '@' . $baseUrl->getHostname() . ($baseUrl->getUrlPath() ? '/' . $baseUrl->getUrlPath() : ''));
332 $htmlhead .= '<link rel="lrdd" type="application/xrd+xml" href="' . $baseUrl . '/xrd/?uri=' . $uri . '" />' . "\n";
333 header('Link: <' . $baseUrl . '/xrd/?uri=' . $uri . '>; rel="lrdd"; type="application/xrd+xml"', false);
335 $dfrn_pages = ['request', 'confirm', 'notify', 'poll'];
336 foreach ($dfrn_pages as $dfrn) {
337 $htmlhead .= '<link rel="dfrn-' . $dfrn . '" href="' . $baseUrl . '/dfrn_' . $dfrn . '/' . $nickname . '" />' . "\n";
339 $htmlhead .= '<link rel="dfrn-poco" href="' . $baseUrl . '/poco/' . $nickname . '" />' . "\n";