* @}
*/
+ /**
+ * @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
*
)", 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
+ *
+ * @return array Contact array in the "probe" structure
+ */
+ private static function getProbeDataFromDatabase($url)
+ {
+ // 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'];
+ $data = DBA::selectFirst('contact', $fields, ['nurl' => Strings::normaliseLink($url)]);
+
+ if (!DBA::isResult($contact)) {
+ $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($contact)) {
+ $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($contact)) {
+ $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
*
if (empty($contact['avatar'])) {
$update_contact = true;
}
+
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);
+ $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);
- }
-
if (!empty($default)) {
$contact = $default;
+ } else {
+ $contact = self::getProbeDataFromDatabase($url);
}
- if (!DBA::isResult($contact)) {
+ if (!empty($contact)) {
return 0;
} else {
$data = array_merge($data, $contact);
$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)) {
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']);
+ }
}