+
+ 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;
+ }