Permalink
Browse files

Check if input is already utf8 encoded before encoding it (fixes bug …

…#17876)

git-svn-id: http://svn.php.net/repository/pear/packages/Services_GeoNames/trunk@303916 c90b9560-bf6c-de11-be94-00142212c4b1
  • Loading branch information...
1 parent cf11167 commit 9300a89d8ea7feea64299c56e66e395006d98afb @izimobil izimobil committed Oct 1, 2010
Showing with 107 additions and 2 deletions.
  1. +31 −2 Services/GeoNames.php
  2. +1 −0 tests/data/test_utf8.dat
  3. +75 −0 tests/test_utf8.phpt
View
@@ -363,10 +363,12 @@ protected function formatQueryString($params = array())
foreach ($params as $name => $value) {
if (is_array($value)) {
foreach ($value as $val) {
- $qString[] = $name . '=' . urlencode(utf8_encode($val));
+ $val = $this->isUtf8($val) ? $val : utf8_encode($val);
+ $qString[] = $name . '=' . urlencode($val);
}
} else {
- $qString[] = $name . '=' . urlencode(utf8_encode($value));
+ $value = $this->isUtf8($value) ? $value : utf8_encode($value);
+ $qString[] = $name . '=' . urlencode($value);
}
}
return implode('&', $qString);
@@ -420,5 +422,32 @@ public function getSupportedEndpoints()
return array_keys($this->endpoints);
}
+ // }}}
+ // isUtf8() {{{
+
+ /**
+ * Check if the given string is a UTF-8 string or an iso-8859-1 one.
+ *
+ * @param string $str The string to check
+ *
+ * @return boolean Wether the string is unicode or not
+ */
+ protected function isUtf8($str)
+ {
+ return (bool)preg_match(
+ '%^(?:
+ [\x09\x0A\x0D\x20-\x7E] # ASCII
+ | [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
+ | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
+ | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
+ | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
+ | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
+ | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
+ | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
+ )*$%xs',
+ $str
+ );
+ }
+
// }}}
}
View
@@ -0,0 +1 @@
+{"totalResultsCount":3,"geonames":[{"countryName":"Finland","adminCode1":"15","fclName":"city, village,...","countryCode":"FI","lng":24.6333333,"fcodeName":"populated place","toponymName":"Mänttä","fcl":"P","name":"Mänttä","fcode":"PPL","geonameId":646705,"lat":62.0333333,"adminName1":"Western Finland","population":6459},{"countryName":"Finland","adminCode1":"15","fclName":"country, state, region,...","countryCode":"FI","lng":24.6333333,"fcodeName":"third-order administrative division","toponymName":"Mänttä","fcl":"A","name":"Mänttä","fcode":"ADM3","geonameId":646706,"lat":62.05,"adminName1":"Western Finland","population":0},{"countryName":"Finland","adminCode1":"15","fclName":"stream, lake, ...","countryCode":"FI","lng":23.0666667,"fcodeName":"shoal(s)","toponymName":"Mänttä","fcl":"H","name":"Mänttä","fcode":"SHOL","geonameId":818216,"lat":63.9833333,"adminName1":"Western Finland","population":0}]}
View
@@ -0,0 +1,75 @@
+--TEST--
+Test for utf-8 strings
+--FILE--
+<?php
+require_once dirname(__FILE__) . '/setup.php';
+require_once 'HTTP/Request2/Observer/Log.php';
+
+$geo = Services_GeoNames_factory(substr(basename(__FILE__), 0, -4));
+
+try {
+ $ret = $geo->search(array('q' => 'mänttä'));
+ print_r($ret);
+} catch (Services_GeoNames_HTTPException $exc) {
+ echo $exc->getMessage() . "\n";
+}
+
+?>
+--EXPECTF--
+Array
+(
+ [0] => stdClass Object
+ (
+ [countryName] => Finland
+ [adminCode1] => 15
+ [fclName] => city, village,...
+ [countryCode] => FI
+ [lng] => 24.6333333
+ [fcodeName] => populated place
+ [toponymName] => Mänttä
+ [fcl] => P
+ [name] => Mänttä
+ [fcode] => PPL
+ [geonameId] => 646705
+ [lat] => 62.0333333
+ [adminName1] => Western Finland
+ [population] => 6459
+ )
+
+ [1] => stdClass Object
+ (
+ [countryName] => Finland
+ [adminCode1] => 15
+ [fclName] => country, state, region,...
+ [countryCode] => FI
+ [lng] => 24.6333333
+ [fcodeName] => third-order administrative division
+ [toponymName] => Mänttä
+ [fcl] => A
+ [name] => Mänttä
+ [fcode] => ADM3
+ [geonameId] => 646706
+ [lat] => 62.05
+ [adminName1] => Western Finland
+ [population] => 0
+ )
+
+ [2] => stdClass Object
+ (
+ [countryName] => Finland
+ [adminCode1] => 15
+ [fclName] => stream, lake, ...
+ [countryCode] => FI
+ [lng] => 23.0666667
+ [fcodeName] => shoal(s)
+ [toponymName] => Mänttä
+ [fcl] => H
+ [name] => Mänttä
+ [fcode] => SHOL
+ [geonameId] => 818216
+ [lat] => 63.9833333
+ [adminName1] => Western Finland
+ [population] => 0
+ )
+
+)

0 comments on commit 9300a89

Please sign in to comment.