Fix for the Open in New Window in Patient/Client->Patients search gui, take 2.
[openemr.git] / phpmyadmin / libraries / charset_conversion.lib.php
blob7adeb90ae6a44825933da11058e14419b89fcba3
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Charset conversion functions.
6 * @version $Id$
7 */
8 if (! defined('PHPMYADMIN')) {
9 exit;
12 /**
13 * Loads the recode or iconv extensions if any of it is not loaded yet
15 if (isset($cfg['AllowAnywhereRecoding'])
16 && $cfg['AllowAnywhereRecoding']
17 && $allow_recoding) {
19 if ($cfg['RecodingEngine'] == 'recode') {
20 if (!@extension_loaded('recode')) {
21 PMA_dl('recode');
22 if (!@extension_loaded('recode')) {
23 echo $strCantLoadRecodeIconv;
24 exit;
27 $PMA_recoding_engine = 'recode';
28 } elseif ($cfg['RecodingEngine'] == 'iconv') {
29 if (!@extension_loaded('iconv')) {
30 PMA_dl('iconv');
31 if (!@extension_loaded('iconv')) {
32 echo $strCantLoadRecodeIconv;
33 exit;
36 $PMA_recoding_engine = 'iconv';
37 } else {
38 if (@extension_loaded('iconv')) {
39 $PMA_recoding_engine = 'iconv';
40 } elseif (@extension_loaded('recode')) {
41 $PMA_recoding_engine = 'recode';
42 } else {
43 PMA_dl('iconv');
44 if (!@extension_loaded('iconv')) {
45 PMA_dl('recode');
46 if (!@extension_loaded('recode')) {
47 echo $strCantLoadRecodeIconv;
48 exit;
49 } else {
50 $PMA_recoding_engine = 'recode';
52 } else {
53 $PMA_recoding_engine = 'iconv';
57 } // end load recode/iconv extension
59 define('PMA_CHARSET_NONE', 0);
60 define('PMA_CHARSET_ICONV', 1);
61 define('PMA_CHARSET_LIBICONV', 2);
62 define('PMA_CHARSET_RECODE', 3);
63 define('PMA_CHARSET_ICONV_AIX', 4);
65 if (!isset($cfg['IconvExtraParams'])) {
66 $cfg['IconvExtraParams'] = '';
69 // Finally detects which function will we use:
70 if (isset($cfg['AllowAnywhereRecoding'])
71 && $cfg['AllowAnywhereRecoding']
72 && $allow_recoding) {
74 if (!isset($PMA_recoding_engine)) {
75 $PMA_recoding_engine = $cfg['RecodingEngine'];
77 if ($PMA_recoding_engine == 'iconv') {
78 if (@function_exists('iconv')) {
79 if ((@stristr(PHP_OS, 'AIX')) && (@strcasecmp(ICONV_IMPL, 'unknown') == 0) && (@strcasecmp(ICONV_VERSION, 'unknown') == 0)) {
80 $PMA_recoding_engine = PMA_CHARSET_ICONV_AIX;
81 } else {
82 $PMA_recoding_engine = PMA_CHARSET_ICONV;
84 } elseif (@function_exists('libiconv')) {
85 $PMA_recoding_engine = PMA_CHARSET_LIBICONV;
86 } else {
87 $PMA_recoding_engine = PMA_CHARSET_NONE;
89 if (!isset($GLOBALS['is_header_sent'])) {
90 include './libraries/header.inc.php';
92 echo $strCantUseRecodeIconv;
93 require_once './libraries/footer.inc.php';
94 exit();
96 } elseif ($PMA_recoding_engine == 'recode') {
97 if (@function_exists('recode_string')) {
98 $PMA_recoding_engine = PMA_CHARSET_RECODE;
99 } else {
100 $PMA_recoding_engine = PMA_CHARSET_NONE;
102 require_once './libraries/header.inc.php';
103 echo $strCantUseRecodeIconv;
104 require_once './libraries/footer.inc.php';
105 exit;
107 } else {
108 if (@function_exists('iconv')) {
109 if ((@stristr(PHP_OS, 'AIX')) && (@strcasecmp(ICONV_IMPL, 'unknown') == 0) && (@strcasecmp(ICONV_VERSION, 'unknown') == 0)) {
110 $PMA_recoding_engine = PMA_CHARSET_ICONV_AIX;
111 } else {
112 $PMA_recoding_engine = PMA_CHARSET_ICONV;
114 } elseif (@function_exists('libiconv')) {
115 $PMA_recoding_engine = PMA_CHARSET_LIBICONV;
116 } elseif (@function_exists('recode_string')) {
117 $PMA_recoding_engine = PMA_CHARSET_RECODE;
118 } else {
119 $PMA_recoding_engine = PMA_CHARSET_NONE;
121 require_once './libraries/header.inc.php';
122 echo $strCantUseRecodeIconv;
123 require_once './libraries/footer.inc.php';
124 exit;
127 } else {
128 $PMA_recoding_engine = PMA_CHARSET_NONE;
131 /* Load AIX iconv wrapper if needed */
132 if ($PMA_recoding_engine == PMA_CHARSET_ICONV_AIX) {
133 require_once './libraries/iconv_wrapper.lib.php';
137 * Converts encoding according to current settings.
139 * @param mixed what to convert (string or array of strings or object returned by mysql_fetch_field)
141 * @return string converted string or array of strings
143 * @global array the configuration array
144 * @global boolean whether recoding is allowed or not
145 * @global string the current charset
146 * @global array the charset to convert to
148 * @access public
150 * @author nijel
152 function PMA_convert_display_charset($what) {
153 global $cfg, $allow_recoding, $charset, $convcharset;
155 if (!(isset($cfg['AllowAnywhereRecoding']) && $cfg['AllowAnywhereRecoding'] && $allow_recoding)
156 || $convcharset == $charset // rabus: if input and output charset are the same, we don't have to do anything...
157 // this constant is not defined before the login:
158 || (defined('PMA_MYSQL_INT_VERSION') && PMA_MYSQL_INT_VERSION >= 40100)) { // lem9: even if AllowAnywhereRecoding is TRUE, do not recode for MySQL >= 4.1.x since MySQL does the job
159 return $what;
160 } elseif (is_array($what)) {
161 $result = array();
162 foreach ($what AS $key => $val) {
163 if (is_string($val) || is_array($val)) {
164 if (is_string($key)) {
165 $result[PMA_convert_display_charset($key)] = PMA_convert_display_charset($val);
166 } else {
167 $result[$key] = PMA_convert_display_charset($val);
169 } else {
170 $result[$key] = $val;
172 } // end while
173 return $result;
174 } elseif (is_string($what)) {
176 switch ($GLOBALS['PMA_recoding_engine']) {
177 case PMA_CHARSET_RECODE:
178 return recode_string($convcharset . '..' . $charset, $what);
179 case PMA_CHARSET_ICONV:
180 return iconv($convcharset, $charset . $cfg['IconvExtraParams'], $what);
181 case PMA_CHARSET_ICONV_AIX:
182 return PMA_aix_iconv_wrapper($convcharset, $charset . $cfg['IconvExtraParams'], $what);
183 case PMA_CHARSET_LIBICONV:
184 return libiconv($convcharset, $charset . $GLOBALS['cfg']['IconvExtraParams'], $what);
185 default:
186 return $what;
188 } elseif (is_object($what)) {
189 // isn't it object returned from mysql_fetch_field ?
190 if (@is_string($what->name)) {
191 $what->name = PMA_convert_display_charset($what->name);
193 if (@is_string($what->table)) {
194 $what->table = PMA_convert_display_charset($what->table);
196 if (@is_string($what->Database)) {
197 $what->Database = PMA_convert_display_charset($what->Database);
199 return $what;
200 } else {
201 // when we don't know what it is we don't touch it...
202 return $what;
204 } // end of the "PMA_convert_display_charset()" function
208 * Converts encoding of text according to current settings.
210 * @param string what to convert
212 * @return string converted text
214 * @global array the configuration array
215 * @global boolean whether recoding is allowed or not
216 * @global string the current charset
217 * @global array the charset to convert to
219 * @access public
221 * @author nijel
223 function PMA_convert_charset($what) {
224 global $cfg, $allow_recoding, $charset, $convcharset;
226 if (!(isset($cfg['AllowAnywhereRecoding']) && $cfg['AllowAnywhereRecoding'] && $allow_recoding)
227 || $convcharset == $charset) { // rabus: if input and output charset are the same, we don't have to do anything...
228 return $what;
229 } else {
230 switch ($GLOBALS['PMA_recoding_engine']) {
231 case PMA_CHARSET_RECODE:
232 return recode_string($charset . '..' . $convcharset, $what);
233 case PMA_CHARSET_ICONV:
234 return iconv($charset, $convcharset . $cfg['IconvExtraParams'], $what);
235 case PMA_CHARSET_ICONV_AIX:
236 return PMA_aix_iconv_wrapper($charset, $convcharset . $cfg['IconvExtraParams'], $what);
237 case PMA_CHARSET_LIBICONV:
238 return libiconv($charset, $convcharset . $GLOBALS['cfg']['IconvExtraParams'], $what);
239 default:
240 return $what;
243 } // end of the "PMA_convert_charset()" function
246 * Converts encoding of text according to parameters with detected
247 * conversion function.
249 * @param string source charset
250 * @param string target charset
251 * @param string what to convert
253 * @return string converted text
255 * @access public
257 * @author nijel
259 function PMA_convert_string($src_charset, $dest_charset, $what) {
260 if ($src_charset == $dest_charset) {
261 return $what;
263 switch ($GLOBALS['PMA_recoding_engine']) {
264 case PMA_CHARSET_RECODE:
265 return recode_string($src_charset . '..' . $dest_charset, $what);
266 case PMA_CHARSET_ICONV:
267 return iconv($src_charset, $dest_charset . $GLOBALS['cfg']['IconvExtraParams'], $what);
268 case PMA_CHARSET_ICONV_AIX:
269 return PMA_aix_iconv_wrapper($src_charset, $dest_charset . $GLOBALS['cfg']['IconvExtraParams'], $what);
270 case PMA_CHARSET_LIBICONV:
271 return libiconv($src_charset, $dest_charset, $what);
272 default:
273 return $what;
275 } // end of the "PMA_convert_string()" function
279 * Converts encoding of file according to parameters with detected
280 * conversion function. The old file will be unlinked and new created and
281 * its file name is returned.
283 * @param string source charset
284 * @param string target charset
285 * @param string file to convert
287 * @return string new temporay file
289 * @access public
291 * @author nijel
293 function PMA_convert_file($src_charset, $dest_charset, $file) {
294 switch ($GLOBALS['PMA_recoding_engine']) {
295 case PMA_CHARSET_RECODE:
296 case PMA_CHARSET_ICONV:
297 case PMA_CHARSET_LIBICONV:
298 $tmpfname = tempnam('', 'PMA_convert_file');
299 $fin = fopen($file, 'r');
300 $fout = fopen($tmpfname, 'w');
301 if ($GLOBALS['PMA_recoding_engine'] == PMA_CHARSET_RECODE) {
302 recode_file($src_charset . '..' . $dest_charset, $fin, $fout);
303 } else {
304 while (!feof($fin)) {
305 $line = fgets($fin, 4096);
306 if ($GLOBALS['PMA_recoding_engine'] == PMA_CHARSET_ICONV) {
307 $dist = iconv($src_charset, $dest_charset . $GLOBALS['cfg']['IconvExtraParams'], $line);
308 } elseif ($GLOBALS['PMA_recoding_engine'] == PMA_CHARSET_ICONV_AIX) {
309 $dist = PMA_aix_iconv_wrapper($src_charset, $dest_charset . $GLOBALS['cfg']['IconvExtraParams'], $line);
310 } else {
311 $dist = libiconv($src_charset, $dest_charset . $GLOBALS['cfg']['IconvExtraParams'], $line);
313 fputs($fout, $dist);
314 } // end while
316 fclose($fin);
317 fclose($fout);
318 unlink($file);
320 return $tmpfname;
321 default:
322 return $file;
324 } // end of the "PMA_convert_file()" function