]> git.mxchange.org Git - friendica.git/blobdiff - src/Model/Contact.php
Update use statement lists with new Friendica\Database\dba class
[friendica.git] / src / Model / Contact.php
index e504849f7c11fbf5dbcfd337cd1af685ad13922b..13283009378787576cc28e50b919e1905efcb873 100644 (file)
@@ -11,19 +11,16 @@ use Friendica\Core\L10n;
 use Friendica\Core\PConfig;
 use Friendica\Core\System;
 use Friendica\Core\Worker;
+use Friendica\Database\dba;
 use Friendica\Database\DBM;
-use Friendica\Model\Photo;
-use Friendica\Model\Profile;
 use Friendica\Network\Probe;
-use Friendica\Protocol\DFRN;
+use Friendica\Object\Image;
 use Friendica\Protocol\Diaspora;
 use Friendica\Protocol\OStatus;
 use Friendica\Protocol\PortableContact;
 use Friendica\Protocol\Salmon;
 use Friendica\Util\DateTimeFormat;
 use Friendica\Util\Network;
-use Friendica\Object\Image;
-use dba;
 
 require_once 'boot.php';
 require_once 'include/dba.php';
@@ -475,34 +472,34 @@ class Contact extends BaseObject
                        $profile = $default;
                }
 
