3 * @file src/Model/Profile.php
5 namespace Friendica\Model;
8 use Friendica\Content\Feature;
9 use Friendica\Content\ForumManager;
10 use Friendica\Content\Text\BBCode;
11 use Friendica\Content\Text\HTML;
12 use Friendica\Core\Addon;
13 use Friendica\Core\Cache;
14 use Friendica\Core\Config;
15 use Friendica\Core\L10n;
16 use Friendica\Core\Logger;
17 use Friendica\Core\PConfig;
18 use Friendica\Core\Protocol;
19 use Friendica\Core\Renderer;
20 use Friendica\Core\System;
21 use Friendica\Core\Worker;
22 use Friendica\Database\DBA;
23 use Friendica\Model\Contact;
24 use Friendica\Protocol\Diaspora;
25 use Friendica\Util\DateTimeFormat;
26 use Friendica\Util\Network;
27 use Friendica\Util\Proxy as ProxyUtils;
28 use Friendica\Util\Strings;
29 use Friendica\Util\Temporal;
34 * @brief Returns default profile for a given user id
36 * @param integer User ID
38 * @return array Profile data
40 public static function getByUID($uid)
42 $profile = DBA::selectFirst('profile', [], ['uid' => $uid, 'is-default' => true]);
47 * @brief Returns a formatted location string from the given profile array
49 * @param array $profile Profile array (Generated from the "profile" table)
51 * @return string Location string
53 public static function formatLocation(array $profile)
57 if (!empty($profile['locality'])) {
58 $location .= $profile['locality'];
61 if (!empty($profile['region']) && (defaults($profile, 'locality', '') != $profile['region'])) {
66 $location .= $profile['region'];
69 if (!empty($profile['country-name'])) {
74 $location .= $profile['country-name'];
82 * Loads a profile into the page sidebar.
84 * The function requires a writeable copy of the main App structure, and the nickname
85 * of a registered local account.
87 * If the viewer is an authenticated remote viewer, the profile displayed is the
88 * one that has been configured for his/her viewing in the Contact manager.
89 * Passing a non-zero profile ID can also allow a preview of a selected profile
92 * Profile information is placed in the App structure for later retrieval.
93 * Honours the owner's chosen theme for display.
95 * @attention Should only be run in the _init() functions of a module. That ensures that
96 * the theme is chosen before the _init() function of a theme is run, which will usually
97 * load a lot of theme-specific content
99 * @brief Loads a profile into the page sidebar.
101 * @param string $nickname string
102 * @param int $profile int
103 * @param array $profiledata array
104 * @param boolean $show_connect Show connect link
106 public static function load(App $a, $nickname, $profile = 0, array $profiledata = [], $show_connect = true)
108 $user = DBA::selectFirst('user', ['uid'], ['nickname' => $nickname, 'account_removed' => false]);
110 if (!DBA::isResult($user) && empty($profiledata)) {
111 Logger::log('profile error: ' . $a->query_string, Logger::DEBUG);
112 notice(L10n::t('Requested account is not available.') . EOL);
117 if (count($profiledata) > 0) {
118 // Add profile data to sidebar
119 $a->page['aside'] .= self::sidebar($profiledata, true, $show_connect);
121 if (!DBA::isResult($user)) {
126 $pdata = self::getByNickname($nickname, $user['uid'], $profile);
128 if (empty($pdata) && empty($profiledata)) {
129 Logger::log('profile error: ' . $a->query_string, Logger::DEBUG);
130 notice(L10n::t('Requested profile is not available.') . EOL);
136 $pdata = ['uid' => 0, 'profile_uid' => 0, 'is-default' => false,'name' => $nickname];
139 // fetch user tags if this isn't the default profile
141 if (!$pdata['is-default']) {
142 $condition = ['uid' => $pdata['profile_uid'], 'is-default' => true];
143 $profile = DBA::selectFirst('profile', ['pub_keywords'], $condition);
144 if (DBA::isResult($profile)) {
145 $pdata['pub_keywords'] = $profile['pub_keywords'];
149 $a->profile = $pdata;
150 $a->profile_uid = $pdata['profile_uid'];
152 $a->profile['mobile-theme'] = PConfig::get($a->profile['profile_uid'], 'system', 'mobile_theme');
153 $a->profile['network'] = Protocol::DFRN;
155 $a->page['title'] = $a->profile['name'] . ' @ ' . Config::get('config', 'sitename');
157 if (!$profiledata && !PConfig::get(local_user(), 'system', 'always_my_theme')) {
158 $_SESSION['theme'] = $a->profile['theme'];
161 $_SESSION['mobile-theme'] = $a->profile['mobile-theme'];
164 * load/reload current theme info
167 Renderer::setActiveTemplateEngine(); // reset the template engine to the default in case the user's theme doesn't specify one
169 $theme_info_file = 'view/theme/' . $a->getCurrentTheme() . '/theme.php';
170 if (file_exists($theme_info_file)) {
171 require_once $theme_info_file;
174 if (local_user() && local_user() == $a->profile['uid'] && $profiledata) {
175 $a->page['aside'] .= Renderer::replaceMacros(
176 Renderer::getMarkupTemplate('profile_edlink.tpl'),
178 '$editprofile' => L10n::t('Edit profile'),
179 '$profid' => $a->profile['id']
184 $block = ((Config::get('system', 'block_public') && !local_user() && !remote_user()) ? true : false);
188 * By now, the contact block isn't shown, when a different profile is given
189 * But: When this profile was on the same server, then we could display the contacts
192 $a->page['aside'] .= self::sidebar($a->profile, $block, $show_connect);
199 * Get all profile data of a local user
201 * If the viewer is an authenticated remote viewer, the profile displayed is the
202 * one that has been configured for his/her viewing in the Contact manager.
203 * Passing a non-zero profile ID can also allow a preview of a selected profile
206 * Includes all available profile data
208 * @brief Get all profile data of a local user
209 * @param string $nickname nick
210 * @param int $uid uid
211 * @param int $profile_id ID of the profile
214 public static function getByNickname($nickname, $uid = 0, $profile_id = 0)
216 if (remote_user() && !empty($_SESSION['remote'])) {
217 foreach ($_SESSION['remote'] as $visitor) {
218 if ($visitor['uid'] == $uid) {
219 $contact = DBA::selectFirst('contact', ['profile-id'], ['id' => $visitor['cid']]);
220 if (DBA::isResult($contact)) {
221 $profile_id = $contact['profile-id'];
231 $profile = DBA::fetchFirst(
232 "SELECT `contact`.`id` AS `contact_id`, `contact`.`photo` AS `contact_photo`,
233 `contact`.`thumb` AS `contact_thumb`, `contact`.`micro` AS `contact_micro`,
234 `profile`.`uid` AS `profile_uid`, `profile`.*,
235 `contact`.`avatar-date` AS picdate, `contact`.`addr`, `contact`.`url`, `user`.*
237 INNER JOIN `contact` on `contact`.`uid` = `profile`.`uid` AND `contact`.`self`
238 INNER JOIN `user` ON `profile`.`uid` = `user`.`uid`
239 WHERE `user`.`nickname` = ? AND `profile`.`id` = ? LIMIT 1",
244 if (!DBA::isResult($profile)) {
245 $profile = DBA::fetchFirst(
246 "SELECT `contact`.`id` AS `contact_id`, `contact`.`photo` as `contact_photo`,
247 `contact`.`thumb` AS `contact_thumb`, `contact`.`micro` AS `contact_micro`,
248 `profile`.`uid` AS `profile_uid`, `profile`.*,
249 `contact`.`avatar-date` AS picdate, `contact`.`addr`, `contact`.`url`, `user`.*
251 INNER JOIN `contact` ON `contact`.`uid` = `profile`.`uid` AND `contact`.`self`
252 INNER JOIN `user` ON `profile`.`uid` = `user`.`uid`
253 WHERE `user`.`nickname` = ? AND `profile`.`is-default` LIMIT 1",
262 * Formats a profile for display in the sidebar.
264 * It is very difficult to templatise the HTML completely
265 * because of all the conditional logic.
267 * @brief Formats a profile for display in the sidebar.
268 * @param array $profile
270 * @param boolean $show_connect Show connect link
272 * @return string HTML sidebar module
274 * @note Returns empty string if passed $profile is wrong type or not populated
276 * @hooks 'profile_sidebar_enter'
277 * array $profile - profile data
278 * @hooks 'profile_sidebar'
281 private static function sidebar($profile, $block = 0, $show_connect = true)
288 // This function can also use contact information in $profile
289 $is_contact = !empty($profile['cid']);
291 if (!is_array($profile) && !count($profile)) {
295 $profile['picdate'] = urlencode(defaults($profile, 'picdate', ''));
297 if (($profile['network'] != '') && ($profile['network'] != Protocol::DFRN)) {
298 $profile['network_link'] = Strings::formatNetworkName($profile['network'], $profile['url']);
300 $profile['network_link'] = '';
303 Addon::callHooks('profile_sidebar_enter', $profile);
306 // don't show connect link to yourself
307 $connect = $profile['uid'] != local_user() ? L10n::t('Connect') : false;
309 // don't show connect link to authenticated visitors either
310 if (remote_user() && !empty($_SESSION['remote'])) {
311 foreach ($_SESSION['remote'] as $visitor) {
312 if ($visitor['uid'] == $profile['uid']) {
319 if (!$show_connect) {
325 // Is the local user already connected to that user?
326 if ($connect && local_user()) {
327 if (isset($profile['url'])) {
328 $profile_url = Strings::normaliseLink($profile['url']);
330 $profile_url = Strings::normaliseLink(System::baseUrl() . '/profile/' . $profile['nickname']);
333 if (DBA::exists('contact', ['pending' => false, 'uid' => local_user(), 'nurl' => $profile_url])) {
338 // Is the remote user already connected to that user?
339 if ($connect && Contact::isFollower(remote_user(), $profile['uid'])) {
343 if ($connect && ($profile['network'] != Protocol::DFRN) && !isset($profile['remoteconnect'])) {
347 $remoteconnect = null;
348 if (isset($profile['remoteconnect'])) {
349 $remoteconnect = $profile['remoteconnect'];
352 if ($connect && ($profile['network'] == Protocol::DFRN) && !isset($remoteconnect)) {
353 $subscribe_feed = L10n::t('Atom feed');
355 $subscribe_feed = false;
358 $wallmessage = false;
359 $wallmessage_link = false;
361 // See issue https://github.com/friendica/friendica/issues/3838
362 // Either we remove the message link for remote users or we enable creating messages from remote users
363 if (remote_user() || (self::getMyURL() && !empty($profile['unkmail']) && ($profile['uid'] != local_user()))) {
364 $wallmessage = L10n::t('Message');
368 "SELECT `url` FROM `contact` WHERE `uid` = %d AND `id` = '%s' AND `rel` = %d",
369 intval($profile['uid']),
370 intval(remote_user()),
371 intval(Contact::FRIEND)
375 "SELECT `url` FROM `contact` WHERE `uid` = %d AND `nurl` = '%s' AND `rel` = %d",
376 intval($profile['uid']),
377 DBA::escape(Strings::normaliseLink(self::getMyURL())),
378 intval(Contact::FRIEND)
382 $remote_url = $r[0]['url'];
383 $message_path = preg_replace('=(.*)/profile/(.*)=ism', '$1/message/new/', $remote_url);
384 $wallmessage_link = $message_path . base64_encode(defaults($profile, 'addr', ''));
385 } else if (!empty($profile['nickname'])) {
386 $wallmessage_link = 'wallmessage/' . $profile['nickname'];
390 // show edit profile to yourself
391 if (!$is_contact && $profile['uid'] == local_user() && Feature::isEnabled(local_user(), 'multi_profiles')) {
392 $profile['edit'] = [System::baseUrl() . '/profiles', L10n::t('Profiles'), '', L10n::t('Manage/edit profiles')];
394 "SELECT * FROM `profile` WHERE `uid` = %d",
399 'chg_photo' => L10n::t('Change profile photo'),
400 'cr_new' => L10n::t('Create New Profile'),
404 if (DBA::isResult($r)) {
405 foreach ($r as $rr) {
406 $profile['menu']['entries'][] = [
407 'photo' => $rr['thumb'],
409 'alt' => L10n::t('Profile Image'),
410 'profile_name' => $rr['profile-name'],
411 'isdefault' => $rr['is-default'],
412 'visibile_to_everybody' => L10n::t('visible to everybody'),
413 'edit_visibility' => L10n::t('Edit visibility'),
418 if (!$is_contact && $profile['uid'] == local_user() && !Feature::isEnabled(local_user(), 'multi_profiles')) {
419 $profile['edit'] = [System::baseUrl() . '/profiles/' . $profile['id'], L10n::t('Edit profile'), '', L10n::t('Edit profile')];
421 'chg_photo' => L10n::t('Change profile photo'),
427 // Fetch the account type
428 $account_type = Contact::getAccountType($profile);
430 if (!empty($profile['address'])
431 || !empty($profile['location'])
432 || !empty($profile['locality'])
433 || !empty($profile['region'])
434 || !empty($profile['postal-code'])
435 || !empty($profile['country-name'])
437 $location = L10n::t('Location:');
440 $gender = !empty($profile['gender']) ? L10n::t('Gender:') : false;
441 $marital = !empty($profile['marital']) ? L10n::t('Status:') : false;
442 $homepage = !empty($profile['homepage']) ? L10n::t('Homepage:') : false;
443 $about = !empty($profile['about']) ? L10n::t('About:') : false;
444 $xmpp = !empty($profile['xmpp']) ? L10n::t('XMPP:') : false;
446 if ((!empty($profile['hidewall']) || $block) && !local_user() && !remote_user()) {
447 $location = $gender = $marital = $homepage = $about = false;
450 $split_name = Diaspora::splitName($profile['name']);
451 $firstname = $split_name['first'];
452 $lastname = $split_name['last'];
454 if (!empty($profile['guid'])) {
456 'guid' => $profile['guid'],
457 'podloc' => System::baseUrl(),
458 'searchable' => (($profile['publish'] && $profile['net-publish']) ? 'true' : 'false' ),
459 'nickname' => $profile['nickname'],
460 'fullname' => $profile['name'],
461 'firstname' => $firstname,
462 'lastname' => $lastname,
463 'photo300' => defaults($profile, 'contact_photo', ''),
464 'photo100' => defaults($profile, 'contact_thumb', ''),
465 'photo50' => defaults($profile, 'contact_micro', ''),
475 $contact_block = HTML::contactBlock();
477 if (is_array($a->profile) && !$a->profile['hide-friends']) {
479 "SELECT `gcontact`.`updated` FROM `contact` INNER JOIN `gcontact` WHERE `gcontact`.`nurl` = `contact`.`nurl` AND `self` AND `uid` = %d LIMIT 1",
480 intval($a->profile['uid'])
482 if (DBA::isResult($r)) {
483 $updated = date('c', strtotime($r[0]['updated']));
487 "SELECT COUNT(*) AS `total` FROM `contact`
489 AND NOT `self` AND NOT `blocked` AND NOT `pending`
490 AND NOT `hidden` AND NOT `archive`
491 AND `network` IN ('%s', '%s', '%s', '')",
492 intval($profile['uid']),
493 DBA::escape(Protocol::DFRN),
494 DBA::escape(Protocol::DIASPORA),
495 DBA::escape(Protocol::OSTATUS)
497 if (DBA::isResult($r)) {
498 $contacts = intval($r[0]['total']);
504 foreach ($profile as $k => $v) {
505 $k = str_replace('-', '_', $k);
509 if (isset($p['about'])) {
510 $p['about'] = BBCode::convert($p['about']);
513 if (empty($p['address']) && !empty($p['location'])) {
514 $p['address'] = $p['location'];
517 if (isset($p['photo'])) {
518 $p['photo'] = ProxyUtils::proxifyUrl($p['photo'], false, ProxyUtils::SIZE_SMALL);
521 $p['url'] = Contact::magicLink(defaults($p, 'url', $profile_url));
523 $tpl = Renderer::getMarkupTemplate('profile_vcard.tpl');
524 $o .= Renderer::replaceMacros($tpl, [
527 '$connect' => $connect,
528 '$remoteconnect' => $remoteconnect,
529 '$subscribe_feed' => $subscribe_feed,
530 '$wallmessage' => $wallmessage,
531 '$wallmessage_link' => $wallmessage_link,
532 '$account_type' => $account_type,
533 '$location' => $location,
534 '$gender' => $gender,
535 '$marital' => $marital,
536 '$homepage' => $homepage,
538 '$network' => L10n::t('Network:'),
539 '$contacts' => $contacts,
540 '$updated' => $updated,
541 '$diaspora' => $diaspora,
542 '$contact_block' => $contact_block,
545 $arr = ['profile' => &$profile, 'entry' => &$o];
547 Addon::callHooks('profile_sidebar', $arr);
552 public static function getBirthdays()
557 if (!local_user() || $a->is_mobile || $a->is_tablet) {
562 * $mobile_detect = new Mobile_Detect();
563 * $is_mobile = $mobile_detect->isMobile() || $mobile_detect->isTablet();
568 $bd_format = L10n::t('g A l F d'); // 8 AM Friday January 18
569 $bd_short = L10n::t('F d');
571 $cachekey = 'get_birthdays:' . local_user();
572 $r = Cache::get($cachekey);
575 "SELECT `event`.*, `event`.`id` AS `eid`, `contact`.* FROM `event`
577 ON `contact`.`id` = `event`.`cid`
578 AND (`contact`.`rel` = ? OR `contact`.`rel` = ?)
579 AND NOT `contact`.`pending`
580 AND NOT `contact`.`hidden`
581 AND NOT `contact`.`blocked`
582 AND NOT `contact`.`archive`
583 AND NOT `contact`.`deleted`
584 WHERE `event`.`uid` = ? AND `type` = 'birthday' AND `start` < ? AND `finish` > ?
585 ORDER BY `start` ASC ",
589 DateTimeFormat::utc('now + 6 days'),
590 DateTimeFormat::utcNow()
592 if (DBA::isResult($s)) {
593 $r = DBA::toArray($s);
594 Cache::set($cachekey, $r, Cache::HOUR);
600 if (DBA::isResult($r)) {
601 $now = strtotime('now');
605 foreach ($r as $rr) {
606 if (strlen($rr['name'])) {
609 if ((strtotime($rr['start'] . ' +00:00') < $now) && (strtotime($rr['finish'] . ' +00:00') > $now)) {
613 $classtoday = $istoday ? ' birthday-today ' : '';
615 foreach ($r as &$rr) {
616 if (!strlen($rr['name'])) {
622 if (in_array($rr['cid'], $cids)) {
625 $cids[] = $rr['cid'];
627 $today = (((strtotime($rr['start'] . ' +00:00') < $now) && (strtotime($rr['finish'] . ' +00:00') > $now)) ? true : false);
629 $rr['link'] = Contact::magicLink($rr['url']);
630 $rr['title'] = $rr['name'];
631 $rr['date'] = L10n::getDay(DateTimeFormat::convert($rr['start'], $a->timezone, 'UTC', $rr['adjust'] ? $bd_format : $bd_short)) . (($today) ? ' ' . L10n::t('[today]') : '');
632 $rr['startime'] = null;
633 $rr['today'] = $today;
637 $tpl = Renderer::getMarkupTemplate('birthdays_reminder.tpl');
638 return Renderer::replaceMacros($tpl, [
639 '$baseurl' => System::baseUrl(),
640 '$classtoday' => $classtoday,
642 '$event_reminders' => L10n::t('Birthday Reminders'),
643 '$event_title' => L10n::t('Birthdays this week:'),
645 '$lbr' => '{', // raw brackets mess up if/endif macro processing
650 public static function getEventsReminderHTML()
655 if (!local_user() || $a->is_mobile || $a->is_tablet) {
660 * $mobile_detect = new Mobile_Detect();
661 * $is_mobile = $mobile_detect->isMobile() || $mobile_detect->isTablet();
666 $bd_format = L10n::t('g A l F d'); // 8 AM Friday January 18
669 $condition = ["`uid` = ? AND `type` != 'birthday' AND `start` < ? AND `start` >= ?",
670 local_user(), DateTimeFormat::utc('now + 7 days'), DateTimeFormat::utc('now - 1 days')];
671 $s = DBA::select('event', [], $condition, ['order' => ['start']]);
675 if (DBA::isResult($s)) {
679 while ($rr = DBA::fetch($s)) {
680 $condition = ['parent-uri' => $rr['uri'], 'uid' => $rr['uid'], 'author-id' => public_contact(),
681 'activity' => [Item::activityToIndex(ACTIVITY_ATTEND), Item::activityToIndex(ACTIVITY_ATTENDMAYBE)],
682 'visible' => true, 'deleted' => false];
683 if (!Item::exists($condition)) {
687 if (strlen($rr['summary'])) {
691 $strt = DateTimeFormat::convert($rr['start'], $rr['adjust'] ? $a->timezone : 'UTC', 'UTC', 'Y-m-d');
692 if ($strt === DateTimeFormat::timezoneNow($a->timezone, 'Y-m-d')) {
696 $title = strip_tags(html_entity_decode(BBCode::convert($rr['summary']), ENT_QUOTES, 'UTF-8'));
698 if (strlen($title) > 35) {
699 $title = substr($title, 0, 32) . '... ';
702 $description = substr(strip_tags(BBCode::convert($rr['desc'])), 0, 32) . '... ';
704 $description = L10n::t('[No description]');
707 $strt = DateTimeFormat::convert($rr['start'], $rr['adjust'] ? $a->timezone : 'UTC');
709 if (substr($strt, 0, 10) < DateTimeFormat::timezoneNow($a->timezone, 'Y-m-d')) {
713 $today = ((substr($strt, 0, 10) === DateTimeFormat::timezoneNow($a->timezone, 'Y-m-d')) ? true : false);
715 $rr['title'] = $title;
716 $rr['description'] = $description;
717 $rr['date'] = L10n::getDay(DateTimeFormat::convert($rr['start'], $rr['adjust'] ? $a->timezone : 'UTC', 'UTC', $bd_format)) . (($today) ? ' ' . L10n::t('[today]') : '');
718 $rr['startime'] = $strt;
719 $rr['today'] = $today;
724 $classtoday = (($istoday) ? 'event-today' : '');
726 $tpl = Renderer::getMarkupTemplate('events_reminder.tpl');
727 return Renderer::replaceMacros($tpl, [
728 '$baseurl' => System::baseUrl(),
729 '$classtoday' => $classtoday,
730 '$count' => count($r),
731 '$event_reminders' => L10n::t('Event Reminders'),
732 '$event_title' => L10n::t('Upcoming events the next 7 days:'),
737 public static function getAdvanced(App $a)
740 $uid = $a->profile['uid'];
742 $o .= Renderer::replaceMacros(
743 Renderer::getMarkupTemplate('section_title.tpl'),
744 ['$title' => L10n::t('Profile')]
747 if ($a->profile['name']) {
748 $tpl = Renderer::getMarkupTemplate('profile_advanced.tpl');
752 $profile['fullname'] = [L10n::t('Full Name:'), $a->profile['name']];
754 if (Feature::isEnabled($uid, 'profile_membersince')) {
755 $profile['membersince'] = [L10n::t('Member since:'), DateTimeFormat::local($a->profile['register_date'])];
758 if ($a->profile['gender']) {
759 $profile['gender'] = [L10n::t('Gender:'), $a->profile['gender']];
762 if (!empty($a->profile['dob']) && $a->profile['dob'] > DBA::NULL_DATE) {
763 $year_bd_format = L10n::t('j F, Y');
764 $short_bd_format = L10n::t('j F');
767 intval($a->profile['dob']) ?
768 DateTimeFormat::utc($a->profile['dob'] . ' 00:00 +00:00', $year_bd_format)
769 : DateTimeFormat::utc('2001-' . substr($a->profile['dob'], 5) . ' 00:00 +00:00', $short_bd_format)
772 $profile['birthday'] = [L10n::t('Birthday:'), $val];
775 if (!empty($a->profile['dob'])
776 && $a->profile['dob'] > DBA::NULL_DATE
777 && $age = Temporal::getAgeByTimezone($a->profile['dob'], $a->profile['timezone'], '')
779 $profile['age'] = [L10n::t('Age:'), $age];
782 if ($a->profile['marital']) {
783 $profile['marital'] = [L10n::t('Status:'), $a->profile['marital']];
786 /// @TODO Maybe use x() here, plus below?
787 if ($a->profile['with']) {
788 $profile['marital']['with'] = $a->profile['with'];
791 if (strlen($a->profile['howlong']) && $a->profile['howlong'] >= DBA::NULL_DATETIME) {
792 $profile['howlong'] = Temporal::getRelativeDate($a->profile['howlong'], L10n::t('for %1$d %2$s'));
795 if ($a->profile['sexual']) {
796 $profile['sexual'] = [L10n::t('Sexual Preference:'), $a->profile['sexual']];
799 if ($a->profile['homepage']) {
800 $profile['homepage'] = [L10n::t('Homepage:'), HTML::toLink($a->profile['homepage'])];
803 if ($a->profile['hometown']) {
804 $profile['hometown'] = [L10n::t('Hometown:'), HTML::toLink($a->profile['hometown'])];
807 if ($a->profile['pub_keywords']) {
808 $profile['pub_keywords'] = [L10n::t('Tags:'), $a->profile['pub_keywords']];
811 if ($a->profile['politic']) {
812 $profile['politic'] = [L10n::t('Political Views:'), $a->profile['politic']];
815 if ($a->profile['religion']) {
816 $profile['religion'] = [L10n::t('Religion:'), $a->profile['religion']];
819 if ($txt = prepare_text($a->profile['about'])) {
820 $profile['about'] = [L10n::t('About:'), $txt];
823 if ($txt = prepare_text($a->profile['interest'])) {
824 $profile['interest'] = [L10n::t('Hobbies/Interests:'), $txt];
827 if ($txt = prepare_text($a->profile['likes'])) {
828 $profile['likes'] = [L10n::t('Likes:'), $txt];
831 if ($txt = prepare_text($a->profile['dislikes'])) {
832 $profile['dislikes'] = [L10n::t('Dislikes:'), $txt];
835 if ($txt = prepare_text($a->profile['contact'])) {
836 $profile['contact'] = [L10n::t('Contact information and Social Networks:'), $txt];
839 if ($txt = prepare_text($a->profile['music'])) {
840 $profile['music'] = [L10n::t('Musical interests:'), $txt];
843 if ($txt = prepare_text($a->profile['book'])) {
844 $profile['book'] = [L10n::t('Books, literature:'), $txt];
847 if ($txt = prepare_text($a->profile['tv'])) {
848 $profile['tv'] = [L10n::t('Television:'), $txt];
851 if ($txt = prepare_text($a->profile['film'])) {
852 $profile['film'] = [L10n::t('Film/dance/culture/entertainment:'), $txt];
855 if ($txt = prepare_text($a->profile['romance'])) {
856 $profile['romance'] = [L10n::t('Love/Romance:'), $txt];
859 if ($txt = prepare_text($a->profile['work'])) {
860 $profile['work'] = [L10n::t('Work/employment:'), $txt];
863 if ($txt = prepare_text($a->profile['education'])) {
864 $profile['education'] = [L10n::t('School/education:'), $txt];
867 //show subcribed forum if it is enabled in the usersettings
868 if (Feature::isEnabled($uid, 'forumlist_profile')) {
869 $profile['forumlist'] = [L10n::t('Forums:'), ForumManager::profileAdvanced($uid)];
872 if ($a->profile['uid'] == local_user()) {
873 $profile['edit'] = [System::baseUrl() . '/profiles/' . $a->profile['id'], L10n::t('Edit profile'), '', L10n::t('Edit profile')];
876 return Renderer::replaceMacros($tpl, [
877 '$title' => L10n::t('Profile'),
878 '$basic' => L10n::t('Basic'),
879 '$advanced' => L10n::t('Advanced'),
880 '$profile' => $profile
887 public static function getTabs($a, $is_owner = false, $nickname = null)
889 if (is_null($nickname)) {
890 $nickname = $a->user['nickname'];
894 if (!empty($_GET['tab'])) {
895 $tab = Strings::escapeTags(trim($_GET['tab']));
898 $url = System::baseUrl() . '/profile/' . $nickname;
902 'label' => L10n::t('Status'),
904 'sel' => !$tab && $a->argv[0] == 'profile' ? 'active' : '',
905 'title' => L10n::t('Status Messages and Posts'),
906 'id' => 'status-tab',
910 'label' => L10n::t('Profile'),
911 'url' => $url . '/?tab=profile',
912 'sel' => $tab == 'profile' ? 'active' : '',
913 'title' => L10n::t('Profile Details'),
914 'id' => 'profile-tab',
918 'label' => L10n::t('Photos'),
919 'url' => System::baseUrl() . '/photos/' . $nickname,
920 'sel' => !$tab && $a->argv[0] == 'photos' ? 'active' : '',
921 'title' => L10n::t('Photo Albums'),
926 'label' => L10n::t('Videos'),
927 'url' => System::baseUrl() . '/videos/' . $nickname,
928 'sel' => !$tab && $a->argv[0] == 'videos' ? 'active' : '',
929 'title' => L10n::t('Videos'),
935 // the calendar link for the full featured events calendar
936 if ($is_owner && $a->theme_events_in_profile) {
938 'label' => L10n::t('Events'),
939 'url' => System::baseUrl() . '/events',
940 'sel' => !$tab && $a->argv[0] == 'events' ? 'active' : '',
941 'title' => L10n::t('Events and Calendar'),
942 'id' => 'events-tab',
945 // if the user is not the owner of the calendar we only show a calendar
946 // with the public events of the calendar owner
947 } elseif (!$is_owner) {
949 'label' => L10n::t('Events'),
950 'url' => System::baseUrl() . '/cal/' . $nickname,
951 'sel' => !$tab && $a->argv[0] == 'cal' ? 'active' : '',
952 'title' => L10n::t('Events and Calendar'),
953 'id' => 'events-tab',
960 'label' => L10n::t('Personal Notes'),
961 'url' => System::baseUrl() . '/notes',
962 'sel' => !$tab && $a->argv[0] == 'notes' ? 'active' : '',
963 'title' => L10n::t('Only You Can See This'),
969 if (!empty($_SESSION['new_member']) && $is_owner) {
971 'label' => L10n::t('Tips for New Members'),
972 'url' => System::baseUrl() . '/newmember',
974 'title' => L10n::t('Tips for New Members'),
975 'id' => 'newmember-tab',
979 if (!$is_owner && empty($a->profile['hide-friends'])) {
981 'label' => L10n::t('Contacts'),
982 'url' => System::baseUrl() . '/viewcontacts/' . $nickname,
983 'sel' => !$tab && $a->argv[0] == 'viewcontacts' ? 'active' : '',
984 'title' => L10n::t('Contacts'),
985 'id' => 'viewcontacts-tab',
990 $arr = ['is_owner' => $is_owner, 'nickname' => $nickname, 'tab' => $tab, 'tabs' => $tabs];
991 Addon::callHooks('profile_tabs', $arr);
993 $tpl = Renderer::getMarkupTemplate('common_tabs.tpl');
995 return Renderer::replaceMacros($tpl, ['$tabs' => $arr['tabs']]);
999 * Retrieves the my_url session variable
1003 public static function getMyURL()
1005 if (!empty($_SESSION['my_url'])) {
1006 return $_SESSION['my_url'];
1012 * Process the 'zrl' parameter and initiate the remote authentication.
1014 * This method checks if the visitor has a public contact entry and
1015 * redirects the visitor to his/her instance to start the magic auth (Authentication)
1018 * Ported from Hubzilla: https://framagit.org/hubzilla/core/blob/master/include/channel.php
1020 * @param App $a Application instance.
1022 public static function zrlInit(App $a)
1024 $my_url = self::getMyURL();
1025 $my_url = Network::isUrlValid($my_url);
1027 if (empty($my_url) || local_user()) {
1031 $arr = ['zrl' => $my_url, 'url' => $a->cmd];
1032 Addon::callHooks('zrl_init', $arr);
1034 // Try to find the public contact entry of the visitor.
1035 $cid = Contact::getIdForURL($my_url);
1037 Logger::log('No contact record found for ' . $my_url, Logger::DEBUG);
1041 $contact = DBA::selectFirst('contact',['id', 'url'], ['id' => $cid]);
1043 if (DBA::isResult($contact) && remote_user() && remote_user() == $contact['id']) {
1044 Logger::log('The visitor ' . $my_url . ' is already authenticated', Logger::DEBUG);
1048 // Avoid endless loops
1049 $cachekey = 'zrlInit:' . $my_url;
1050 if (Cache::get($cachekey)) {
1051 Logger::log('URL ' . $my_url . ' already tried to authenticate.', Logger::DEBUG);
1054 Cache::set($cachekey, true, Cache::MINUTE);
1057 Logger::log('Not authenticated. Invoking reverse magic-auth for ' . $my_url, Logger::DEBUG);
1059 Worker::add(PRIORITY_LOW, 'GProbe', $my_url);
1061 // Try to avoid recursion - but send them home to do a proper magic auth.
1062 $query = str_replace(array('?zrl=', '&zid='), array('?rzrl=', '&rzrl='), $a->query_string);
1063 // The other instance needs to know where to redirect.
1064 $dest = urlencode($a->getBaseURL() . '/' . $query);
1066 // We need to extract the basebath from the profile url
1067 // to redirect the visitors '/magic' module.
1068 // Note: We should have the basepath of a contact also in the contact table.
1069 $urlarr = explode('/profile/', $contact['url']);
1070 $basepath = $urlarr[0];
1072 if ($basepath != $a->getBaseURL() && !strstr($dest, '/magic') && !strstr($dest, '/rmagic')) {
1073 $magic_path = $basepath . '/magic' . '?f=&owa=1&dest=' . $dest;
1075 // We have to check if the remote server does understand /magic without invoking something
1076 $serverret = Network::curl($basepath . '/magic');
1077 if ($serverret->isSuccess()) {
1078 Logger::log('Doing magic auth for visitor ' . $my_url . ' to ' . $magic_path, Logger::DEBUG);
1079 System::externalRedirect($magic_path);
1085 * OpenWebAuth authentication.
1087 * Ported from Hubzilla: https://framagit.org/hubzilla/core/blob/master/include/zid.php
1089 * @param string $token
1091 public static function openWebAuthInit($token)
1095 // Clean old OpenWebAuthToken entries.
1096 OpenWebAuthToken::purge('owt', '3 MINUTE');
1098 // Check if the token we got is the same one
1099 // we have stored in the database.
1100 $visitor_handle = OpenWebAuthToken::getMeta('owt', 0, $token);
1102 if($visitor_handle === false) {
1106 // Try to find the public contact entry of the visitor.
1107 $cid = Contact::getIdForURL($visitor_handle);
1109 Logger::log('owt: unable to finger ' . $visitor_handle, Logger::DEBUG);
1113 $visitor = DBA::selectFirst('contact', [], ['id' => $cid]);
1115 // Authenticate the visitor.
1116 $_SESSION['authenticated'] = 1;
1117 $_SESSION['visitor_id'] = $visitor['id'];
1118 $_SESSION['visitor_handle'] = $visitor['addr'];
1119 $_SESSION['visitor_home'] = $visitor['url'];
1120 $_SESSION['my_url'] = $visitor['url'];
1122 /// @todo replace this and the query for this variable with some cleaner functionality
1123 $_SESSION['remote'] = [];
1125 $remote_contacts = DBA::select('contact', ['id', 'uid'], ['nurl' => $visitor['nurl'], 'rel' => [Contact::FOLLOWER, Contact::FRIEND]]);
1126 while ($contact = DBA::fetch($remote_contacts)) {
1127 if (($contact['uid'] == 0) || Contact::isBlockedByUser($visitor['id'], $contact['uid'])) {
1131 $_SESSION['remote'][] = ['cid' => $contact['id'], 'uid' => $contact['uid'], 'url' => $visitor['url']];
1134 'visitor' => $visitor,
1135 'url' => $a->query_string
1138 * @hooks magic_auth_success
1139 * Called when a magic-auth was successful.
1140 * * \e array \b visitor
1141 * * \e string \b url
1143 Addon::callHooks('magic_auth_success', $arr);
1145 $a->contact = $arr['visitor'];
1147 info(L10n::t('OpenWebAuth: %1$s welcomes %2$s', $a->getHostName(), $visitor['name']));
1149 Logger::log('OpenWebAuth: auth success from ' . $visitor['addr'], Logger::DEBUG);
1152 public static function zrl($s, $force = false)
1157 if ((!strpos($s, '/profile/')) && (!$force)) {
1160 if ($force && substr($s, -1, 1) !== '/') {
1163 $achar = strpos($s, '?') ? '&' : '?';
1164 $mine = self::getMyURL();
1165 if ($mine && !Strings::compareLink($mine, $s)) {
1166 return $s . $achar . 'zrl=' . urlencode($mine);
1172 * Get the user ID of the page owner.
1174 * Used from within PCSS themes to set theme parameters. If there's a
1175 * puid request variable, that is the "page owner" and normally their theme
1176 * settings take precedence; unless a local user sets the "always_my_theme"
1177 * system pconfig, which means they don't want to see anybody else's theme
1178 * settings except their own while on this site.
1180 * @brief Get the user ID of the page owner
1181 * @return int user ID
1183 * @note Returns local_user instead of user ID if "always_my_theme"
1186 public static function getThemeUid()
1188 $uid = (!empty($_REQUEST['puid']) ? intval($_REQUEST['puid']) : 0);
1189 if ((local_user()) && ((PConfig::get(local_user(), 'system', 'always_my_theme')) || (!$uid))) {
1190 return local_user();
1197 * Stip zrl parameter from a string.
1199 * @param string $s The input string.
1200 * @return string The zrl.
1202 public static function stripZrls($s)
1204 return preg_replace('/[\?&]zrl=(.*?)([\?&]|$)/is', '', $s);
1208 * Stip query parameter from a string.
1210 * @param string $s The input string.
1211 * @return string The query parameter.
1213 public static function stripQueryParam($s, $param)
1215 return preg_replace('/[\?&]' . $param . '=(.*?)(&|$)/ism', '$2', $s);