]> git.mxchange.org Git - friendica.git/blobdiff - src/Object/Contact.php
Move Object\Profile to Model\Profile
[friendica.git] / src / Object / Contact.php
index 23c3649df70b44c8e891888ae8d7214d9284dde1..f72ec025a4dbda0bcbba72a56acb8068ccb6569c 100644 (file)
@@ -28,6 +28,52 @@ require_once 'include/text.php';
  */
 class Contact extends BaseObject
 {
+       /**
+        * Creates the self-contact for the provided user id
+        *
+        * @param int $uid
+        * @return bool Operation success
+        */
+       public static function createSelfFromUserId($uid)
+       {
+               // Only create the entry if it doesn't exist yet
+               if (dba::exists('contact', ['uid' => intval($uid), 'self'])) {
+                       return true;
+               }
+
+               $user = dba::select('user', ['uid', 'username', 'nickname'], ['uid' => intval($uid)], ['limit' => 1]);
+               if (!DBM::is_result($user)) {
+                       return false;
+               }
+
+               $return = dba::insert('contact', [
+                       'uid'         => $user['uid'],
+                       'created'     => datetime_convert(),
+                       'self'        => 1,
+                       'name'        => $user['username'],
+                       'nick'        => $user['nickname'],
+                       'photo'       => System::baseUrl() . '/photo/profile/' . $user['uid'] . '.jpg',
+                       'thumb'       => System::baseUrl() . '/photo/avatar/'  . $user['uid'] . '.jpg',
+                       'micro'       => System::baseUrl() . '/photo/micro/'   . $user['uid'] . '.jpg',
+                       'blocked'     => 0,
+                       'pending'     => 0,
+                       'url'         => System::baseUrl() . '/profile/' . $user['nickname'],
+                       'nurl'        => normalise_link(System::baseUrl() . '/profile/' . $user['nickname']),
+                       'addr'        => $user['nickname'] . '@' . substr(System::baseUrl(), strpos(System::baseUrl(), '://') + 3),
+                       'request'     => System::baseUrl() . '/dfrn_request/' . $user['nickname'],
+                       'notify'      => System::baseUrl() . '/dfrn_notify/'  . $user['nickname'],
+                       'poll'        => System::baseUrl() . '/dfrn_poll/'    . $user['nickname'],
+                       'confirm'     => System::baseUrl() . '/dfrn_confirm/' . $user['nickname'],
+                       'poco'        => System::baseUrl() . '/poco/'         . $user['nickname'],
+                       'name-date'   => datetime_convert(),
+                       'uri-date'    => datetime_convert(),
+                       'avatar-date' => datetime_convert(),
+                       'closeness'   => 0
+               ]);
+
+               return $return;
+       }
+
        /**
         * @brief Marks a contact for removal
         *
@@ -95,8 +141,8 @@ class Contact extends BaseObject
         */
        public static function markForArchival(array $contact)
        {
-               // Contact already archived, nothing to do
-               if ($contact['archive']) {
+               // Contact already archived or "self" contact? => nothing to do
+               if ($contact['archive'] || $contact['self']) {
                        return;
                }
 
@@ -104,7 +150,7 @@ class Contact extends BaseObject
                        dba::update('contact', array('term-date' => datetime_convert()), array('id' => $contact['id']));
 
                        if ($contact['url'] != '') {
-                               dba::update('contact', array('term-date' => datetime_convert()), array('`nurl` = ? AND `term-date` <= ?', normalise_link($contact['url']), NULL_DATE));
+                               dba::update('contact', array('term-date' => datetime_convert()), array('`nurl` = ? AND `term-date` <= ? AND NOT `self`', normalise_link($contact['url']), NULL_DATE));
                        }
                } else {
                        /* @todo
@@ -123,7 +169,7 @@ class Contact extends BaseObject
                                dba::update('contact', array('archive' => 1), array('id' => $contact['id']));
 
                                if ($contact['url'] != '') {
-                                       dba::update('contact', array('archive' => 1), array('nurl' => normalise_link($contact['url'])));
+                                       dba::update('contact', array('archive' => 1), array('nurl' => normalise_link($contact['url']), 'self' => false));
                                }
                        }
                }
@@ -139,7 +185,7 @@ class Contact extends BaseObject
         */
        public static function unmarkForArchival(array $contact)
        {
-               $condition = array('`id` = ? AND (`term-date` > ? OR `archive`)', $contact[`id`], NULL_DATE);
+               $condition = array('`id` = ? AND (`term-date` > ? OR `archive`)', $contact['id'], NULL_DATE);
                $exists = dba::exists('contact', $condition);
 
                // We don't need to update, we never marked this contact for archival
@@ -648,7 +694,8 @@ class Contact extends BaseObject
 
                self::updateAvatar($data["photo"], $uid, $contact_id);
 
-               $contact = dba::select('contact', array('url', 'nurl', 'addr', 'alias', 'name', 'nick', 'keywords', 'location', 'about', 'avatar-date'), array('id' => $contact_id), array('limit' => 1));
+               $fields = array('url', 'nurl', 'addr', 'alias', 'name', 'nick', 'keywords', 'location', 'about', 'avatar-date', 'pubkey');
+               $contact = dba::select('contact', $fields, array('id' => $contact_id), array('limit' => 1));
 
                // This condition should always be true
                if (!DBM::is_result($contact)) {
@@ -662,6 +709,13 @@ class Contact extends BaseObject
                        'name' => $data['name'],
                        'nick' => $data['nick']);
 
+               // Only fill the pubkey if it was empty before. We have to prevent identity theft.
+               if (!empty($contact['pubkey'])) {
+                       unset($contact['pubkey']);
+               } else {
+                       $updated['pubkey'] = $data['pubkey'];
+               }
+
                if ($data['keywords'] != '') {
                        $updated['keywords'] = $data['keywords'];
                }
@@ -822,7 +876,33 @@ class Contact extends BaseObject
        }
 
        /**
-        * @brief Updates the avatar links in a contact only if needed
+        * @brief Blocks a contact
+        *
+        * @param int $uid
+        * @return bool
+        */
+       public static function block($uid)
+       {
+               $return = dba::update('contact', ['blocked' => true], ['id' => $uid]);
+
+               return $return;
+       }
+
+       /**
+        * @brief Unblocks a contact
+        *
+        * @param int $uid
+        * @return bool
+        */
+       public static function unblock($uid)
+       {
+               $return = dba::update('contact', ['blocked' => false], ['id' => $uid]);
+
+               return $return;
+  }
+
+  /**
+   * @brief Updates the avatar links in a contact only if needed
         *
         * @param string $avatar Link to avatar picture
         * @param int    $uid    User id of contact owner