]> git.mxchange.org Git - friendica.git/blobdiff - src/Protocol/Diaspora.php
Better english
[friendica.git] / src / Protocol / Diaspora.php
index 89fbb6e5f32f8f7d7034f50adffdb1585064f5d8..a0d99959bb46ebd88fee456828f471ad926c7458 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /**
- * @file include/diaspora.php
+ * @file src/Protocol/diaspora.php
  * @brief The implementation of the diaspora protocol
  *
  * The new protocol is described here: http://diaspora.github.io/diaspora_federation/index.html
@@ -17,17 +17,19 @@ use Friendica\Core\PConfig;
 use Friendica\Core\Worker;
 use Friendica\Database\DBM;
 use Friendica\Model\Contact;
-use Friendica\Model\GlobalContact;
+use Friendica\Model\GContact;
+use Friendica\Model\Group;
 use Friendica\Model\Profile;
+use Friendica\Model\User;
 use Friendica\Network\Probe;
 use Friendica\Util\XML;
 
 use dba;
 use SimpleXMLElement;
 
+require_once 'include/dba.php';
 require_once 'include/items.php';
 require_once 'include/bb2diaspora.php';
-require_once 'include/group.php';
 require_once 'include/datetime.php';
 require_once 'include/queue_fn.php';
 
@@ -37,7 +39,6 @@ require_once 'include/queue_fn.php';
  */
 class Diaspora
 {
-
        /**
         * @brief Return a list of relay servers
         *
@@ -664,7 +665,6 @@ class Diaspora
                        } elseif (!in_array($fieldname, array("author_signature", "parent_author_signature", "target_author_signature"))) {
                                if ($signed_data != "") {
                                        $signed_data .= ";";
-                                       $signed_data_parent .= ";";
                                }
 
                                $signed_data .= $entry;
@@ -927,10 +927,12 @@ class Diaspora
        /**
         * @brief Get a contact id for a given handle
         *
+        * @todo Move to Friendica\Model\Contact
+        *
         * @param int    $uid    The user id
         * @param string $handle The handle in the format user@domain.tld
         *
-        * @return The contact id
+        * @return int Contact id
         */
        private static function contactByHandle($uid, $handle)
        {
@@ -1091,7 +1093,7 @@ class Diaspora
                preg_replace_callback(
                        $expression,
                        function ($match) use ($item) {
-                               return self::fetchGuidSub($match, $item);
+                               self::fetchGuidSub($match, $item);
                        },
                        $item["body"]
                );
@@ -1099,7 +1101,7 @@ class Diaspora
                preg_replace_callback(
                        "&\[url=/posts/([^\[\]]*)\](.*)\[\/url\]&Usi",
                        function ($match) use ($item) {
-                               return self::fetchGuidSub($match, $item);
+                               self::fetchGuidSub($match, $item);
                        },
                        $item["body"]
                );
@@ -1112,7 +1114,7 @@ class Diaspora
         * @param string $body        The item body to replace links from
         * @param string $author_link The author link for missing local contact fallback
         *
-        * @return the replaced string
+        * @return string the replaced string
         */
        public static function replacePeopleGuid($body, $author_link)
        {
@@ -2247,9 +2249,9 @@ class Diaspora
                                        "addr" => $author, "nick" => $nick, "keywords" => $keywords,
                                        "hide" => !$searchable, "nsfw" => $nsfw);
 
-               $gcid = GlobalContact::update($gcontact);
+               $gcid = GContact::update($gcontact);
 
-               GlobalContact::link($gcid, $importer["uid"], $contact["id"]);
+               GContact::link($gcid, $importer["uid"], $contact["id"]);
 
                logger("Profile of contact ".$contact["id"]." stored for user ".$importer["uid"], LOGGER_DEBUG);
 
@@ -2308,16 +2310,18 @@ class Diaspora
                                $A = "[url=".$self[0]["url"]."]".$self[0]["name"]."[/url]";
                                $B = "[url=".$contact["url"]."]".$contact["name"]."[/url]";
                                $BPhoto = "[url=".$contact["url"]."][img]".$contact["thumb"]."[/img][/url]";
-                               $arr["body"] = sprintf(t("%1$s is now friends with %2$s"), $A, $B)."\n\n\n".$Bphoto;
+                               $arr["body"] = sprintf(t('%1$s is now friends with %2$s'), $A, $B)."\n\n\n".$BPhoto;
 
                                $arr["object"] = self::constructNewFriendObject($contact);
 
                                $arr["last-child"] = 1;
 
-                               $arr["allow_cid"] = $user[0]["allow_cid"];
-                               $arr["allow_gid"] = $user[0]["allow_gid"];
-                               $arr["deny_cid"]  = $user[0]["deny_cid"];
-                               $arr["deny_gid"]  = $user[0]["deny_gid"];
+                               $user = dba::select('user', ['allow_cid', 'allow_gid', 'deny_cid', 'deny_gid'], ['uid' => $importer["uid"]], ['limit' => 1]);
+
+                               $arr["allow_cid"] = $user["allow_cid"];
+                               $arr["allow_gid"] = $user["allow_gid"];
+                               $arr["deny_cid"]  = $user["deny_cid"];
+                               $arr["deny_gid"]  = $user["deny_gid"];
 
                                $i = item_store($arr);
                                if ($i) {
@@ -2462,11 +2466,7 @@ class Diaspora
 
                logger("Author ".$author." was added as contact number ".$contact_record["id"].".", LOGGER_DEBUG);
 
-               $def_gid = get_default_group($importer['uid'], $ret["network"]);
-
-               if (intval($def_gid)) {
-                       group_add_member($importer["uid"], "", $contact_record["id"], $def_gid);
-               }
+               Group::addMember(User::getDefaultGroup($importer['uid'], $ret["network"]), $contact_record['id']);
 
                Contact::updateAvatar($ret["photo"], $importer['uid'], $contact_record["id"], true);
 
@@ -3210,7 +3210,7 @@ class Diaspora
                        $return_code = self::transmit($owner, $contact, $envelope, $public_batch, false, $guid);
                }
 
-               logger("guid: ".$item["guid"]." result ".$return_code, LOGGER_DEBUG);
+               logger("guid: ".$guid." result ".$return_code, LOGGER_DEBUG);
 
                return $return_code;
        }
@@ -3963,6 +3963,62 @@ class Diaspora
                return self::buildAndTransmit($owner, $contact, $type, $message, false, $item["guid"]);
        }
 
