+
+ /**
+ * @param integer $id contact id
+ * @return boolean
+ */
+ public static function updateFromProbe($id)
+ {
+ /*
+ 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'];
+ $contact = dba::selectFirst('contact', $fields, ['id' => $id]);
+ if (!DBM::is_result($contact)) {
+ return false;
+ }
+
+ $ret = Probe::uri($contact["url"]);
+
+ // If Probe::uri fails the network code will be different
+ if ($ret["network"] != $contact["network"]) {
+ return false;
+ }
+
+ $update = false;
+
+ // make sure to not overwrite existing values with blank entries
+ foreach ($ret as $key => $val) {
+ if (isset($contact[$key]) && ($contact[$key] != "") && ($val == "")) {
+ $ret[$key] = $contact[$key];
+ }
+
+ if (isset($contact[$key]) && ($ret[$key] != $contact[$key])) {
+ $update = true;
+ }
+ }
+
+ if (!$update) {
+ return true;
+ }
+
+ dba::update(
+ 'contact', [
+ 'url' => $ret['url'],
+ 'nurl' => normalise_link($ret['url']),
+ 'addr' => $ret['addr'],
+ 'alias' => $ret['alias'],
+ 'batch' => $ret['batch'],
+ 'notify' => $ret['notify'],
+ 'poll' => $ret['poll'],
+ 'poco' => $ret['poco']
+ ],
+ ['id' => $id]
+ );
+
+ // Update the corresponding gcontact entry
+ PortableContact::lastUpdated($ret["url"]);
+
+ return true;
+ }
+
+ /**
+ * Takes a $uid and a url/handle and adds a new contact
+ * Currently if the contact is DFRN, interactive needs to be true, to redirect to the
+ * dfrn_request page.
+ *
+ * Otherwise this can be used to bulk add StatusNet contacts, Twitter contacts, etc.
+ *
+ * Returns an array
+ * $return['success'] boolean true if successful
+ * $return['message'] error text if success is false.
+ *
+ * @brief Takes a $uid and a url/handle and adds a new contact
+ * @param int $uid
+ * @param string $url
+ * @param bool $interactive
+ * @param string $network
+ * @return boolean|string
+ */
+ public static function createFromProbe($uid, $url, $interactive = false, $network = '')
+ {
+ $result = ['cid' => -1, 'success' => false, 'message' => ''];
+
+ $a = get_app();
+
+ // remove ajax junk, e.g. Twitter
+ $url = str_replace('/#!/', '/', $url);
+
+ if (!Network::isUrlAllowed($url)) {
+ $result['message'] = L10n::t('Disallowed profile URL.');
+ return $result;
+ }
+
+ if (Network::isUrlBlocked($url)) {
+ $result['message'] = L10n::t('Blocked domain');
+ return $result;
+ }
+
+ if (!$url) {
+ $result['message'] = L10n::t('Connect URL missing.');
+ return $result;
+ }
+
+ $arr = ['url' => $url, 'contact' => []];
+
+ Addon::callHooks('follow', $arr);
+
+ if (empty($arr)) {
+ $result['message'] = L10n::t('The contact could not be added. Please check the relevant network credentials in your Settings -> Social Networks page.');
+ return $result;
+ }
+
+ if (x($arr['contact'], 'name')) {
+ $ret = $arr['contact'];
+ } else {
+ $ret = Probe::uri($url, $network, $uid, false);
+ }
+
+ if (($network != '') && ($ret['network'] != $network)) {
+ logger('Expected network ' . $network . ' does not match actual network ' . $ret['network']);
+ return $result;
+ }
+
+ // check if we already have a contact
+ // the poll url is more reliable than the profile url, as we may have
+ // indirect links or webfinger links
+
+ $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `poll` IN ('%s', '%s') AND `network` = '%s' AND NOT `pending` LIMIT 1",
+ intval($uid),
+ dbesc($ret['poll']),
+ dbesc(normalise_link($ret['poll'])),
+ dbesc($ret['network'])
+ );
+
+ if (!DBM::is_result($r)) {
+ $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `nurl` = '%s' AND `network` = '%s' AND NOT `pending` LIMIT 1",
+ intval($uid),
+ dbesc(normalise_link($url)),
+ dbesc($ret['network'])
+ );
+ }
+
+ if (($ret['network'] === NETWORK_DFRN) && !DBM::is_result($r)) {
+ if ($interactive) {
+ if (strlen($a->path)) {
+ $myaddr = bin2hex(System::baseUrl() . '/profile/' . $a->user['nickname']);
+ } else {
+ $myaddr = bin2hex($a->user['nickname'] . '@' . $a->get_hostname());
+ }
+
+ goaway($ret['request'] . "&addr=$myaddr");
+
+ // NOTREACHED
+ }
+ } elseif (Config::get('system', 'dfrn_only') && ($ret['network'] != NETWORK_DFRN)) {
+ $result['message'] = L10n::t('This site is not configured to allow communications with other networks.') . EOL;
+ $result['message'] != L10n::t('No compatible communication protocols or feeds were discovered.') . EOL;
+ return $result;
+ }
+
+ // This extra param just confuses things, remove it
+ if ($ret['network'] === NETWORK_DIASPORA) {
+ $ret['url'] = str_replace('?absolute=true', '', $ret['url']);
+ }
+
+ // do we have enough information?
+
+ if (!((x($ret, 'name')) && (x($ret, 'poll')) && ((x($ret, 'url')) || (x($ret, 'addr'))))) {
+ $result['message'] .= L10n::t('The profile address specified does not provide adequate information.') . EOL;
+ if (!x($ret, 'poll')) {
+ $result['message'] .= L10n::t('No compatible communication protocols or feeds were discovered.') . EOL;
+ }
+ if (!x($ret, 'name')) {
+ $result['message'] .= L10n::t('An author or name was not found.') . EOL;
+ }
+ if (!x($ret, 'url')) {
+ $result['message'] .= L10n::t('No browser URL could be matched to this address.') . EOL;
+ }
+ if (strpos($url, '@') !== false) {
+ $result['message'] .= L10n::t('Unable to match @-style Identity Address with a known protocol or email contact.') . EOL;
+ $result['message'] .= L10n::t('Use mailto: in front of address to force email check.') . EOL;
+ }
+ return $result;
+ }
+
+ if ($ret['network'] === NETWORK_OSTATUS && Config::get('system', 'ostatus_disabled')) {
+ $result['message'] .= L10n::t('The profile address specified belongs to a network which has been disabled on this site.') . EOL;
+ $ret['notify'] = '';
+ }
+
+ if (!$ret['notify']) {
+ $result['message'] .= L10n::t('Limited profile. This person will be unable to receive direct/personal notifications from you.') . EOL;
+ }
+
+ $writeable = ((($ret['network'] === NETWORK_OSTATUS) && ($ret['notify'])) ? 1 : 0);
+
+ $subhub = (($ret['network'] === NETWORK_OSTATUS) ? true : false);
+
+ $hidden = (($ret['network'] === NETWORK_MAIL) ? 1 : 0);
+
+ if (in_array($ret['network'], [NETWORK_MAIL, NETWORK_DIASPORA])) {
+ $writeable = 1;
+ }
+
+ if (DBM::is_result($r)) {
+ // update contact
+ $new_relation = (($r[0]['rel'] == CONTACT_IS_FOLLOWER) ? CONTACT_IS_FRIEND : CONTACT_IS_SHARING);
+
+ $fields = ['rel' => $new_relation, 'subhub' => $subhub, 'readonly' => false];
+ dba::update('contact', $fields, ['id' => $r[0]['id']]);
+ } else {
+ $new_relation = ((in_array($ret['network'], [NETWORK_MAIL])) ? CONTACT_IS_FRIEND : CONTACT_IS_SHARING);
+
+ // create contact record
+ dba::insert('contact', [
+ 'uid' => $uid,
+ 'created' => DateTimeFormat::utcNow(),
+ 'url' => $ret['url'],
+ 'nurl' => normalise_link($ret['url']),
+ 'addr' => $ret['addr'],
+ 'alias' => $ret['alias'],
+ 'batch' => $ret['batch'],
+ 'notify' => $ret['notify'],
+ 'poll' => $ret['poll'],
+ 'poco' => $ret['poco'],
+ 'name' => $ret['name'],
+ 'nick' => $ret['nick'],
+ 'network' => $ret['network'],
+ 'pubkey' => $ret['pubkey'],
+ 'rel' => $new_relation,
+ 'priority'=> $ret['priority'],
+ 'writable'=> $writeable,
+ 'hidden' => $hidden,
+ 'blocked' => 0,
+ 'readonly'=> 0,
+ 'pending' => 0,
+ 'subhub' => $subhub
+ ]);
+ }
+
+ $contact = dba::selectFirst('contact', [], ['url' => $ret['url'], 'network' => $ret['network'], 'uid' => $uid]);
+ if (!DBM::is_result($contact)) {
+ $result['message'] .= L10n::t('Unable to retrieve contact information.') . EOL;
+ return $result;
+ }
+
+ $contact_id = $contact['id'];
+ $result['cid'] = $contact_id;
+
+ Group::addMember(User::getDefaultGroup($uid, $contact["network"]), $contact_id);
+
+ // Update the avatar
+ self::updateAvatar($ret['photo'], $uid, $contact_id);
+
+ // pull feed and consume it, which should subscribe to the hub.
+
+ Worker::add(PRIORITY_HIGH, "OnePoll", $contact_id, "force");
+
+ $r = q("SELECT `contact`.*, `user`.* FROM `contact` INNER JOIN `user` ON `contact`.`uid` = `user`.`uid`
+ WHERE `user`.`uid` = %d AND `contact`.`self` LIMIT 1",
+ intval($uid)
+ );
+
+ if (DBM::is_result($r)) {
+ if (in_array($contact['network'], [NETWORK_OSTATUS, NETWORK_DFRN])) {
+ // create a follow slap
+ $item = [];
+ $item['verb'] = ACTIVITY_FOLLOW;
+ $item['follow'] = $contact["url"];
+ $slap = OStatus::salmon($item, $r[0]);
+ if (!empty($contact['notify'])) {
+ Salmon::slapper($r[0], $contact['notify'], $slap);
+ }
+ } elseif ($contact['network'] == NETWORK_DIASPORA) {
+ $ret = Diaspora::sendShare($a->user, $contact);
+ logger('share returns: ' . $ret);
+ }
+ }
+
+ $result['success'] = true;
+ return $result;
+ }
+
+ public static function updateSslPolicy($contact, $new_policy)
+ {
+ $ssl_changed = false;
+ if ((intval($new_policy) == 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['notify'] = str_replace('https:', 'http:', $contact['notify']);
+ $contact['poll'] = str_replace('https:', 'http:', $contact['poll']);
+ $contact['confirm'] = str_replace('https:', 'http:', $contact['confirm']);
+ $contact['poco'] = str_replace('https:', 'http:', $contact['poco']);
+ }
+
+ if ((intval($new_policy) == 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']);
+ $contact['notify'] = str_replace('http:', 'https:', $contact['notify']);
+ $contact['poll'] = str_replace('http:', 'https:', $contact['poll']);
+ $contact['confirm'] = str_replace('http:', 'https:', $contact['confirm']);
+ $contact['poco'] = str_replace('http:', 'https:', $contact['poco']);
+ }
+
+ if ($ssl_changed) {
+ $fields = ['url' => $contact['url'], 'request' => $contact['request'],
+ 'notify' => $contact['notify'], 'poll' => $contact['poll'],
+ 'confirm' => $contact['confirm'], 'poco' => $contact['poco']];
+ dba::update('contact', $fields, ['id' => $contact['id']]);
+ }
+
+ return $contact;
+ }
+
+ public static function addRelationship($importer, $contact, $datarray, $item, $sharing = false) {
+ $url = notags(trim($datarray['author-link']));
+ $name = notags(trim($datarray['author-name']));
+ $photo = notags(trim($datarray['author-avatar']));
+ $nick = '';
+
+ if (is_object($item)) {
+ $rawtag = $item->get_item_tags(NAMESPACE_ACTIVITY,'actor');
+ if ($rawtag && $rawtag[0]['child'][NAMESPACE_POCO]['preferredUsername'][0]['data']) {
+ $nick = $rawtag[0]['child'][NAMESPACE_POCO]['preferredUsername'][0]['data'];
+ }
+ } else {
+ $nick = $item;
+ }
+
+ if (is_array($contact)) {
+ if (($contact['rel'] == CONTACT_IS_SHARING)
+ || ($sharing && $contact['rel'] == CONTACT_IS_FOLLOWER)) {
+ dba::update('contact', ['rel' => CONTACT_IS_FRIEND, 'writable' => true],
+ ['id' => $contact['id'], 'uid' => $importer['uid']]);
+ }
+ // send email notification to owner?
+ } else {
+ if (dba::exists('contact', ['nurl' => normalise_link($url), 'uid' => $importer['uid'], 'pending' => true])) {
+ logger('ignoring duplicated connection request from pending contact ' . $url);
+ return;
+ }
+
+ // create contact record
+ q("INSERT INTO `contact` (`uid`, `created`, `url`, `nurl`, `name`, `nick`, `photo`, `network`, `rel`,
+ `blocked`, `readonly`, `pending`, `writable`)
+ VALUES (%d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, 0, 0, 1, 1)",
+ intval($importer['uid']),
+ dbesc(DateTimeFormat::utcNow()),
+ dbesc($url),
+ dbesc(normalise_link($url)),
+ dbesc($name),
+ dbesc($nick),
+ dbesc($photo),
+ dbesc(NETWORK_OSTATUS),
+ intval(CONTACT_IS_FOLLOWER)
+ );
+
+ $contact_record = [
+ 'id' => dba::lastInsertId(),
+ 'network' => NETWORK_OSTATUS
+ ];
+ Contact::updateAvatar($photo, $importer["uid"], $contact_record["id"], true);
+
+ /// @TODO Encapsulate this into a function/method
+ $fields = ['uid', 'username', 'email', 'page-flags', 'notify-flags', 'language'];
+ $user = dba::selectFirst('user', $fields, ['uid' => $importer['uid']]);
+ if (DBM::is_result($user) && !in_array($user['page-flags'], [PAGE_SOAPBOX, PAGE_FREELOVE, PAGE_COMMUNITY])) {
+ // create notification
+ $hash = random_string();
+
+ if (is_array($contact_record)) {
+ dba::insert('intro', ['uid' => $importer['uid'], 'contact-id' => $contact_record['id'],
+ 'blocked' => false, 'knowyou' => false,
+ 'hash' => $hash, 'datetime' => DateTimeFormat::utcNow()]);
+ }
+
+ Group::addMember(User::getDefaultGroup($importer['uid'], $contact_record["network"]), $contact_record['id']);
+
+ if (($user['notify-flags'] & NOTIFY_INTRO) &&
+ in_array($user['page-flags'], [PAGE_NORMAL])) {
+
+ notification([
+ 'type' => NOTIFY_INTRO,
+ 'notify_flags' => $user['notify-flags'],
+ 'language' => $user['language'],
+ 'to_name' => $user['username'],
+ 'to_email' => $user['email'],
+ 'uid' => $user['uid'],
+ 'link' => System::baseUrl() . '/notifications/intro',
+ 'source_name' => ((strlen(stripslashes($contact_record['name']))) ? stripslashes($contact_record['name']) : L10n::t('[Name Withheld]')),
+ 'source_link' => $contact_record['url'],
+ 'source_photo' => $contact_record['photo'],
+ 'verb' => ($sharing ? ACTIVITY_FRIEND : ACTIVITY_FOLLOW),
+ 'otype' => 'intro'
+ ]);
+
+ }
+ } elseif (DBM::is_result($user) && in_array($user['page-flags'], [PAGE_SOAPBOX, PAGE_FREELOVE, PAGE_COMMUNITY])) {
+ q("UPDATE `contact` SET `pending` = 0 WHERE `uid` = %d AND `url` = '%s' AND `pending` LIMIT 1",
+ intval($importer['uid']),
+ dbesc($url)
+ );
+ }
+ }
+ }
+
+ public static function removeFollower($importer, $contact, array $datarray = [], $item = "") {
+
+ if (($contact['rel'] == CONTACT_IS_FRIEND) || ($contact['rel'] == CONTACT_IS_SHARING)) {
+ dba::update('contact', ['rel' => CONTACT_IS_SHARING], ['id' => $contact['id']]);
+ } else {
+ Contact::remove($contact['id']);
+ }
+ }
+
+ public static function removeSharer($importer, $contact, array $datarray = [], $item = "") {
+
+ if (($contact['rel'] == CONTACT_IS_FRIEND) || ($contact['rel'] == CONTACT_IS_FOLLOWER)) {
+ dba::update('contact', ['rel' => CONTACT_IS_FOLLOWER], ['id' => $contact['id']]);
+ } else {
+ Contact::remove($contact['id']);
+ }
+ }
+
+ /**
+ * @brief Create a birthday event.
+ *
+ * Update the year and the birthday.
+ */
+ public static function updateBirthdays()
+ {
+ // This only handles foreign or alien networks where a birthday has been provided.
+ // In-network birthdays are handled within local_delivery
+
+ $r = q("SELECT * FROM `contact` WHERE `bd` != '' AND `bd` > '0001-01-01' AND SUBSTRING(`bd`, 1, 4) != `bdyear` ");
+ if (DBM::is_result($r)) {
+ foreach ($r as $rr) {
+ logger('update_contact_birthday: ' . $rr['bd']);
+
+ $nextbd = DateTimeFormat::utcNow('Y') . substr($rr['bd'], 4);
+
+ /*
+ * Add new birthday event for this person
+ *
+ * $bdtext is just a readable placeholder in case the event is shared
+ * with others. We will replace it during presentation to our $importer
+ * to contain a sparkle link and perhaps a photo.
+ */
+
+ // Check for duplicates
+ $s = q("SELECT `id` FROM `event` WHERE `uid` = %d AND `cid` = %d AND `start` = '%s' AND `type` = '%s' LIMIT 1",
+ intval($rr['uid']), intval($rr['id']), dbesc(DateTimeFormat::utc($nextbd)), dbesc('birthday'));
+
+ if (DBM::is_result($s)) {
+ continue;
+ }
+
+ $bdtext = L10n::t('%s\'s birthday', $rr['name']);
+ $bdtext2 = L10n::t('Happy Birthday %s', ' [url=' . $rr['url'] . ']' . $rr['name'] . '[/url]');
+
+ q("INSERT INTO `event` (`uid`,`cid`,`created`,`edited`,`start`,`finish`,`summary`,`desc`,`type`,`adjust`)
+ VALUES ( %d, %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%d' ) ", intval($rr['uid']), intval($rr['id']),
+ dbesc(DateTimeFormat::utcNow()), dbesc(DateTimeFormat::utcNow()), dbesc(DateTimeFormat::utc($nextbd)),
+ dbesc(DateTimeFormat::utc($nextbd . ' + 1 day ')), dbesc($bdtext), dbesc($bdtext2), dbesc('birthday'),
+ intval(0)
+ );
+
+
+ // update bdyear
+ q("UPDATE `contact` SET `bdyear` = '%s', `bd` = '%s' WHERE `uid` = %d AND `id` = %d", dbesc(substr($nextbd, 0, 4)),
+ dbesc($nextbd), intval($rr['uid']), intval($rr['id'])
+ );
+ }
+ }
+ }
+
+ /**
+ * Remove the unavailable contact ids from the provided list
+ *
+ * @param array $contact_ids Contact id list
+ */
+ public static function pruneUnavailable(array &$contact_ids)
+ {
+ if (empty($contact_ids)) {
+ return;
+ }
+
+ $str = dbesc(implode(',', $contact_ids));
+
+ $stmt = dba::p("SELECT `id` FROM `contact` WHERE `id` IN ( " . $str . ") AND `blocked` = 0 AND `pending` = 0 AND `archive` = 0");
+
+ $return = [];
+ while($contact = dba::fetch($stmt)) {
+ $return[] = $contact['id'];
+ }
+
+ dba::close($stmt);
+
+ $contact_ids = $return;
+ }