update new IdnaConvert class with needed fixes in them

Signed-off-by: Michael Kaufmann (d00p) <d00p@froxlor.org>
This commit is contained in:
Michael Kaufmann (d00p)
2016-06-19 20:15:33 +02:00
parent 5789e9a8a4
commit b22e70804b
2 changed files with 34 additions and 25 deletions

View File

@@ -54,12 +54,10 @@ namespace Mso\IdnaConvert;
class IdnaConvert { class IdnaConvert {
const Version = '1.0.2'; const Version = '1.1.0';
const SubVersion = 'main'; const SubVersion = 'main';
// Internal settings, do not touch! // Internal settings, do not touch!
const PunycodePrefix = 'xn--';
protected $encoding = 'utf8'; // Default input charset is UTF-8 protected $encoding = 'utf8'; // Default input charset is UTF-8
protected $strictMode = false; // Behave strict or not protected $strictMode = false; // Behave strict or not
protected $idnVersion = '2008'; // Can be either 2003 (old) or 2008 (default) protected $idnVersion = '2008'; // Can be either 2003 (old) or 2008 (default)
@@ -203,21 +201,17 @@ class IdnaConvert {
list ($email_pref, $input) = explode('@', $input, 2); list ($email_pref, $input) = explode('@', $input, 2);
$arr = explode('.', $input); $arr = explode('.', $input);
foreach ($arr as $k => $v) { foreach ($arr as $k => $v) {
if (preg_match('!^' . preg_quote(self::PunycodePrefix, '!') . '!', $v)) { $conv = $punyCode->decode($v);
$conv = $punyCode->decode($v); if ($conv) {
if ($conv) { $arr[$k] = $conv;
$arr[$k] = $conv;
}
} }
} }
$input = join('.', $arr); $input = join('.', $arr);
$arr = explode('.', $email_pref); $arr = explode('.', $email_pref);
foreach ($arr as $k => $v) { foreach ($arr as $k => $v) {
if (preg_match('!^' . preg_quote(self::PunycodePrefix, '!') . '!', $v)) { $conv = $punyCode->decode($v);
$conv = $punyCode->decode($v); if ($conv) {
if ($conv) { $arr[$k] = $conv;
$arr[$k] = $conv;
}
} }
} }
$email_pref = join('.', $arr); $email_pref = join('.', $arr);
@@ -248,7 +242,9 @@ class IdnaConvert {
$arr = explode('.', $input); $arr = explode('.', $input);
foreach ($arr as $k => $v) { foreach ($arr as $k => $v) {
$conv = $punyCode->decode($v); $conv = $punyCode->decode($v);
$arr[$k] = ($conv) ? $conv : $v; if ($conv) {
$arr[$k] = $conv;
}
} }
$return = join('.', $arr); $return = join('.', $arr);
} }

View File

@@ -77,11 +77,32 @@ class Punycode implements PunycodeInterface
$this->UnicodeTranscoder = $UnicodeTranscoder; $this->UnicodeTranscoder = $UnicodeTranscoder;
} }
/**
* Returns the used prefix for punycode-encoded strings
* @return string
*/
public function getPunycodePrefix() public function getPunycodePrefix()
{ {
return self::punycodePrefix; return self::punycodePrefix;
} }
/**
* Checks, whether or not the provided string is a valid punycode string
* @param string $encoded
* @return boolean
*/
public function validate($encoded) {
// Check for existence of the prefix
if (strpos($encoded, self::punycodePrefix) !== 0) {
return false;
}
// If nothing is left after the prefix, it is hopeless
if (strlen(trim($encoded)) <= strlen(self::punycodePrefix)) {
return false;
}
return true;
}
/** /**
* The actual decoding algorithm * The actual decoding algorithm
* @param string * @param string
@@ -89,19 +110,11 @@ class Punycode implements PunycodeInterface
*/ */
public function decode($encoded) public function decode($encoded)
{ {
$decoded = []; if (!$this->validate($encoded)) {
// find the Punycode prefix
if (!preg_match('!^' . preg_quote(self::punycodePrefix, '!') . '!', $encoded)) {
// *** froxlor patch ***
return $encoded;
// *** end froxlor patch ***
throw new \InvalidArgumentException('This is not a punycode string');
}
$encode_test = preg_replace('!^' . preg_quote(self::punycodePrefix, '!') . '!', '', $encoded);
// If nothing left after removing the prefix, it is hopeless
if (!$encode_test) {
return false; return false;
} }
$decoded = [];
// Find last occurence of the delimiter // Find last occurence of the delimiter
$delim_pos = strrpos($encoded, '-'); $delim_pos = strrpos($encoded, '-');
if ($delim_pos > self::byteLength(self::punycodePrefix)) { if ($delim_pos > self::byteLength(self::punycodePrefix)) {