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 require_once 'boot.php';
30 class Index extends BaseModule
32 public static function rawContent(array $parameters = [])
34 if (ActivityPub::isRequest()) {
35 $user = DBA::selectFirst('user', ['uid'], ['nickname' => $parameters['nickname']]);
36 if (DBA::isResult($user)) {
37 // The function returns an empty array when the account is removed, expired or blocked
38 $data = ActivityPub\Transmitter::getProfile($user['uid']);
40 System::jsonExit($data, 'application/activity+json');
44 if (DBA::exists('userd', ['username' => $parameters['nickname']])) {
46 $data = ActivityPub\Transmitter::getDeletedUser($parameters['nickname']);
48 System::jsonError(410, $data);
50 // Any other case (unknown, blocked, nverified, expired, no profile, no self contact)
51 System::jsonError(404, []);
56 public static function content(array $parameters = [])
60 Profile::load($a, $parameters['nickname']);
63 throw new HTTPException\NotFoundException(DI::l10n()->t('Profile not found.'));
66 $remote_contact_id = Session::getRemoteContactID($a->profile_uid);
68 if (DI::config()->get('system', 'block_public') && !local_user() && !$remote_contact_id) {
72 $is_owner = local_user() == $a->profile_uid;
75 if (!empty($a->profile['hidewall']) && !$is_owner && !$remote_contact_id) {
76 throw new HTTPException\ForbiddenException(DI::l10n()->t('Access to this profile has been restricted.'));
79 if (!empty($a->profile['page-flags']) && $a->profile['page-flags'] == User::PAGE_FLAGS_COMMUNITY) {
80 DI::page()['htmlhead'] .= '<meta name="friendica.community" content="true" />' . "\n";
83 DI::page()['htmlhead'] .= self::buildHtmlHead($a->profile, $parameters['nickname'], $remote_contact_id);
85 Nav::setSelected('home');
87 $is_owner = local_user() == $a->profile['uid'];
88 $o = Profile::getTabs($a, 'profile', $is_owner, $a->profile['nickname']);
90 if (!empty($a->profile['hidewall']) && !$is_owner && !$remote_contact_id) {
91 notice(DI::l10n()->t('Access to this profile has been restricted.'));
95 $view_as_contacts = [];
96 $view_as_contact_id = 0;
98 $view_as_contact_id = intval($_GET['viewas'] ?? 0);
100 $view_as_contacts = Contact::selectToArray(['id', 'name'], [
101 'uid' => local_user(),
102 'rel' => [Contact::FOLLOWER, Contact::SHARING, Contact::FRIEND],
103 'network' => Protocol::DFRN,
107 // User manually provided a contact ID they aren't privy to, silently defaulting to their own view
108 if (!in_array($view_as_contact_id, array_column($view_as_contacts, 'id'))) {
109 $view_as_contact_id = 0;
115 $basic_fields += self::buildField('fullname', DI::l10n()->t('Full Name:'), $a->profile['name']);
117 if (Feature::isEnabled($a->profile_uid, 'profile_membersince')) {
118 $basic_fields += self::buildField(
120 DI::l10n()->t('Member since:'),
121 DateTimeFormat::local($a->profile['register_date'])
125 if (!empty($a->profile['dob']) && $a->profile['dob'] > DBA::NULL_DATE) {
126 $year_bd_format = DI::l10n()->t('j F, Y');
127 $short_bd_format = DI::l10n()->t('j F');
129 $dob = DI::l10n()->getDay(
130 intval($a->profile['dob']) ?
131 DateTimeFormat::utc($a->profile['dob'] . ' 00:00 +00:00', $year_bd_format)
132 : DateTimeFormat::utc('2001-' . substr($a->profile['dob'], 5) . ' 00:00 +00:00', $short_bd_format)
135 $basic_fields += self::buildField('dob', DI::l10n()->t('Birthday:'), $dob);
137 if ($age = Temporal::getAgeByTimezone($a->profile['dob'], $a->profile['timezone'])) {
138 $basic_fields += self::buildField('age', DI::l10n()->t('Age: '), DI::l10n()->tt('%d year old', '%d years old', $age));
142 if ($a->profile['pdesc']) {
143 $basic_fields += self::buildField('pdesc', DI::l10n()->t('Description:'), HTML::toLink($a->profile['pdesc']));
146 if ($a->profile['xmpp']) {
147 $basic_fields += self::buildField('xmpp', DI::l10n()->t('XMPP:'), $a->profile['xmpp']);
150 if ($a->profile['homepage']) {
151 $basic_fields += self::buildField('homepage', DI::l10n()->t('Homepage:'), HTML::toLink($a->profile['homepage']));
154 $o .= Profile::getTabs($a, 'profile', $is_owner, $a->profile['nickname']);
156 $a->profile['address']
157 || $a->profile['locality']
158 || $a->profile['postal-code']
159 || $a->profile['region']
160 || $a->profile['country-name']
162 $basic_fields += self::buildField('location', DI::l10n()->t('Location:'), Profile::formatLocation($a->profile));
165 if ($a->profile['pub_keywords']) {
167 foreach (explode(',', $a->profile['pub_keywords']) as $tag_label) {
169 'url' => '/search?tag=' . $tag_label,
170 'label' => Term::TAG_CHARACTER[Term::HASHTAG] . $tag_label,
174 $basic_fields += self::buildField('pub_keywords', DI::l10n()->t('Tags:'), $tags);
179 // Defaults to the current logged in user self contact id to show self-only fields
180 $contact_id = $view_as_contact_id ?: $remote_contact_id ?: 0;
182 if ($is_owner && $contact_id === 0) {
183 $profile_fields = DI::profileField()->selectByUserId($a->profile_uid);
185 $profile_fields = DI::profileField()->selectByContactId($contact_id, $a->profile_uid);
188 foreach ($profile_fields as $profile_field) {
189 $custom_fields += self::buildField(
190 'custom_' . $profile_field->order,
191 $profile_field->label,
192 BBCode::convert($profile_field->value),
197 //show subcribed forum if it is enabled in the usersettings
198 if (Feature::isEnabled($a->profile_uid, 'forumlist_profile')) {
199 $custom_fields += self::buildField(
201 DI::l10n()->t('Forums:'),
202 ForumManager::profileAdvanced($a->profile_uid)
206 $tpl = Renderer::getMarkupTemplate('profile/index.tpl');
207 $o .= Renderer::replaceMacros($tpl, [
208 '$title' => DI::l10n()->t('Profile'),
209 '$view_as_contacts' => $view_as_contacts,
210 '$view_as_contact_id' => $view_as_contact_id,
211 '$view_as' => DI::l10n()->t('View profile as:'),
212 '$basic' => DI::l10n()->t('Basic'),
213 '$advanced' => DI::l10n()->t('Advanced'),
214 '$is_owner' => $a->profile_uid == local_user(),
215 '$query_string' => DI::args()->getQueryString(),
216 '$basic_fields' => $basic_fields,
217 '$custom_fields' => $custom_fields,
218 '$profile' => $a->profile,
220 'url' => DI::baseUrl() . '/settings/profile', DI::l10n()->t('Edit profile'),
222 'label' => DI::l10n()->t('Edit profile')
226 Hook::callAll('profile_advanced', $o);
232 * Creates a profile field structure to be used in the profile template
234 * @param string $name Arbitrary name of the field
235 * @param string $label Display label of the field
236 * @param mixed $value Display value of the field
237 * @param string $class Optional CSS class to apply to the field
240 private static function buildField(string $name, string $label, $value, string $class = 'aprofile')
243 'id' => 'aprofile-' . $name,
250 private static function buildHtmlHead(array $profile, string $nickname, int $remote_contact_id)
252 $baseUrl = DI::baseUrl();
256 if (!empty($profile['page-flags']) && $profile['page-flags'] == User::PAGE_FLAGS_COMMUNITY) {
257 $htmlhead .= '<meta name="friendica.community" content="true" />' . "\n";
260 if (!empty($profile['openidserver'])) {
261 $htmlhead .= '<link rel="openid.server" href="' . $profile['openidserver'] . '" />' . "\n";
264 if (!empty($profile['openid'])) {
265 $delegate = strstr($profile['openid'], '://') ? $profile['openid'] : 'https://' . $profile['openid'];
266 $htmlhead .= '<link rel="openid.delegate" href="' . $delegate . '" />' . "\n";
270 $blocked = !local_user() && !$remote_contact_id && DI::config()->get('system', 'block_public');
271 $userblock = !local_user() && !$remote_contact_id && $profile['hidewall'];
272 if (!$blocked && !$userblock) {
273 $keywords = str_replace(['#', ',', ' ', ',,'], ['', ' ', ',', ','], $profile['pub_keywords'] ?? '');
274 if (strlen($keywords)) {
275 $htmlhead .= '<meta name="keywords" content="' . $keywords . '" />' . "\n";
279 $htmlhead .= '<meta name="dfrn-global-visibility" content="' . ($profile['net-publish'] ? 'true' : 'false') . '" />' . "\n";
281 if (!$profile['net-publish'] || $profile['hidewall']) {
282 $htmlhead .= '<meta content="noindex, noarchive" name="robots" />' . "\n";
285 $htmlhead .= '<link rel="alternate" type="application/atom+xml" href="' . $baseUrl . '/dfrn_poll/' . $nickname . '" title="DFRN: ' . DI::l10n()->t('%s\'s timeline', $profile['username']) . '"/>' . "\n";
286 $htmlhead .= '<link rel="alternate" type="application/atom+xml" href="' . $baseUrl . '/feed/' . $nickname . '/" title="' . DI::l10n()->t('%s\'s posts', $profile['username']) . '"/>' . "\n";
287 $htmlhead .= '<link rel="alternate" type="application/atom+xml" href="' . $baseUrl . '/feed/' . $nickname . '/comments" title="' . DI::l10n()->t('%s\'s comments', $profile['username']) . '"/>' . "\n";
288 $htmlhead .= '<link rel="alternate" type="application/atom+xml" href="' . $baseUrl . '/feed/' . $nickname . '/activity" title="' . DI::l10n()->t('%s\'s timeline', $profile['username']) . '"/>' . "\n";
289 $uri = urlencode('acct:' . $profile['nickname'] . '@' . $baseUrl->getHostname() . ($baseUrl->getUrlPath() ? '/' . $baseUrl->getUrlPath() : ''));
290 $htmlhead .= '<link rel="lrdd" type="application/xrd+xml" href="' . $baseUrl . '/xrd/?uri=' . $uri . '" />' . "\n";
291 header('Link: <' . $baseUrl . '/xrd/?uri=' . $uri . '>; rel="lrdd"; type="application/xrd+xml"', false);
293 $dfrn_pages = ['request', 'confirm', 'notify', 'poll'];
294 foreach ($dfrn_pages as $dfrn) {
295 $htmlhead .= '<link rel="dfrn-' . $dfrn . '" href="' . $baseUrl . '/dfrn_' . $dfrn . '/' . $nickname . '" />' . "\n";
297 $htmlhead .= '<link rel="dfrn-poco" href="' . $baseUrl . '/poco/' . $nickname . '" />' . "\n";