Rename special chars to unicode chars, remove special chars in username

This commit is contained in:
Diego Najar 2018-08-21 20:11:31 +02:00
parent 82d7d1ae7e
commit f98f14670c
6 changed files with 23 additions and 19 deletions

View File

@ -517,7 +517,7 @@ function createUser($args) {
// Filter form fields // Filter form fields
$tmp = array(); $tmp = array();
$tmp['username'] = $args['new_username']; $tmp['username'] = Text::removeSpecialCharacters($args['new_username']);
$tmp['password'] = $args['new_password']; $tmp['password'] = $args['new_password'];
$tmp['role'] = $args['role']; $tmp['role'] = $args['role'];
$tmp['email'] = $args['email']; $tmp['email'] = $args['email'];

View File

@ -2,7 +2,7 @@
class Text { class Text {
private static $specialChars = array( private static $unicodeChars = array(
// Latin // Latin
'À'=>'A', 'Á'=>'A', 'Â'=>'A', 'Ã'=>'A', 'Ä'=>'A', 'Å'=>'A', 'Æ'=>'AE', 'Ç'=>'C', 'À'=>'A', 'Á'=>'A', 'Â'=>'A', 'Ã'=>'A', 'Ä'=>'A', 'Å'=>'A', 'Æ'=>'AE', 'Ç'=>'C',
'È'=>'E', 'É'=>'E', 'Ê'=>'E', 'Ë'=>'E', 'Ì'=>'I', 'Í'=>'I', 'Î'=>'I', 'Ï'=>'I', 'È'=>'E', 'É'=>'E', 'Ê'=>'E', 'Ë'=>'E', 'Ì'=>'I', 'Í'=>'I', 'Î'=>'I', 'Ï'=>'I',
@ -121,6 +121,11 @@ class Text {
return str_replace(array_keys($replace), array_values($replace), $text); return str_replace(array_keys($replace), array_values($replace), $text);
} }
public static function removeSpecialCharacters($string, $replace='')
{
return preg_replace("/[\/_|+:!@#$%^&*()'\"<>\\\`}{;=,?\[\]~. -]+/", $replace, $string);
}
// Convert unicode characters to utf-8 characters // Convert unicode characters to utf-8 characters
// Characters that cannot be converted will be removed from the string // Characters that cannot be converted will be removed from the string
// This function can return an empty string // This function can return an empty string
@ -131,15 +136,15 @@ class Text {
if (EXTREME_FRIENDLY_URL) { if (EXTREME_FRIENDLY_URL) {
$string = self::lowercase($string); $string = self::lowercase($string);
$string = trim($string, $separator); $string = trim($string, $separator);
$string = preg_replace("/[\/_|+:!@#$%^&*()';=,?\[\]~. -]+/", $separator, $string); $string = self::removeSpecialCharacters($string, $separator);
$string = trim($string, $separator); $string = trim($string, $separator);
return $string; return $string;
} }
// Transliterate characters to ASCII // Transliterate characters to ASCII
$specialCharsFromDictionary = $L->getSpecialChars(); $unicodeCharsFromDictionary = $L->getunicodeChars();
$string = str_replace(array_keys($specialCharsFromDictionary), $specialCharsFromDictionary, $string); $string = str_replace(array_keys($unicodeCharsFromDictionary), $unicodeCharsFromDictionary, $string);
$string = str_replace(array_keys(self::$specialChars), self::$specialChars, $string); $string = str_replace(array_keys(self::$unicodeChars), self::$unicodeChars, $string);
if (function_exists('iconv')) { if (function_exists('iconv')) {
if (@iconv(CHARSET, 'ASCII//TRANSLIT//IGNORE', $string)!==false) { if (@iconv(CHARSET, 'ASCII//TRANSLIT//IGNORE', $string)!==false) {

View File

@ -5,7 +5,7 @@ class Language extends dbJSON {
public $db; public $db;
public $currentLanguage; public $currentLanguage;
public $dates; public $dates;
public $specialChars; public $unicodeChars;
function __construct($currentLanguage) function __construct($currentLanguage)
{ {
@ -13,7 +13,7 @@ class Language extends dbJSON {
$this->db = array(); $this->db = array();
$this->currentLanguage = $currentLanguage; $this->currentLanguage = $currentLanguage;
$this->dates = array(); $this->dates = array();
$this->specialChars = array(); $this->unicodeChars = array();
// Load default language // Load default language
$filename = PATH_LANGUAGES.DEFAULT_LANGUAGE_FILE; $filename = PATH_LANGUAGES.DEFAULT_LANGUAGE_FILE;
@ -40,10 +40,10 @@ class Language extends dbJSON {
unset($this->db['dates']); unset($this->db['dates']);
} }
// Special chars // Unicode chars
if (isset($this->db['special-chars'])) { if (isset($this->db['unicode-chars'])) {
$this->specialChars = $this->db['special-chars']; $this->unicodeChars = $this->db['unicode-chars'];
unset($this->db['special-chars']); unset($this->db['unicode-chars']);
} }
} }
@ -68,7 +68,6 @@ class Language extends dbJSON {
return $explode[0]; return $explode[0];
} }
// Return the translation, if the translation doesn't exist returns the English translation // Return the translation, if the translation doesn't exist returns the English translation
public function get($string) public function get($string)
{ {
@ -147,8 +146,8 @@ class Language extends dbJSON {
} }
// Returns array with all the special characters from this language // Returns array with all the special characters from this language
public function getSpecialChars() public function getunicodeChars()
{ {
return $this->specialChars; return $this->unicodeChars;
} }
} }

View File

@ -648,7 +648,7 @@ class Pages extends dbJSON {
global $L; global $L;
if (Text::isEmpty($text)) { if (Text::isEmpty($text)) {
$text = 'empty'; $text = $L->g('empty');
} }
if (Text::isEmpty($parent)) { if (Text::isEmpty($parent)) {
@ -659,7 +659,7 @@ class Pages extends dbJSON {
// cleanURL can return empty string // cleanURL can return empty string
if (Text::isEmpty($newKey)) { if (Text::isEmpty($newKey)) {
$key = $L->g('empty'); $newKey = $L->g('empty');
} }
if ($newKey!==$oldKey) { if ($newKey!==$oldKey) {

View File

@ -50,7 +50,7 @@
"November": "November", "November": "November",
"December": "Dezember" "December": "Dezember"
}, },
"special-chars": { "unicode-chars": {
"ä": "ae", "ä": "ae",
"ö": "oe", "ö": "oe",
"ü": "ue" "ü": "ue"

View File

@ -50,7 +50,7 @@
"November": "November", "November": "November",
"December": "Dezember" "December": "Dezember"
}, },
"special-chars": { "unicode-chars": {
"ä": "ae", "ä": "ae",
"ö": "oe", "ö": "oe",
"ü": "ue", "ü": "ue",