Skip to content

Commit

Permalink
[mms] Fix handling broken text input with all multibyte drivers.
Browse files Browse the repository at this point in the history
  • Loading branch information
slusarz committed Mar 4, 2015
1 parent 6f58696 commit a57d409
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 19 deletions.
9 changes: 8 additions & 1 deletion framework/Util/lib/Horde/String.php
Expand Up @@ -309,6 +309,8 @@ public static function substr($string, $start, $length = null,
return '';
}

$error = false;

/* Try mbstring. */
if (Horde_Util::extensionExists('mbstring')) {
$track_errors = ini_set('track_errors', 1);
Expand All @@ -322,6 +324,7 @@ public static function substr($string, $start, $length = null,
if (!isset($php_errormsg)) {
return $ret;
}
$error = true;
}

/* Try iconv. */
Expand All @@ -332,6 +335,7 @@ public static function substr($string, $start, $length = null,
if ($ret !== false) {
return $ret;
}
$error = true;
}

/* Try intl. */
Expand All @@ -350,9 +354,12 @@ public static function substr($string, $start, $length = null,
if ($ret !== false) {
return $ret;
}
$error = true;
}

return substr($string, $start, $length);
return $error
? ''
: substr($string, $start, $length);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions framework/Util/package.xml
Expand Up @@ -34,7 +34,7 @@
</stability>
<license uri="http://www.horde.org/licenses/lgpl21">LGPL-2.1</license>
<notes>
*
* [mms] Fix handling broken text input with all multibyte drivers.
</notes>
<contents>
<dir baseinstalldir="/" name="/">
Expand Down Expand Up @@ -750,7 +750,7 @@ Converted to package.xml 2.0 for pear.horde.org
<date>2015-03-02</date>
<license uri="http://www.horde.org/licenses/lgpl21">LGPL-2.1</license>
<notes>
*
* [mms] Fix handling broken text input with all multibyte drivers.
</notes>
</release>
</changelog>
Expand Down
126 changes: 110 additions & 16 deletions framework/Util/test/Horde/Util/StringTest.php
Expand Up @@ -352,24 +352,118 @@ public function testPad()
Horde_String::pad('abc', 4, ' ', STR_PAD_RIGHT, 'utf-8'));
}

public function testSubstr()
/**
* @dataProvider substrProvider
*/
public function testSubstr($match, $string, $start, $length)
{
$string = "Lörem ipsüm dölör sit ämet";
$this->assertEquals(
't ämet',
Horde_String::substr($string, 20, null, 'utf-8'));
$this->assertEquals(
't ämet',
Horde_String::substr($string, -6, null, 'utf-8'));
$this->assertEquals(
'Lörem',
Horde_String::substr($string, 0, 5, 'utf-8'));
$this->assertEquals(
'Lörem',
Horde_String::substr($string, 0, -21, 'utf-8'));
$this->assertEquals(
'ipsüm',
Horde_String::substr($string, 6, 5, 'utf-8'));
$match,
Horde_String::substr($string, $start, $length, 'utf-8')
);
}

public function substrProvider()
{
return array(
array(
't ämet',
"Lörem ipsüm dölör sit ämet",
20,
null
),
array(
't ämet',
"Lörem ipsüm dölör sit ämet",
-6,
null
),
array(
'Lörem',
"Lörem ipsüm dölör sit ämet",
0,
5
),
array(
'Lörem',
"Lörem ipsüm dölör sit ämet",
0,
-21
),
array(
'ipsüm',
"Lörem ipsüm dölör sit ämet",
6,
5
),
/* These are illegal UTF-8 encodings. */
array(
'',
base64_decode('2KvYpw=='),
2,
2
),
array(
'',
base64_decode('2KU='),
1,
1
),
array(
'',
base64_decode('2KvYpw=='),
2,
2
),
array(
'',
base64_decode('2KI='),
1,
1
),
array(
'',
base64_decode('5L6L'),
1,
1
),
array(
'',
base64_decode('5rWL'),
1,
1
),
array(
'',
base64_decode('5ris'),
1,
1
),
array(
'',
base64_decode('0L/RgNC40LzQtQ=='),
5,
5
),
array(
'',
base64_decode('0LA='),
1,
1
),
array(
'',
base64_decode('4KSJ'),
1,
1
),
array(
'',
base64_decode('4KSq4KSw4KSV'),
3,
3
),
);
}

public function testWordwrap()
Expand Down

0 comments on commit a57d409

Please sign in to comment.