Skip to content

Commit bcc0003

Browse files
committed
Added function to convert ISO-8559-1 to UTF-8
1 parent 123f959 commit bcc0003

File tree

3 files changed

+59
-1
lines changed

3 files changed

+59
-1
lines changed

lib/Sabre/DAV/StringUtil.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,27 @@ static public function textMatch($haystack, $needle, $collation, $matchType = 'c
6464

6565
}
6666

67+
}
68+
69+
/**
70+
* This method takes an input string, checks if it's not valid UTF-8 and
71+
* attempts to convert it to UTF-8 if it's not.
72+
*
73+
* Note that currently this can only convert ISO-8559-1 to UTF-8 (latin-1),
74+
* anything else will likely fail.
75+
*
76+
* @param string $input
77+
* @return string
78+
*/
79+
static public function ensureUTF8($input) {
80+
81+
$encoding = mb_detect_encoding($input , array('UTF-8','ISO-8859-1'), true);
82+
83+
if ($encoding === 'ISO-8859-1') {
84+
return utf8_encode($input);
85+
} else {
86+
return $input;
87+
}
6788

6889
}
6990

lib/Sabre/DAV/URLUtil.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ static function decodePathSegment($path) {
8686

8787
case 'ISO-8859-1' :
8888
$path = utf8_encode($path);
89+
8990
}
9091

9192
return $path;

tests/Sabre/DAV/StringUtilTest.php

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ class Sabre_DAV_StringUtilTest extends PHPUnit_Framework_TestCase {
55
/**
66
* @dataProvider dataset
77
*/
8-
function testEverything($haystack, $needle, $collation, $matchType, $result) {
8+
function testTextMatch($haystack, $needle, $collation, $matchType, $result) {
99

1010
$this->assertEquals($result, Sabre_DAV_StringUtil::textMatch($haystack, $needle, $collation, $matchType));
1111

@@ -81,4 +81,40 @@ public function testBadMatchType() {
8181

8282
}
8383

84+
public function testEnsureUTF8_ascii() {
85+
86+
$inputString = "harkema";
87+
$outputString = "harkema";
88+
89+
$this->assertEquals(
90+
$outputString,
91+
Sabre_DAV_StringUtil::ensureUTF8($inputString)
92+
);
93+
94+
}
95+
96+
public function testEnsureUTF8_latin1() {
97+
98+
$inputString = "m\xfcnster";
99+
$outputString = "münster";
100+
101+
$this->assertEquals(
102+
$outputString,
103+
Sabre_DAV_StringUtil::ensureUTF8($inputString)
104+
);
105+
106+
}
107+
108+
public function testEnsureUTF8_utf8() {
109+
110+
$inputString = "m\xc3\xbcnster";
111+
$outputString = "münster";
112+
113+
$this->assertEquals(
114+
$outputString,
115+
Sabre_DAV_StringUtil::ensureUTF8($inputString)
116+
);
117+
118+
}
119+
84120
}

0 commit comments

Comments
 (0)