3 * @file mod/profiles.php
7 use Friendica\BaseModule;
8 use Friendica\Content\ContactSelector;
9 use Friendica\Content\Feature;
10 use Friendica\Content\Nav;
11 use Friendica\Core\Addon;
12 use Friendica\Core\Config;
13 use Friendica\Core\L10n;
14 use Friendica\Core\PConfig;
15 use Friendica\Core\System;
16 use Friendica\Core\Worker;
17 use Friendica\Database\DBA;
18 use Friendica\Model\Contact;
19 use Friendica\Model\GContact;
20 use Friendica\Model\Profile;
21 use Friendica\Module\Login;
22 use Friendica\Network\Probe;
23 use Friendica\Util\DateTimeFormat;
24 use Friendica\Util\Temporal;
26 function profiles_init(App $a) {
28 Nav::setSelected('profiles');
34 if (($a->argc > 2) && ($a->argv[1] === "drop") && intval($a->argv[2])) {
35 $r = q("SELECT * FROM `profile` WHERE `id` = %d AND `uid` = %d AND `is-default` = 0 LIMIT 1",
39 if (! DBA::isResult($r)) {
40 notice(L10n::t('Profile not found.') . EOL);
41 $a->internalRedirect('profiles');
45 BaseModule::checkFormSecurityTokenRedirectOnError('/profiles', 'profile_drop', 't');
47 // move every contact using this profile as their default to the user default
49 $r = q("UPDATE `contact` SET `profile-id` = (SELECT `profile`.`id` AS `profile-id` FROM `profile` WHERE `profile`.`is-default` = 1 AND `profile`.`uid` = %d LIMIT 1) WHERE `profile-id` = %d AND `uid` = %d ",
54 $r = q("DELETE FROM `profile` WHERE `id` = %d AND `uid` = %d",
58 if (DBA::isResult($r)) {
59 info(L10n::t('Profile deleted.').EOL);
62 $a->internalRedirect('profiles');
66 if (($a->argc > 1) && ($a->argv[1] === 'new')) {
68 BaseModule::checkFormSecurityTokenRedirectOnError('/profiles', 'profile_new', 't');
70 $r0 = q("SELECT `id` FROM `profile` WHERE `uid` = %d",
71 intval(local_user()));
73 $num_profiles = (DBA::isResult($r0) ? count($r0) : 0);
75 $name = L10n::t('Profile-') . ($num_profiles + 1);
77 $r1 = q("SELECT `name`, `photo`, `thumb` FROM `profile` WHERE `uid` = %d AND `is-default` = 1 LIMIT 1",
78 intval(local_user()));
80 $r2 = q("INSERT INTO `profile` (`uid` , `profile-name` , `name`, `photo`, `thumb`)
81 VALUES ( %d, '%s', '%s', '%s', '%s' )",
84 DBA::escape($r1[0]['name']),
85 DBA::escape($r1[0]['photo']),
86 DBA::escape($r1[0]['thumb'])
89 $r3 = q("SELECT `id` FROM `profile` WHERE `uid` = %d AND `profile-name` = '%s' LIMIT 1",
94 info(L10n::t('New profile created.') . EOL);
95 if (DBA::isResult($r3) && count($r3) == 1) {
96 $a->internalRedirect('profiles/' . $r3[0]['id']);
99 $a->internalRedirect('profiles');
102 if (($a->argc > 2) && ($a->argv[1] === 'clone')) {
104 BaseModule::checkFormSecurityTokenRedirectOnError('/profiles', 'profile_clone', 't');
106 $r0 = q("SELECT `id` FROM `profile` WHERE `uid` = %d",
107 intval(local_user()));
109 $num_profiles = (DBA::isResult($r0) ? count($r0) : 0);
111 $name = L10n::t('Profile-') . ($num_profiles + 1);
112 $r1 = q("SELECT * FROM `profile` WHERE `uid` = %d AND `id` = %d LIMIT 1",
113 intval(local_user()),
116 if(! DBA::isResult($r1)) {
117 notice(L10n::t('Profile unavailable to clone.') . EOL);
122 $r1[0]['is-default'] = 0;
123 $r1[0]['publish'] = 0;
124 $r1[0]['net-publish'] = 0;
125 $r1[0]['profile-name'] = DBA::escape($name);
127 DBA::insert('profile', $r1[0]);
129 $r3 = q("SELECT `id` FROM `profile` WHERE `uid` = %d AND `profile-name` = '%s' LIMIT 1",
130 intval(local_user()),
133 info(L10n::t('New profile created.') . EOL);
134 if ((DBA::isResult($r3)) && (count($r3) == 1)) {
135 $a->internalRedirect('profiles/'.$r3[0]['id']);
138 $a->internalRedirect('profiles');
140 return; // NOTREACHED
144 if (($a->argc > 1) && (intval($a->argv[1]))) {
145 $r = q("SELECT id FROM `profile` WHERE `id` = %d AND `uid` = %d LIMIT 1",
149 if (! DBA::isResult($r)) {
150 notice(L10n::t('Profile not found.') . EOL);
155 Profile::load($a, $a->user['nickname'], $r[0]['id']);
159 function profile_clean_keywords($keywords)
161 $keywords = str_replace(",", " ", $keywords);
162 $keywords = explode(" ", $keywords);
165 foreach ($keywords as $keyword) {
166 $keyword = trim(strtolower($keyword));
167 $keyword = trim($keyword, "#");
168 if ($keyword != "") {
169 $cleaned[] = $keyword;
173 $keywords = implode(", ", $cleaned);
178 function profiles_post(App $a) {
180 if (! local_user()) {
181 notice(L10n::t('Permission denied.') . EOL);
185 $namechanged = false;
187 Addon::callHooks('profile_post', $_POST);
189 if (($a->argc > 1) && ($a->argv[1] !== "new") && intval($a->argv[1])) {
190 $orig = q("SELECT * FROM `profile` WHERE `id` = %d AND `uid` = %d LIMIT 1",
194 if (! DBA::isResult($orig)) {
195 notice(L10n::t('Profile not found.') . EOL);
199 BaseModule::checkFormSecurityTokenRedirectOnError('/profiles', 'profile_edit');
201 $is_default = (($orig[0]['is-default']) ? 1 : 0);
203 $profile_name = notags(trim($_POST['profile_name']));
204 if (! strlen($profile_name)) {
205 notice(L10n::t('Profile Name is required.') . EOL);
209 $dob = $_POST['dob'] ? escape_tags(trim($_POST['dob'])) : '0000-00-00';
211 $y = substr($dob, 0, 4);
212 if ((! ctype_digit($y)) || ($y < 1900)) {
215 $ignore_year = false;
217 if (!in_array($dob, ['0000-00-00', '0001-01-01'])) {
218 if (strpos($dob, '0000-') === 0 || strpos($dob, '0001-') === 0) {
220 $dob = substr($dob, 5);
224 $dob = '0000-' . DateTimeFormat::utc('1900-' . $dob, 'm-d');
226 $dob = DateTimeFormat::utc($dob, 'Y-m-d');
230 $name = notags(trim($_POST['name']));
232 if (! strlen($name)) {
236 if ($orig[0]['name'] != $name) {
240 $pdesc = notags(trim($_POST['pdesc']));
241 $gender = notags(trim($_POST['gender']));
242 $address = notags(trim($_POST['address']));
243 $locality = notags(trim($_POST['locality']));
244 $region = notags(trim($_POST['region']));
245 $postal_code = notags(trim($_POST['postal_code']));
246 $country_name = notags(trim($_POST['country_name']));
247 $pub_keywords = profile_clean_keywords(notags(trim($_POST['pub_keywords'])));
248 $prv_keywords = profile_clean_keywords(notags(trim($_POST['prv_keywords'])));
249 $marital = notags(trim($_POST['marital']));
250 $howlong = notags(trim($_POST['howlong']));
252 $with = ((x($_POST,'with')) ? notags(trim($_POST['with'])) : '');
254 if (! strlen($howlong)) {
255 $howlong = DBA::NULL_DATETIME;
257 $howlong = DateTimeFormat::convert($howlong, 'UTC', date_default_timezone_get());
259 // linkify the relationship target if applicable
261 $withchanged = false;
264 if ($with != strip_tags($orig[0]['with'])) {
268 if (strpos($lookup, '@') === 0) {
269 $lookup = substr($lookup, 1);
271 $lookup = str_replace('_',' ', $lookup);
272 if (strpos($lookup, '@') || (strpos($lookup, 'http://'))) {
274 $links = @Probe::lrdd($lookup);
276 foreach ($links as $link) {
277 if ($link['@attributes']['rel'] === 'http://webfinger.net/rel/profile-page') {
278 $prf = $link['@attributes']['href'];
285 $r = q("SELECT * FROM `contact` WHERE `name` = '%s' AND `uid` = %d LIMIT 1",
286 DBA::escape($newname),
289 if (! DBA::isResult($r)) {
290 $r = q("SELECT * FROM `contact` WHERE `nick` = '%s' AND `uid` = %d LIMIT 1",
291 DBA::escape($lookup),
295 if (DBA::isResult($r)) {
297 $newname = $r[0]['name'];
302 $with = str_replace($lookup, '<a href="' . $prf . '">' . $newname . '</a>', $with);
303 if (strpos($with, '@') === 0) {
304 $with = substr($with, 1);
308 $with = $orig[0]['with'];
312 /// @TODO Not flexible enough for later expansion, let's have more OOP here
313 $sexual = notags(trim($_POST['sexual']));
314 $xmpp = notags(trim($_POST['xmpp']));
315 $homepage = notags(trim($_POST['homepage']));
316 if ((strpos($homepage, 'http') !== 0) && (strlen($homepage))) {
317 // neither http nor https in URL, add them
318 $homepage = 'http://'.$homepage;
320 $hometown = notags(trim($_POST['hometown']));
321 $politic = notags(trim($_POST['politic']));
322 $religion = notags(trim($_POST['religion']));
324 $likes = escape_tags(trim($_POST['likes']));
325 $dislikes = escape_tags(trim($_POST['dislikes']));
327 $about = escape_tags(trim($_POST['about']));
328 $interest = escape_tags(trim($_POST['interest']));
329 $contact = escape_tags(trim($_POST['contact']));
330 $music = escape_tags(trim($_POST['music']));
331 $book = escape_tags(trim($_POST['book']));
332 $tv = escape_tags(trim($_POST['tv']));
333 $film = escape_tags(trim($_POST['film']));
334 $romance = escape_tags(trim($_POST['romance']));
335 $work = escape_tags(trim($_POST['work']));
336 $education = escape_tags(trim($_POST['education']));
338 $hide_friends = (($_POST['hide-friends'] == 1) ? 1: 0);
340 PConfig::set(local_user(), 'system', 'detailled_profile', (($_POST['detailled_profile'] == 1) ? 1: 0));
345 if ($marital != $orig[0]['marital']) {
346 $changes[] = '[color=#ff0000]♥[/color] ' . L10n::t('Marital Status');
350 $changes[] = '[color=#ff0000]♥[/color] ' . L10n::t('Romantic Partner');
351 $value = strip_tags($with);
353 if ($likes != $orig[0]['likes']) {
354 $changes[] = L10n::t('Likes');
357 if ($dislikes != $orig[0]['dislikes']) {
358 $changes[] = L10n::t('Dislikes');
361 if ($work != $orig[0]['work']) {
362 $changes[] = L10n::t('Work/Employment');
364 if ($religion != $orig[0]['religion']) {
365 $changes[] = L10n::t('Religion');
368 if ($politic != $orig[0]['politic']) {
369 $changes[] = L10n::t('Political Views');
372 if ($gender != $orig[0]['gender']) {
373 $changes[] = L10n::t('Gender');
376 if ($sexual != $orig[0]['sexual']) {
377 $changes[] = L10n::t('Sexual Preference');
380 if ($xmpp != $orig[0]['xmpp']) {
381 $changes[] = L10n::t('XMPP');
384 if ($homepage != $orig[0]['homepage']) {
385 $changes[] = L10n::t('Homepage');
388 if ($interest != $orig[0]['interest']) {
389 $changes[] = L10n::t('Interests');
392 if ($address != $orig[0]['address']) {
393 $changes[] = L10n::t('Address');
394 // New address not sent in notifications, potential privacy issues
395 // in case this leaks to unintended recipients. Yes, it's in the public
396 // profile but that doesn't mean we have to broadcast it to everybody.
398 if ($locality != $orig[0]['locality'] || $region != $orig[0]['region']
399 || $country_name != $orig[0]['country-name']) {
400 $changes[] = L10n::t('Location');
401 $comma1 = ((($locality) && ($region || $country_name)) ? ', ' : ' ');
402 $comma2 = (($region && $country_name) ? ', ' : '');
403 $value = $locality . $comma1 . $region . $comma2 . $country_name;
407 $r = q("UPDATE `profile`
408 SET `profile-name` = '%s',
416 `postal-code` = '%s',
417 `country-name` = '%s',
427 `pub_keywords` = '%s',
428 `prv_keywords` = '%s',
442 WHERE `id` = %d AND `uid` = %d",
443 DBA::escape($profile_name),
446 DBA::escape($gender),
448 DBA::escape($address),
449 DBA::escape($locality),
450 DBA::escape($region),
451 DBA::escape($postal_code),
452 DBA::escape($country_name),
453 DBA::escape($marital),
455 DBA::escape($howlong),
456 DBA::escape($sexual),
458 DBA::escape($homepage),
459 DBA::escape($hometown),
460 DBA::escape($politic),
461 DBA::escape($religion),
462 DBA::escape($pub_keywords),
463 DBA::escape($prv_keywords),
465 DBA::escape($dislikes),
467 DBA::escape($interest),
468 DBA::escape($contact),
473 DBA::escape($romance),
475 DBA::escape($education),
476 intval($hide_friends),
481 /// @TODO decide to use DBA::isResult() here and check $r
483 info(L10n::t('Profile updated.') . EOL);
488 $r = q("UPDATE `user` set `username` = '%s' where `uid` = %d",
494 Contact::updateSelfFromUserID(local_user());
496 // Update global directory in background
497 $url = $_SESSION['my_url'];
498 if ($url && strlen(Config::get('system', 'directory'))) {
499 Worker::add(PRIORITY_LOW, "Directory", $url);
502 Worker::add(PRIORITY_LOW, 'ProfileUpdate', local_user());
504 // Update the global contact for the user
505 GContact::updateForUser(local_user());
510 function profiles_content(App $a) {
512 if (! local_user()) {
513 notice(L10n::t('Permission denied.') . EOL);
514 return Login::form();
519 if (($a->argc > 1) && (intval($a->argv[1]))) {
520 $r = q("SELECT * FROM `profile` WHERE `id` = %d AND `uid` = %d LIMIT 1",
524 if (! DBA::isResult($r)) {
525 notice(L10n::t('Profile not found.') . EOL);
529 $a->page['htmlhead'] .= replace_macros(get_markup_template('profed_head.tpl'), [
530 '$baseurl' => System::baseUrl(true),
533 $opt_tpl = get_markup_template("profile-hide-friends.tpl");
534 $hide_friends = replace_macros($opt_tpl,[
536 'hide-friends', //Name
537 L10n::t('Hide contacts and friends:'), //Label
538 !!$r[0]['hide-friends'], //Value
540 [L10n::t('No'), L10n::t('Yes')] //Off - On strings
542 '$desc' => L10n::t('Hide your contact/friend list from viewers of this profile?'),
543 '$yes_str' => L10n::t('Yes'),
544 '$no_str' => L10n::t('No'),
545 '$yes_selected' => (($r[0]['hide-friends']) ? " checked=\"checked\" " : ""),
546 '$no_selected' => (($r[0]['hide-friends'] == 0) ? " checked=\"checked\" " : "")
549 $personal_account = !(in_array($a->user["page-flags"],
550 [Contact::PAGE_COMMUNITY, Contact::PAGE_PRVGROUP]));
552 $detailled_profile = (PConfig::get(local_user(), 'system', 'detailled_profile') AND $personal_account);
554 $is_default = (($r[0]['is-default']) ? 1 : 0);
555 $tpl = get_markup_template("profile_edit.tpl");
556 $o .= replace_macros($tpl, [
557 '$personal_account' => $personal_account,
558 '$detailled_profile' => $detailled_profile,
561 'detailled_profile', //Name
562 L10n::t('Show more profile fields:'), //Label
563 $detailled_profile, //Value
565 [L10n::t('No'), L10n::t('Yes')] //Off - On strings
568 '$multi_profiles' => Feature::isEnabled(local_user(), 'multi_profiles'),
569 '$form_security_token' => BaseModule::getFormSecurityToken("profile_edit"),
570 '$form_security_token_photo' => BaseModule::getFormSecurityToken("profile_photo"),
571 '$profile_clone_link' => ((Feature::isEnabled(local_user(), 'multi_profiles')) ? 'profiles/clone/' . $r[0]['id'] . '?t=' . BaseModule::getFormSecurityToken("profile_clone") : ""),
572 '$profile_drop_link' => 'profiles/drop/' . $r[0]['id'] . '?t=' . BaseModule::getFormSecurityToken("profile_drop"),
574 '$profile_action' => L10n::t('Profile Actions'),
575 '$banner' => L10n::t('Edit Profile Details'),
576 '$submit' => L10n::t('Submit'),
577 '$profpic' => L10n::t('Change Profile Photo'),
578 '$profpiclink' => '/photos/' . $a->user['nickname'],
579 '$viewprof' => L10n::t('View this profile'),
580 '$viewallprof' => L10n::t('View all profiles'),
581 '$editvis' => L10n::t('Edit visibility'),
582 '$cr_prof' => L10n::t('Create a new profile using these settings'),
583 '$cl_prof' => L10n::t('Clone this profile'),
584 '$del_prof' => L10n::t('Delete this profile'),
586 '$lbl_basic_section' => L10n::t('Basic information'),
587 '$lbl_picture_section' => L10n::t('Profile picture'),
588 '$lbl_location_section' => L10n::t('Location'),
589 '$lbl_preferences_section' => L10n::t('Preferences'),
590 '$lbl_status_section' => L10n::t('Status information'),
591 '$lbl_about_section' => L10n::t('Additional information'),
592 '$lbl_interests_section' => L10n::t('Interests'),
593 '$lbl_personal_section' => L10n::t('Personal'),
594 '$lbl_relation_section' => L10n::t('Relation'),
595 '$lbl_miscellaneous_section' => L10n::t('Miscellaneous'),
597 '$lbl_profile_photo' => L10n::t('Upload Profile Photo'),
598 '$lbl_gender' => L10n::t('Your Gender:'),
599 '$lbl_marital' => L10n::t('<span class="heart">♥</span> Marital Status:'),
600 '$lbl_sexual' => L10n::t('Sexual Preference:'),
601 '$lbl_ex2' => L10n::t('Example: fishing photography software'),
603 '$disabled' => (($is_default) ? 'onclick="return false;" style="color: #BBBBFF;"' : ''),
604 '$baseurl' => System::baseUrl(true),
605 '$profile_id' => $r[0]['id'],
606 '$profile_name' => ['profile_name', L10n::t('Profile Name:'), $r[0]['profile-name'], L10n::t('Required'), '*'],
607 '$is_default' => $is_default,
608 '$default' => (($is_default) ? '<p id="profile-edit-default-desc">' . L10n::t('This is your <strong>public</strong> profile.<br />It <strong>may</strong> be visible to anybody using the internet.') . '</p>' : ""),
609 '$name' => ['name', L10n::t('Your Full Name:'), $r[0]['name']],
610 '$pdesc' => ['pdesc', L10n::t('Title/Description:'), $r[0]['pdesc']],
611 '$dob' => Temporal::getDateofBirthField($r[0]['dob']),
612 '$hide_friends' => $hide_friends,
613 '$address' => ['address', L10n::t('Street Address:'), $r[0]['address']],
614 '$locality' => ['locality', L10n::t('Locality/City:'), $r[0]['locality']],
615 '$region' => ['region', L10n::t('Region/State:'), $r[0]['region']],
616 '$postal_code' => ['postal_code', L10n::t('Postal/Zip Code:'), $r[0]['postal-code']],
617 '$country_name' => ['country_name', L10n::t('Country:'), $r[0]['country-name']],
618 '$age' => ((intval($r[0]['dob'])) ? '(' . L10n::t('Age: ') . Temporal::getAgeByTimezone($r[0]['dob'],$a->user['timezone'],$a->user['timezone']) . ')' : ''),
619 '$gender' => ContactSelector::gender($r[0]['gender']),
620 '$marital' => ['selector' => ContactSelector::maritalStatus($r[0]['marital']), 'value' => $r[0]['marital']],
621 '$with' => ['with', L10n::t("Who: \x28if applicable\x29"), strip_tags($r[0]['with']), L10n::t('Examples: cathy123, Cathy Williams, cathy@example.com')],
622 '$howlong' => ['howlong', L10n::t('Since [date]:'), ($r[0]['howlong'] <= DBA::NULL_DATETIME ? '' : DateTimeFormat::local($r[0]['howlong']))],
623 '$sexual' => ['selector' => ContactSelector::sexualPreference($r[0]['sexual']), 'value' => $r[0]['sexual']],
624 '$about' => ['about', L10n::t('Tell us about yourself...'), $r[0]['about']],
625 '$xmpp' => ['xmpp', L10n::t("XMPP \x28Jabber\x29 address:"), $r[0]['xmpp'], L10n::t("The XMPP address will be propagated to your contacts so that they can follow you.")],
626 '$homepage' => ['homepage', L10n::t('Homepage URL:'), $r[0]['homepage']],
627 '$hometown' => ['hometown', L10n::t('Hometown:'), $r[0]['hometown']],
628 '$politic' => ['politic', L10n::t('Political Views:'), $r[0]['politic']],
629 '$religion' => ['religion', L10n::t('Religious Views:'), $r[0]['religion']],
630 '$pub_keywords' => ['pub_keywords', L10n::t('Public Keywords:'), $r[0]['pub_keywords'], L10n::t("\x28Used for suggesting potential friends, can be seen by others\x29")],
631 '$prv_keywords' => ['prv_keywords', L10n::t('Private Keywords:'), $r[0]['prv_keywords'], L10n::t("\x28Used for searching profiles, never shown to others\x29")],
632 '$likes' => ['likes', L10n::t('Likes:'), $r[0]['likes']],
633 '$dislikes' => ['dislikes', L10n::t('Dislikes:'), $r[0]['dislikes']],
634 '$music' => ['music', L10n::t('Musical interests'), $r[0]['music']],
635 '$book' => ['book', L10n::t('Books, literature'), $r[0]['book']],
636 '$tv' => ['tv', L10n::t('Television'), $r[0]['tv']],
637 '$film' => ['film', L10n::t('Film/dance/culture/entertainment'), $r[0]['film']],
638 '$interest' => ['interest', L10n::t('Hobbies/Interests'), $r[0]['interest']],
639 '$romance' => ['romance', L10n::t('Love/romance'), $r[0]['romance']],
640 '$work' => ['work', L10n::t('Work/employment'), $r[0]['work']],
641 '$education' => ['education', L10n::t('School/education'), $r[0]['education']],
642 '$contact' => ['contact', L10n::t('Contact information and Social Networks'), $r[0]['contact']],
645 $arr = ['profile' => $r[0], 'entry' => $o];
646 Addon::callHooks('profile_edit', $arr);
650 // If we don't support multi profiles, don't display this list.
651 if (!Feature::isEnabled(local_user(), 'multi_profiles')) {
652 $r = q("SELECT * FROM `profile` WHERE `uid` = %d AND `is-default`=1",
655 if (DBA::isResult($r)) {
656 //Go to the default profile.
657 $a->internalRedirect('profiles/' . $r[0]['id']);
661 $r = q("SELECT * FROM `profile` WHERE `uid` = %d",
664 if (DBA::isResult($r)) {
666 $tpl = get_markup_template('profile_entry.tpl');
669 foreach ($r as $rr) {
670 $profiles .= replace_macros($tpl, [
671 '$photo' => $a->removeBaseURL($rr['thumb']),
673 '$alt' => L10n::t('Profile Image'),
674 '$profile_name' => $rr['profile-name'],
675 '$visible' => (($rr['is-default']) ? '<strong>' . L10n::t('visible to everybody') . '</strong>'
676 : '<a href="'.'profperm/'.$rr['id'].'" />' . L10n::t('Edit visibility') . '</a>')
680 $tpl_header = get_markup_template('profile_listing_header.tpl');
681 $o .= replace_macros($tpl_header,[
682 '$header' => L10n::t('Edit/Manage Profiles'),
683 '$chg_photo' => L10n::t('Change profile photo'),
684 '$cr_new' => L10n::t('Create New Profile'),
685 '$cr_new_link' => 'profiles/new?t=' . BaseModule::getFormSecurityToken("profile_new"),
686 '$profiles' => $profiles