Files
friendica/mod/profiles.php

679 lines
23 KiB
PHP

<?php
/**
* @file mod/profiles.php
*/
use Friendica\App;
use Friendica\BaseModule;
use Friendica\Content\ContactSelector;
use Friendica\Content\Feature;
use Friendica\Content\Nav;
use Friendica\Core\Config;
use Friendica\Core\Hook;
use Friendica\Core\L10n;
use Friendica\Core\PConfig;
use Friendica\Core\Renderer;
use Friendica\Core\System;
use Friendica\Core\Worker;
use Friendica\Database\DBA;
use Friendica\Model\Contact;
use Friendica\Model\GContact;
use Friendica\Model\Profile;
use Friendica\Model\User;
use Friendica\Module\Login;
use Friendica\Network\Probe;
use Friendica\Util\DateTimeFormat;
use Friendica\Util\Strings;
use Friendica\Util\Temporal;
function profiles_init(App $a) {
Nav::setSelected('profiles');
if (! local_user()) {
return;
}
if (($a->argc > 2) && ($a->argv[1] === "drop") && intval($a->argv[2])) {
$r = q("SELECT * FROM `profile` WHERE `id` = %d AND `uid` = %d AND `is-default` = 0 LIMIT 1",
intval($a->argv[2]),
intval(local_user())
);
if (! DBA::isResult($r)) {
notice(L10n::t('Profile not found.') . EOL);
$a->internalRedirect('profiles');
return; // NOTREACHED
}
BaseModule::checkFormSecurityTokenRedirectOnError('/profiles', 'profile_drop', 't');
// move every contact using this profile as their default to the user default
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 ",
intval(local_user()),
intval($a->argv[2]),
intval(local_user())
);
q("DELETE FROM `profile` WHERE `id` = %d AND `uid` = %d",
intval($a->argv[2]),
intval(local_user())
);
if (DBA::isResult($r)) {
info(L10n::t('Profile deleted.').EOL);
}
$a->internalRedirect('profiles');
return; // NOTREACHED
}
if (($a->argc > 1) && ($a->argv[1] === 'new')) {
BaseModule::checkFormSecurityTokenRedirectOnError('/profiles', 'profile_new', 't');
$r0 = q("SELECT `id` FROM `profile` WHERE `uid` = %d",
intval(local_user()));
$num_profiles = (DBA::isResult($r0) ? count($r0) : 0);
$name = L10n::t('Profile-') . ($num_profiles + 1);
$r1 = q("SELECT `name`, `photo`, `thumb` FROM `profile` WHERE `uid` = %d AND `is-default` = 1 LIMIT 1",
intval(local_user()));
q("INSERT INTO `profile` (`uid` , `profile-name` , `name`, `photo`, `thumb`)
VALUES ( %d, '%s', '%s', '%s', '%s' )",
intval(local_user()),
DBA::escape($name),
DBA::escape($r1[0]['name']),
DBA::escape($r1[0]['photo']),
DBA::escape($r1[0]['thumb'])
);
$r3 = q("SELECT `id` FROM `profile` WHERE `uid` = %d AND `profile-name` = '%s' LIMIT 1",
intval(local_user()),
DBA::escape($name)
);
info(L10n::t('New profile created.') . EOL);
if (DBA::isResult($r3) && count($r3) == 1) {
$a->internalRedirect('profiles/' . $r3[0]['id']);
}
$a->internalRedirect('profiles');
}
if (($a->argc > 2) && ($a->argv[1] === 'clone')) {
BaseModule::checkFormSecurityTokenRedirectOnError('/profiles', 'profile_clone', 't');
$r0 = q("SELECT `id` FROM `profile` WHERE `uid` = %d",
intval(local_user()));
$num_profiles = (DBA::isResult($r0) ? count($r0) : 0);
$name = L10n::t('Profile-') . ($num_profiles + 1);
$r1 = q("SELECT * FROM `profile` WHERE `uid` = %d AND `id` = %d LIMIT 1",
intval(local_user()),
intval($a->argv[2])
);
if(! DBA::isResult($r1)) {
notice(L10n::t('Profile unavailable to clone.') . EOL);
exit();
}
unset($r1[0]['id']);
$r1[0]['is-default'] = 0;
$r1[0]['publish'] = 0;
$r1[0]['net-publish'] = 0;
$r1[0]['profile-name'] = DBA::escape($name);
DBA::insert('profile', $r1[0]);
$r3 = q("SELECT `id` FROM `profile` WHERE `uid` = %d AND `profile-name` = '%s' LIMIT 1",
intval(local_user()),
DBA::escape($name)
);
info(L10n::t('New profile created.') . EOL);
if ((DBA::isResult($r3)) && (count($r3) == 1)) {
$a->internalRedirect('profiles/'.$r3[0]['id']);
}
$a->internalRedirect('profiles');
return; // NOTREACHED
}
if (($a->argc > 1) && (intval($a->argv[1]))) {
$r = q("SELECT id FROM `profile` WHERE `id` = %d AND `uid` = %d LIMIT 1",
intval($a->argv[1]),
intval(local_user())
);
if (! DBA::isResult($r)) {
notice(L10n::t('Profile not found.') . EOL);
exit();
}
Profile::load($a, $a->user['nickname'], $r[0]['id']);
}
}
function profile_clean_keywords($keywords)
{
$keywords = str_replace(",", " ", $keywords);
$keywords = explode(" ", $keywords);
$cleaned = [];
foreach ($keywords as $keyword) {
$keyword = trim(strtolower($keyword));
$keyword = trim($keyword, "#");
if ($keyword != "") {
$cleaned[] = $keyword;
}
}
$keywords = implode(", ", $cleaned);
return $keywords;
}
function profiles_post(App $a) {
if (! local_user()) {
notice(L10n::t('Permission denied.') . EOL);
return;
}
$namechanged = false;
Hook::callAll('profile_post', $_POST);
if (($a->argc > 1) && ($a->argv[1] !== "new") && intval($a->argv[1])) {
$orig = q("SELECT * FROM `profile` WHERE `id` = %d AND `uid` = %d LIMIT 1",
intval($a->argv[1]),
intval(local_user())
);
if (! DBA::isResult($orig)) {
notice(L10n::t('Profile not found.') . EOL);
return;
}
BaseModule::checkFormSecurityTokenRedirectOnError('/profiles', 'profile_edit');
$is_default = (($orig[0]['is-default']) ? 1 : 0);
$profile_name = Strings::escapeTags(trim($_POST['profile_name']));
if (! strlen($profile_name)) {
notice(L10n::t('Profile Name is required.') . EOL);
return;
}
$dob = $_POST['dob'] ? Strings::escapeHtml(trim($_POST['dob'])) : '0000-00-00';
$y = substr($dob, 0, 4);
if ((! ctype_digit($y)) || ($y < 1900)) {
$ignore_year = true;
} else {
$ignore_year = false;
}
if (!in_array($dob, ['0000-00-00', DBA::NULL_DATE])) {
if (strpos($dob, '0000-') === 0 || strpos($dob, '0001-') === 0) {
$ignore_year = true;
$dob = substr($dob, 5);
}
if ($ignore_year) {
$dob = '0000-' . DateTimeFormat::utc('1900-' . $dob, 'm-d');
} else {
$dob = DateTimeFormat::utc($dob, 'Y-m-d');
}
}
$name = Strings::escapeTags(trim($_POST['name']));
if (! strlen($name)) {
$name = '[No Name]';
}
if ($orig[0]['name'] != $name) {
$namechanged = true;
}
$pdesc = Strings::escapeTags(trim($_POST['pdesc']));
$gender = Strings::escapeTags(trim($_POST['gender']));
$address = Strings::escapeTags(trim($_POST['address']));
$locality = Strings::escapeTags(trim($_POST['locality']));
$region = Strings::escapeTags(trim($_POST['region']));
$postal_code = Strings::escapeTags(trim($_POST['postal_code']));
$country_name = Strings::escapeTags(trim($_POST['country_name']));
$pub_keywords = profile_clean_keywords(Strings::escapeTags(trim($_POST['pub_keywords'])));
$prv_keywords = profile_clean_keywords(Strings::escapeTags(trim($_POST['prv_keywords'])));
$marital = Strings::escapeTags(trim($_POST['marital']));
$howlong = Strings::escapeTags(trim($_POST['howlong']));
$with = (!empty($_POST['with']) ? Strings::escapeTags(trim($_POST['with'])) : '');
if (! strlen($howlong)) {
$howlong = DBA::NULL_DATETIME;
} else {
$howlong = DateTimeFormat::convert($howlong, 'UTC', date_default_timezone_get());
}
// linkify the relationship target if applicable
$withchanged = false;
if (strlen($with)) {
if ($with != strip_tags($orig[0]['with'])) {
$withchanged = true;
$prf = '';
$lookup = $with;