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\Logger;
16 use Friendica\Core\PConfig;
17 use Friendica\Core\Protocol;
18 use Friendica\Core\Renderer;
19 use Friendica\Core\System;
20 use Friendica\Core\Worker;
21 use Friendica\Database\DBA;
22 use Friendica\Model\Contact;
23 use Friendica\Protocol\Diaspora;
24 use Friendica\Util\DateTimeFormat;
25 use Friendica\Util\Network;
26 use Friendica\Util\Proxy as ProxyUtils;
27 use Friendica\Util\Temporal;
29 require_once 'include/dba.php';
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.
100 * @param object $a App
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 $a->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 = x($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_name'] = format_network_name($profile['network'], $profile['url']);
300 $profile['network_name'] = '';
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 = normalise_link($profile['url']);
330 $profile_url = normalise_link(System::baseUrl() . '/profile/' . $profile['nickname']);
333 if (DBA::exists('contact', ['pending' => false, 'uid' => local_user(), 'nurl' => $profile_url])) {
338 if ($connect && ($profile['network'] != Protocol::DFRN) && !isset($profile['remoteconnect'])) {
342 $remoteconnect = null;
343 if (isset($profile['remoteconnect'])) {
344 $remoteconnect = $profile['remoteconnect'];
347 if ($connect && ($profile['network'] == Protocol::DFRN) && !isset($remoteconnect)) {
348 $subscribe_feed = L10n::t('Atom feed');
350 $subscribe_feed = false;
353 $wallmessage = false;
354 $wallmessage_link = false;
356 // See issue https://github.com/friendica/friendica/issues/3838
357 // Either we remove the message link for remote users or we enable creating messages from remote users
358 if (remote_user() || (self::getMyURL() && x($profile, 'unkmail') && ($profile['uid'] != local_user()))) {
359 $wallmessage = L10n::t('Message');
363 "SELECT `url` FROM `contact` WHERE `uid` = %d AND `id` = '%s' AND `rel` = %d",
364 intval($profile['uid']),
365 intval(remote_user()),
366 intval(Contact::FRIEND)
370 "SELECT `url` FROM `contact` WHERE `uid` = %d AND `nurl` = '%s' AND `rel` = %d",
371 intval($profile['uid']),
372 DBA::escape(normalise_link(self::getMyURL())),
373 intval(Contact::FRIEND)
377 $remote_url = $r[0]['url'];
378 $message_path = preg_replace('=(.*)/profile/(.*)=ism', '$1/message/new/', $remote_url);
379 $wallmessage_link = $message_path . base64_encode(defaults($profile, 'addr', ''));
380 } else if (!empty($profile['nickname'])) {
381 $wallmessage_link = 'wallmessage/' . $profile['nickname'];
385 // show edit profile to yourself
386 if (!$is_contact && $profile['uid'] == local_user() && Feature::isEnabled(local_user(), 'multi_profiles')) {
387 $profile['edit'] = [System::baseUrl() . '/profiles', L10n::t('Profiles'), '', L10n::t('Manage/edit profiles')];
389 "SELECT * FROM `profile` WHERE `uid` = %d",
394 'chg_photo' => L10n::t('Change profile photo'),
395 'cr_new' => L10n::t('Create New Profile'),
399 if (DBA::isResult($r)) {
400 foreach ($r as $rr) {
401 $profile['menu']['entries'][] = [
402 'photo' => $rr['thumb'],
404 'alt' => L10n::t('Profile Image'),
405 'profile_name' => $rr['profile-name'],
406 'isdefault' => $rr['is-default'],
407 'visibile_to_everybody' => L10n::t('visible to everybody'),
408 'edit_visibility' => L10n::t('Edit visibility'),
413 if (!$is_contact && $profile['uid'] == local_user() && !Feature::isEnabled(local_user(), 'multi_profiles')) {
414 $profile['edit'] = [System::baseUrl() . '/profiles/' . $profile['id'], L10n::t('Edit profile'), '', L10n::t('Edit profile')];
416 'chg_photo' => L10n::t('Change profile photo'),
422 // Fetch the account type
423 $account_type = Contact::getAccountType($profile);
425 if (x($profile, 'address')
426 || x($profile, 'location')
427 || x($profile, 'locality')
428 || x($profile, 'region')
429 || x($profile, 'postal-code')
430 || x($profile, 'country-name')
432 $location = L10n::t('Location:');
435 $gender = x($profile, 'gender') ? L10n::t('Gender:') : false;
436 $marital = x($profile, 'marital') ? L10n::t('Status:') : false;
437 $homepage = x($profile, 'homepage') ? L10n::t('Homepage:') : false;
438 $about = x($profile, 'about') ? L10n::t('About:') : false;
439 $xmpp = x($profile, 'xmpp') ? L10n::t('XMPP:') : false;
441 if ((x($profile, 'hidewall') || $block) && !local_user() && !remote_user()) {
442 $location = $gender = $marital = $homepage = $about = false;
445 $split_name = Diaspora::splitName($profile['name']);
446 $firstname = $split_name['first'];
447 $lastname = $split_name['last'];
449 if (x($profile, 'guid')) {
451 'guid' => $profile['guid'],
452 'podloc' => System::baseUrl(),
453 'searchable' => (($profile['publish'] && $profile['net-publish']) ? 'true' : 'false' ),
454 'nickname' => $profile['nickname'],
455 'fullname' => $profile['name'],
456 'firstname' => $firstname,
457 'lastname' => $lastname,
458 'photo300' => defaults($profile, 'contact_photo', ''),
459 'photo100' => defaults($profile, 'contact_thumb', ''),
460 'photo50' => defaults($profile, 'contact_micro', ''),
470 $contact_block = contact_block();
472 if (is_array($a->profile) && !$a->profile['hide-friends']) {
474 "SELECT `gcontact`.`updated` FROM `contact` INNER JOIN `gcontact` WHERE `gcontact`.`nurl` = `contact`.`nurl` AND `self` AND `uid` = %d LIMIT 1",
475 intval($a->profile['uid'])
477 if (DBA::isResult($r)) {
478 $updated = date('c', strtotime($r[0]['updated']));
482 "SELECT COUNT(*) AS `total` FROM `contact`
484 AND NOT `self` AND NOT `blocked` AND NOT `pending`
485 AND NOT `hidden` AND NOT `archive`
486 AND `network` IN ('%s', '%s', '%s', '')",
487 intval($profile['uid']),
488 DBA::escape(Protocol::DFRN),
489 DBA::escape(Protocol::DIASPORA),
490 DBA::escape(Protocol::OSTATUS)
492 if (DBA::isResult($r)) {
493 $contacts = intval($r[0]['total']);
499 foreach ($profile as $k => $v) {
500 $k = str_replace('-', '_', $k);
504 if (isset($p['about'])) {
505 $p['about'] = BBCode::convert($p['about']);
508 if (isset($p['address'])) {
509 $p['address'] = BBCode::convert($p['address']);
510 } elseif (isset($p['location'])) {
511 $p['address'] = BBCode::convert($p['location']);
514 if (isset($p['photo'])) {
515 $p['photo'] = ProxyUtils::proxifyUrl($p['photo'], false, ProxyUtils::SIZE_SMALL);
518 $p['url'] = Contact::magicLink(defaults($p, 'url', $profile_url));
520 $tpl = Renderer::getMarkupTemplate('profile_vcard.tpl');
521 $o .= Renderer::replaceMacros($tpl, [
524 '$connect' => $connect,
525 '$remoteconnect' => $remoteconnect,
526 '$subscribe_feed' => $subscribe_feed,
527 '$wallmessage' => $wallmessage,
528 '$wallmessage_link' => $wallmessage_link,
529 '$account_type' => $account_type,
530 '$location' => $location,
531 '$gender' => $gender,
532 '$marital' => $marital,
533 '$homepage' => $homepage,
535 '$network' => L10n::t('Network:'),
536 '$contacts' => $contacts,
537 '$updated' => $updated,
538 '$diaspora' => $diaspora,
539 '$contact_block' => $contact_block,
542 $arr = ['profile' => &$profile, 'entry' => &$o];
544 Addon::callHooks('profile_sidebar', $arr);
549 public static function getBirthdays()
554 if (!local_user() || $a->is_mobile || $a->is_tablet) {
559 * $mobile_detect = new Mobile_Detect();
560 * $is_mobile = $mobile_detect->isMobile() || $mobile_detect->isTablet();
565 $bd_format = L10n::t('g A l F d'); // 8 AM Friday January 18
566 $bd_short = L10n::t('F d');
568 $cachekey = 'get_birthdays:' . local_user();
569 $r = Cache::get($cachekey);
572 "SELECT `event`.*, `event`.`id` AS `eid`, `contact`.* FROM `event`
573 INNER JOIN `contact` ON `contact`.`id` = `event`.`cid`
574 WHERE `event`.`uid` = ? AND `type` = 'birthday' AND `start` < ? AND `finish` > ?
575 ORDER BY `start` ASC ",
577 DateTimeFormat::utc('now + 6 days'),
578 DateTimeFormat::utcNow()
580 if (DBA::isResult($s)) {
581 $r = DBA::toArray($s);
582 Cache::set($cachekey, $r, Cache::HOUR);
588 if (DBA::isResult($r)) {
589 $now = strtotime('now');
593 foreach ($r as $rr) {
594 if (strlen($rr['name'])) {
597 if ((strtotime($rr['start'] . ' +00:00') < $now) && (strtotime($rr['finish'] . ' +00:00') > $now)) {
601 $classtoday = $istoday ? ' birthday-today ' : '';
603 foreach ($r as &$rr) {
604 if (!strlen($rr['name'])) {
610 if (in_array($rr['cid'], $cids)) {
613 $cids[] = $rr['cid'];
615 $today = (((strtotime($rr['start'] . ' +00:00') < $now) && (strtotime($rr['finish'] . ' +00:00') > $now)) ? true : false);
617 $rr['link'] = Contact::magicLink($rr['url']);
618 $rr['title'] = $rr['name'];
619 $rr['date'] = day_translate(DateTimeFormat::convert($rr['start'], $a->timezone, 'UTC', $rr['adjust'] ? $bd_format : $bd_short)) . (($today) ? ' ' . L10n::t('[today]') : '');
620 $rr['startime'] = null;
621 $rr['today'] = $today;
625 $tpl = Renderer::getMarkupTemplate('birthdays_reminder.tpl');
626 return Renderer::replaceMacros($tpl, [
627 '$baseurl' => System::baseUrl(),
628 '$classtoday' => $classtoday,
630 '$event_reminders' => L10n::t('Birthday Reminders'),
631 '$event_title' => L10n::t('Birthdays this week:'),
633 '$lbr' => '{', // raw brackets mess up if/endif macro processing
638 public static function getEventsReminderHTML()
643 if (!local_user() || $a->is_mobile || $a->is_tablet) {
648 * $mobile_detect = new Mobile_Detect();
649 * $is_mobile = $mobile_detect->isMobile() || $mobile_detect->isTablet();
654 $bd_format = L10n::t('g A l F d'); // 8 AM Friday January 18
657 $condition = ["`uid` = ? AND `type` != 'birthday' AND `start` < ? AND `start` >= ?",
658 local_user(), DateTimeFormat::utc('now + 7 days'), DateTimeFormat::utc('now - 1 days')];
659 $s = DBA::select('event', [], $condition, ['order' => ['start']]);
663 if (DBA::isResult($s)) {
667 while ($rr = DBA::fetch($s)) {
668 $condition = ['parent-uri' => $rr['uri'], 'uid' => $rr['uid'], 'author-id' => public_contact(),
669 'activity' => [Item::activityToIndex(ACTIVITY_ATTEND), Item::activityToIndex(ACTIVITY_ATTENDMAYBE)],
670 'visible' => true, 'deleted' => false];
671 if (!Item::exists($condition)) {
675 if (strlen($rr['summary'])) {
679 $strt = DateTimeFormat::convert($rr['start'], $rr['adjust'] ? $a->timezone : 'UTC', 'UTC', 'Y-m-d');
680 if ($strt === DateTimeFormat::timezoneNow($a->timezone, 'Y-m-d')) {
684 $title = strip_tags(html_entity_decode(BBCode::convert($rr['summary']), ENT_QUOTES, 'UTF-8'));
686 if (strlen($title) > 35) {
687 $title = substr($title, 0, 32) . '... ';
690 $description = substr(strip_tags(BBCode::convert($rr['desc'])), 0, 32) . '... ';
692 $description = L10n::t('[No description]');
695 $strt = DateTimeFormat::convert($rr['start'], $rr['adjust'] ? $a->timezone : 'UTC');
697 if (substr($strt, 0, 10) < DateTimeFormat::timezoneNow($a->timezone, 'Y-m-d')) {
701 $today = ((substr($strt, 0, 10) === DateTimeFormat::timezoneNow($a->timezone, 'Y-m-d')) ? true : false);
703 $rr['title'] = $title;
704 $rr['description'] = $description;
705 $rr['date'] = day_translate(DateTimeFormat::convert($rr['start'], $rr['adjust'] ? $a->timezone : 'UTC', 'UTC', $bd_format)) . (($today) ? ' ' . L10n::t('[today]') : '');
706 $rr['startime'] = $strt;
707 $rr['today'] = $today;
712 $classtoday = (($istoday) ? 'event-today' : '');
714 $tpl = Renderer::getMarkupTemplate('events_reminder.tpl');
715 return Renderer::replaceMacros($tpl, [
716 '$baseurl' => System::baseUrl(),
717 '$classtoday' => $classtoday,
718 '$count' => count($r),
719 '$event_reminders' => L10n::t('Event Reminders'),
720 '$event_title' => L10n::t('Upcoming events the next 7 days:'),
725 public static function getAdvanced(App $a)
728 $uid = $a->profile['uid'];
730 $o .= Renderer::replaceMacros(
731 Renderer::getMarkupTemplate('section_title.tpl'),
732 ['$title' => L10n::t('Profile')]
735 if ($a->profile['name']) {
736 $tpl = Renderer::getMarkupTemplate('profile_advanced.tpl');
740 $profile['fullname'] = [L10n::t('Full Name:'), $a->profile['name']];
742 if (Feature::isEnabled($uid, 'profile_membersince')) {
743 $profile['membersince'] = [L10n::t('Member since:'), DateTimeFormat::local($a->profile['register_date'])];
746 if ($a->profile['gender']) {
747 $profile['gender'] = [L10n::t('Gender:'), $a->profile['gender']];
750 if (($a->profile['dob']) && ($a->profile['dob'] > '0001-01-01')) {
751 $year_bd_format = L10n::t('j F, Y');
752 $short_bd_format = L10n::t('j F');
754 $val = day_translate(
755 intval($a->profile['dob']) ?
756 DateTimeFormat::utc($a->profile['dob'] . ' 00:00 +00:00', $year_bd_format)
757 : DateTimeFormat::utc('2001-' . substr($a->profile['dob'], 5) . ' 00:00 +00:00', $short_bd_format)
760 $profile['birthday'] = [L10n::t('Birthday:'), $val];
763 if (!empty($a->profile['dob'])
764 && $a->profile['dob'] > '0001-01-01'
765 && $age = Temporal::getAgeByTimezone($a->profile['dob'], $a->profile['timezone'], '')
767 $profile['age'] = [L10n::t('Age:'), $age];
770 if ($a->profile['marital']) {
771 $profile['marital'] = [L10n::t('Status:'), $a->profile['marital']];
774 /// @TODO Maybe use x() here, plus below?
775 if ($a->profile['with']) {
776 $profile['marital']['with'] = $a->profile['with'];
779 if (strlen($a->profile['howlong']) && $a->profile['howlong'] >= DBA::NULL_DATETIME) {
780 $profile['howlong'] = Temporal::getRelativeDate($a->profile['howlong'], L10n::t('for %1$d %2$s'));
783 if ($a->profile['sexual']) {
784 $profile['sexual'] = [L10n::t('Sexual Preference:'), $a->profile['sexual']];
787 if ($a->profile['homepage']) {
788 $profile['homepage'] = [L10n::t('Homepage:'), linkify($a->profile['homepage'])];
791 if ($a->profile['hometown']) {
792 $profile['hometown'] = [L10n::t('Hometown:'), linkify($a->profile['hometown'])];
795 if ($a->profile['pub_keywords']) {
796 $profile['pub_keywords'] = [L10n::t('Tags:'), $a->profile['pub_keywords']];
799 if ($a->profile['politic']) {
800 $profile['politic'] = [L10n::t('Political Views:'), $a->profile['politic']];
803 if ($a->profile['religion']) {
804 $profile['religion'] = [L10n::t('Religion:'), $a->profile['religion']];
807 if ($txt = prepare_text($a->profile['about'])) {
808 $profile['about'] = [L10n::t('About:'), $txt];
811 if ($txt = prepare_text($a->profile['interest'])) {
812 $profile['interest'] = [L10n::t('Hobbies/Interests:'), $txt];
815 if ($txt = prepare_text($a->profile['likes'])) {
816 $profile['likes'] = [L10n::t('Likes:'), $txt];
819 if ($txt = prepare_text($a->profile['dislikes'])) {
820 $profile['dislikes'] = [L10n::t('Dislikes:'), $txt];
823 if ($txt = prepare_text($a->profile['contact'])) {
824 $profile['contact'] = [L10n::t('Contact information and Social Networks:'), $txt];
827 if ($txt = prepare_text($a->profile['music'])) {
828 $profile['music'] = [L10n::t('Musical interests:'), $txt];
831 if ($txt = prepare_text($a->profile['book'])) {
832 $profile['book'] = [L10n::t('Books, literature:'), $txt];
835 if ($txt = prepare_text($a->profile['tv'])) {
836 $profile['tv'] = [L10n::t('Television:'), $txt];
839 if ($txt = prepare_text($a->profile['film'])) {
840 $profile['film'] = [L10n::t('Film/dance/culture/entertainment:'), $txt];
843 if ($txt = prepare_text($a->profile['romance'])) {
844 $profile['romance'] = [L10n::t('Love/Romance:'), $txt];
847 if ($txt = prepare_text($a->profile['work'])) {
848 $profile['work'] = [L10n::t('Work/employment:'), $txt];
851 if ($txt = prepare_text($a->profile['education'])) {
852 $profile['education'] = [L10n::t('School/education:'), $txt];
855 //show subcribed forum if it is enabled in the usersettings
856 if (Feature::isEnabled($uid, 'forumlist_profile')) {
857 $profile['forumlist'] = [L10n::t('Forums:'), ForumManager::profileAdvanced($uid)];
860 if ($a->profile['uid'] == local_user()) {
861 $profile['edit'] = [System::baseUrl() . '/profiles/' . $a->profile['id'], L10n::t('Edit profile'), '', L10n::t('Edit profile')];
864 return Renderer::replaceMacros($tpl, [
865 '$title' => L10n::t('Profile'),
866 '$basic' => L10n::t('Basic'),
867 '$advanced' => L10n::t('Advanced'),
868 '$profile' => $profile
875 public static function getTabs($a, $is_owner = false, $nickname = null)
877 if (is_null($nickname)) {
878 $nickname = $a->user['nickname'];
882 if (x($_GET, 'tab')) {
883 $tab = notags(trim($_GET['tab']));
886 $url = System::baseUrl() . '/profile/' . $nickname;
890 'label' => L10n::t('Status'),
892 'sel' => !$tab && $a->argv[0] == 'profile' ? 'active' : '',
893 'title' => L10n::t('Status Messages and Posts'),
894 'id' => 'status-tab',
898 'label' => L10n::t('Profile'),
899 'url' => $url . '/?tab=profile',
900 'sel' => $tab == 'profile' ? 'active' : '',
901 'title' => L10n::t('Profile Details'),
902 'id' => 'profile-tab',
906 'label' => L10n::t('Photos'),
907 'url' => System::baseUrl() . '/photos/' . $nickname,
908 'sel' => !$tab && $a->argv[0] == 'photos' ? 'active' : '',
909 'title' => L10n::t('Photo Albums'),
914 'label' => L10n::t('Videos'),
915 'url' => System::baseUrl() . '/videos/' . $nickname,
916 'sel' => !$tab && $a->argv[0] == 'videos' ? 'active' : '',
917 'title' => L10n::t('Videos'),
923 // the calendar link for the full featured events calendar
924 if ($is_owner && $a->theme_events_in_profile) {
926 'label' => L10n::t('Events'),
927 'url' => System::baseUrl() . '/events',
928 'sel' => !$tab && $a->argv[0] == 'events' ? 'active' : '',
929 'title' => L10n::t('Events and Calendar'),
930 'id' => 'events-tab',
933 // if the user is not the owner of the calendar we only show a calendar
934 // with the public events of the calendar owner
935 } elseif (!$is_owner) {
937 'label' => L10n::t('Events'),
938 'url' => System::baseUrl() . '/cal/' . $nickname,
939 'sel' => !$tab && $a->argv[0] == 'cal' ? 'active' : '',
940 'title' => L10n::t('Events and Calendar'),
941 'id' => 'events-tab',
948 'label' => L10n::t('Personal Notes'),
949 'url' => System::baseUrl() . '/notes',
950 'sel' => !$tab && $a->argv[0] == 'notes' ? 'active' : '',
951 'title' => L10n::t('Only You Can See This'),
957 if (!empty($_SESSION['new_member']) && $is_owner) {
959 'label' => L10n::t('Tips for New Members'),
960 'url' => System::baseUrl() . '/newmember',
962 'title' => L10n::t('Tips for New Members'),
963 'id' => 'newmember-tab',
967 if (!$is_owner && empty($a->profile['hide-friends'])) {
969 'label' => L10n::t('Contacts'),
970 'url' => System::baseUrl() . '/viewcontacts/' . $nickname,
971 'sel' => !$tab && $a->argv[0] == 'viewcontacts' ? 'active' : '',
972 'title' => L10n::t('Contacts'),
973 'id' => 'viewcontacts-tab',
978 $arr = ['is_owner' => $is_owner, 'nickname' => $nickname, 'tab' => $tab, 'tabs' => $tabs];
979 Addon::callHooks('profile_tabs', $arr);
981 $tpl = Renderer::getMarkupTemplate('common_tabs.tpl');
983 return Renderer::replaceMacros($tpl, ['$tabs' => $arr['tabs']]);
987 * Retrieves the my_url session variable
991 public static function getMyURL()
993 if (x($_SESSION, 'my_url')) {
994 return $_SESSION['my_url'];
1000 * Process the 'zrl' parameter and initiate the remote authentication.
1002 * This method checks if the visitor has a public contact entry and
1003 * redirects the visitor to his/her instance to start the magic auth (Authentication)
1006 * Ported from Hubzilla: https://framagit.org/hubzilla/core/blob/master/include/channel.php
1008 * @param App $a Application instance.
1010 public static function zrlInit(App $a)
1012 $my_url = self::getMyURL();
1013 $my_url = Network::isUrlValid($my_url);
1015 if (empty($my_url) || local_user()) {
1019 $arr = ['zrl' => $my_url, 'url' => $a->cmd];
1020 Addon::callHooks('zrl_init', $arr);
1022 // Try to find the public contact entry of the visitor.
1023 $cid = Contact::getIdForURL($my_url);
1025 Logger::log('No contact record found for ' . $my_url, Logger::DEBUG);
1029 $contact = DBA::selectFirst('contact',['id', 'url'], ['id' => $cid]);
1031 if (DBA::isResult($contact) && remote_user() && remote_user() == $contact['id']) {
1032 Logger::log('The visitor ' . $my_url . ' is already authenticated', Logger::DEBUG);
1036 // Avoid endless loops
1037 $cachekey = 'zrlInit:' . $my_url;
1038 if (Cache::get($cachekey)) {
1039 Logger::log('URL ' . $my_url . ' already tried to authenticate.', Logger::DEBUG);
1042 Cache::set($cachekey, true, Cache::MINUTE);
1045 Logger::log('Not authenticated. Invoking reverse magic-auth for ' . $my_url, Logger::DEBUG);
1047 Worker::add(PRIORITY_LOW, 'GProbe', $my_url);
1049 // Try to avoid recursion - but send them home to do a proper magic auth.
1050 $query = str_replace(array('?zrl=', '&zid='), array('?rzrl=', '&rzrl='), $a->query_string);
1051 // The other instance needs to know where to redirect.
1052 $dest = urlencode($a->getBaseURL() . '/' . $query);
1054 // We need to extract the basebath from the profile url
1055 // to redirect the visitors '/magic' module.
1056 // Note: We should have the basepath of a contact also in the contact table.
1057 $urlarr = explode('/profile/', $contact['url']);
1058 $basepath = $urlarr[0];
1060 if ($basepath != $a->getBaseURL() && !strstr($dest, '/magic') && !strstr($dest, '/rmagic')) {
1061 $magic_path = $basepath . '/magic' . '?f=&owa=1&dest=' . $dest;
1063 // We have to check if the remote server does understand /magic without invoking something
1064 $serverret = Network::curl($basepath . '/magic');
1065 if ($serverret->isSuccess()) {
1066 Logger::log('Doing magic auth for visitor ' . $my_url . ' to ' . $magic_path, Logger::DEBUG);
1067 System::externalRedirect($magic_path);
1073 * OpenWebAuth authentication.
1075 * Ported from Hubzilla: https://framagit.org/hubzilla/core/blob/master/include/zid.php
1077 * @param string $token
1079 public static function openWebAuthInit($token)
1083 // Clean old OpenWebAuthToken entries.
1084 OpenWebAuthToken::purge('owt', '3 MINUTE');
1086 // Check if the token we got is the same one
1087 // we have stored in the database.
1088 $visitor_handle = OpenWebAuthToken::getMeta('owt', 0, $token);
1090 if($visitor_handle === false) {
1094 // Try to find the public contact entry of the visitor.
1095 $cid = Contact::getIdForURL($visitor_handle);
1097 Logger::log('owt: unable to finger ' . $visitor_handle, Logger::DEBUG);
1101 $visitor = DBA::selectFirst('contact', [], ['id' => $cid]);
1103 // Authenticate the visitor.
1104 $_SESSION['authenticated'] = 1;
1105 $_SESSION['visitor_id'] = $visitor['id'];
1106 $_SESSION['visitor_handle'] = $visitor['addr'];
1107 $_SESSION['visitor_home'] = $visitor['url'];
1108 $_SESSION['my_url'] = $visitor['url'];
1111 'visitor' => $visitor,
1112 'url' => $a->query_string
1115 * @hooks magic_auth_success
1116 * Called when a magic-auth was successful.
1117 * * \e array \b visitor
1118 * * \e string \b url
1120 Addon::callHooks('magic_auth_success', $arr);
1122 $a->contact = $arr['visitor'];
1124 info(L10n::t('OpenWebAuth: %1$s welcomes %2$s', $a->getHostName(), $visitor['name']));
1126 Logger::log('OpenWebAuth: auth success from ' . $visitor['addr'], Logger::DEBUG);
1129 public static function zrl($s, $force = false)
1134 if ((!strpos($s, '/profile/')) && (!$force)) {
1137 if ($force && substr($s, -1, 1) !== '/') {
1140 $achar = strpos($s, '?') ? '&' : '?';
1141 $mine = self::getMyURL();
1142 if ($mine && !link_compare($mine, $s)) {
1143 return $s . $achar . 'zrl=' . urlencode($mine);
1149 * Get the user ID of the page owner.
1151 * Used from within PCSS themes to set theme parameters. If there's a
1152 * puid request variable, that is the "page owner" and normally their theme
1153 * settings take precedence; unless a local user sets the "always_my_theme"
1154 * system pconfig, which means they don't want to see anybody else's theme
1155 * settings except their own while on this site.
1157 * @brief Get the user ID of the page owner
1158 * @return int user ID
1160 * @note Returns local_user instead of user ID if "always_my_theme"
1163 public static function getThemeUid()
1165 $uid = ((!empty($_REQUEST['puid'])) ? intval($_REQUEST['puid']) : 0);
1166 if ((local_user()) && ((PConfig::get(local_user(), 'system', 'always_my_theme')) || (!$uid))) {
1167 return local_user();
1174 * Stip zrl parameter from a string.
1176 * @param string $s The input string.
1177 * @return string The zrl.
1179 public static function stripZrls($s)
1181 return preg_replace('/[\?&]zrl=(.*?)([\?&]|$)/is', '', $s);
1185 * Stip query parameter from a string.
1187 * @param string $s The input string.
1188 * @return string The query parameter.
1190 public static function stripQueryParam($s, $param)
1192 return preg_replace('/[\?&]' . $param . '=(.*?)(&|$)/ism', '$2', $s);