summaryrefslogtreecommitdiff
path: root/www/fengoffice/patches/patch-aa
blob: 3a9a5dfe9034d634a39d2ae258661bc9463780e3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
$NetBSD: patch-aa,v 1.2 2010/12/01 15:55:27 taca Exp $

Unofficial patch to avoid iconv(GNU or gnu libc) portability problem.
(mb-iconv.patch from http://www.fengoffice.com/web/bugs/view.php?id=6)

--- environment/classes/encoding/EncodingConverter.class.php.orig	2010-09-13 20:30:20.000000000 +0000
+++ environment/classes/encoding/EncodingConverter.class.php
@@ -6,6 +6,16 @@ class EncodingConverter
 	var $_last_err_filename;
 	var $_last_err_line;
 	var $_last_err_func;
+	private static $gnu_iconv = NULL;
+
+	static function iconv_error($errno, $errstr) {
+		if (preg_match("/Wrong charset/", $errstr) > 0) {
+			self::$gnu_iconv = false;
+			return true;
+		} else {
+			return false;
+		}
+	}
 
 	function _handleError($err, $msg, $errfile, $errline, $errcontext) {
 		$trace = debug_backtrace();
@@ -25,12 +35,29 @@ class EncodingConverter
 	}
 
 	function convert($in_enc, $out_enc, $str, $return_original_on_error = true, $ignore_non_compatible = true) {
+		$mbflag = function_exists("mb_convert_encoding");
+
+		if (!$mbflag && is_null(self::$gnu_iconv)) {
+			$dummy = "a";
+			$ascii = 'us-ascii';
+			$func = set_error_handler("self::iconv_error");
+			$r = iconv($ascii, $ascii . "//ignore", $dummy);
+			restore_error_handler();
+			if ($r == $dummy) {
+				self::$gnu_iconv = true;
+			}
+		}
+
 		$this->_last_err = null;
 		set_error_handler(array(&$this, '_handleError'));
-		
-		if ($ignore_non_compatible) $out_enc .= "//IGNORE";
-		
-		$retval = iconv($in_enc, $out_enc, $str);
+
+		if(!$mbflag) {
+			if (self::$gnu_iconv)
+				if ($ignore_non_compatible) $out_enc .= "//IGNORE";
+			$retval = iconv($in_enc, $out_enc, $str);
+		} else { 
+			$retval = mb_convert_encoding($str, $out_enc, $in_enc);
+		}
 		
 		restore_error_handler();
 		if ($this->hasError()) {