3 * @copyright Copyright (C) 2010-2023, 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;
25 use Friendica\Content\Feature;
26 use Friendica\Content\GroupManager;
27 use Friendica\Content\Nav;
28 use Friendica\Content\Text\BBCode;
29 use Friendica\Core\Config\Capability\IManageConfigValues;
30 use Friendica\Core\Hook;
31 use Friendica\Core\L10n;
32 use Friendica\Core\Protocol;
33 use Friendica\Core\Renderer;
34 use Friendica\Core\Session\Capability\IHandleUserSessions;
35 use Friendica\Core\System;
36 use Friendica\Database\Database;
37 use Friendica\Database\DBA;
38 use Friendica\Model\Contact;
39 use Friendica\Model\Profile as ProfileModel;
40 use Friendica\Model\Tag;
41 use Friendica\Model\User;
42 use Friendica\Module\BaseProfile;
43 use Friendica\Module\Response;
44 use Friendica\Module\Security\Login;
45 use Friendica\Network\HTTPException;
46 use Friendica\Profile\ProfileField\Repository\ProfileField;
47 use Friendica\Protocol\ActivityPub;
48 use Friendica\Util\DateTimeFormat;
49 use Friendica\Util\Profiler;
50 use Friendica\Util\Strings;
51 use Friendica\Util\Temporal;
52 use Psr\Log\LoggerInterface;
54 class Profile extends BaseProfile
60 /** @var IHandleUserSessions */
62 /** @var IManageConfigValues */
66 /** @var ProfileField */
67 private $profileField;
69 public function __construct(ProfileField $profileField, App\Page $page, IManageConfigValues $config, IHandleUserSessions $session, App $app, Database $database, L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, Response $response, array $server, array $parameters = [])
71 parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
73 $this->database = $database;
75 $this->session = $session;
76 $this->config = $config;
78 $this->profileField = $profileField;
81 protected function rawContent(array $request = [])
83 if (ActivityPub::isRequest()) {
84 $user = $this->database->selectFirst('user', ['uid'], ['nickname' => $this->parameters['nickname'] ?? '', 'account_removed' => false]);
87 $data = ActivityPub\Transmitter::getProfile($user['uid']);
88 header('Access-Control-Allow-Origin: *');
89 header('Cache-Control: max-age=23200, stale-while-revalidate=23200');
90 header('Vary: Accept', false);
91 System::jsonExit($data, 'application/activity+json');
92 } catch (HTTPException\NotFoundException $e) {
93 System::jsonError(404, ['error' => 'Record not found']);
97 if ($this->database->exists('userd', ['username' => $this->parameters['nickname']])) {
99 $data = ActivityPub\Transmitter::getDeletedUser($this->parameters['nickname']);
101 System::jsonError(410, $data);
103 // Any other case (unknown, blocked, nverified, expired, no profile, no self contact)
104 System::jsonError(404, []);
108 header('Vary: Accept', false);
111 protected function content(array $request = []): string
113 $profile = ProfileModel::load($this->app, $this->parameters['nickname'] ?? '');
115 throw new HTTPException\NotFoundException($this->t('Profile not found.'));
118 $remote_contact_id = $this->session->getRemoteContactID($profile['uid']);
120 if ($this->config->get('system', 'block_public') && !$this->session->isAuthenticated()) {
121 return Login::form();
124 if (!empty($profile['hidewall']) && !$this->session->isAuthenticated()) {
125 $this->baseUrl->redirect('profile/' . $profile['nickname'] . '/restricted');
128 if (!empty($profile['page-flags']) && $profile['page-flags'] == User::PAGE_FLAGS_COMMUNITY) {
129 $this->page['htmlhead'] .= '<meta name="friendica.community" content="true" />' . "\n";
132 $this->page['htmlhead'] .= $this->buildHtmlHead($profile, $this->parameters['nickname']);
134 Nav::setSelected('home');
136 $is_owner = $this->session->getLocalUserId() == $profile['uid'];
137 $o = self::getTabsHTML('profile', $is_owner, $profile['nickname'], $profile['hide-friends']);
139 $view_as_contacts = [];
140 $view_as_contact_id = 0;
141 $view_as_contact_alert = '';
143 $view_as_contact_id = intval($request['viewas'] ?? 0);
145 $view_as_contacts = Contact::selectToArray(['id', 'name'], [
146 'uid' => $this->session->getLocalUserId(),
147 'rel' => [Contact::FOLLOWER, Contact::SHARING, Contact::FRIEND],
148 'network' => Protocol::DFRN,
152 $view_as_contact_ids = array_column($view_as_contacts, 'id');
154 // User manually provided a contact ID they aren't privy to, silently defaulting to their own view
155 if (!in_array($view_as_contact_id, $view_as_contact_ids)) {
156 $view_as_contact_id = 0;
159 if (($key = array_search($view_as_contact_id, $view_as_contact_ids)) !== false) {
160 $view_as_contact_alert = $this->t(
161 'You\'re currently viewing your profile as <b>%s</b> <a href="%s" class="btn btn-sm pull-right">Cancel</a>',
162 htmlentities($view_as_contacts[$key]['name'], ENT_COMPAT, 'UTF-8'),
163 'profile/' . $this->parameters['nickname'] . '/profile'
170 $basic_fields += self::buildField('fullname', $this->t('Full Name:'), $profile['name']);
172 if (Feature::isEnabled($profile['uid'], 'profile_membersince')) {
173 $basic_fields += self::buildField(
175 $this->t('Member since:'),
176 DateTimeFormat::local($profile['register_date'])
180 if (!empty($profile['dob']) && $profile['dob'] > DBA::NULL_DATE) {
181 $year_bd_format = $this->t('j F, Y');
182 $short_bd_format = $this->t('j F');
184 $dob = $this->l10n->getDay(
185 intval($profile['dob']) ?
186 DateTimeFormat::utc($profile['dob'] . ' 00:00 +00:00', $year_bd_format)
187 : DateTimeFormat::utc('2001-' . substr($profile['dob'], 5) . ' 00:00 +00:00', $short_bd_format)
190 $basic_fields += self::buildField('dob', $this->t('Birthday:'), $dob);
192 if ($age = Temporal::getAgeByTimezone($profile['dob'], $profile['timezone'])) {
193 $basic_fields += self::buildField('age', $this->t('Age: '), $this->tt('%d year old', '%d years old', $age));
197 if ($profile['about']) {
198 $basic_fields += self::buildField('about', $this->t('Description:'), BBCode::convertForUriId($profile['uri-id'], $profile['about']));
201 if ($profile['xmpp']) {
202 $basic_fields += self::buildField('xmpp', $this->t('XMPP:'), $profile['xmpp']);
205 if ($profile['matrix']) {
206 $basic_fields += self::buildField('matrix', $this->t('Matrix:'), $profile['matrix']);
209 if ($profile['homepage']) {
210 $basic_fields += self::buildField(
212 $this->t('Homepage:'),
213 $this->tryRelMe($profile['homepage']) ?: $profile['homepage']
219 || $profile['locality']
220 || $profile['postal-code']
221 || $profile['region']
222 || $profile['country-name']
224 $basic_fields += self::buildField('location', $this->t('Location:'), ProfileModel::formatLocation($profile));
227 if ($profile['pub_keywords']) {
229 // Separator is defined in Module\Settings\Profile\Index::cleanKeywords
230 foreach (explode(', ', $profile['pub_keywords']) as $tag_label) {
232 'url' => '/search?tag=' . $tag_label,
233 'label' => Tag::TAG_CHARACTER[Tag::HASHTAG] . $tag_label,
237 $basic_fields += self::buildField('pub_keywords', $this->t('Tags:'), $tags);
242 // Defaults to the current logged in user self contact id to show self-only fields
243 $contact_id = $view_as_contact_id ?: $remote_contact_id ?: 0;
245 if ($is_owner && $contact_id === 0) {
246 $profile_fields = $this->profileField->selectByUserId($profile['uid']);
248 $profile_fields = $this->profileField->selectByContactId($contact_id, $profile['uid']);
251 foreach ($profile_fields as $profile_field) {
252 $custom_fields += self::buildField(
253 'custom_' . $profile_field->order,
254 $profile_field->label,
255 $this->tryRelMe($profile_field->value) ?: BBCode::convertForUriId($profile['uri-id'], $profile_field->value),
260 //show subscribed group if it is enabled in the usersettings
261 if (Feature::isEnabled($profile['uid'], 'forumlist_profile')) {
262 $custom_fields += self::buildField(
265 GroupManager::profileAdvanced($profile['uid'])
269 $tpl = Renderer::getMarkupTemplate('profile/profile.tpl');
270 $o .= Renderer::replaceMacros($tpl, [
271 '$title' => $this->t('Profile'),
272 '$yourself' => $this->t('Yourself'),
273 '$view_as_contacts' => $view_as_contacts,
274 '$view_as_contact_id' => $view_as_contact_id,
275 '$view_as_contact_alert' => $view_as_contact_alert,
276 '$view_as' => $this->t('View profile as:'),
277 '$submit' => $this->t('Submit'),
278 '$basic' => $this->t('Basic'),
279 '$advanced' => $this->t('Advanced'),
280 '$is_owner' => $profile['uid'] == $this->session->getLocalUserId(),
281 '$query_string' => $this->args->getQueryString(),
282 '$basic_fields' => $basic_fields,
283 '$custom_fields' => $custom_fields,
284 '$profile' => $profile,
286 'url' => 'settings/profile', $this->t('Edit profile'),
288 'label' => $this->t('Edit profile')
291 'url' => $this->args->getQueryString() . '#viewas',
293 'label' => $this->t('View as')
297 Hook::callAll('profile_advanced', $o);
303 * Creates a profile field structure to be used in the profile template
305 * @param string $name Arbitrary name of the field
306 * @param string $label Display label of the field
307 * @param mixed $value Display value of the field
308 * @param string $class Optional CSS class to apply to the field
311 private static function buildField(string $name, string $label, $value, string $class = 'aprofile'): array
314 'id' => 'aprofile-' . $name,
321 private function buildHtmlHead(array $profile, string $nickname): string
325 if (!empty($profile['page-flags']) && $profile['page-flags'] == User::PAGE_FLAGS_COMMUNITY) {
326 $htmlhead .= '<meta name="friendica.community" content="true" />' . "\n";
329 if (!empty($profile['openidserver'])) {
330 $htmlhead .= '<link rel="openid.server" href="' . $profile['openidserver'] . '" />' . "\n";
333 if (!empty($profile['openid'])) {
334 $delegate = strstr($profile['openid'], '://') ? $profile['openid'] : 'https://' . $profile['openid'];
335 $htmlhead .= '<link rel="openid.delegate" href="' . $delegate . '" />' . "\n";
339 $blocked = !$this->session->isAuthenticated() && $this->config->get('system', 'block_public');
340 $userblock = !$this->session->isAuthenticated() && $profile['hidewall'];
341 if (!$blocked && !$userblock) {
342 $keywords = str_replace(['#', ',', ' ', ',,'], ['', ' ', ',', ','], $profile['pub_keywords'] ?? '');
343 if (strlen($keywords)) {
344 $htmlhead .= '<meta name="keywords" content="' . $keywords . '" />' . "\n";
348 $htmlhead .= '<meta name="dfrn-global-visibility" content="' . ($profile['net-publish'] ? 'true' : 'false') . '" />' . "\n";
350 if (!$profile['net-publish']) {
351 $htmlhead .= '<meta content="noindex, noarchive" name="robots" />' . "\n";
354 $htmlhead .= '<link rel="alternate" type="application/atom+xml" href="' . $this->baseUrl . '/dfrn_poll/' . $nickname . '" title="DFRN: ' . $this->t('%s\'s timeline', $profile['name']) . '"/>' . "\n";
355 $htmlhead .= '<link rel="alternate" type="application/atom+xml" href="' . $this->baseUrl . '/feed/' . $nickname . '/" title="' . $this->t('%s\'s posts', $profile['name']) . '"/>' . "\n";
356 $htmlhead .= '<link rel="alternate" type="application/atom+xml" href="' . $this->baseUrl . '/feed/' . $nickname . '/comments" title="' . $this->t('%s\'s comments', $profile['name']) . '"/>' . "\n";
357 $htmlhead .= '<link rel="alternate" type="application/atom+xml" href="' . $this->baseUrl . '/feed/' . $nickname . '/activity" title="' . $this->t('%s\'s timeline', $profile['name']) . '"/>' . "\n";
358 $uri = urlencode('acct:' . $profile['nickname'] . '@' . $this->baseUrl->getHost() . ($this->baseUrl->getPath() ? '/' . $this->baseUrl->getPath() : ''));
359 $htmlhead .= '<link rel="lrdd" type="application/xrd+xml" href="' . $this->baseUrl . '/xrd/?uri=' . $uri . '" />' . "\n";
360 header('Link: <' . $this->baseUrl . '/xrd/?uri=' . $uri . '>; rel="lrdd"; type="application/xrd+xml"', false);
362 $dfrn_pages = ['request', 'confirm', 'notify', 'poll'];
363 foreach ($dfrn_pages as $dfrn) {
364 $htmlhead .= '<link rel="dfrn-' . $dfrn . '" href="' . $this->baseUrl . '/dfrn_' . $dfrn . '/' . $nickname . '" />' . "\n";
371 * Check if the input is an HTTP(S) link and returns a rel="me" link if yes, empty string if not
373 * @param string $input
376 private function tryRelMe(string $input): string
378 if (preg_match(Strings::onlyLinkRegEx(), trim($input))) {
379 return '<a href="' . trim($input) . '" target="_blank" rel="noopener noreferrer me">' . trim($input) . '</a>';