]> git.mxchange.org Git - friendica.git/blobdiff - src/Model/Contact.php
Rename namespace
[friendica.git] / src / Model / Contact.php
index 6f0de5acd960e2c9bcf9db98f3f84c2eebb32e0b..08cad6cd84eb6ec6deabf402119d0acac096438c 100644 (file)
@@ -12,16 +12,17 @@ use Friendica\Core\Hook;
 use Friendica\Core\L10n;
 use Friendica\Core\Logger;
 use Friendica\Core\Protocol;
+use Friendica\Core\Session;
 use Friendica\Core\System;
 use Friendica\Core\Worker;
 use Friendica\Database\DBA;
 use Friendica\Network\Probe;
 use Friendica\Object\Image;
+use Friendica\Protocol\Activity;
 use Friendica\Protocol\ActivityPub;
 use Friendica\Protocol\DFRN;
 use Friendica\Protocol\Diaspora;
 use Friendica\Protocol\OStatus;
-use Friendica\Protocol\PortableContact;
 use Friendica\Protocol\Salmon;
 use Friendica\Util\DateTimeFormat;
 use Friendica\Util\Network;
@@ -270,14 +271,17 @@ class Contact extends BaseObject
         * @param string $url The contact link
         *
         * @return string basepath
+        * @return boolean $dont_update Don't update the contact
         * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         * @throws \ImagickException
         */
-       public static function getBasepath($url)
+       public static function getBasepath($url, $dont_update = false)
        {
                $contact = DBA::selectFirst('contact', ['baseurl'], ['uid' => 0, 'nurl' => Strings::normaliseLink($url)]);
                if (!empty($contact['baseurl'])) {
                        return $contact['baseurl'];
+               } elseif ($dont_update) {
+                       return '';
                }
 
                self::updateFromProbeByURL($url, true);
@@ -290,6 +294,18 @@ class Contact extends BaseObject
                return '';
        }
 
