]> git.mxchange.org Git - friendica.git/blobdiff - src/Model/Contact.php
Merge pull request #4376 from MrPetovan/task/3878-move-include-like-to-src
[friendica.git] / src / Model / Contact.php
index acdb4815e6b2374c4dcd64a331a632c46fcfe661..3ec9f5ab48929b88cb77c5bcdf2dbf1274873cb9 100644 (file)
@@ -20,6 +20,7 @@ use Friendica\Protocol\DFRN;
 use Friendica\Protocol\OStatus;
 use Friendica\Protocol\PortableContact;
 use Friendica\Protocol\Salmon;
+use Friendica\Util\Network;
 use dba;
 
 require_once 'boot.php';
@@ -1131,12 +1132,12 @@ class Contact extends BaseObject
                // remove ajax junk, e.g. Twitter
                $url = str_replace('/#!/', '/', $url);
 
-               if (!allowed_url($url)) {
+               if (!Network::isUrlAllowed($url)) {
                        $result['message'] = L10n::t('Disallowed profile URL.');
                        return $result;
                }
 
-               if (blocked_url($url)) {
+               if (Network::isUrlBlocked($url)) {
                        $result['message'] = L10n::t('Blocked domain');
                        return $result;
                }
@@ -1150,6 +1151,11 @@ class Contact extends BaseObject
 
                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 {
@@ -1320,4 +1326,146 @@ class Contact extends BaseObject
                $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']));
+
+               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['network'] == NETWORK_OSTATUS && $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 {
+                       // 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(datetime_convert()),
+                               dbesc($url),
+                               dbesc(normalise_link($url)),
+                               dbesc($name),
+                               dbesc($nick),
+                               dbesc($photo),
+                               dbesc(NETWORK_OSTATUS),
+                               intval(CONTACT_IS_FOLLOWER)
+                       );
+
+                       $r = q("SELECT `id`, `network` FROM `contact` WHERE `uid` = %d AND `url` = '%s' AND `pending` = 1 LIMIT 1",
+                                       intval($importer['uid']),
+                                       dbesc($url)
+                       );
+                       if (DBM::is_result($r)) {
+                               $contact_record = $r[0];
+                               Contact::updateAvatar($photo, $importer["uid"], $contact_record["id"], true);
+                       }
+
+                       /// @TODO Encapsulate this into a function/method
+                       $r = q("SELECT * FROM `user` WHERE `uid` = %d LIMIT 1",
+                               intval($importer['uid'])
+                       );
+                       if (DBM::is_result($r) && !in_array($r[0]['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' => datetime_convert()]);
+                               }
+
+                               Group::addMember(User::getDefaultGroup($importer['uid'], $contact_record["network"]), $contact_record['id']);
+
+                               if (($r[0]['notify-flags'] & NOTIFY_INTRO) &&
+                                       in_array($r[0]['page-flags'], [PAGE_NORMAL])) {
+
+                                       notification([
+                                               'type'         => NOTIFY_INTRO,
+                                               'notify_flags' => $r[0]['notify-flags'],
+                                               'language'     => $r[0]['language'],
+                                               'to_name'      => $r[0]['username'],
+                                               'to_email'     => $r[0]['email'],
+                                               'uid'          => $r[0]['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($r) && in_array($r[0]['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']);
+               }
+       }
 }