3 namespace Friendica\Module\Profile;
5 use Friendica\BaseModule;
6 use Friendica\Content\Feature;
7 use Friendica\Content\ForumManager;
8 use Friendica\Content\Nav;
9 use Friendica\Content\Text\BBCode;
10 use Friendica\Content\Text\HTML;
11 use Friendica\Core\Hook;
12 use Friendica\Core\Protocol;
13 use Friendica\Core\Renderer;
14 use Friendica\Core\Session;
15 use Friendica\Core\System;
16 use Friendica\Database\DBA;
18 use Friendica\Model\Contact;
19 use Friendica\Model\Profile;
20 use Friendica\Model\Term;
21 use Friendica\Model\User;
22 use Friendica\Module\Security\Login;
23 use Friendica\Network\HTTPException;
24 use Friendica\Protocol\ActivityPub;
25 use Friendica\Util\DateTimeFormat;
26 use Friendica\Util\Temporal;
28 class Index extends BaseModule
30 public static function rawContent(array $parameters = [])
32 if (ActivityPub::isRequest()) {
33 $user = DBA::selectFirst('user', ['uid'], ['nickname' => $parameters['nickname']]);
34 if (DBA::isResult($user)) {
35 // The function returns an empty array when the account is removed, expired or blocked
36 $data = ActivityPub\Transmitter::getProfile($user['uid']);
38 System::jsonExit($data, 'application/activity+json');
42 if (DBA::exists('userd', ['username' => $parameters['nickname']])) {
44 $data = ActivityPub\Transmitter::getDeletedUser($parameters['nickname']);
46 System::jsonError(410, $data);
48 // Any other case (unknown, blocked, nverified, expired, no profile, no self contact)
49 System::jsonError(404, []);
54 public static function content(array $parameters = [])
58 Profile::load($a, $parameters['nickname']);
61 throw new HTTPException\NotFoundException(DI::l10n()->t('Profile not found.'));
64 $remote_contact_id = Session::getRemoteContactID($a->profile_uid);
66 if (DI::config()->get('system', 'block_public') && !local_user() && !$remote_contact_id) {
70 $is_owner = local_user() == $a->profile_uid;
72 if (!empty($a->profile['hidewall']) && !$is_owner && !$remote_contact_id) {
73 throw new HTTPException\ForbiddenException(DI::l10n()->t('Access to this profile has been restricted.'));
76 if (!empty($a->profile['page-flags']) && $a->profile['page-flags'] == User::PAGE_FLAGS_COMMUNITY) {
77 DI::page()['htmlhead'] .= '<meta name="friendica.community" content="true" />' . "\n";
80 DI::page()['htmlhead'] .= self::buildHtmlHead($a->profile, $parameters['nickname'], $remote_contact_id);
82 Nav::setSelected('home');
84 $is_owner = local_user() == $a->profile['uid'];
85 $o = Profile::getTabs($a, 'profile', $is_owner, $a->profile['nickname']);
87 if (!empty($a->profile['hidewall']) && !$is_owner && !$remote_contact_id) {
88 notice(DI::l10n()->t('Access to this profile has been restricted.'));
92 $view_as_contacts = [];
93 $view_as_contact_id = 0;
95 $view_as_contact_id = intval($_GET['viewas'] ?? 0);
97 $view_as_contacts = Contact::selectToArray(['id', 'name'], [
98 'uid' => local_user(),
99 'rel' => [Contact::FOLLOWER, Contact::SHARING, Contact::FRIEND],
100 'network' => Protocol::DFRN,
104 // User manually provided a contact ID they aren't privy to, silently defaulting to their own view
105 if (!in_array($view_as_contact_id, array_column($view_as_contacts, 'id'))) {
106 $view_as_contact_id = 0;
112 $basic_fields += self::buildField('fullname', DI::l10n()->t('Full Name:'), $a->profile['name']);
114 if (Feature::isEnabled($a->profile_uid, 'profile_membersince')) {
115 $basic_fields += self::buildField(
117 DI::l10n()->t('Member since:'),
118 DateTimeFormat::local($a->profile['register_date'])
122 if (!empty($a->profile['dob']) && $a->profile['dob'] > DBA::NULL_DATE) {
123 $year_bd_format = DI::l10n()->t('j F, Y');
124 $short_bd_format = DI::l10n()->t('j F');
126 $dob = DI::l10n()->getDay(
127 intval($a->profile['dob']) ?
128 DateTimeFormat::utc($a->profile['dob'] . ' 00:00 +00:00', $year_bd_format)
129 : DateTimeFormat::utc('2001-' . substr($a->profile['dob'], 5) . ' 00:00 +00:00', $short_bd_format)
132 $basic_fields += self::buildField('dob', DI::l10n()->t('Birthday:'), $dob);
134 if ($age = Temporal::getAgeByTimezone($a->profile['dob'], $a->profile['timezone'])) {
135 $basic_fields += self::buildField('age', DI::l10n()->t('Age: '), DI::l10n()->tt('%d year old', '%d years old', $age));
139 if ($a->profile['pdesc']) {
140 $basic_fields += self::buildField('pdesc', DI::l10n()->t('Description:'), HTML::toLink($a->profile['pdesc']));
143 if ($a->profile['xmpp']) {
144 $basic_fields += self::buildField('xmpp', DI::l10n()->t('XMPP:'), $a->profile['xmpp']);
147 if ($a->profile['homepage']) {
148 $basic_fields += self::buildField('homepage', DI::l10n()->t('Homepage:'), HTML::toLink($a->profile['homepage']));
152 $a->profile['address']
153 || $a->profile['locality']
154 || $a->profile['postal-code']
155 || $a->profile['region']
156 || $a->profile['country-name']
158 $basic_fields += self::buildField('location', DI::l10n()->t('Location:'), Profile::formatLocation($a->profile));
161 if ($a->profile['pub_keywords']) {
163 foreach (explode(',', $a->profile['pub_keywords']) as $tag_label) {
165 'url' => '/search?tag=' . $tag_label,
166 'label' => Term::TAG_CHARACTER[Term::HASHTAG] . $tag_label,
170 $basic_fields += self::buildField('pub_keywords', DI::l10n()->t('Tags:'), $tags);
175 // Defaults to the current logged in user self contact id to show self-only fields
176 $contact_id = $view_as_contact_id ?: $remote_contact_id ?: 0;
178 if ($is_owner && $contact_id === 0) {
179 $profile_fields = DI::profileField()->selectByUserId($a->profile_uid);
181 $profile_fields = DI::profileField()->selectByContactId($contact_id, $a->profile_uid);
184 foreach ($profile_fields as $profile_field) {
185 $custom_fields += self::buildField(
186 'custom_' . $profile_field->order,
187 $profile_field->label,
188 BBCode::convert($profile_field->value),
193 //show subcribed forum if it is enabled in the usersettings
194 if (Feature::isEnabled($a->profile_uid, 'forumlist_profile')) {
195 $custom_fields += self::buildField(
197 DI::l10n()->t('Forums:'),
198 ForumManager::profileAdvanced($a->profile_uid)
202 $tpl = Renderer::getMarkupTemplate('profile/index.tpl');
203 $o .= Renderer::replaceMacros($tpl, [
204 '$title' => DI::l10n()->t('Profile'),
205 '$view_as_contacts' => $view_as_contacts,
206 '$view_as_contact_id' => $view_as_contact_id,
207 '$view_as' => DI::l10n()->t('View profile as:'),
208 '$basic' => DI::l10n()->t('Basic'),
209 '$advanced' => DI::l10n()->t('Advanced'),
210 '$is_owner' => $a->profile_uid == local_user(),
211 '$query_string' => DI::args()->getQueryString(),
212 '$basic_fields' => $basic_fields,
213 '$custom_fields' => $custom_fields,
214 '$profile' => $a->profile,
216 'url' => DI::baseUrl() . '/settings/profile', DI::l10n()->t('Edit profile'),
218 'label' => DI::l10n()->t('Edit profile')
222 Hook::callAll('profile_advanced', $o);
228 * Creates a profile field structure to be used in the profile template
230 * @param string $name Arbitrary name of the field
231 * @param string $label Display label of the field
232 * @param mixed $value Display value of the field
233 * @param string $class Optional CSS class to apply to the field
236 private static function buildField(string $name, string $label, $value, string $class = 'aprofile')
239 'id' => 'aprofile-' . $name,
246 private static function buildHtmlHead(array $profile, string $nickname, int $remote_contact_id)
248 $baseUrl = DI::baseUrl();
252 if (!empty($profile['page-flags']) && $profile['page-flags'] == User::PAGE_FLAGS_COMMUNITY) {
253 $htmlhead .= '<meta name="friendica.community" content="true" />' . "\n";
256 if (!empty($profile['openidserver'])) {
257 $htmlhead .= '<link rel="openid.server" href="' . $profile['openidserver'] . '" />' . "\n";
260 if (!empty($profile['openid'])) {
261 $delegate = strstr($profile['openid'], '://') ? $profile['openid'] : 'https://' . $profile['openid'];
262 $htmlhead .= '<link rel="openid.delegate" href="' . $delegate . '" />' . "\n";
266 $blocked = !local_user() && !$remote_contact_id && DI::config()->get('system', 'block_public');
267 $userblock = !local_user() && !$remote_contact_id && $profile['hidewall'];
268 if (!$blocked && !$userblock) {
269 $keywords = str_replace(['#', ',', ' ', ',,'], ['', ' ', ',', ','], $profile['pub_keywords'] ?? '');
270 if (strlen($keywords)) {
271 $htmlhead .= '<meta name="keywords" content="' . $keywords . '" />' . "\n";
275 $htmlhead .= '<meta name="dfrn-global-visibility" content="' . ($profile['net-publish'] ? 'true' : 'false') . '" />' . "\n";
277 if (!$profile['net-publish'] || $profile['hidewall']) {
278 $htmlhead .= '<meta content="noindex, noarchive" name="robots" />' . "\n";
281 $htmlhead .= '<link rel="alternate" type="application/atom+xml" href="' . $baseUrl . '/dfrn_poll/' . $nickname . '" title="DFRN: ' . DI::l10n()->t('%s\'s timeline', $profile['username']) . '"/>' . "\n";
282 $htmlhead .= '<link rel="alternate" type="application/atom+xml" href="' . $baseUrl . '/feed/' . $nickname . '/" title="' . DI::l10n()->t('%s\'s posts', $profile['username']) . '"/>' . "\n";
283 $htmlhead .= '<link rel="alternate" type="application/atom+xml" href="' . $baseUrl . '/feed/' . $nickname . '/comments" title="' . DI::l10n()->t('%s\'s comments', $profile['username']) . '"/>' . "\n";
284 $htmlhead .= '<link rel="alternate" type="application/atom+xml" href="' . $baseUrl . '/feed/' . $nickname . '/activity" title="' . DI::l10n()->t('%s\'s timeline', $profile['username']) . '"/>' . "\n";
285 $uri = urlencode('acct:' . $profile['nickname'] . '@' . $baseUrl->getHostname() . ($baseUrl->getUrlPath() ? '/' . $baseUrl->getUrlPath() : ''));
286 $htmlhead .= '<link rel="lrdd" type="application/xrd+xml" href="' . $baseUrl . '/xrd/?uri=' . $uri . '" />' . "\n";
287 header('Link: <' . $baseUrl . '/xrd/?uri=' . $uri . '>; rel="lrdd"; type="application/xrd+xml"', false);
289 $dfrn_pages = ['request', 'confirm', 'notify', 'poll'];
290 foreach ($dfrn_pages as $dfrn) {
291 $htmlhead .= '<link rel="dfrn-' . $dfrn . '" href="' . $baseUrl . '/dfrn_' . $dfrn . '/' . $nickname . '" />' . "\n";
293 $htmlhead .= '<link rel="dfrn-poco" href="' . $baseUrl . '/poco/' . $nickname . '" />' . "\n";