forked from GNUsocial/gnu-social
Better use of Nickname validation functions
Nickname verifications on registration and updates for profiles (not yet groups) have been improved. Minor bugs in RegisterAction were also fixed, where multiple forms would be outputed because the function did not return after showForm(). This will be solved more permanently with throwing exceptions in the future.
This commit is contained in:
@@ -355,24 +355,12 @@ class FacebookfinishloginAction extends Action
|
||||
}
|
||||
|
||||
try {
|
||||
$nickname = Nickname::normalize($this->trimmed('newname'));
|
||||
$nickname = Nickname::normalize($this->trimmed('newname'), true);
|
||||
} catch (NicknameException $e) {
|
||||
$this->showForm($e->getMessage());
|
||||
return;
|
||||
}
|
||||
|
||||
if (!User::allowed_nickname($nickname)) {
|
||||
// TRANS: Form validation error displayed when picking a nickname that is not allowed.
|
||||
$this->showForm(_m('Nickname not allowed.'));
|
||||
return;
|
||||
}
|
||||
|
||||
if (User::getKV('nickname', $nickname)) {
|
||||
// TRANS: Form validation error displayed when picking a nickname that is already in use.
|
||||
$this->showForm(_m('Nickname already in use. Try another one.'));
|
||||
return;
|
||||
}
|
||||
|
||||
$args = array(
|
||||
'nickname' => $nickname,
|
||||
'fullname' => $this->fbuser->name,
|
||||
@@ -603,58 +591,23 @@ class FacebookfinishloginAction extends Action
|
||||
|
||||
function bestNewNickname()
|
||||
{
|
||||
if (!empty($this->fbuser->username)) {
|
||||
$nickname = $this->nicknamize($this->fbuser->username);
|
||||
if ($this->isNewNickname($nickname)) {
|
||||
return $nickname;
|
||||
}
|
||||
try {
|
||||
$nickname = Nickname::normalize($this->fbuser->username, true);
|
||||
return $nickname;
|
||||
} catch (NicknameException $e) {
|
||||
// Failed to normalize nickname, but let's try the full name
|
||||
}
|
||||
|
||||
// Try the full name
|
||||
|
||||
$fullname = $this->fbuser->name;
|
||||
|
||||
if (!empty($fullname)) {
|
||||
$fullname = $this->nicknamize($fullname);
|
||||
if ($this->isNewNickname($fullname)) {
|
||||
return $fullname;
|
||||
}
|
||||
try {
|
||||
$nickname = Nickname::normalize($this->fbuser->name, true);
|
||||
return $nickname;
|
||||
} catch (NicknameException $e) {
|
||||
// Any more ideas? Nope.
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Given a string, try to make it work as a nickname
|
||||
*/
|
||||
function nicknamize($str)
|
||||
{
|
||||
$str = preg_replace('/\W/', '', $str);
|
||||
return strtolower($str);
|
||||
}
|
||||
|
||||
/*
|
||||
* Is the desired nickname already taken?
|
||||
*
|
||||
* @return boolean result
|
||||
*/
|
||||
function isNewNickname($str)
|
||||
{
|
||||
if (!Nickname::isValid($str)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!User::allowed_nickname($str)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (User::getKV('nickname', $str)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/*
|
||||
* Do we already have a user record with this email?
|
||||
* (emails have to be unique but they can change)
|
||||
|
@@ -345,24 +345,12 @@ class FinishopenidloginAction extends Action
|
||||
}
|
||||
|
||||
try {
|
||||
$nickname = Nickname::normalize($this->trimmed('newname'));
|
||||
$nickname = Nickname::normalize($this->trimmed('newname'), true);
|
||||
} catch (NicknameException $e) {
|
||||
$this->showForm($e->getMessage());
|
||||
return;
|
||||
}
|
||||
|
||||
if (!User::allowed_nickname($nickname)) {
|
||||
// TRANS: OpenID plugin message. The entered new user name is blacklisted.
|
||||
$this->showForm(_m('Nickname not allowed.'));
|
||||
return;
|
||||
}
|
||||
|
||||
if (User::getKV('nickname', $nickname)) {
|
||||
// TRANS: OpenID plugin message. The entered new user name is already used.
|
||||
$this->showForm(_m('Nickname already in use. Try another one.'));
|
||||
return;
|
||||
}
|
||||
|
||||
list($display, $canonical, $sreg) = $this->getSavedValues();
|
||||
|
||||
if (!$display || !$canonical) {
|
||||
@@ -500,8 +488,8 @@ class FinishopenidloginAction extends Action
|
||||
// Try the passed-in nickname
|
||||
|
||||
if (!empty($sreg['nickname'])) {
|
||||
$nickname = $this->nicknamize($sreg['nickname']);
|
||||
if ($this->isNewNickname($nickname)) {
|
||||
$nickname = common_nicknamize($sreg['nickname']);
|
||||
if (Nickname::isValid($nickname, true)) {
|
||||
return $nickname;
|
||||
}
|
||||
}
|
||||
@@ -509,8 +497,8 @@ class FinishopenidloginAction extends Action
|
||||
// Try the full name
|
||||
|
||||
if (!empty($sreg['fullname'])) {
|
||||
$fullname = $this->nicknamize($sreg['fullname']);
|
||||
if ($this->isNewNickname($fullname)) {
|
||||
$fullname = common_nicknamize($sreg['fullname']);
|
||||
if (Nickname::isValid($fullname, true)) {
|
||||
return $fullname;
|
||||
}
|
||||
}
|
||||
@@ -519,7 +507,7 @@ class FinishopenidloginAction extends Action
|
||||
|
||||
$from_url = $this->openidToNickname($display);
|
||||
|
||||
if ($from_url && $this->isNewNickname($from_url)) {
|
||||
if ($from_url && Nickname::isValid($from_url, true)) {
|
||||
return $from_url;
|
||||
}
|
||||
|
||||
@@ -528,20 +516,6 @@ class FinishopenidloginAction extends Action
|
||||
return null;
|
||||
}
|
||||
|
||||
function isNewNickname($str)
|
||||
{
|
||||
if (!Nickname::isValid($str)) {
|
||||
return false;
|
||||
}
|
||||
if (!User::allowed_nickname($str)) {
|
||||
return false;
|
||||
}
|
||||
if (User::getKV('nickname', $str)) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
function openidToNickname($openid)
|
||||
{
|
||||
if (Auth_Yadis_identifierScheme($openid) == 'XRI') {
|
||||
@@ -570,7 +544,7 @@ class FinishopenidloginAction extends Action
|
||||
// =evan.prodromou
|
||||
// or @gratis*evan.prodromou
|
||||
$parts = explode('*', substr($base, 1));
|
||||
return $this->nicknamize(array_pop($parts));
|
||||
return common_nicknamize(array_pop($parts));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -582,10 +556,4 @@ class FinishopenidloginAction extends Action
|
||||
return $xri;
|
||||
}
|
||||
}
|
||||
|
||||
// Given a string, try to make it work as a nickname
|
||||
function nicknamize($str)
|
||||
{
|
||||
return common_nicknamize($str);
|
||||
}
|
||||
}
|
||||
|
@@ -519,24 +519,12 @@ class TwitterauthorizationAction extends Action
|
||||
}
|
||||
|
||||
try {
|
||||
$nickname = Nickname::normalize($this->trimmed('newname'));
|
||||
$nickname = Nickname::normalize($this->trimmed('newname'), true);
|
||||
} catch (NicknameException $e) {
|
||||
$this->showForm($e->getMessage());
|
||||
return;
|
||||
}
|
||||
|
||||
if (!User::allowed_nickname($nickname)) {
|
||||
// TRANS: Client error displayed when trying to create a new user with an invalid username.
|
||||
$this->showForm(_m('Nickname not allowed.'));
|
||||
return;
|
||||
}
|
||||
|
||||
if (User::getKV('nickname', $nickname)) {
|
||||
// TRANS: Client error displayed when trying to create a new user with a username that is already in use.
|
||||
$this->showForm(_m('Nickname already in use. Try another one.'));
|
||||
return;
|
||||
}
|
||||
|
||||
$fullname = trim($this->tw_fields['fullname']);
|
||||
|
||||
$args = array('nickname' => $nickname, 'fullname' => $fullname);
|
||||
@@ -688,36 +676,10 @@ class TwitterauthorizationAction extends Action
|
||||
|
||||
function bestNewNickname()
|
||||
{
|
||||
if (!empty($this->tw_fields['fullname'])) {
|
||||
$nickname = $this->nicknamize($this->tw_fields['fullname']);
|
||||
if ($this->isNewNickname($nickname)) {
|
||||
return $nickname;
|
||||
}
|
||||
try {
|
||||
return Nickname::normalize($this->tw_fields['fullname'], true);
|
||||
} catch (NicknameException $e) {
|
||||
return null
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
// Given a string, try to make it work as a nickname
|
||||
|
||||
function nicknamize($str)
|
||||
{
|
||||
$str = preg_replace('/\W/', '', $str);
|
||||
$str = str_replace(array('-', '_'), '', $str);
|
||||
return strtolower($str);
|
||||
}
|
||||
|
||||
function isNewNickname($str)
|
||||
{
|
||||
if (!Nickname::isValid($str)) {
|
||||
return false;
|
||||
}
|
||||
if (!User::allowed_nickname($str)) {
|
||||
return false;
|
||||
}
|
||||
if (User::getKV('nickname', $str)) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user