From fb88d78862530566ec69615c71cdcad02769484c Mon Sep 17 00:00:00 2001 From: Michael Date: Wed, 20 Dec 2017 20:31:25 +0000 Subject: [PATCH] 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 8828324c54..67167ac930 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';