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\Core\Addon;
12 use Friendica\Core\Cache;
13 use Friendica\Core\Config;
14 use Friendica\Core\L10n;
15 use Friendica\Core\PConfig;
16 use Friendica\Core\Protocol;
17 use Friendica\Core\System;
18 use Friendica\Core\Worker;
19 use Friendica\Database\DBA;
20 use Friendica\Model\Contact;
21 use Friendica\Protocol\Diaspora;
22 use Friendica\Util\DateTimeFormat;
23 use Friendica\Util\Network;
24 use Friendica\Util\Proxy as ProxyUtils;
25 use Friendica\Util\Temporal;
27 require_once 'include/dba.php';
32 * @brief Returns default profile for a given user id
34 * @param integer User ID
36 * @return array Profile data
38 public static function getByUID($uid)
40 $profile = DBA::selectFirst('profile', [], ['uid' => $uid, 'is-default' => true]);
45 * @brief Returns a formatted location string from the given profile array
47 * @param array $profile Profile array (Generated from the "profile" table)
49 * @return string Location string
51 public static function formatLocation(array $profile)
55 if (!empty($profile['locality'])) {
56 $location .= $profile['locality'];
59 if (!empty($profile['region']) && (defaults($profile, 'locality', '') != $profile['region'])) {
64 $location .= $profile['region'];
67 if (!empty($profile['country-name'])) {
72 $location .= $profile['country-name'];
80 * Loads a profile into the page sidebar.
82 * The function requires a writeable copy of the main App structure, and the nickname
83 * of a registered local account.
85 * If the viewer is an authenticated remote viewer, the profile displayed is the
86 * one that has been configured for his/her viewing in the Contact manager.
87 * Passing a non-zero profile ID can also allow a preview of a selected profile
90 * Profile information is placed in the App structure for later retrieval.
91 * Honours the owner's chosen theme for display.
93 * @attention Should only be run in the _init() functions of a module. That ensures that
94 * the theme is chosen before the _init() function of a theme is run, which will usually
95 * load a lot of theme-specific content
97 * @brief Loads a profile into the page sidebar.
98 * @param object $a App
99 * @param string $nickname string
100 * @param int $profile int
101 * @param array $profiledata array
102 * @param boolean $show_connect Show connect link
104 public static function load(App $a, $nickname, $profile = 0, array $profiledata = [], $show_connect = true)
106 $user = DBA::selectFirst('user', ['uid'], ['nickname' => $nickname, 'account_removed' => false]);
108 if (!DBA::isResult($user) && empty($profiledata)) {
109 logger('profile error: ' . $a->query_string, LOGGER_DEBUG);
110 notice(L10n::t('Requested account is not available.') . EOL);
115 if (count($profiledata) > 0) {
116 // Add profile data to sidebar
117 $a->page['aside'] .= self::sidebar($profiledata, true, $show_connect);
119 if (!DBA::isResult($user)) {
124 $pdata = self::getByNickname($nickname, $user['uid'], $profile);
126 if (empty($pdata) && empty($profiledata)) {
127 logger('profile error: ' . $a->query_string, LOGGER_DEBUG);
128 notice(L10n::t('Requested profile is not available.') . EOL);
134 $pdata = ['uid' => 0, 'profile_uid' => 0, 'is-default' => false,'name' => $nickname];
137 // fetch user tags if this isn't the default profile
139 if (!$pdata['is-default']) {
140 $condition = ['uid' => $pdata['profile_uid'], 'is-default' => true];
141 $profile = DBA::selectFirst('profile', ['pub_keywords'], $condition);
142 if (DBA::isResult($profile)) {
143 $pdata['pub_keywords'] = $profile['pub_keywords'];
147 $a->profile = $pdata;
148 $a->profile_uid = $pdata['profile_uid'];
150 $a->profile['mobile-theme'] = PConfig::get($a->profile['profile_uid'], 'system', 'mobile_theme');
151 $a->profile['network'] = Protocol::DFRN;
153 $a->page['title'] = $a->profile['name'] . ' @ ' . Config::get('config', 'sitename');
155 if (!$profiledata && !PConfig::get(local_user(), 'system', 'always_my_theme')) {
156 $_SESSION['theme'] = $a->profile['theme'];
159 $_SESSION['mobile-theme'] = $a->profile['mobile-theme'];
162 * load/reload current theme info
165 $a->setActiveTemplateEngine(); // reset the template engine to the default in case the user's theme doesn't specify one
167 $theme_info_file = 'view/theme/' . $a->getCurrentTheme() . '/theme.php';
168 if (file_exists($theme_info_file)) {
169 require_once $theme_info_file;
172 if (local_user() && local_user() == $a->profile['uid'] && $profiledata) {
173 $a->page['aside'] .= replace_macros(
174 get_markup_template('profile_edlink.tpl'),
176 '$editprofile' => L10n::t('Edit profile'),
177 '$profid' => $a->profile['id']
182 $block = ((Config::get('system', 'block_public') && !local_user() && !remote_user()) ? true : false);
186 * By now, the contact block isn't shown, when a different profile is given
187 * But: When this profile was on the same server, then we could display the contacts
190 $a->page['aside'] .= self::sidebar($a->profile, $block, $show_connect);
197 * Get all profile data of a local user
199 * If the viewer is an authenticated remote viewer, the profile displayed is the
200 * one that has been configured for his/her viewing in the Contact manager.
201 * Passing a non-zero profile ID can also allow a preview of a selected profile
204 * Includes all available profile data
206 * @brief Get all profile data of a local user
207 * @param string $nickname nick
208 * @param int $uid uid
209 * @param int $profile_id ID of the profile
212 public static function getByNickname($nickname, $uid = 0, $profile_id = 0)
214 if (remote_user() && !empty($_SESSION['remote'])) {
215 foreach ($_SESSION['remote'] as $visitor) {
216 if ($visitor['uid'] == $uid) {
217 $contact = DBA::selectFirst('contact', ['profile-id'], ['id' => $visitor['cid']]);
218 if (DBA::isResult($contact)) {
219 $profile_id = $contact['profile-id'];
229 $profile = DBA::fetchFirst(
230 "SELECT `contact`.`id` AS `contact_id`, `contact`.`photo` AS `contact_photo`,
231 `contact`.`thumb` AS `contact_thumb`, `contact`.`micro` AS `contact_micro`,
232 `profile`.`uid` AS `profile_uid`, `profile`.*,
233 `contact`.`avatar-date` AS picdate, `contact`.`addr`, `contact`.`url`, `user`.*
235 INNER JOIN `contact` on `contact`.`uid` = `profile`.`uid` AND `contact`.`self`
236 INNER JOIN `user` ON `profile`.`uid` = `user`.`uid`
237 WHERE `user`.`nickname` = ? AND `profile`.`id` = ? LIMIT 1",
242 if (!DBA::isResult($profile)) {
243 $profile = DBA::fetchFirst(
244 "SELECT `contact`.`id` AS `contact_id`, `contact`.`photo` as `contact_photo`,
245 `contact`.`thumb` AS `contact_thumb`, `contact`.`micro` AS `contact_micro`,
246 `profile`.`uid` AS `profile_uid`, `profile`.*,
247 `contact`.`avatar-date` AS picdate, `contact`.`addr`, `contact`.`url`, `user`.*
249 INNER JOIN `contact` ON `contact`.`uid` = `profile`.`uid` AND `contact`.`self`
250 INNER JOIN `user` ON `profile`.`uid` = `user`.`uid`
251 WHERE `user`.`nickname` = ? AND `profile`.`is-default` LIMIT 1",
260 * Formats a profile for display in the sidebar.
262 * It is very difficult to templatise the HTML completely
263 * because of all the conditional logic.
265 * @brief Formats a profile for display in the sidebar.
266 * @param array $profile
268 * @param boolean $show_connect Show connect link
270 * @return string HTML sidebar module
272 * @note Returns empty string if passed $profile is wrong type or not populated
274 * @hooks 'profile_sidebar_enter'
275 * array $profile - profile data
276 * @hooks 'profile_sidebar'
279 private static function sidebar($profile, $block = 0, $show_connect = true)
286 // This function can also use contact information in $profile
287 $is_contact = x($profile, 'cid');
289 if (!is_array($profile) && !count($profile)) {
293 $profile['picdate'] = urlencode(defaults($profile, 'picdate', ''));
295 if (($profile['network'] != '') && ($profile['network'] != Protocol::DFRN)) {
296 $profile['network_name'] = format_network_name($profile['network'], $profile['url']);
298 $profile['network_name'] = '';
301 Addon::callHooks('profile_sidebar_enter', $profile);
304 // don't show connect link to yourself
305 $connect = $profile['uid'] != local_user() ? L10n::t('Connect') : false;
307 // don't show connect link to authenticated visitors either
308 if (remote_user() && !empty($_SESSION['remote'])) {
309 foreach ($_SESSION['remote'] as $visitor) {
310 if ($visitor['uid'] == $profile['uid']) {
317 if (!$show_connect) {
323 // Is the local user already connected to that user?
324 if ($connect && local_user()) {
325 if (isset($profile['url'])) {
326 $profile_url = normalise_link($profile['url']);
328 $profile_url = normalise_link(System::baseUrl() . '/profile/' . $profile['nickname']);
331 if (DBA::exists('contact', ['pending' => false, 'uid' => local_user(), 'nurl' => $profile_url])) {
336 if ($connect && ($profile['network'] != Protocol::DFRN) && !isset($profile['remoteconnect'])) {
340 $remoteconnect = null;
341 if (isset($profile['remoteconnect'])) {
342 $remoteconnect = $profile['remoteconnect'];
345 if ($connect && ($profile['network'] == Protocol::DFRN) && !isset($remoteconnect)) {
346 $subscribe_feed = L10n::t('Atom feed');
348 $subscribe_feed = false;
351 $wallmessage = false;
352 $wallmessage_link = false;
354 // See issue https://github.com/friendica/friendica/issues/3838
355 // Either we remove the message link for remote users or we enable creating messages from remote users
356 if (remote_user() || (self::getMyURL() && x($profile, 'unkmail') && ($profile['uid'] != local_user()))) {
357 $wallmessage = L10n::t('Message');
361 "SELECT `url` FROM `contact` WHERE `uid` = %d AND `id` = '%s' AND `rel` = %d",
362 intval($profile['uid']),
363 intval(remote_user()),
364 intval(Contact::FRIEND)
368 "SELECT `url` FROM `contact` WHERE `uid` = %d AND `nurl` = '%s' AND `rel` = %d",
369 intval($profile['uid']),
370 DBA::escape(normalise_link(self::getMyURL())),
371 intval(Contact::FRIEND)
375 $remote_url = $r[0]['url'];
376 $message_path = preg_replace('=(.*)/profile/(.*)=ism', '$1/message/new/', $remote_url);
377 $wallmessage_link = $message_path . base64_encode(defaults($profile, 'addr', ''));
378 } else if (!empty($profile['nickname'])) {
379 $wallmessage_link = 'wallmessage/' . $profile['nickname'];
383 // show edit profile to yourself
384 if (!$is_contact && $profile['uid'] == local_user() && Feature::isEnabled(local_user(), 'multi_profiles')) {
385 $profile['edit'] = [System::baseUrl() . '/profiles', L10n::t('Profiles'), '', L10n::t('Manage/edit profiles')];
387 "SELECT * FROM `profile` WHERE `uid` = %d",
392 'chg_photo' => L10n::t('Change profile photo'),
393 'cr_new' => L10n::t('Create New Profile'),
397 if (DBA::isResult($r)) {
398 foreach ($r as $rr) {
399 $profile['menu']['entries'][] = [
400 'photo' => $rr['thumb'],
402 'alt' => L10n::t('Profile Image'),
403 'profile_name' => $rr['profile-name'],
404 'isdefault' => $rr['is-default'],
405 'visibile_to_everybody' => L10n::t('visible to everybody'),
406 'edit_visibility' => L10n::t('Edit visibility'),
411 if (!$is_contact && $profile['uid'] == local_user() && !Feature::isEnabled(local_user(), 'multi_profiles')) {
412 $profile['edit'] = [System::baseUrl() . '/profiles/' . $profile['id'], L10n::t('Edit profile'), '', L10n::t('Edit profile')];
414 'chg_photo' => L10n::t('Change profile photo'),
420 // Fetch the account type
421 $account_type = Contact::getAccountType($profile);
423 if (x($profile, 'address')
424 || x($profile, 'location')
425 || x($profile, 'locality')
426 || x($profile, 'region')
427 || x($profile, 'postal-code')
428 || x($profile, 'country-name')
430 $location = L10n::t('Location:');
433 $gender = x($profile, 'gender') ? L10n::t('Gender:') : false;
434 $marital = x($profile, 'marital') ? L10n::t('Status:') : false;
435 $homepage = x($profile, 'homepage') ? L10n::t('Homepage:') : false;
436 $about = x($profile, 'about') ? L10n::t('About:') : false;
437 $xmpp = x($profile, 'xmpp') ? L10n::t('XMPP:') : false;
439 if ((x($profile, 'hidewall') || $block) && !local_user() && !remote_user()) {
440 $location = $gender = $marital = $homepage = $about = false;
443 $split_name = Diaspora::splitName($profile['name']);
444 $firstname = $split_name['first'];
445 $lastname = $split_name['last'];
447 if (x($profile, 'guid')) {
449 'guid' => $profile['guid'],
450 'podloc' => System::baseUrl(),
451 'searchable' => (($profile['publish'] && $profile['net-publish']) ? 'true' : 'false' ),
452 'nickname' => $profile['nickname'],
453 'fullname' => $profile['name'],
454 'firstname' => $firstname,
455 'lastname' => $lastname,
456 'photo300' => defaults($profile, 'contact_photo', ''),
457 'photo100' => defaults($profile, 'contact_thumb', ''),
458 'photo50' => defaults($profile, 'contact_micro', ''),
468 $contact_block = contact_block();
470 if (is_array($a->profile) && !$a->profile['hide-friends']) {
472 "SELECT `gcontact`.`updated` FROM `contact` INNER JOIN `gcontact` WHERE `gcontact`.`nurl` = `contact`.`nurl` AND `self` AND `uid` = %d LIMIT 1",
473 intval($a->profile['uid'])
475 if (DBA::isResult($r)) {
476 $updated = date('c', strtotime($r[0]['updated']));
480 "SELECT COUNT(*) AS `total` FROM `contact`
482 AND NOT `self` AND NOT `blocked` AND NOT `pending`
483 AND NOT `hidden` AND NOT `archive`
484 AND `network` IN ('%s', '%s', '%s', '')",
485 intval($profile['uid']),
486 DBA::escape(Protocol::DFRN),
487 DBA::escape(Protocol::DIASPORA),
488 DBA::escape(Protocol::OSTATUS)
490 if (DBA::isResult($r)) {
491 $contacts = intval($r[0]['total']);
497 foreach ($profile as $k => $v) {
498 $k = str_replace('-', '_', $k);
502 if (isset($p['about'])) {
503 $p['about'] = BBCode::convert($p['about']);
506 if (isset($p['address'])) {
507 $p['address'] = BBCode::convert($p['address']);
508 } elseif (isset($p['location'])) {
509 $p['address'] = BBCode::convert($p['location']);
512 if (isset($p['photo'])) {
513 $p['photo'] = ProxyUtils::proxifyUrl($p['photo'], false, ProxyUtils::SIZE_SMALL);
516 $p['url'] = Contact::magicLink(defaults($p, 'url', $profile_url));
518 $tpl = get_markup_template('profile_vcard.tpl');
519 $o .= replace_macros($tpl, [
522 '$connect' => $connect,
523 '$remoteconnect' => $remoteconnect,
524 '$subscribe_feed' => $subscribe_feed,
525 '$wallmessage' => $wallmessage,
526 '$wallmessage_link' => $wallmessage_link,
527 '$account_type' => $account_type,
528 '$location' => $location,
529 '$gender' => $gender,
530 '$marital' => $marital,
531 '$homepage' => $homepage,
533 '$network' => L10n::t('Network:'),
534 '$contacts' => $contacts,
535 '$updated' => $updated,
536 '$diaspora' => $diaspora,
537 '$contact_block' => $contact_block,
540 $arr = ['profile' => &$profile, 'entry' => &$o];
542 Addon::callHooks('profile_sidebar', $arr);
547 public static function getBirthdays()
552 if (!local_user() || $a->is_mobile || $a->is_tablet) {
557 * $mobile_detect = new Mobile_Detect();
558 * $is_mobile = $mobile_detect->isMobile() || $mobile_detect->isTablet();
563 $bd_format = L10n::t('g A l F d'); // 8 AM Friday January 18
564 $bd_short = L10n::t('F d');
566 $cachekey = 'get_birthdays:' . local_user();
567 $r = Cache::get($cachekey);
570 "SELECT `event`.*, `event`.`id` AS `eid`, `contact`.* FROM `event`
571 INNER JOIN `contact` ON `contact`.`id` = `event`.`cid`
572 WHERE `event`.`uid` = ? AND `type` = 'birthday' AND `start` < ? AND `finish` > ?
573 ORDER BY `start` ASC ",
575 DateTimeFormat::utc('now + 6 days'),
576 DateTimeFormat::utcNow()
578 if (DBA::isResult($s)) {
579 $r = DBA::toArray($s);
580 Cache::set($cachekey, $r, CACHE_HOUR);
586 if (DBA::isResult($r)) {
587 $now = strtotime('now');
591 foreach ($r as $rr) {
592 if (strlen($rr['name'])) {
595 if ((strtotime($rr['start'] . ' +00:00') < $now) && (strtotime($rr['finish'] . ' +00:00') > $now)) {
599 $classtoday = $istoday ? ' birthday-today ' : '';
601 foreach ($r as &$rr) {
602 if (!strlen($rr['name'])) {
608 if (in_array($rr['cid'], $cids)) {
611 $cids[] = $rr['cid'];
613 $today = (((strtotime($rr['start'] . ' +00:00') < $now) && (strtotime($rr['finish'] . ' +00:00') > $now)) ? true : false);
615 $rr['link'] = Contact::magicLink($rr['url']);
616 $rr['title'] = $rr['name'];
617 $rr['date'] = day_translate(DateTimeFormat::convert($rr['start'], $a->timezone, 'UTC', $rr['adjust'] ? $bd_format : $bd_short)) . (($today) ? ' ' . L10n::t('[today]') : '');
618 $rr['startime'] = null;
619 $rr['today'] = $today;
623 $tpl = get_markup_template('birthdays_reminder.tpl');
624 return replace_macros($tpl, [
625 '$baseurl' => System::baseUrl(),
626 '$classtoday' => $classtoday,
628 '$event_reminders' => L10n::t('Birthday Reminders'),
629 '$event_title' => L10n::t('Birthdays this week:'),
631 '$lbr' => '{', // raw brackets mess up if/endif macro processing
636 public static function getEventsReminderHTML()
641 if (!local_user() || $a->is_mobile || $a->is_tablet) {
646 * $mobile_detect = new Mobile_Detect();
647 * $is_mobile = $mobile_detect->isMobile() || $mobile_detect->isTablet();
652 $bd_format = L10n::t('g A l F d'); // 8 AM Friday January 18
655 $condition = ["`uid` = ? AND `type` != 'birthday' AND `start` < ? AND `start` >= ?",
656 local_user(), DateTimeFormat::utc('now + 7 days'), DateTimeFormat::utc('now - 1 days')];
657 $s = DBA::select('event', [], $condition, ['order' => ['start']]);
661 if (DBA::isResult($s)) {
665 while ($rr = DBA::fetch($s)) {
666 $condition = ['parent-uri' => $rr['uri'], 'uid' => $rr['uid'], 'author-id' => public_contact(),
667 'activity' => [Item::activityToIndex(ACTIVITY_ATTEND), Item::activityToIndex(ACTIVITY_ATTENDMAYBE)],
668 'visible' => true, 'deleted' => false];
669 if (!Item::exists($condition)) {
673 if (strlen($rr['summary'])) {
677 $strt = DateTimeFormat::convert($rr['start'], $rr['adjust'] ? $a->timezone : 'UTC', 'UTC', 'Y-m-d');
678 if ($strt === DateTimeFormat::timezoneNow($a->timezone, 'Y-m-d')) {
682 $title = strip_tags(html_entity_decode(BBCode::convert($rr['summary']), ENT_QUOTES, 'UTF-8'));
684 if (strlen($title) > 35) {
685 $title = substr($title, 0, 32) . '... ';
688 $description = substr(strip_tags(BBCode::convert($rr['desc'])), 0, 32) . '... ';
690 $description = L10n::t('[No description]');
693 $strt = DateTimeFormat::convert($rr['start'], $rr['adjust'] ? $a->timezone : 'UTC');
695 if (substr($strt, 0, 10) < DateTimeFormat::timezoneNow($a->timezone, 'Y-m-d')) {
699 $today = ((substr($strt, 0, 10) === DateTimeFormat::timezoneNow($a->timezone, 'Y-m-d')) ? true : false);
701 $rr['title'] = $title;
702 $rr['description'] = $description;
703 $rr['date'] = day_translate(DateTimeFormat::convert($rr['start'], $rr['adjust'] ? $a->timezone : 'UTC', 'UTC', $bd_format)) . (($today) ? ' ' . L10n::t('[today]') : '');
704 $rr['startime'] = $strt;
705 $rr['today'] = $today;
710 $classtoday = (($istoday) ? 'event-today' : '');
712 $tpl = get_markup_template('events_reminder.tpl');
713 return replace_macros($tpl, [
714 '$baseurl' => System::baseUrl(),
715 '$classtoday' => $classtoday,
716 '$count' => count($r),
717 '$event_reminders' => L10n::t('Event Reminders'),
718 '$event_title' => L10n::t('Upcoming events the next 7 days:'),
723 public static function getAdvanced(App $a)
726 $uid = $a->profile['uid'];
728 $o .= replace_macros(
729 get_markup_template('section_title.tpl'),
730 ['$title' => L10n::t('Profile')]
733 if ($a->profile['name']) {
734 $tpl = get_markup_template('profile_advanced.tpl');
738 $profile['fullname'] = [L10n::t('Full Name:'), $a->profile['name']];
740 if (Feature::isEnabled($uid, 'profile_membersince')) {
741 $profile['membersince'] = [L10n::t('Member since:'), DateTimeFormat::local($a->profile['register_date'])];
744 if ($a->profile['gender']) {
745 $profile['gender'] = [L10n::t('Gender:'), $a->profile['gender']];
748 if (($a->profile['dob']) && ($a->profile['dob'] > '0001-01-01')) {
749 $year_bd_format = L10n::t('j F, Y');
750 $short_bd_format = L10n::t('j F');
752 $val = day_translate(
753 intval($a->profile['dob']) ?
754 DateTimeFormat::utc($a->profile['dob'] . ' 00:00 +00:00', $year_bd_format)
755 : DateTimeFormat::utc('2001-' . substr($a->profile['dob'], 5) . ' 00:00 +00:00', $short_bd_format)
758 $profile['birthday'] = [L10n::t('Birthday:'), $val];
761 if (!empty($a->profile['dob'])
762 && $a->profile['dob'] > '0001-01-01'
763 && $age = Temporal::getAgeByTimezone($a->profile['dob'], $a->profile['timezone'], '')
765 $profile['age'] = [L10n::t('Age:'), $age];
768 if ($a->profile['marital']) {
769 $profile['marital'] = [L10n::t('Status:'), $a->profile['marital']];
772 /// @TODO Maybe use x() here, plus below?
773 if ($a->profile['with']) {
774 $profile['marital']['with'] = $a->profile['with'];
777 if (strlen($a->profile['howlong']) && $a->profile['howlong'] >= NULL_DATE) {
778 $profile['howlong'] = Temporal::getRelativeDate($a->profile['howlong'], L10n::t('for %1$d %2$s'));
781 if ($a->profile['sexual']) {
782 $profile['sexual'] = [L10n::t('Sexual Preference:'), $a->profile['sexual']];
785 if ($a->profile['homepage']) {
786 $profile['homepage'] = [L10n::t('Homepage:'), linkify($a->profile['homepage'])];
789 if ($a->profile['hometown']) {
790 $profile['hometown'] = [L10n::t('Hometown:'), linkify($a->profile['hometown'])];
793 if ($a->profile['pub_keywords']) {
794 $profile['pub_keywords'] = [L10n::t('Tags:'), $a->profile['pub_keywords']];
797 if ($a->profile['politic']) {
798 $profile['politic'] = [L10n::t('Political Views:'), $a->profile['politic']];
801 if ($a->profile['religion']) {
802 $profile['religion'] = [L10n::t('Religion:'), $a->profile['religion']];
805 if ($txt = prepare_text($a->profile['about'])) {
806 $profile['about'] = [L10n::t('About:'), $txt];
809 if ($txt = prepare_text($a->profile['interest'])) {
810 $profile['interest'] = [L10n::t('Hobbies/Interests:'), $txt];
813 if ($txt = prepare_text($a->profile['likes'])) {
814 $profile['likes'] = [L10n::t('Likes:'), $txt];
817 if ($txt = prepare_text($a->profile['dislikes'])) {
818 $profile['dislikes'] = [L10n::t('Dislikes:'), $txt];
821 if ($txt = prepare_text($a->profile['contact'])) {
822 $profile['contact'] = [L10n::t('Contact information and Social Networks:'), $txt];
825 if ($txt = prepare_text($a->profile['music'])) {
826 $profile['music'] = [L10n::t('Musical interests:'), $txt];
829 if ($txt = prepare_text($a->profile['book'])) {
830 $profile['book'] = [L10n::t('Books, literature:'), $txt];
833 if ($txt = prepare_text($a->profile['tv'])) {
834 $profile['tv'] = [L10n::t('Television:'), $txt];
837 if ($txt = prepare_text($a->profile['film'])) {
838 $profile['film'] = [L10n::t('Film/dance/culture/entertainment:'), $txt];
841 if ($txt = prepare_text($a->profile['romance'])) {
842 $profile['romance'] = [L10n::t('Love/Romance:'), $txt];
845 if ($txt = prepare_text($a->profile['work'])) {
846 $profile['work'] = [L10n::t('Work/employment:'), $txt];
849 if ($txt = prepare_text($a->profile['education'])) {
850 $profile['education'] = [L10n::t('School/education:'), $txt];
853 //show subcribed forum if it is enabled in the usersettings
854 if (Feature::isEnabled($uid, 'forumlist_profile')) {
855 $profile['forumlist'] = [L10n::t('Forums:'), ForumManager::profileAdvanced($uid)];
858 if ($a->profile['uid'] == local_user()) {
859 $profile['edit'] = [System::baseUrl() . '/profiles/' . $a->profile['id'], L10n::t('Edit profile'), '', L10n::t('Edit profile')];
862 return replace_macros($tpl, [
863 '$title' => L10n::t('Profile'),
864 '$basic' => L10n::t('Basic'),
865 '$advanced' => L10n::t('Advanced'),
866 '$profile' => $profile
873 public static function getTabs($a, $is_owner = false, $nickname = null)
875 if (is_null($nickname)) {
876 $nickname = $a->user['nickname'];
880 if (x($_GET, 'tab')) {
881 $tab = notags(trim($_GET['tab']));
884 $url = System::baseUrl() . '/profile/' . $nickname;
888 'label' => L10n::t('Status'),
890 'sel' => !$tab && $a->argv[0] == 'profile' ? 'active' : '',
891 'title' => L10n::t('Status Messages and Posts'),
892 'id' => 'status-tab',
896 'label' => L10n::t('Profile'),
897 'url' => $url . '/?tab=profile',
898 'sel' => $tab == 'profile' ? 'active' : '',
899 'title' => L10n::t('Profile Details'),
900 'id' => 'profile-tab',
904 'label' => L10n::t('Photos'),
905 'url' => System::baseUrl() . '/photos/' . $nickname,
906 'sel' => !$tab && $a->argv[0] == 'photos' ? 'active' : '',
907 'title' => L10n::t('Photo Albums'),
912 'label' => L10n::t('Videos'),
913 'url' => System::baseUrl() . '/videos/' . $nickname,
914 'sel' => !$tab && $a->argv[0] == 'videos' ? 'active' : '',
915 'title' => L10n::t('Videos'),
921 // the calendar link for the full featured events calendar
922 if ($is_owner && $a->theme_events_in_profile) {
924 'label' => L10n::t('Events'),
925 'url' => System::baseUrl() . '/events',
926 'sel' => !$tab && $a->argv[0] == 'events' ? 'active' : '',
927 'title' => L10n::t('Events and Calendar'),
928 'id' => 'events-tab',
931 // if the user is not the owner of the calendar we only show a calendar
932 // with the public events of the calendar owner
933 } elseif (!$is_owner) {
935 'label' => L10n::t('Events'),
936 'url' => System::baseUrl() . '/cal/' . $nickname,
937 'sel' => !$tab && $a->argv[0] == 'cal' ? 'active' : '',
938 'title' => L10n::t('Events and Calendar'),
939 'id' => 'events-tab',
946 'label' => L10n::t('Personal Notes'),
947 'url' => System::baseUrl() . '/notes',
948 'sel' => !$tab && $a->argv[0] == 'notes' ? 'active' : '',
949 'title' => L10n::t('Only You Can See This'),
955 if (!empty($_SESSION['new_member']) && $is_owner) {
957 'label' => L10n::t('Tips for New Members'),
958 'url' => System::baseUrl() . '/newmember',
960 'title' => L10n::t('Tips for New Members'),
961 'id' => 'newmember-tab',
965 if (!$is_owner && empty($a->profile['hide-friends'])) {
967 'label' => L10n::t('Contacts'),
968 'url' => System::baseUrl() . '/viewcontacts/' . $nickname,
969 'sel' => !$tab && $a->argv[0] == 'viewcontacts' ? 'active' : '',
970 'title' => L10n::t('Contacts'),
971 'id' => 'viewcontacts-tab',
976 $arr = ['is_owner' => $is_owner, 'nickname' => $nickname, 'tab' => $tab, 'tabs' => $tabs];
977 Addon::callHooks('profile_tabs', $arr);
979 $tpl = get_markup_template('common_tabs.tpl');
981 return replace_macros($tpl, ['$tabs' => $arr['tabs']]);
985 * Retrieves the my_url session variable
989 public static function getMyURL()
991 if (x($_SESSION, 'my_url')) {
992 return $_SESSION['my_url'];
998 * Process the 'zrl' parameter and initiate the remote authentication.
1000 * This method checks if the visitor has a public contact entry and
1001 * redirects the visitor to his/her instance to start the magic auth (Authentication)
1004 * Ported from Hubzilla: https://framagit.org/hubzilla/core/blob/master/include/channel.php
1006 * @param App $a Application instance.
1008 public static function zrlInit(App $a)
1010 $my_url = self::getMyURL();
1011 $my_url = Network::isUrlValid($my_url);
1013 if (empty($my_url) || local_user()) {
1017 $arr = ['zrl' => $my_url, 'url' => $a->cmd];
1018 Addon::callHooks('zrl_init', $arr);
1020 // Try to find the public contact entry of the visitor.
1021 $cid = Contact::getIdForURL($my_url);
1023 logger('No contact record found for ' . $my_url, LOGGER_DEBUG);
1027 $contact = DBA::selectFirst('contact',['id', 'url'], ['id' => $cid]);
1029 if (DBA::isResult($contact) && remote_user() && remote_user() == $contact['id']) {
1030 logger('The visitor ' . $my_url . ' is already authenticated', LOGGER_DEBUG);
1034 // Avoid endless loops
1035 $cachekey = 'zrlInit:' . $my_url;
1036 if (Cache::get($cachekey)) {
1037 logger('URL ' . $my_url . ' already tried to authenticate.', LOGGER_DEBUG);
1040 Cache::set($cachekey, true, CACHE_MINUTE);
1043 logger('Not authenticated. Invoking reverse magic-auth for ' . $my_url, LOGGER_DEBUG);
1045 Worker::add(PRIORITY_LOW, 'GProbe', $my_url);
1047 // Try to avoid recursion - but send them home to do a proper magic auth.
1048 $query = str_replace(array('?zrl=', '&zid='), array('?rzrl=', '&rzrl='), $a->query_string);
1049 // The other instance needs to know where to redirect.
1050 $dest = urlencode(System::baseUrl() . '/' . $query);
1052 // We need to extract the basebath from the profile url
1053 // to redirect the visitors '/magic' module.
1054 // Note: We should have the basepath of a contact also in the contact table.
1055 $urlarr = explode('/profile/', $contact['url']);
1056 $basepath = $urlarr[0];
1058 if ($basepath != System::baseUrl() && !strstr($dest, '/magic') && !strstr($dest, '/rmagic')) {
1059 $magic_path = $basepath . '/magic' . '?f=&owa=1&dest=' . $dest;
1061 // We have to check if the remote server does understand /magic without invoking something
1062 $serverret = Network::curl($basepath . '/magic');
1063 if ($serverret->isSuccess()) {
1064 logger('Doing magic auth for visitor ' . $my_url . ' to ' . $magic_path, LOGGER_DEBUG);
1065 goaway($magic_path);
1071 * OpenWebAuth authentication.
1073 * Ported from Hubzilla: https://framagit.org/hubzilla/core/blob/master/include/zid.php
1075 * @param string $token
1077 public static function openWebAuthInit($token)
1081 // Clean old OpenWebAuthToken entries.
1082 OpenWebAuthToken::purge('owt', '3 MINUTE');
1084 // Check if the token we got is the same one
1085 // we have stored in the database.
1086 $visitor_handle = OpenWebAuthToken::getMeta('owt', 0, $token);
1088 if($visitor_handle === false) {
1092 // Try to find the public contact entry of the visitor.
1093 $cid = Contact::getIdForURL($visitor_handle);
1095 logger('owt: unable to finger ' . $visitor_handle, LOGGER_DEBUG);
1099 $visitor = DBA::selectFirst('contact', [], ['id' => $cid]);
1101 // Authenticate the visitor.
1102 $_SESSION['authenticated'] = 1;
1103 $_SESSION['visitor_id'] = $visitor['id'];
1104 $_SESSION['visitor_handle'] = $visitor['addr'];
1105 $_SESSION['visitor_home'] = $visitor['url'];
1106 $_SESSION['my_url'] = $visitor['url'];
1109 'visitor' => $visitor,
1110 'url' => $a->query_string
1113 * @hooks magic_auth_success
1114 * Called when a magic-auth was successful.
1115 * * \e array \b visitor
1116 * * \e string \b url
1118 Addon::callHooks('magic_auth_success', $arr);
1120 $a->contact = $arr['visitor'];
1122 info(L10n::t('OpenWebAuth: %1$s welcomes %2$s', $a->getHostName(), $visitor['name']));
1124 logger('OpenWebAuth: auth success from ' . $visitor['addr'], LOGGER_DEBUG);
1127 public static function zrl($s, $force = false)
1132 if ((!strpos($s, '/profile/')) && (!$force)) {
1135 if ($force && substr($s, -1, 1) !== '/') {
1138 $achar = strpos($s, '?') ? '&' : '?';
1139 $mine = self::getMyURL();
1140 if ($mine && !link_compare($mine, $s)) {
1141 return $s . $achar . 'zrl=' . urlencode($mine);
1147 * Get the user ID of the page owner.
1149 * Used from within PCSS themes to set theme parameters. If there's a
1150 * puid request variable, that is the "page owner" and normally their theme
1151 * settings take precedence; unless a local user sets the "always_my_theme"
1152 * system pconfig, which means they don't want to see anybody else's theme
1153 * settings except their own while on this site.
1155 * @brief Get the user ID of the page owner
1156 * @return int user ID
1158 * @note Returns local_user instead of user ID if "always_my_theme"
1161 public static function getThemeUid()
1163 $uid = ((!empty($_REQUEST['puid'])) ? intval($_REQUEST['puid']) : 0);
1164 if ((local_user()) && ((PConfig::get(local_user(), 'system', 'always_my_theme')) || (!$uid))) {
1165 return local_user();
1172 * Stip zrl parameter from a string.
1174 * @param string $s The input string.
1175 * @return string The zrl.
1177 public static function stripZrls($s)
1179 return preg_replace('/[\?&]zrl=(.*?)([\?&]|$)/is', '', $s);
1183 * Stip query parameter from a string.
1185 * @param string $s The input string.
1186 * @return string The query parameter.
1188 public static function stripQueryParam($s, $param)
1190 return preg_replace('/[\?&]' . $param . '=(.*?)(&|$)/ism', '$2', $s);