+       /**
+        * @brief Split a name into first name and last name
+        *
+        * @param string $name The name
+        *
+        * @return array The array with "first" and "last"
+        */
+       public static function splitName($name) {
+               $name = trim($name);
+
+               // Is the name longer than 64 characters? Then cut the rest of it.
+               if (strlen($name) > 64) {
+                       if ((strpos($name, ' ') <= 64) && (strpos($name, ' ') !== false)) {
+                               $name = trim(substr($name, 0, strrpos(substr($name, 0, 65), ' ')));
+                       } else {
+                               $name = substr($name, 0, 64);
+                       }
+               }
+
+               // Take the first word as first name
+               $first = ((strpos($name, ' ') ? trim(substr($name, 0, strpos($name, ' '))) : $name));
+               $last = (($first === $name) ? '' : trim(substr($name, strlen($first))));
+               if ((strlen($first) < 32) && (strlen($last) < 32)) {
+                       return ['first' => $first, 'last' => $last];
+               }
+
+               // Take the last word as last name
+               $first = ((strrpos($name, ' ') ? trim(substr($name, 0, strrpos($name, ' '))) : $name));
+               $last = (($first === $name) ? '' : trim(substr($name, strlen($first))));
+
+               if ((strlen($first) < 32) && (strlen($last) < 32)) {
+                       return ['first' => $first, 'last' => $last];
+               }
+
+               // Take the first 32 characters if there is no space in the first 32 characters
+               if ((strpos($name, ' ') > 32) || (strpos($name, ' ') === false)) {
+                       $first = substr($name, 0, 32);
+                       $last = substr($name, 32);
+                       return ['first' => $first, 'last' => $last];
+               }
+
+               $first = trim(substr($name, 0, strrpos(substr($name, 0, 33), ' ')));
+               $last = (($first === $name) ? '' : trim(substr($name, strlen($first))));
+
+               // Check if the last name is longer than 32 characters
+               if (strlen($last) > 32) {
+                       if (strpos($last, ' ') <= 32) {
+                               $last = trim(substr($last, 0, strrpos(substr($last, 0, 33), ' ')));
+                       } else {
+                               $last = substr($last, 0, 32);
+                       }
+               }
+
+               return ['first' => $first, 'last' => $last];
+       }
+
        /**
         * @brief Create profile data
         *
@@ -3986,11 +4042,12 @@ class Diaspora
                }
 
                $profile = $r[0];
-
                $handle = $profile["addr"];
-               $first = ((strpos($profile['name'], ' ')
-                       ? trim(substr($profile['name'], 0, strpos($profile['name'], ' '))) : $profile['name']));
-               $last = (($first === $profile['name']) ? '' : trim(substr($profile['name'], strlen($first))));
+
+               $split_name = self::splitName($profile['name']);
+               $first = $split_name['first'];
+               $last = $split_name['last'];
+
                $large = System::baseUrl().'/photo/custom/300/'.$profile['uid'].'.jpg';
                $medium = System::baseUrl().'/photo/custom/100/'.$profile['uid'].'.jpg';
                $small = System::baseUrl().'/photo/custom/50/'  .$profile['uid'].'.jpg';
@@ -4051,6 +4108,11 @@ class Diaspora
                        return;
                }
 
+               $owner = User::getOwnerDataById($uid);
+               if (!$owner) {
+                       return;
+               }
+
                if (!$recips) {
                        $recips = q(
                                "SELECT `id`,`name`,`network`,`pubkey`,`notify` FROM `contact` WHERE `network` = '%s'
@@ -4069,7 +4131,7 @@ class Diaspora
 
                foreach ($recips as $recip) {
                        logger("Send updated profile data for user ".$uid." to contact ".$recip["id"], LOGGER_DEBUG);
-                       self::buildAndTransmit($profile, $recip, "profile", $message, false, "", true);
+                       self::buildAndTransmit($owner, $recip, "profile", $message, false, "", true);
                }
        }