use Friendica\Protocol\OStatus;
use Friendica\Protocol\PortableContact;
use Friendica\Protocol\Salmon;
+use Friendica\Util\BaseURL;
use Friendica\Util\DateTimeFormat;
use Friendica\Util\Network;
use Friendica\Util\Strings;
* @}
*/
+ /**
+ * @param integer $id
+ * @return array|boolean Contact record if it exists, false otherwise
+ * @throws \Exception
+ */
+ public static function getById($id)
+ {
+ return DBA::selectFirst('contact', [], ['id' => $id]);
+ }
+
/**
* @brief Tests if the given contact is a follower
*
}
DBA::update('user-contact', ['blocked' => $blocked], ['cid' => $cdata['public'], 'uid' => $uid], true);
+
+ if ($blocked) {
+ // Blocked contact can't be in any group
+ self::removeFromGroups($cid);
+ }
}
/**
}
if ($update) {
- $fields['name-date'] = DateTimeFormat::utcNow();
+ if ($fields['name'] != $self['name']) {
+ $fields['name-date'] = DateTimeFormat::utcNow();
+ }
+ $fields['updated'] = DateTimeFormat::utcNow();
DBA::update('contact', $fields, ['id' => $self['id']]);
// Update the public contact as well
$sparkle = false;
if (($contact['network'] === Protocol::DFRN) && !$contact['self']) {
$sparkle = true;
- $profile_link = System::baseUrl() . '/redir/' . $contact['id'];
+ $profile_link = System::baseUrl() . '/redir/' . $contact['id'] . '?url=' . $contact['url'];
} else {
$profile_link = $contact['url'];
}
}
if ($sparkle) {
- $status_link = $profile_link . '?url=status';
- $photos_link = $profile_link . '?url=photos';
- $profile_link = $profile_link . '?url=profile';
+ $status_link = $profile_link . '?tab=status';
+ $photos_link = str_replace('/profile/', '/photos/', $profile_link);
+ $profile_link = $profile_link . '?tab=profile';
}
if (in_array($contact['network'], [Protocol::DFRN, Protocol::DIASPORA]) && !$contact['self']) {
)", intval($uid), intval($uid));
}
+ /**
+ * Have a look at all contact tables for a given profile url.
+ * This function works as a replacement for probing the contact.
+ *
+ * @param string $url Contact URL
+ * @param integer $cid Contact ID
+ *
+ * @return array Contact array in the "probe" structure
+ */
+ private static function getProbeDataFromDatabase($url, $cid)
+ {
+ // The link could be provided as http although we stored it as https
+ $ssl_url = str_replace('http://', 'https://', $url);
+
+ $fields = ['url', 'addr', 'alias', 'notify', 'poll', 'name', 'nick',
+ 'photo', 'keywords', 'location', 'about', 'network',
+ 'priority', 'batch', 'request', 'confirm', 'poco'];
+
+ if (!empty($cid)) {
+ $data = DBA::selectFirst('contact', $fields, ['id' => $cid]);
+ if (DBA::isResult($data)) {
+ return $data;
+ }
+ }
+
+ $data = DBA::selectFirst('contact', $fields, ['nurl' => Strings::normaliseLink($url)]);
+
+ if (!DBA::isResult($data)) {
+ $condition = ['alias' => [$url, Strings::normaliseLink($url), $ssl_url]];
+ $data = DBA::selectFirst('contact', $fields, $condition);
+ }
+
+ if (DBA::isResult($data)) {
+ // For security reasons we don't fetch key data from our users
+ $data["pubkey"] = '';
+ return $data;
+ }
+
+ $fields = ['url', 'addr', 'alias', 'notify', 'name', 'nick',
+ 'photo', 'keywords', 'location', 'about', 'network'];
+ $data = DBA::selectFirst('gcontact', $fields, ['nurl' => Strings::normaliseLink($url)]);
+
+ if (!DBA::isResult($data)) {
+ $condition = ['alias' => [$url, Strings::normaliseLink($url), $ssl_url]];
+ $data = DBA::selectFirst('contact', $fields, $condition);
+ }
+
+ if (DBA::isResult($data)) {
+ $data["pubkey"] = '';
+ $data["poll"] = '';
+ $data["priority"] = 0;
+ $data["batch"] = '';
+ $data["request"] = '';
+ $data["confirm"] = '';
+ $data["poco"] = '';
+ return $data;
+ }
+
+ $data = ActivityPub::probeProfile($url, false);
+ if (!empty($data)) {
+ return $data;
+ }
+
+ $fields = ['url', 'addr', 'alias', 'notify', 'poll', 'name', 'nick',
+ 'photo', 'network', 'priority', 'batch', 'request', 'confirm'];
+ $data = DBA::selectFirst('fcontact', $fields, ['url' => $url]);
+
+ if (!DBA::isResult($data)) {
+ $condition = ['alias' => [$url, Strings::normaliseLink($url), $ssl_url]];
+ $data = DBA::selectFirst('contact', $fields, $condition);
+ }
+
+ if (DBA::isResult($data)) {
+ $data["pubkey"] = '';
+ $data["keywords"] = '';
+ $data["location"] = '';
+ $data["about"] = '';
+ $data["poco"] = '';
+ return $data;
+ }
+
+ return [];
+ }
+
/**
* @brief Fetch the contact id for a given URL and user
*
/// @todo Verify if we can't use Contact::getDetailsByUrl instead of the following
// We first try the nurl (http://server.tld/nick), most common case
- $contact = DBA::selectFirst('contact', ['id', 'avatar', 'avatar-date'], ['nurl' => Strings::normaliseLink($url), 'uid' => $uid, 'deleted' => false]);
+ $contact = DBA::selectFirst('contact', ['id', 'avatar', 'updated', 'network'], ['nurl' => Strings::normaliseLink($url), 'uid' => $uid, 'deleted' => false]);
// Then the addr (nick@server.tld)
if (!DBA::isResult($contact)) {
- $contact = DBA::selectFirst('contact', ['id', 'avatar', 'avatar-date'], ['addr' => $url, 'uid' => $uid, 'deleted' => false]);
+ $contact = DBA::selectFirst('contact', ['id', 'avatar', 'updated', 'network'], ['addr' => $url, 'uid' => $uid, 'deleted' => false]);
}
// Then the alias (which could be anything)
// The link could be provided as http although we stored it as https
$ssl_url = str_replace('http://', 'https://', $url);
$condition = ['`alias` IN (?, ?, ?) AND `uid` = ? AND NOT `deleted`', $url, Strings::normaliseLink($url), $ssl_url, $uid];
- $contact = DBA::selectFirst('contact', ['id', 'avatar', 'avatar-date'], $condition);
+ $contact = DBA::selectFirst('contact', ['id', 'avatar', 'updated', 'network'], $condition);
}
if (DBA::isResult($contact)) {
$contact_id = $contact["id"];
// Update the contact every 7 days
- $update_contact = ($contact['avatar-date'] < DateTimeFormat::utc('now -7 days'));
+ $update_contact = ($contact['updated'] < DateTimeFormat::utc('now -7 days'));
// We force the update if the avatar is empty
if (empty($contact['avatar'])) {
$update_contact = true;
}
+
+ // Update the contact in the background if needed but it is called by the frontend
+ if ($update_contact && $no_update && in_array($contact['network'], Protocol::NATIVE_SUPPORT)) {
+ Worker::add(PRIORITY_LOW, "UpdateContact", $contact_id);
+ }
+
if (!$update_contact || $no_update) {
return $contact_id;
}
return 0;
}
- // When we don't want to update, we look if some of our users already know this contact
- if ($no_update) {
- $fields = ['url', 'addr', 'alias', 'notify', 'poll', 'name', 'nick',
- 'photo', 'keywords', 'location', 'about', 'network',
- 'priority', 'batch', 'request', 'confirm', 'poco'];
- $data = DBA::selectFirst('contact', $fields, ['nurl' => Strings::normaliseLink($url)]);
-
- if (DBA::isResult($data)) {
- // For security reasons we don't fetch key data from our users
- $data["pubkey"] = '';
- }
+ // When we don't want to update, we look if we know this contact in any way
+ if ($no_update && empty($default)) {
+ $data = self::getProbeDataFromDatabase($url, $contact_id);
+ $background_update = true;
} else {
$data = [];
+ $background_update = false;
}
if (empty($data)) {
return 0;
}
- // Get data from the gcontact table
- $fields = ['name', 'nick', 'url', 'photo', 'addr', 'alias', 'network'];
- $contact = DBA::selectFirst('gcontact', $fields, ['nurl' => Strings::normaliseLink($url)]);
- if (!DBA::isResult($contact)) {
- $contact = DBA::selectFirst('contact', $fields, ['nurl' => Strings::normaliseLink($url)]);
- }
-
- if (!DBA::isResult($contact)) {
- $fields = ['url', 'addr', 'alias', 'notify', 'poll', 'name', 'nick',
- 'photo', 'keywords', 'location', 'about', 'network',
- 'priority', 'batch', 'request', 'confirm', 'poco'];
- $contact = DBA::selectFirst('contact', $fields, ['addr' => $url]);
- }
-
- // The link could be provided as http although we stored it as https
- $ssl_url = str_replace('http://', 'https://', $url);
-
- if (!DBA::isResult($contact)) {
- $condition = ['alias' => [$url, Strings::normaliseLink($url), $ssl_url]];
- $contact = DBA::selectFirst('contact', $fields, $condition);
- }
-
- if (!DBA::isResult($contact)) {
- $fields = ['url', 'addr', 'alias', 'notify', 'poll', 'name', 'nick',
- 'photo', 'network', 'priority', 'batch', 'request', 'confirm'];
- $condition = ['url' => [$url, Strings::normaliseLink($url), $ssl_url]];
- $contact = DBA::selectFirst('fcontact', $fields, $condition);
+ $contact = array_merge(self::getProbeDataFromDatabase($url, $contact_id), $default);
+ if (empty($contact)) {
+ return 0;
}
- if (!empty($default)) {
- $contact = $default;
- }
+ $data = array_merge($data, $contact);
+ }
- if (!DBA::isResult($contact)) {
- return 0;
- } else {
- $data = array_merge($data, $contact);
- }
+ if (empty($data)) {
+ return 0;
}
- if (!$contact_id && ($data["alias"] != '') && ($data["alias"] != $url) && !$in_loop) {
+ if (!$contact_id && !empty($data['alias']) && ($data['alias'] != $url) && !$in_loop) {
$contact_id = self::getIdForURL($data["alias"], $uid, true, $default, true);
}
$fields = [
'uid' => $uid,
'created' => DateTimeFormat::utcNow(),
- 'url' => $data["url"],
- 'nurl' => Strings::normaliseLink($data["url"]),
- 'addr' => $data["addr"],
- 'alias' => $data["alias"],
- 'notify' => $data["notify"],
- 'poll' => $data["poll"],
- 'name' => $data["name"],
- 'nick' => $data["nick"],
- 'photo' => $data["photo"],
- 'keywords' => $data["keywords"],
- 'location' => $data["location"],
- 'about' => $data["about"],
- 'network' => $data["network"],
- 'pubkey' => $data["pubkey"],
+ 'url' => $data['url'],
+ 'nurl' => Strings::normaliseLink($data['url']),
+ 'addr' => defaults($data, 'addr', ''),
+ 'alias' => defaults($data, 'alias', ''),
+ 'notify' => defaults($data, 'notify', ''),
+ 'poll' => defaults($data, 'poll', ''),
+ 'name' => defaults($data, 'name', ''),
+ 'nick' => defaults($data, 'nick', ''),
+ 'photo' => defaults($data, 'photo', ''),
+ 'keywords' => defaults($data, 'keywords', ''),
+ 'location' => defaults($data, 'location', ''),
+ 'about' => defaults($data, 'about', ''),
+ 'network' => $data['network'],
+ 'pubkey' => defaults($data, 'pubkey', ''),
'rel' => self::SHARING,
- 'priority' => $data["priority"],
- 'batch' => $data["batch"],
- 'request' => $data["request"],
- 'confirm' => $data["confirm"],
- 'poco' => $data["poco"],
+ 'priority' => defaults($data, 'priority', 0),
+ 'batch' => defaults($data, 'batch', ''),
+ 'request' => defaults($data, 'request', ''),
+ 'confirm' => defaults($data, 'confirm', ''),
+ 'poco' => defaults($data, 'poco', ''),
'name-date' => DateTimeFormat::utcNow(),
'uri-date' => DateTimeFormat::utcNow(),
'avatar-date' => DateTimeFormat::utcNow(),
$contact_id = $contacts[0]["id"];
+ // Update in the background when we fetched the data solely from the database
+ if ($background_update) {
+ Worker::add(PRIORITY_LOW, "UpdateContact", $contact_id);
+ }
+
// Update the newly created contact from data in the gcontact table
$gcontact = DBA::selectFirst('gcontact', ['location', 'about', 'keywords', 'gender'], ['nurl' => Strings::normaliseLink($data["url"])]);
if (DBA::isResult($gcontact)) {
// Only use the information when the probing hadn't fetched these values
- if ($data['keywords'] != '') {
+ if (!empty($data['keywords'])) {
unset($gcontact['keywords']);
}
- if ($data['location'] != '') {
+ if (!empty($data['location'])) {
unset($gcontact['location']);
}
- if ($data['about'] != '') {
+ if (!empty($data['about'])) {
unset($gcontact['about']);
}
DBA::update('contact', $gcontact, ['id' => $contact_id]);
}
}
- self::updateAvatar($data["photo"], $uid, $contact_id);
+ if (!empty($data['photo'])) {
+ self::updateAvatar($data['photo'], $uid, $contact_id);
+ }
$fields = ['url', 'nurl', 'addr', 'alias', 'name', 'nick', 'keywords', 'location', 'about', 'avatar-date', 'pubkey'];
$contact = DBA::selectFirst('contact', $fields, ['id' => $contact_id]);
}
$updated = ['addr' => $data['addr'],
- 'alias' => $data['alias'],
+ 'alias' => defaults($data, 'alias', ''),
'url' => $data['url'],
'nurl' => Strings::normaliseLink($data['url']),
'name' => $data['name'],
'nick' => $data['nick']];
- if ($data['keywords'] != '') {
+ if (!empty($data['keywords'])) {
$updated['keywords'] = $data['keywords'];
}
- if ($data['location'] != '') {
+ if (!empty($data['location'])) {
$updated['location'] = $data['location'];
}
// Update the technical stuff as well - if filled
- if ($data['notify'] != '') {
+ if (!empty($data['notify'])) {
$updated['notify'] = $data['notify'];
}
- if ($data['poll'] != '') {
+ if (!empty($data['poll'])) {
$updated['poll'] = $data['poll'];
}
- if ($data['batch'] != '') {
+ if (!empty($data['batch'])) {
$updated['batch'] = $data['batch'];
}
- if ($data['request'] != '') {
+ if (!empty($data['request'])) {
$updated['request'] = $data['request'];
}
- if ($data['confirm'] != '') {
+ if (!empty($data['confirm'])) {
$updated['confirm'] = $data['confirm'];
}
- if ($data['poco'] != '') {
+ if (!empty($data['poco'])) {
$updated['poco'] = $data['poco'];
}
$updated['pubkey'] = $data['pubkey'];
}
- if (($data["addr"] != $contact["addr"]) || ($data["alias"] != $contact["alias"])) {
+ if (($data['addr'] != $contact['addr']) || (!empty($data['alias']) && ($data['alias'] != $contact['alias']))) {
$updated['uri-date'] = DateTimeFormat::utcNow();
}
if (($data["name"] != $contact["name"]) || ($data["nick"] != $contact["nick"])) {
$updated['name-date'] = DateTimeFormat::utcNow();
}
- $updated['avatar-date'] = DateTimeFormat::utcNow();
+ $updated['updated'] = DateTimeFormat::utcNow();
DBA::update('contact', $updated, ['id' => $contact_id], $contact);
/**
* @param integer $id contact id
* @param string $network Optional network we are probing for
+ * @param boolean $force Optional forcing of network probing (otherwise we use the cached data)
* @return boolean
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
* @throws \ImagickException
*/
- public static function updateFromProbe($id, $network = '')
+ public static function updateFromProbe($id, $network = '', $force = false)
{
/*
Warning: Never ever fetch the public key via Probe::uri and write it into the contacts.
This will reliably kill your communication with Friendica contacts.
*/
- $fields = ['url', 'nurl', 'addr', 'alias', 'batch', 'notify', 'poll', 'poco', 'network'];
+ $fields = ['avatar', 'uid', 'name', 'nick', 'url', 'addr', 'batch', 'notify',
+ 'poll', 'request', 'confirm', 'poco', 'network', 'alias'];
$contact = DBA::selectFirst('contact', $fields, ['id' => $id]);
if (!DBA::isResult($contact)) {
return false;
}
- $ret = Probe::uri($contact["url"], $network);
+ $uid = $contact['uid'];
+ unset($contact['uid']);
+
+ $contact['photo'] = $contact['avatar'];
+ unset($contact['avatar']);
+
+ $ret = Probe::uri($contact['url'], $network, $uid, !$force);
// If Probe::uri fails the network code will be different (mostly "feed" or "unkn")
- if (($ret["network"] != $contact["network"]) && !in_array($ret["network"], [Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::DIASPORA, $network])) {
+ if (in_array($ret['network'], [Protocol::FEED, Protocol::PHANTOM]) && ($ret['network'] != $contact['network'])) {
+ return false;
+ }
+
+ if (!in_array($ret['network'], Protocol::NATIVE_SUPPORT)) {
return false;
}
// make sure to not overwrite existing values with blank entries
foreach ($ret as $key => $val) {
- if (isset($contact[$key]) && ($contact[$key] != "") && ($val == "")) {
+ if (!isset($contact[$key])) {
+ unset($ret[$key]);
+ } elseif (($contact[$key] != '') && ($val == '')) {
$ret[$key] = $contact[$key];
- }
-
- if (isset($contact[$key]) && ($ret[$key] != $contact[$key])) {
+ } elseif ($ret[$key] != $contact[$key]) {
$update = true;
}
}
return true;
}
- DBA::update(
- 'contact', [
- 'url' => $ret['url'],
- 'nurl' => Strings::normaliseLink($ret['url']),
- 'network' => $ret['network'],
- 'addr' => $ret['addr'],
- 'alias' => $ret['alias'],
- 'batch' => $ret['batch'],
- 'notify' => $ret['notify'],
- 'poll' => $ret['poll'],
- 'poco' => $ret['poco']
- ],
- ['id' => $id]
- );
+ $ret['nurl'] = Strings::normaliseLink($ret['url']);
+ $ret['updated'] = DateTimeFormat::utcNow();
+
+ self::updateAvatar($ret['photo'], $uid, $id, true);
+
+ unset($ret['photo']);
+ DBA::update('contact', $ret, ['id' => $id]);
// Update the corresponding gcontact entry
PortableContact::lastUpdated($ret["url"]);
public static function updateSslPolicy(array $contact, $new_policy)
{
$ssl_changed = false;
- if ((intval($new_policy) == SSL_POLICY_SELFSIGN || $new_policy === 'self') && strstr($contact['url'], 'https:')) {
+ if ((intval($new_policy) == BaseURL::SSL_POLICY_SELFSIGN || $new_policy === 'self') && strstr($contact['url'], 'https:')) {
$ssl_changed = true;
$contact['url'] = str_replace('https:', 'http:', $contact['url']);
$contact['request'] = str_replace('https:', 'http:', $contact['request']);
$contact['poco'] = str_replace('https:', 'http:', $contact['poco']);
}
- if ((intval($new_policy) == SSL_POLICY_FULL || $new_policy === 'full') && strstr($contact['url'], 'http:')) {
+ if ((intval($new_policy) == BaseURL::SSL_POLICY_FULL || $new_policy === 'full') && strstr($contact['url'], 'http:')) {
$ssl_changed = true;
$contact['url'] = str_replace('http:', 'https:', $contact['url']);
$contact['request'] = str_replace('http:', 'https:', $contact['request']);
return $redirect;
}
+
+ /**
+ * Remove a contact from all groups
+ *
+ * @param integer $contact_id
+ *
+ * @return boolean Success
+ */
+ public static function removeFromGroups($contact_id)
+ {
+ return DBA::delete('group_member', ['contact-id' => $contact_id]);
+ }
+
+ /**
+ * Is the contact a forum?
+ *
+ * @param integer $contactid ID of the contact
+ *
+ * @return boolean "true" if it is a forum
+ */
+ public static function isForum($contactid)
+ {
+ $fields = ['forum', 'prv'];
+ $condition = ['id' => $contactid];
+ $contact = DBA::selectFirst('contact', $fields, $condition);
+ if (!DBA::isResult($contact)) {
+ return false;
+ }
+
+ // Is it a forum?
+ return ($contact['forum'] || $contact['prv']);
+ }
}