+       /**
+        * Check if the given contact url is on the same server
+        *
+        * @param string $url The contact link
+        *
+        * @return boolean Is it the same server?
+        */
+       public static function isLocal($url)
+       {
+               return Strings::compareLink(self::getBasepath($url, true), System::baseUrl());
+       }
+
        /**
         * Returns the public contact id of the given user id
         *
@@ -812,7 +828,7 @@ class Contact extends BaseObject
                } elseif (in_array($protocol, [Protocol::OSTATUS, Protocol::DFRN])) {
                        // create an unfollow slap
                        $item = [];
-                       $item['verb'] = NAMESPACE_OSTATUS . "/unfollow";
+                       $item['verb'] = Activity\ANamespace::OSTATUS . "/unfollow";
                        $item['follow'] = $contact["url"];
                        $item['body'] = '';
                        $item['title'] = '';
@@ -1062,14 +1078,14 @@ class Contact extends BaseObject
                        $profile["micro"] = $profile["thumb"];
                }
 
-               if ((empty($profile["addr"]) || empty($profile["name"])) && (defaults($profile, "gid", 0) != 0)
+               if ((empty($profile["addr"]) || empty($profile["name"])) && !empty($profile["gid"])
                        && in_array($profile["network"], Protocol::FEDERATED)
                ) {
                        Worker::add(PRIORITY_LOW, "UpdateGContact", $url);
                }
 
                // Show contact details of Diaspora contacts only if connected
-               if ((defaults($profile, "cid", 0) == 0) && (defaults($profile, "network", "") == Protocol::DIASPORA)) {
+               if (empty($profile["cid"]) && ($profile["network"] ?? "") == Protocol::DIASPORA) {
                        $profile["location"] = "";
                        $profile["about"] = "";
                        $profile["gender"] = "";
@@ -1178,7 +1194,7 @@ class Contact extends BaseObject
                $sparkle = false;
                if (($contact['network'] === Protocol::DFRN) && !$contact['self'] && empty($contact['pending'])) {
                        $sparkle = true;
-                       $profile_link = System::baseUrl() . '/redir/' . $contact['id'] . '?url=' . $contact['url'];
+                       $profile_link = System::baseUrl() . '/redir/' . $contact['id'];
                } else {
                        $profile_link = $contact['url'];
                }
@@ -1198,7 +1214,7 @@ class Contact extends BaseObject
                }
 
                if (($contact['network'] == Protocol::DFRN) && !$contact['self'] && empty($contact['pending'])) {
-                       $poke_link = System::baseUrl() . '/poke/?f=&c=' . $contact['id'];
+                       $poke_link = System::baseUrl() . '/poke/?c=' . $contact['id'];
                }
 
                $contact_url = System::baseUrl() . '/contact/' . $contact['id'];
@@ -1465,7 +1481,7 @@ class Contact extends BaseObject
                        $data = Probe::uri($url, "", $uid);
                        // Ensure that there is a gserver entry
                        if (!empty($data['baseurl']) && ($data['network'] != Protocol::PHANTOM)) {
-                               PortableContact::checkServer($data['baseurl']);
+                               GServer::check($data['baseurl']);
                        }
                }
 
@@ -1488,25 +1504,25 @@ class Contact extends BaseObject
                                'created'   => DateTimeFormat::utcNow(),
                                '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', ''),
+                               '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'    => defaults($data, 'pubkey', ''),
+                               'pubkey'    => $data['pubkey'] ?? '',
                                'rel'       => self::SHARING,
-                               'priority'  => defaults($data, 'priority', 0),
-                               'batch'     => defaults($data, 'batch', ''),
-                               'request'   => defaults($data, 'request', ''),
-                               'confirm'   => defaults($data, 'confirm', ''),
-                               'poco'      => defaults($data, 'poco', ''),
-                               'baseurl'   => defaults($data, 'baseurl', ''),
+                               'priority'  => $data['priority'] ?? 0,
+                               'batch'     => $data['batch'] ?? '',
+                               'request'   => $data['request'] ?? '',
+                               'confirm'   => $data['confirm'] ?? '',
+                               'poco'      => $data['poco'] ?? '',
+                               'baseurl'   => $data['baseurl'] ?? '',
                                'name-date' => DateTimeFormat::utcNow(),
                                'uri-date'  => DateTimeFormat::utcNow(),
                                'avatar-date' => DateTimeFormat::utcNow(),
@@ -1573,7 +1589,7 @@ class Contact extends BaseObject
                        $fields = ['addr', 'alias', 'name', 'nick', 'keywords', 'location', 'about', 'baseurl'];
 
                        foreach ($fields as $field) {
-                               $updated[$field] = defaults($data, $field, $contact[$field]);
+                               $updated[$field] = ($data[$field] ?? '') ?: $contact[$field];
                        }
 
                        if (($updated['addr'] != $contact['addr']) || (!empty($data['alias']) && ($data['alias'] != $contact['alias']))) {
@@ -2350,7 +2366,7 @@ class Contact extends BaseObject
                        if (in_array($protocol, [Protocol::OSTATUS, Protocol::DFRN])) {
                                // create a follow slap
                                $item = [];
-                               $item['verb'] = ACTIVITY_FOLLOW;
+                               $item['verb'] = Activity::FOLLOW;
                                $item['follow'] = $contact["url"];
                                $item['body'] = '';
                                $item['title'] = '';
@@ -2453,9 +2469,9 @@ class Contact extends BaseObject
                        return false;
                }
 
-               $url = defaults($datarray, 'author-link', $pub_contact['url']);
+               $url = ($datarray['author-link'] ?? '') ?: $pub_contact['url'];
                $name = $pub_contact['name'];
-               $photo = defaults($pub_contact, 'avatar', $pub_contact["photo"]);
+               $photo = ($pub_contact['avatar'] ?? '') ?: $pub_contact["photo"];
                $nick = $pub_contact['nick'];
                $network = $pub_contact['network'];
 
@@ -2486,6 +2502,9 @@ class Contact extends BaseObject
                                                ['id' => $contact['id'], 'uid' => $importer['uid']]);
                        }
 
+                       // Ensure to always have the correct network type, independent from the connection request method
+                       self::updateFromProbe($contact['id'], '', true);
+
                        return true;
                } else {
                        // send email notification to owner?
@@ -2511,15 +2530,14 @@ class Contact extends BaseObject
                                'writable' => 1,
                        ]);
 
-                       $contact_record = [
-                               'id' => DBA::lastInsertId(),
-                               'network' => $network,
-                               'name' => $name,
-                               'url' => $url,
-                               'photo' => $photo
-                       ];
+                       $contact_id = DBA::lastInsertId();
+
+                       // Ensure to always have the correct network type, independent from the connection request method
+                       self::updateFromProbe($contact_id, '', true);
+
+                       Contact::updateAvatar($photo, $importer["uid"], $contact_id, true);
 
-                       Contact::updateAvatar($photo, $importer["uid"], $contact_record["id"], true);
+                       $contact_record = DBA::selectFirst('contact', ['id', 'network', 'name', 'url', 'photo'], ['id' => $contact_id]);
 
                        /// @TODO Encapsulate this into a function/method
                        $fields = ['uid', 'username', 'email', 'page-flags', 'notify-flags', 'language'];
@@ -2550,7 +2568,7 @@ class Contact extends BaseObject
                                                '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),
+                                               'verb'         => ($sharing ? Activity::FRIEND : Activity::FOLLOW),
                                                'otype'        => 'intro'
                                        ]);
                                }
@@ -2662,7 +2680,7 @@ class Contact extends BaseObject
         */
        public static function magicLink($contact_url, $url = '')
        {
-               if (!local_user() && !remote_user()) {
+               if (!Session::isAuthenticated()) {
                        return $url ?: $contact_url; // Equivalent to: ($url != '') ? $url : $contact_url;
                }
 
@@ -2708,7 +2726,7 @@ class Contact extends BaseObject
        {
                $destination = $url ?: $contact['url']; // Equivalent to ($url != '') ? $url : $contact['url'];
 
-               if ((!local_user() && !remote_user()) || ($contact['network'] != Protocol::DFRN)) {
+               if (!Session::isAuthenticated() || ($contact['network'] != Protocol::DFRN)) {
                        return $destination;
                }
 
@@ -2727,7 +2745,7 @@ class Contact extends BaseObject
 
                $redirect = 'redir/' . $contact['id'];
 
-               if ($url != '') {
+               if (($url != '') && !Strings::compareLink($contact['url'], $url)) {
                        $redirect .= '?url=' . $url;
                }