-               if (($profile["photo"] == "") && isset($default["photo"])) {
+               if (empty($profile["photo"]) && isset($default["photo"])) {
                        $profile["photo"] = $default["photo"];
                }
 
-               if (($profile["name"] == "") && isset($default["name"])) {
+               if (empty($profile["name"]) && isset($default["name"])) {
                        $profile["name"] = $default["name"];
                }
 
-               if (($profile["network"] == "") && isset($default["network"])) {
+               if (empty($profile["network"]) && isset($default["network"])) {
                        $profile["network"] = $default["network"];
                }
 
-               if (($profile["thumb"] == "") && isset($profile["photo"])) {
+               if (empty($profile["thumb"]) && isset($profile["photo"])) {
                        $profile["thumb"] = $profile["photo"];
                }
 
-               if (($profile["micro"] == "") && isset($profile["thumb"])) {
+               if (empty($profile["micro"]) && isset($profile["thumb"])) {
                        $profile["micro"] = $profile["thumb"];
                }
 
-               if ((($profile["addr"] == "") || ($profile["name"] == "")) && ($profile["gid"] != 0)
+               if ((empty($profile["addr"]) || empty($profile["name"])) && (defaults($profile, "gid", 0) != 0)
                        && in_array($profile["network"], [NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_OSTATUS])
                ) {
                        Worker::add(PRIORITY_LOW, "UpdateGContact", $profile["gid"]);
                }
 
                // Show contact details of Diaspora contacts only if connected
-               if (($profile["cid"] == 0) && ($profile["network"] == NETWORK_DIASPORA)) {
+               if ((defaults($profile, "cid", 0) == 0) && (defaults($profile, "network", "") == NETWORK_DIASPORA)) {
                        $profile["location"] = "";
                        $profile["about"] = "";
                        $profile["gender"] = "";
@@ -596,7 +593,7 @@ class Contact extends BaseObject
                        $uid = local_user();
                }
 
-               if ($contact['uid'] != $uid) {
+               if (empty($contact['uid']) || ($contact['uid'] != $uid)) {
                        if ($uid == 0) {
                                $profile_link = self::magicLink($contact['url']);
                                $menu = ['profile' => [L10n::t('View Profile'), $profile_link, true]];
@@ -696,49 +693,20 @@ class Contact extends BaseObject
         *
         * @return array
         */
-       public static function getUngroupedList($uid, $start = 0, $count = 0)
+       public static function getUngroupedList($uid)
        {
-               if (!$count) {
-                       $r = q(
-                               "SELECT COUNT(*) AS `total`
-                                FROM `contact`
-                                WHERE `uid` = %d
-                                AND NOT `self`
-                                AND NOT `blocked`
-                                AND NOT `pending`
-                                AND `id` NOT IN (
-                                       SELECT DISTINCT(`contact-id`)
-                                       FROM `group_member`
-                                       WHERE `uid` = %d
-                               )",
-                               intval($uid),
-                               intval($uid)
-                       );
-
-                       return $r;
-               }
-
-               $r = q(
-                       "SELECT *
-                       FROM `contact`
-                       WHERE `uid` = %d
-                       AND NOT `self`
-                       AND NOT `blocked`
-                       AND NOT `pending`
-                       AND `id` NOT IN (
-                               SELECT DISTINCT(`contact-id`)
-                               FROM `group_member`
-                               INNER JOIN `group` ON `group`.`id` = `group_member`.`gid`
-                               WHERE `group`.`uid` = %d
-                       )
-                       LIMIT %d, %d",
-                       intval($uid),
-                       intval($uid),
-                       intval($start),
-                       intval($count)
-               );
-
-               return $r;
+               return q("SELECT *
+                          FROM `contact`
+                          WHERE `uid` = %d
+                          AND NOT `self`
+                          AND NOT `blocked`
+                          AND NOT `pending`
+                          AND `id` NOT IN (
+                               SELECT DISTINCT(`contact-id`)
+                               FROM `group_member`
+                               INNER JOIN `group` ON `group`.`id` = `group_member`.`gid`
+                               WHERE `group`.`uid` = %d
+                          )", intval($uid), intval($uid));
        }
 
        /**
@@ -1064,15 +1032,17 @@ class Contact extends BaseObject
 
                $contact = ($r[0]["contact-type"] == ACCOUNT_TYPE_COMMUNITY ? 'owner-id' : 'author-id');
 
-               $condition = ["`$contact` = ? AND `verb` = ? AND " . $sql,
-                       $author_id, ACTIVITY_POST, local_user()];
+               $condition = ["`$contact` = ? AND `gravity` IN (?, ?) AND " . $sql,
+                       $author_id, GRAVITY_PARENT, GRAVITY_COMMENT, local_user()];
                $params = ['order' => ['created' => true],
                        'limit' => [$a->pager['start'], $a->pager['itemspage']]];
-               $r = Item::select(local_user(), [], $condition, $params);
+               $r = Item::selectForUser(local_user(), [], $condition, $params);
 
-               $o = conversation($a, dba::inArray($r), 'contact-posts', false);
+               $items = Item::inArray($r);
 
-               $o .= alt_pager($a, count($r));
+               $o = conversation($a, $items, 'contact-posts', false);
+
+               $o .= alt_pager($a, count($items));
 
                return $o;
        }
@@ -1343,7 +1313,7 @@ class Contact extends BaseObject
 
                if (($ret['network'] === NETWORK_DFRN) && !DBM::is_result($r)) {
                        if ($interactive) {
-                               if (strlen($a->path)) {
+                               if (strlen($a->urlpath)) {
                                        $myaddr = bin2hex(System::baseUrl() . '/profile/' . $a->user['nickname']);
                                } else {
                                        $myaddr = bin2hex($a->user['nickname'] . '@' . $a->get_hostname());
@@ -1560,8 +1530,12 @@ class Contact extends BaseObject
 
                        $contact_record = [
                                'id' => dba::lastInsertId(),
-                               'network' => NETWORK_OSTATUS
+                               'network' => NETWORK_OSTATUS,
+                               'name' => $name,
+                               'url' => $url,
+                               'photo' => $photo
                        ];
+
                        Contact::updateAvatar($photo, $importer["uid"], $contact_record["id"], true);
 
                        /// @TODO Encapsulate this into a function/method
@@ -1730,8 +1704,21 @@ class Contact extends BaseObject
         */
        public static function magicLinkbyId($cid, $url = '')
        {
-               $contact = dba::selectFirst('contact', ['network', 'url', 'uid'], ['id' => $cid]);
+               $contact = dba::selectFirst('contact', ['id', 'network', 'url', 'uid'], ['id' => $cid]);
 
+               return self::magicLinkbyContact($contact, $url);
+        }
+
+       /**
+        * @brief Returns a magic link to authenticate remote visitors
+        *
+        * @param array $contact The contact array with "uid", "network" and "url"
+        * @param integer $url An url that we will be redirected to after the authentication
+        *
+        * @return string with "redir" link
+        */
+       public static function magicLinkbyContact($contact, $url = '')
+       {
                if ($contact['network'] != NETWORK_DFRN) {
                        return $url ?: $contact['url']; // Equivalent to ($url != '') ? $url : $contact['url'];
                }
@@ -1745,7 +1732,7 @@ class Contact extends BaseObject
                        return self::magicLink($contact['url'], $url);
                }
 
-               $redirect = 'redir/' . $cid;
+               $redirect = 'redir/' . $contact['id'];
 
                if ($url != '') {
                        $redirect .= '?url=' . $url;