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\System;
17 use Friendica\Core\Worker;
18 use Friendica\Database\DBA;
19 use Friendica\Protocol\Diaspora;
20 use Friendica\Util\DateTimeFormat;
21 use Friendica\Util\Network;
22 use Friendica\Util\Temporal;
24 require_once 'include/dba.php';
25 require_once 'mod/proxy.php';
30 * @brief Returns a formatted location string from the given profile array
32 * @param array $profile Profile array (Generated from the "profile" table)
34 * @return string Location string
36 public static function formatLocation(array $profile)
40 if (!empty($profile['locality'])) {
41 $location .= $profile['locality'];
44 if (!empty($profile['region']) && (defaults($profile, 'locality', '') != $profile['region'])) {
49 $location .= $profile['region'];
52 if (!empty($profile['country-name'])) {
57 $location .= $profile['country-name'];
65 * Loads a profile into the page sidebar.
67 * The function requires a writeable copy of the main App structure, and the nickname
68 * of a registered local account.
70 * If the viewer is an authenticated remote viewer, the profile displayed is the
71 * one that has been configured for his/her viewing in the Contact manager.
72 * Passing a non-zero profile ID can also allow a preview of a selected profile
75 * Profile information is placed in the App structure for later retrieval.
76 * Honours the owner's chosen theme for display.
78 * @attention Should only be run in the _init() functions of a module. That ensures that
79 * the theme is chosen before the _init() function of a theme is run, which will usually
80 * load a lot of theme-specific content
82 * @brief Loads a profile into the page sidebar.
83 * @param object $a App
84 * @param string $nickname string
85 * @param int $profile int
86 * @param array $profiledata array
87 * @param boolean $show_connect Show connect link
89 public static function load(App $a, $nickname, $profile = 0, array $profiledata = [], $show_connect = true)
91 $user = DBA::selectFirst('user', ['uid'], ['nickname' => $nickname, 'account_removed' => false]);
93 if (!DBA::isResult($user) && empty($profiledata)) {
94 logger('profile error: ' . $a->query_string, LOGGER_DEBUG);
95 notice(L10n::t('Requested account is not available.') . EOL);
100 if (count($profiledata) > 0) {
101 // Add profile data to sidebar
102 $a->page['aside'] .= self::sidebar($profiledata, true, $show_connect);
104 if (!DBA::isResult($user)) {
109 $pdata = self::getByNickname($nickname, $user['uid'], $profile);
111 if (empty($pdata) && empty($profiledata)) {
112 logger('profile error: ' . $a->query_string, LOGGER_DEBUG);
113 notice(L10n::t('Requested profile is not available.') . EOL);
118 // fetch user tags if this isn't the default profile
120 if (!$pdata['is-default']) {
122 "SELECT `pub_keywords` FROM `profile` WHERE `uid` = %d AND `is-default` = 1 LIMIT 1",
123 intval($pdata['profile_uid'])
125 if ($x && count($x)) {
126 $pdata['pub_keywords'] = $x[0]['pub_keywords'];
130 $a->profile = $pdata;
131 $a->profile_uid = $pdata['profile_uid'];
133 $a->profile['mobile-theme'] = PConfig::get($a->profile['profile_uid'], 'system', 'mobile_theme');
134 $a->profile['network'] = NETWORK_DFRN;
136 $a->page['title'] = $a->profile['name'] . ' @ ' . Config::get('config', 'sitename');
138 if (!$profiledata && !PConfig::get(local_user(), 'system', 'always_my_theme')) {
139 $_SESSION['theme'] = $a->profile['theme'];
142 $_SESSION['mobile-theme'] = $a->profile['mobile-theme'];
145 * load/reload current theme info
148 $a->set_template_engine(); // reset the template engine to the default in case the user's theme doesn't specify one
150 $theme_info_file = 'view/theme/' . $a->getCurrentTheme() . '/theme.php';
151 if (file_exists($theme_info_file)) {
152 require_once $theme_info_file;
155 if (local_user() && local_user() == $a->profile['uid'] && $profiledata) {
156 $a->page['aside'] .= replace_macros(
157 get_markup_template('profile_edlink.tpl'),
159 '$editprofile' => L10n::t('Edit profile'),
160 '$profid' => $a->profile['id']
165 $block = ((Config::get('system', 'block_public') && !local_user() && !remote_user()) ? true : false);
169 * By now, the contact block isn't shown, when a different profile is given
170 * But: When this profile was on the same server, then we could display the contacts
173 $a->page['aside'] .= self::sidebar($a->profile, $block, $show_connect);
180 * Get all profile data of a local user
182 * If the viewer is an authenticated remote viewer, the profile displayed is the
183 * one that has been configured for his/her viewing in the Contact manager.
184 * Passing a non-zero profile ID can also allow a preview of a selected profile
187 * Includes all available profile data
189 * @brief Get all profile data of a local user
190 * @param string $nickname nick
191 * @param int $uid uid
192 * @param int $profile_id ID of the profile
195 public static function getByNickname($nickname, $uid = 0, $profile_id = 0)
197 if (remote_user() && count($_SESSION['remote'])) {
198 foreach ($_SESSION['remote'] as $visitor) {
199 if ($visitor['uid'] == $uid) {
200 $contact = DBA::selectFirst('contact', ['profile-id'], ['id' => $visitor['cid']]);
201 if (DBA::isResult($contact)) {
202 $profile_id = $contact['profile-id'];
212 $profile = DBA::fetchFirst(
213 "SELECT `contact`.`id` AS `contact_id`, `contact`.`photo` AS `contact_photo`,
214 `contact`.`thumb` AS `contact_thumb`, `contact`.`micro` AS `contact_micro`,
215 `profile`.`uid` AS `profile_uid`, `profile`.*,
216 `contact`.`avatar-date` AS picdate, `contact`.`addr`, `contact`.`url`, `user`.*
218 INNER JOIN `contact` on `contact`.`uid` = `profile`.`uid` AND `contact`.`self`
219 INNER JOIN `user` ON `profile`.`uid` = `user`.`uid`
220 WHERE `user`.`nickname` = ? AND `profile`.`id` = ? LIMIT 1",
225 if (!DBA::isResult($profile)) {
226 $profile = DBA::fetchFirst(
227 "SELECT `contact`.`id` AS `contact_id`, `contact`.`photo` as `contact_photo`,
228 `contact`.`thumb` AS `contact_thumb`, `contact`.`micro` AS `contact_micro`,
229 `profile`.`uid` AS `profile_uid`, `profile`.*,
230 `contact`.`avatar-date` AS picdate, `contact`.`addr`, `contact`.`url`, `user`.*
232 INNER JOIN `contact` ON `contact`.`uid` = `profile`.`uid` AND `contact`.`self`
233 INNER JOIN `user` ON `profile`.`uid` = `user`.`uid`
234 WHERE `user`.`nickname` = ? AND `profile`.`is-default` LIMIT 1",
243 * Formats a profile for display in the sidebar.
245 * It is very difficult to templatise the HTML completely
246 * because of all the conditional logic.
248 * @brief Formats a profile for display in the sidebar.
249 * @param array $profile
251 * @param boolean $show_connect Show connect link
253 * @return string HTML sidebar module
255 * @note Returns empty string if passed $profile is wrong type or not populated
257 * @hooks 'profile_sidebar_enter'
258 * array $profile - profile data
259 * @hooks 'profile_sidebar'
262 private static function sidebar($profile, $block = 0, $show_connect = true)
269 // This function can also use contact information in $profile
270 $is_contact = x($profile, 'cid');
272 if (!is_array($profile) && !count($profile)) {
276 $profile['picdate'] = urlencode(defaults($profile, 'picdate', ''));
278 if (($profile['network'] != '') && ($profile['network'] != NETWORK_DFRN)) {
279 $profile['network_name'] = format_network_name($profile['network'], $profile['url']);
281 $profile['network_name'] = '';
284 Addon::callHooks('profile_sidebar_enter', $profile);
287 // don't show connect link to yourself
288 $connect = $profile['uid'] != local_user() ? L10n::t('Connect') : false;
290 // don't show connect link to authenticated visitors either
291 if (remote_user() && count($_SESSION['remote'])) {
292 foreach ($_SESSION['remote'] as $visitor) {
293 if ($visitor['uid'] == $profile['uid']) {
300 if (!$show_connect) {
306 // Is the local user already connected to that user?
307 if ($connect && local_user()) {
308 if (isset($profile['url'])) {
309 $profile_url = normalise_link($profile['url']);
311 $profile_url = normalise_link(System::baseUrl() . '/profile/' . $profile['nickname']);
314 if (DBA::exists('contact', ['pending' => false, 'uid' => local_user(), 'nurl' => $profile_url])) {
319 if ($connect && ($profile['network'] != NETWORK_DFRN) && !isset($profile['remoteconnect'])) {
323 $remoteconnect = null;
324 if (isset($profile['remoteconnect'])) {
325 $remoteconnect = $profile['remoteconnect'];
328 if ($connect && ($profile['network'] == NETWORK_DFRN) && !isset($remoteconnect)) {
329 $subscribe_feed = L10n::t('Atom feed');
331 $subscribe_feed = false;
334 if (remote_user() || (self::getMyURL() && x($profile, 'unkmail') && ($profile['uid'] != local_user()))) {
335 $wallmessage = L10n::t('Message');
336 $wallmessage_link = 'wallmessage/' . $profile['nickname'];
340 "SELECT `url` FROM `contact` WHERE `uid` = %d AND `id` = '%s' AND `rel` = %d",
341 intval($profile['uid']),
342 intval(remote_user()),
343 intval(CONTACT_IS_FRIEND)
347 "SELECT `url` FROM `contact` WHERE `uid` = %d AND `nurl` = '%s' AND `rel` = %d",
348 intval($profile['uid']),
349 dbesc(normalise_link(self::getMyURL())),
350 intval(CONTACT_IS_FRIEND)
354 $remote_url = $r[0]['url'];
355 $message_path = preg_replace('=(.*)/profile/(.*)=ism', '$1/message/new/', $remote_url);
356 $wallmessage_link = $message_path . base64_encode($profile['addr']);
359 $wallmessage = false;
360 $wallmessage_link = false;
363 // show edit profile to yourself
364 if (!$is_contact && $profile['uid'] == local_user() && Feature::isEnabled(local_user(), 'multi_profiles')) {
365 $profile['edit'] = [System::baseUrl() . '/profiles', L10n::t('Profiles'), '', L10n::t('Manage/edit profiles')];
367 "SELECT * FROM `profile` WHERE `uid` = %d",
372 'chg_photo' => L10n::t('Change profile photo'),
373 'cr_new' => L10n::t('Create New Profile'),
377 if (DBA::isResult($r)) {
378 foreach ($r as $rr) {
379 $profile['menu']['entries'][] = [
380 'photo' => $rr['thumb'],
382 'alt' => L10n::t('Profile Image'),
383 'profile_name' => $rr['profile-name'],
384 'isdefault' => $rr['is-default'],
385 'visibile_to_everybody' => L10n::t('visible to everybody'),
386 'edit_visibility' => L10n::t('Edit visibility'),
391 if (!$is_contact && $profile['uid'] == local_user() && !Feature::isEnabled(local_user(), 'multi_profiles')) {
392 $profile['edit'] = [System::baseUrl() . '/profiles/' . $profile['id'], L10n::t('Edit profile'), '', L10n::t('Edit profile')];
394 'chg_photo' => L10n::t('Change profile photo'),
400 // Fetch the account type
401 $account_type = Contact::getAccountType($profile);
403 if (x($profile, 'address')
404 || x($profile, 'location')
405 || x($profile, 'locality')
406 || x($profile, 'region')
407 || x($profile, 'postal-code')
408 || x($profile, 'country-name')
410 $location = L10n::t('Location:');
413 $gender = x($profile, 'gender') ? L10n::t('Gender:') : false;
414 $marital = x($profile, 'marital') ? L10n::t('Status:') : false;
415 $homepage = x($profile, 'homepage') ? L10n::t('Homepage:') : false;
416 $about = x($profile, 'about') ? L10n::t('About:') : false;
417 $xmpp = x($profile, 'xmpp') ? L10n::t('XMPP:') : false;
419 if ((x($profile, 'hidewall') || $block) && !local_user() && !remote_user()) {
420 $location = $gender = $marital = $homepage = $about = false;
423 $split_name = Diaspora::splitName($profile['name']);
424 $firstname = $split_name['first'];
425 $lastname = $split_name['last'];
427 if (x($profile, 'guid')) {
429 'guid' => $profile['guid'],
430 'podloc' => System::baseUrl(),
431 'searchable' => (($profile['publish'] && $profile['net-publish']) ? 'true' : 'false' ),
432 'nickname' => $profile['nickname'],
433 'fullname' => $profile['name'],
434 'firstname' => $firstname,
435 'lastname' => $lastname,
436 'photo300' => defaults($profile, 'contact_photo', ''),
437 'photo100' => defaults($profile, 'contact_thumb', ''),
438 'photo50' => defaults($profile, 'contact_micro', ''),
448 $contact_block = contact_block();
450 if (is_array($a->profile) && !$a->profile['hide-friends']) {
452 "SELECT `gcontact`.`updated` FROM `contact` INNER JOIN `gcontact` WHERE `gcontact`.`nurl` = `contact`.`nurl` AND `self` AND `uid` = %d LIMIT 1",
453 intval($a->profile['uid'])
455 if (DBA::isResult($r)) {
456 $updated = date('c', strtotime($r[0]['updated']));
460 "SELECT COUNT(*) AS `total` FROM `contact`
462 AND NOT `self` AND NOT `blocked` AND NOT `pending`
463 AND NOT `hidden` AND NOT `archive`
464 AND `network` IN ('%s', '%s', '%s', '')",
465 intval($profile['uid']),
467 dbesc(NETWORK_DIASPORA),
468 dbesc(NETWORK_OSTATUS)
470 if (DBA::isResult($r)) {
471 $contacts = intval($r[0]['total']);
477 foreach ($profile as $k => $v) {
478 $k = str_replace('-', '_', $k);
482 if (isset($p['about'])) {
483 $p['about'] = BBCode::convert($p['about']);
486 if (isset($p['address'])) {
487 $p['address'] = BBCode::convert($p['address']);
489 $p['address'] = BBCode::convert($p['location']);
492 if (isset($p['photo'])) {
493 $p['photo'] = proxy_url($p['photo'], false, PROXY_SIZE_SMALL);
496 $p['url'] = Contact::magicLink(defaults($p, 'url', $profile_url));
498 $tpl = get_markup_template('profile_vcard.tpl');
499 $o .= replace_macros($tpl, [
502 '$connect' => $connect,
503 '$remoteconnect' => $remoteconnect,
504 '$subscribe_feed' => $subscribe_feed,
505 '$wallmessage' => $wallmessage,
506 '$wallmessage_link' => $wallmessage_link,
507 '$account_type' => $account_type,
508 '$location' => $location,
509 '$gender' => $gender,
510 '$marital' => $marital,
511 '$homepage' => $homepage,
513 '$network' => L10n::t('Network:'),
514 '$contacts' => $contacts,
515 '$updated' => $updated,
516 '$diaspora' => $diaspora,
517 '$contact_block' => $contact_block,
520 $arr = ['profile' => &$profile, 'entry' => &$o];
522 Addon::callHooks('profile_sidebar', $arr);
527 public static function getBirthdays()
532 if (!local_user() || $a->is_mobile || $a->is_tablet) {
537 * $mobile_detect = new Mobile_Detect();
538 * $is_mobile = $mobile_detect->isMobile() || $mobile_detect->isTablet();
543 $bd_format = L10n::t('g A l F d'); // 8 AM Friday January 18
544 $bd_short = L10n::t('F d');
546 $cachekey = 'get_birthdays:' . local_user();
547 $r = Cache::get($cachekey);
550 "SELECT `event`.*, `event`.`id` AS `eid`, `contact`.* FROM `event`
551 INNER JOIN `contact` ON `contact`.`id` = `event`.`cid`
552 WHERE `event`.`uid` = ? AND `type` = 'birthday' AND `start` < ? AND `finish` > ?
553 ORDER BY `start` ASC ",
555 DateTimeFormat::utc('now + 6 days'),
556 DateTimeFormat::utcNow()
558 if (DBA::isResult($s)) {
559 $r = DBA::toArray($s);
560 Cache::set($cachekey, $r, CACHE_HOUR);
566 if (DBA::isResult($r)) {
567 $now = strtotime('now');
571 foreach ($r as $rr) {
572 if (strlen($rr['name'])) {
575 if ((strtotime($rr['start'] . ' +00:00') < $now) && (strtotime($rr['finish'] . ' +00:00') > $now)) {
579 $classtoday = $istoday ? ' birthday-today ' : '';
581 foreach ($r as &$rr) {
582 if (!strlen($rr['name'])) {
588 if (in_array($rr['cid'], $cids)) {
591 $cids[] = $rr['cid'];
593 $today = (((strtotime($rr['start'] . ' +00:00') < $now) && (strtotime($rr['finish'] . ' +00:00') > $now)) ? true : false);
595 $rr['link'] = Contact::magicLink($rr['url']);
596 $rr['title'] = $rr['name'];
597 $rr['date'] = day_translate(DateTimeFormat::convert($rr['start'], $a->timezone, 'UTC', $rr['adjust'] ? $bd_format : $bd_short)) . (($today) ? ' ' . L10n::t('[today]') : '');
598 $rr['startime'] = null;
599 $rr['today'] = $today;
603 $tpl = get_markup_template('birthdays_reminder.tpl');
604 return replace_macros($tpl, [
605 '$baseurl' => System::baseUrl(),
606 '$classtoday' => $classtoday,
608 '$event_reminders' => L10n::t('Birthday Reminders'),
609 '$event_title' => L10n::t('Birthdays this week:'),
611 '$lbr' => '{', // raw brackets mess up if/endif macro processing
616 public static function getEventsReminderHTML()
621 if (!local_user() || $a->is_mobile || $a->is_tablet) {
626 * $mobile_detect = new Mobile_Detect();
627 * $is_mobile = $mobile_detect->isMobile() || $mobile_detect->isTablet();
632 $bd_format = L10n::t('g A l F d'); // 8 AM Friday January 18
639 ON `item`.`uid` = `event`.`uid`
640 AND `item`.`parent-uri` = `event`.`uri`
641 WHERE `event`.`uid` = ?
642 AND `event`.`type` != 'birthday'
643 AND `event`.`start` < ?
644 AND `event`.`start` >= ?
645 AND `item`.`author-id` = ?
646 AND (`item`.`verb` = ? OR `item`.`verb` = ?)
648 AND NOT `item`.`deleted`
649 ORDER BY `event`.`start` ASC",
651 DateTimeFormat::utc('now + 7 days'),
652 DateTimeFormat::utc('now - 1 days'),
660 if (DBA::isResult($s)) {
663 while ($rr = DBA::fetch($s)) {
664 if (strlen($rr['name'])) {
668 $strt = DateTimeFormat::convert($rr['start'], $rr['convert'] ? $a->timezone : 'UTC', 'UTC', 'Y-m-d');
669 if ($strt === DateTimeFormat::timezoneNow($a->timezone, 'Y-m-d')) {
673 $title = strip_tags(html_entity_decode(BBCode::convert($rr['summary']), ENT_QUOTES, 'UTF-8'));
675 if (strlen($title) > 35) {
676 $title = substr($title, 0, 32) . '... ';
679 $description = substr(strip_tags(BBCode::convert($rr['desc'])), 0, 32) . '... ';
681 $description = L10n::t('[No description]');
684 $strt = DateTimeFormat::convert($rr['start'], $rr['convert'] ? $a->timezone : 'UTC');
686 if (substr($strt, 0, 10) < DateTimeFormat::timezoneNow($a->timezone, 'Y-m-d')) {
690 $today = ((substr($strt, 0, 10) === DateTimeFormat::timezoneNow($a->timezone, 'Y-m-d')) ? true : false);
692 $rr['title'] = $title;
693 $rr['description'] = $description;
694 $rr['date'] = day_translate(DateTimeFormat::convert($rr['start'], $rr['adjust'] ? $a->timezone : 'UTC', 'UTC', $bd_format)) . (($today) ? ' ' . L10n::t('[today]') : '');
695 $rr['startime'] = $strt;
696 $rr['today'] = $today;
701 $classtoday = (($istoday) ? 'event-today' : '');
703 $tpl = get_markup_template('events_reminder.tpl');
704 return replace_macros($tpl, [
705 '$baseurl' => System::baseUrl(),
706 '$classtoday' => $classtoday,
707 '$count' => count($r),
708 '$event_reminders' => L10n::t('Event Reminders'),
709 '$event_title' => L10n::t('Upcoming events the next 7 days:'),
714 public static function getAdvanced(App $a)
717 $uid = $a->profile['uid'];
719 $o .= replace_macros(
720 get_markup_template('section_title.tpl'),
721 ['$title' => L10n::t('Profile')]
724 if ($a->profile['name']) {
725 $tpl = get_markup_template('profile_advanced.tpl');
729 $profile['fullname'] = [L10n::t('Full Name:'), $a->profile['name']];
731 if (Feature::isEnabled($uid, 'profile_membersince')) {
732 $profile['membersince'] = [L10n::t('Member since:'), DateTimeFormat::local($a->profile['register_date'])];
735 if ($a->profile['gender']) {
736 $profile['gender'] = [L10n::t('Gender:'), $a->profile['gender']];
739 if (($a->profile['dob']) && ($a->profile['dob'] > '0001-01-01')) {
740 $year_bd_format = L10n::t('j F, Y');
741 $short_bd_format = L10n::t('j F');
743 $val = day_translate(
744 intval($a->profile['dob']) ?
745 DateTimeFormat::utc($a->profile['dob'] . ' 00:00 +00:00', $year_bd_format)
746 : DateTimeFormat::utc('2001-' . substr($a->profile['dob'], 5) . ' 00:00 +00:00', $short_bd_format)
749 $profile['birthday'] = [L10n::t('Birthday:'), $val];
752 if (!empty($a->profile['dob'])
753 && $a->profile['dob'] > '0001-01-01'
754 && $age = Temporal::getAgeByTimezone($a->profile['dob'], $a->profile['timezone'], '')
756 $profile['age'] = [L10n::t('Age:'), $age];
759 if ($a->profile['marital']) {
760 $profile['marital'] = [L10n::t('Status:'), $a->profile['marital']];
763 /// @TODO Maybe use x() here, plus below?
764 if ($a->profile['with']) {
765 $profile['marital']['with'] = $a->profile['with'];
768 if (strlen($a->profile['howlong']) && $a->profile['howlong'] >= NULL_DATE) {
769 $profile['howlong'] = Temporal::getRelativeDate($a->profile['howlong'], L10n::t('for %1$d %2$s'));
772 if ($a->profile['sexual']) {
773 $profile['sexual'] = [L10n::t('Sexual Preference:'), $a->profile['sexual']];
776 if ($a->profile['homepage']) {
777 $profile['homepage'] = [L10n::t('Homepage:'), linkify($a->profile['homepage'])];
780 if ($a->profile['hometown']) {
781 $profile['hometown'] = [L10n::t('Hometown:'), linkify($a->profile['hometown'])];
784 if ($a->profile['pub_keywords']) {
785 $profile['pub_keywords'] = [L10n::t('Tags:'), $a->profile['pub_keywords']];
788 if ($a->profile['politic']) {
789 $profile['politic'] = [L10n::t('Political Views:'), $a->profile['politic']];
792 if ($a->profile['religion']) {
793 $profile['religion'] = [L10n::t('Religion:'), $a->profile['religion']];
796 if ($txt = prepare_text($a->profile['about'])) {
797 $profile['about'] = [L10n::t('About:'), $txt];
800 if ($txt = prepare_text($a->profile['interest'])) {
801 $profile['interest'] = [L10n::t('Hobbies/Interests:'), $txt];
804 if ($txt = prepare_text($a->profile['likes'])) {
805 $profile['likes'] = [L10n::t('Likes:'), $txt];
808 if ($txt = prepare_text($a->profile['dislikes'])) {
809 $profile['dislikes'] = [L10n::t('Dislikes:'), $txt];
812 if ($txt = prepare_text($a->profile['contact'])) {
813 $profile['contact'] = [L10n::t('Contact information and Social Networks:'), $txt];
816 if ($txt = prepare_text($a->profile['music'])) {
817 $profile['music'] = [L10n::t('Musical interests:'), $txt];
820 if ($txt = prepare_text($a->profile['book'])) {
821 $profile['book'] = [L10n::t('Books, literature:'), $txt];
824 if ($txt = prepare_text($a->profile['tv'])) {
825 $profile['tv'] = [L10n::t('Television:'), $txt];
828 if ($txt = prepare_text($a->profile['film'])) {
829 $profile['film'] = [L10n::t('Film/dance/culture/entertainment:'), $txt];
832 if ($txt = prepare_text($a->profile['romance'])) {
833 $profile['romance'] = [L10n::t('Love/Romance:'), $txt];
836 if ($txt = prepare_text($a->profile['work'])) {
837 $profile['work'] = [L10n::t('Work/employment:'), $txt];
840 if ($txt = prepare_text($a->profile['education'])) {
841 $profile['education'] = [L10n::t('School/education:'), $txt];
844 //show subcribed forum if it is enabled in the usersettings
845 if (Feature::isEnabled($uid, 'forumlist_profile')) {
846 $profile['forumlist'] = [L10n::t('Forums:'), ForumManager::profileAdvanced($uid)];
849 if ($a->profile['uid'] == local_user()) {
850 $profile['edit'] = [System::baseUrl() . '/profiles/' . $a->profile['id'], L10n::t('Edit profile'), '', L10n::t('Edit profile')];
853 return replace_macros($tpl, [
854 '$title' => L10n::t('Profile'),
855 '$basic' => L10n::t('Basic'),
856 '$advanced' => L10n::t('Advanced'),
857 '$profile' => $profile
864 public static function getTabs($a, $is_owner = false, $nickname = null)
866 if (is_null($nickname)) {
867 $nickname = $a->user['nickname'];
871 if (x($_GET, 'tab')) {
872 $tab = notags(trim($_GET['tab']));
875 $url = System::baseUrl() . '/profile/' . $nickname;
879 'label' => L10n::t('Status'),
881 'sel' => !$tab && $a->argv[0] == 'profile' ? 'active' : '',
882 'title' => L10n::t('Status Messages and Posts'),
883 'id' => 'status-tab',
887 'label' => L10n::t('Profile'),
888 'url' => $url . '/?tab=profile',
889 'sel' => $tab == 'profile' ? 'active' : '',
890 'title' => L10n::t('Profile Details'),
891 'id' => 'profile-tab',
895 'label' => L10n::t('Photos'),
896 'url' => System::baseUrl() . '/photos/' . $nickname,
897 'sel' => !$tab && $a->argv[0] == 'photos' ? 'active' : '',
898 'title' => L10n::t('Photo Albums'),
903 'label' => L10n::t('Videos'),
904 'url' => System::baseUrl() . '/videos/' . $nickname,
905 'sel' => !$tab && $a->argv[0] == 'videos' ? 'active' : '',
906 'title' => L10n::t('Videos'),
912 // the calendar link for the full featured events calendar
913 if ($is_owner && $a->theme_events_in_profile) {
915 'label' => L10n::t('Events'),
916 'url' => System::baseUrl() . '/events',
917 'sel' => !$tab && $a->argv[0] == 'events' ? 'active' : '',
918 'title' => L10n::t('Events and Calendar'),
919 'id' => 'events-tab',
922 // if the user is not the owner of the calendar we only show a calendar
923 // with the public events of the calendar owner
924 } elseif (!$is_owner) {
926 'label' => L10n::t('Events'),
927 'url' => System::baseUrl() . '/cal/' . $nickname,
928 'sel' => !$tab && $a->argv[0] == 'cal' ? 'active' : '',
929 'title' => L10n::t('Events and Calendar'),
930 'id' => 'events-tab',
937 'label' => L10n::t('Personal Notes'),
938 'url' => System::baseUrl() . '/notes',
939 'sel' => !$tab && $a->argv[0] == 'notes' ? 'active' : '',
940 'title' => L10n::t('Only You Can See This'),
946 if (!empty($_SESSION['new_member']) && $is_owner) {
948 'label' => L10n::t('Tips for New Members'),
949 'url' => System::baseUrl() . '/newmember',
951 'title' => L10n::t('Tips for New Members'),
952 'id' => 'newmember-tab',
956 if (!$is_owner && empty($a->profile['hide-friends'])) {
958 'label' => L10n::t('Contacts'),
959 'url' => System::baseUrl() . '/viewcontacts/' . $nickname,
960 'sel' => !$tab && $a->argv[0] == 'viewcontacts' ? 'active' : '',
961 'title' => L10n::t('Contacts'),
962 'id' => 'viewcontacts-tab',
967 $arr = ['is_owner' => $is_owner, 'nickname' => $nickname, 'tab' => $tab, 'tabs' => $tabs];
968 Addon::callHooks('profile_tabs', $arr);
970 $tpl = get_markup_template('common_tabs.tpl');
972 return replace_macros($tpl, ['$tabs' => $arr['tabs']]);
976 * Retrieves the my_url session variable
980 public static function getMyURL()
982 if (x($_SESSION, 'my_url')) {
983 return $_SESSION['my_url'];
989 * Process the 'zrl' parameter and initiate the remote authentication.
991 * This method checks if the visitor has a public contact entry and
992 * redirects the visitor to his/her instance to start the magic auth (Authentication)
995 * Ported from Hubzilla: https://framagit.org/hubzilla/core/blob/master/include/channel.php
997 * @param App $a Application instance.
999 public static function zrlInit(App $a)
1001 $my_url = self::getMyURL();
1002 $my_url = Network::isUrlValid($my_url);
1005 if (!local_user()) {
1006 // Is it a DDoS attempt?
1007 // The check fetches the cached value from gprobe to reduce the load for this system
1008 $urlparts = parse_url($my_url);
1010 $result = Cache::get('gprobe:' . $urlparts['host']);
1011 if ((!is_null($result)) && (in_array($result['network'], [NETWORK_FEED, NETWORK_PHANTOM]))) {
1012 logger('DDoS attempt detected for ' . $urlparts['host'] . ' by ' . $_SERVER['REMOTE_ADDR'] . '. server data: ' . print_r($_SERVER, true), LOGGER_DEBUG);
1016 Worker::add(PRIORITY_LOW, 'GProbe', $my_url);
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 // The visitor is already authenticated.
1034 logger('Not authenticated. Invoking reverse magic-auth for ' . $my_url, LOGGER_DEBUG);
1036 // Try to avoid recursion - but send them home to do a proper magic auth.
1037 $query = str_replace(array('?zrl=', '&zid='), array('?rzrl=', '&rzrl='), $a->query_string);
1038 // The other instance needs to know where to redirect.
1039 $dest = urlencode(System::baseUrl() . '/' . $query);
1041 // We need to extract the basebath from the profile url
1042 // to redirect the visitors '/magic' module.
1043 // Note: We should have the basepath of a contact also in the contact table.
1044 $urlarr = explode('/profile/', $contact['url']);
1045 $basepath = $urlarr[0];
1047 if ($basepath != System::baseUrl() && !strstr($dest, '/magic') && !strstr($dest, '/rmagic')) {
1048 goaway($basepath . '/magic' . '?f=&owa=1&dest=' . $dest);
1055 * OpenWebAuth authentication.
1057 * Ported from Hubzilla: https://framagit.org/hubzilla/core/blob/master/include/zid.php
1059 * @param string $token
1061 public static function openWebAuthInit($token)
1065 // Clean old OpenWebAuthToken entries.
1066 OpenWebAuthToken::purge('owt', '3 MINUTE');
1068 // Check if the token we got is the same one
1069 // we have stored in the database.
1070 $visitor_handle = OpenWebAuthToken::getMeta('owt', 0, $token);
1072 if($visitor_handle === false) {
1076 // Try to find the public contact entry of the visitor.
1077 $cid = Contact::getIdForURL($visitor_handle);
1079 logger('owt: unable to finger ' . $visitor_handle, LOGGER_DEBUG);
1083 $visitor = DBA::selectFirst('contact', [], ['id' => $cid]);
1085 // Authenticate the visitor.
1086 $_SESSION['authenticated'] = 1;
1087 $_SESSION['visitor_id'] = $visitor['id'];
1088 $_SESSION['visitor_handle'] = $visitor['addr'];
1089 $_SESSION['visitor_home'] = $visitor['url'];
1090 $_SESSION['my_url'] = $visitor['url'];
1093 'visitor' => $visitor,
1094 'url' => $a->query_string
1097 * @hooks magic_auth_success
1098 * Called when a magic-auth was successful.
1099 * * \e array \b visitor
1100 * * \e string \b url
1102 Addon::callHooks('magic_auth_success', $arr);
1104 $a->contact = $arr['visitor'];
1106 info(L10n::t('OpenWebAuth: %1$s welcomes %2$s', $a->get_hostname(), $visitor['name']));
1108 logger('OpenWebAuth: auth success from ' . $visitor['addr'], LOGGER_DEBUG);
1111 public static function zrl($s, $force = false)
1116 if ((!strpos($s, '/profile/')) && (!$force)) {
1119 if ($force && substr($s, -1, 1) !== '/') {
1122 $achar = strpos($s, '?') ? '&' : '?';
1123 $mine = self::getMyURL();
1124 if ($mine && !link_compare($mine, $s)) {
1125 return $s . $achar . 'zrl=' . urlencode($mine);
1131 * Get the user ID of the page owner.
1133 * Used from within PCSS themes to set theme parameters. If there's a
1134 * puid request variable, that is the "page owner" and normally their theme
1135 * settings take precedence; unless a local user sets the "always_my_theme"
1136 * system pconfig, which means they don't want to see anybody else's theme
1137 * settings except their own while on this site.
1139 * @brief Get the user ID of the page owner
1140 * @return int user ID
1142 * @note Returns local_user instead of user ID if "always_my_theme"
1145 public static function getThemeUid()
1147 $uid = ((!empty($_REQUEST['puid'])) ? intval($_REQUEST['puid']) : 0);
1148 if ((local_user()) && ((PConfig::get(local_user(), 'system', 'always_my_theme')) || (!$uid))) {
1149 return local_user();
1156 * Stip zrl parameter from a string.
1158 * @param string $s The input string.
1159 * @return string The zrl.
1161 public static function stripZrls($s)
1163 return preg_replace('/[\?&]zrl=(.*?)([\?&]|$)/is', '', $s);
1167 * Stip query parameter from a string.
1169 * @param string $s The input string.
1170 * @return string The query parameter.
1172 public static function stripQueryParam($s, $param)
1174 return preg_replace('/[\?&]' . $param . '=(.*?)(&|$)/ism', '$2', $s);