From fb88d78862530566ec69615c71cdcad02769484c Mon Sep 17 00:00:00 2001 From: Michael Date: Wed, 20 Dec 2017 20:31:25 +0000 Subject: [PATCH 1/3] Split the first name and last name so that they fit into the length restrictions of Diaspora --- src/Protocol/Diaspora.php | 65 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 61 insertions(+), 4 deletions(-) diff --git a/src/Protocol/Diaspora.php b/src/Protocol/Diaspora.php index 8828324c5..67167ac93 100644 --- a/src/Protocol/Diaspora.php +++ b/src/Protocol/Diaspora.php @@ -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" + */ + private 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)))); + + $splitted_name = self::splitName($profile['name']); + $first = $splitted_name['first']; + $last = $splitted_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'; From 008c97c570ea62749a3f486b42c424365863d627 Mon Sep 17 00:00:00 2001 From: Michael Date: Wed, 20 Dec 2017 21:15:13 +0000 Subject: [PATCH 2/3] Split the name in the hcard --- include/identity.php | 7 ++++--- src/Protocol/Diaspora.php | 2 +- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/include/identity.php b/include/identity.php index 9a0900dc1..eebb8fe51 100644 --- a/include/identity.php +++ b/include/identity.php @@ -13,6 +13,7 @@ use Friendica\Core\System; use Friendica\Core\Worker; use Friendica\Database\DBM; use Friendica\Model\Contact; +use Friendica\Protocol\Diaspora; require_once 'include/bbcode.php'; require_once 'mod/proxy.php'; @@ -374,9 +375,9 @@ function profile_sidebar($profile, $block = 0) $location = $pdesc = $gender = $marital = $homepage = $about = false; } - $firstname = ((strpos($profile['name'], ' ')) - ? trim(substr($profile['name'], 0, strpos($profile['name'], ' '))) : $profile['name']); - $lastname = (($firstname === $profile['name']) ? '' : trim(substr($profile['name'], strlen($firstname)))); + $splitted_name = Diaspora::splitName($profile['name']); + $firstname = $splitted_name['first']; + $lastname = $splitted_name['last']; if ($profile['guid'] != "") { $diaspora = array( diff --git a/src/Protocol/Diaspora.php b/src/Protocol/Diaspora.php index 67167ac93..09899ffa6 100644 --- a/src/Protocol/Diaspora.php +++ b/src/Protocol/Diaspora.php @@ -3970,7 +3970,7 @@ class Diaspora * * @return array The array with "first" and "last" */ - private static function splitName($name) { + public static function splitName($name) { $name = trim($name); // Is the name longer than 64 characters? Then cut the rest of it. From 8c2d455b98126d9817dc62b20fbe07d8140958e3 Mon Sep 17 00:00:00 2001 From: Michael Date: Thu, 21 Dec 2017 04:53:57 +0000 Subject: [PATCH 3/3] Better english --- include/identity.php | 6 +++--- src/Protocol/Diaspora.php | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/include/identity.php b/include/identity.php index eebb8fe51..8f515580f 100644 --- a/include/identity.php +++ b/include/identity.php @@ -375,9 +375,9 @@ function profile_sidebar($profile, $block = 0) $location = $pdesc = $gender = $marital = $homepage = $about = false; } - $splitted_name = Diaspora::splitName($profile['name']); - $firstname = $splitted_name['first']; - $lastname = $splitted_name['last']; + $split_name = Diaspora::splitName($profile['name']); + $firstname = $split_name['first']; + $lastname = $split_name['last']; if ($profile['guid'] != "") { $diaspora = array( diff --git a/src/Protocol/Diaspora.php b/src/Protocol/Diaspora.php index 09899ffa6..a0d99959b 100644 --- a/src/Protocol/Diaspora.php +++ b/src/Protocol/Diaspora.php @@ -4044,9 +4044,9 @@ class Diaspora $profile = $r[0]; $handle = $profile["addr"]; - $splitted_name = self::splitName($profile['name']); - $first = $splitted_name['first']; - $last = $splitted_name['last']; + $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';