diff e2gallerypro/e2upload/Backend/Assets/getid3/module.lib.iconv_replacement.php @ 3:3f6b44aa6b35 judyates

[svn r4] added ability to buy stuff, from a Prints page, but it doesn't work well with the css, and it also has not been fitted into the perl make system.
author rlm
date Mon, 22 Feb 2010 08:02:39 -0500
parents
children
line wrap: on
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/e2gallerypro/e2upload/Backend/Assets/getid3/module.lib.iconv_replacement.php	Mon Feb 22 08:02:39 2010 -0500
     1.3 @@ -0,0 +1,415 @@
     1.4 +<?php
     1.5 +// +----------------------------------------------------------------------+
     1.6 +// | PHP version 5                                                        |
     1.7 +// +----------------------------------------------------------------------+
     1.8 +// | Copyright (c) 2002-2006 James Heinrich, Allan Hansen                 |
     1.9 +// +----------------------------------------------------------------------+
    1.10 +// | This source file is subject to version 2 of the GPL license,         |
    1.11 +// | that is bundled with this package in the file license.txt and is     |
    1.12 +// | available through the world-wide-web at the following url:           |
    1.13 +// | http://www.gnu.org/copyleft/gpl.html                                 |
    1.14 +// +----------------------------------------------------------------------+
    1.15 +// | getID3() - http://getid3.sourceforge.net or http://www.getid3.org    |
    1.16 +// +----------------------------------------------------------------------+
    1.17 +// | Authors: James Heinrich <infoØgetid3*org>                            |
    1.18 +// |          Allan Hansen <ahØartemis*dk>                                |
    1.19 +// +----------------------------------------------------------------------+
    1.20 +// | module.lib.iconv_replacement.php                                     |
    1.21 +// | getID3() library file.                                               |
    1.22 +// | dependencies: NONE, required by getid3.php if no iconv() present.    |
    1.23 +// +----------------------------------------------------------------------+
    1.24 +//
    1.25 +// $Id: module.lib.iconv_replacement.php,v 1.4 2006/11/02 10:48:02 ah Exp $
    1.26 +
    1.27 +
    1.28 +class getid3_iconv_replacement
    1.29 +{
    1.30 +
    1.31 +    public static function iconv($in_charset, $out_charset, $string) {
    1.32 +
    1.33 +        if ($in_charset == $out_charset) {
    1.34 +            return $string;
    1.35 +        }
    1.36 +        
    1.37 +        static $supported_charsets = array (
    1.38 +            'ISO-8859-1'    => 'iso88591', 
    1.39 +            'UTF-8'         => 'utf8',
    1.40 +            'UTF-16BE'      => 'utf16be', 
    1.41 +            'UTF-16LE'      => 'utf16le', 
    1.42 +            'UTF-16'        => 'utf16'
    1.43 +        );
    1.44 +
    1.45 +        // Convert
    1.46 +        $function_name = 'iconv_' . @$supported_charsets[$in_charset] . '_' . @$supported_charsets[$out_charset];
    1.47 +        
    1.48 +        if (is_callable(array('getid3_iconv_replacement', $function_name))) {
    1.49 +            return getid3_iconv_replacement::$function_name($string);
    1.50 +        }
    1.51 +        
    1.52 +        // Invalid charset used
    1.53 +        if (!@$supported_charsets[$in_charset]) {
    1.54 +            throw new getid3_exception('PHP does not have iconv() support - cannot use ' . $in_charset . ' charset.');
    1.55 +        }
    1.56 +        
    1.57 +        if (!@$supported_charsets[$out_charset]) {
    1.58 +            throw new getid3_exception('PHP does not have iconv() support - cannot use ' . $out_charset . ' charset.');
    1.59 +        }
    1.60 +    }
    1.61 +
    1.62 +
    1.63 +
    1.64 +    public static function iconv_int_utf8($charval) {
    1.65 +        if ($charval < 128) {
    1.66 +            // 0bbbbbbb
    1.67 +            $newcharstring = chr($charval);
    1.68 +        } elseif ($charval < 2048) {
    1.69 +            // 110bbbbb 10bbbbbb
    1.70 +            $newcharstring  = chr(($charval >> 6) | 0xC0);
    1.71 +            $newcharstring .= chr(($charval & 0x3F) | 0x80);
    1.72 +        } elseif ($charval < 65536) {
    1.73 +            // 1110bbbb 10bbbbbb 10bbbbbb
    1.74 +            $newcharstring  = chr(($charval >> 12) | 0xE0);
    1.75 +            $newcharstring .= chr(($charval >>  6) | 0xC0);
    1.76 +            $newcharstring .= chr(($charval & 0x3F) | 0x80);
    1.77 +        } else {
    1.78 +            // 11110bbb 10bbbbbb 10bbbbbb 10bbbbbb
    1.79 +            $newcharstring  = chr(($charval >> 18) | 0xF0);
    1.80 +            $newcharstring .= chr(($charval >> 12) | 0xC0);
    1.81 +            $newcharstring .= chr(($charval >>  6) | 0xC0);
    1.82 +            $newcharstring .= chr(($charval & 0x3F) | 0x80);
    1.83 +        }
    1.84 +        return $newcharstring;
    1.85 +    }
    1.86 +
    1.87 +
    1.88 +
    1.89 +    // ISO-8859-1 => UTF-8
    1.90 +    public static function iconv_iso88591_utf8($string, $bom=false) {
    1.91 +        if (function_exists('utf8_encode')) {
    1.92 +            return utf8_encode($string);
    1.93 +        }
    1.94 +        // utf8_encode() unavailable, use getID3()'s iconv() conversions (possibly PHP is compiled without XML support)
    1.95 +        $newcharstring = '';
    1.96 +        if ($bom) {
    1.97 +            $newcharstring .= "\xEF\xBB\xBF";
    1.98 +        }
    1.99 +        for ($i = 0; $i < strlen($string); $i++) {
   1.100 +            $charval = ord($string{$i});
   1.101 +            $newcharstring .= getid3_iconv_replacement::iconv_int_utf8($charval);
   1.102 +        }
   1.103 +        return $newcharstring;
   1.104 +    }
   1.105 +
   1.106 +
   1.107 +
   1.108 +    // ISO-8859-1 => UTF-16BE
   1.109 +    public static function iconv_iso88591_utf16be($string, $bom=false) {
   1.110 +        $newcharstring = '';
   1.111 +        if ($bom) {
   1.112 +            $newcharstring .= "\xFE\xFF";
   1.113 +        }
   1.114 +        for ($i = 0; $i < strlen($string); $i++) {
   1.115 +            $newcharstring .= "\x00".$string{$i};
   1.116 +        }
   1.117 +        return $newcharstring;
   1.118 +    }
   1.119 +
   1.120 +
   1.121 +
   1.122 +    // ISO-8859-1 => UTF-16LE
   1.123 +    public static function iconv_iso88591_utf16le($string, $bom=false) {
   1.124 +        $newcharstring = '';
   1.125 +        if ($bom) {
   1.126 +            $newcharstring .= "\xFF\xFE";
   1.127 +        }
   1.128 +        for ($i = 0; $i < strlen($string); $i++) {
   1.129 +            $newcharstring .= $string{$i}."\x00";
   1.130 +        }
   1.131 +        return $newcharstring;
   1.132 +    }
   1.133 +
   1.134 +
   1.135 +
   1.136 +    // ISO-8859-1 => UTF-16
   1.137 +    public static function iconv_iso88591_utf16($string) {
   1.138 +        return getid3_lib::iconv_iso88591_utf16le($string, true);
   1.139 +    }
   1.140 +
   1.141 +
   1.142 +
   1.143 +    // UTF-8 => ISO-8859-1
   1.144 +    public static function iconv_utf8_iso88591($string) {
   1.145 +        if (function_exists('utf8_decode')) {
   1.146 +            return utf8_decode($string);
   1.147 +        }
   1.148 +        // utf8_decode() unavailable, use getID3()'s iconv() conversions (possibly PHP is compiled without XML support)
   1.149 +        $newcharstring = '';
   1.150 +        $offset = 0;
   1.151 +        $stringlength = strlen($string);
   1.152 +        while ($offset < $stringlength) {
   1.153 +            if ((ord($string{$offset}) | 0x07) == 0xF7) {
   1.154 +                // 11110bbb 10bbbbbb 10bbbbbb 10bbbbbb
   1.155 +                $charval = ((ord($string{($offset + 0)}) & 0x07) << 18) &
   1.156 +                           ((ord($string{($offset + 1)}) & 0x3F) << 12) &
   1.157 +                           ((ord($string{($offset + 2)}) & 0x3F) <<  6) &
   1.158 +                            (ord($string{($offset + 3)}) & 0x3F);
   1.159 +                $offset += 4;
   1.160 +            } elseif ((ord($string{$offset}) | 0x0F) == 0xEF) {
   1.161 +                // 1110bbbb 10bbbbbb 10bbbbbb
   1.162 +                $charval = ((ord($string{($offset + 0)}) & 0x0F) << 12) &
   1.163 +                           ((ord($string{($offset + 1)}) & 0x3F) <<  6) &
   1.164 +                            (ord($string{($offset + 2)}) & 0x3F);
   1.165 +                $offset += 3;
   1.166 +            } elseif ((ord($string{$offset}) | 0x1F) == 0xDF) {
   1.167 +                // 110bbbbb 10bbbbbb
   1.168 +                $charval = ((ord($string{($offset + 0)}) & 0x1F) <<  6) &
   1.169 +                            (ord($string{($offset + 1)}) & 0x3F);
   1.170 +                $offset += 2;
   1.171 +            } elseif ((ord($string{$offset}) | 0x7F) == 0x7F) {
   1.172 +                // 0bbbbbbb
   1.173 +                $charval = ord($string{$offset});
   1.174 +                $offset += 1;
   1.175 +            } else {
   1.176 +                // error? throw some kind of warning here?
   1.177 +                $charval = false;
   1.178 +                $offset += 1;
   1.179 +            }
   1.180 +            if ($charval !== false) {
   1.181 +                $newcharstring .= (($charval < 256) ? chr($charval) : '?');
   1.182 +            }
   1.183 +        }
   1.184 +        return $newcharstring;
   1.185 +    }
   1.186 +
   1.187 +
   1.188 +
   1.189 +    // UTF-8 => UTF-16BE
   1.190 +    public static function iconv_utf8_utf16be($string, $bom=false) {
   1.191 +        $newcharstring = '';
   1.192 +        if ($bom) {
   1.193 +            $newcharstring .= "\xFE\xFF";
   1.194 +        }
   1.195 +        $offset = 0;
   1.196 +        $stringlength = strlen($string);
   1.197 +        while ($offset < $stringlength) {
   1.198 +            if ((ord($string{$offset}) | 0x07) == 0xF7) {
   1.199 +                // 11110bbb 10bbbbbb 10bbbbbb 10bbbbbb
   1.200 +                $charval = ((ord($string{($offset + 0)}) & 0x07) << 18) &
   1.201 +                           ((ord($string{($offset + 1)}) & 0x3F) << 12) &
   1.202 +                           ((ord($string{($offset + 2)}) & 0x3F) <<  6) &
   1.203 +                            (ord($string{($offset + 3)}) & 0x3F);
   1.204 +                $offset += 4;
   1.205 +            } elseif ((ord($string{$offset}) | 0x0F) == 0xEF) {
   1.206 +                // 1110bbbb 10bbbbbb 10bbbbbb
   1.207 +                $charval = ((ord($string{($offset + 0)}) & 0x0F) << 12) &
   1.208 +                           ((ord($string{($offset + 1)}) & 0x3F) <<  6) &
   1.209 +                            (ord($string{($offset + 2)}) & 0x3F);
   1.210 +                $offset += 3;
   1.211 +            } elseif ((ord($string{$offset}) | 0x1F) == 0xDF) {
   1.212 +                // 110bbbbb 10bbbbbb
   1.213 +                $charval = ((ord($string{($offset + 0)}) & 0x1F) <<  6) &
   1.214 +                            (ord($string{($offset + 1)}) & 0x3F);
   1.215 +                $offset += 2;
   1.216 +            } elseif ((ord($string{$offset}) | 0x7F) == 0x7F) {
   1.217 +                // 0bbbbbbb
   1.218 +                $charval = ord($string{$offset});
   1.219 +                $offset += 1;
   1.220 +            } else {
   1.221 +                // error? throw some kind of warning here?
   1.222 +                $charval = false;
   1.223 +                $offset += 1;
   1.224 +            }
   1.225 +            if ($charval !== false) {
   1.226 +                $newcharstring .= (($charval < 65536) ? getid3_lib::BigEndian2String($charval, 2) : "\x00".'?');
   1.227 +            }
   1.228 +        }
   1.229 +        return $newcharstring;
   1.230 +    }
   1.231 +
   1.232 +
   1.233 +
   1.234 +    // UTF-8 => UTF-16LE
   1.235 +    public static function iconv_utf8_utf16le($string, $bom=false) {
   1.236 +        $newcharstring = '';
   1.237 +        if ($bom) {
   1.238 +            $newcharstring .= "\xFF\xFE";
   1.239 +        }
   1.240 +        $offset = 0;
   1.241 +        $stringlength = strlen($string);
   1.242 +        while ($offset < $stringlength) {
   1.243 +            if ((ord($string{$offset}) | 0x07) == 0xF7) {
   1.244 +                // 11110bbb 10bbbbbb 10bbbbbb 10bbbbbb
   1.245 +                $charval = ((ord($string{($offset + 0)}) & 0x07) << 18) &
   1.246 +                           ((ord($string{($offset + 1)}) & 0x3F) << 12) &
   1.247 +                           ((ord($string{($offset + 2)}) & 0x3F) <<  6) &
   1.248 +                            (ord($string{($offset + 3)}) & 0x3F);
   1.249 +                $offset += 4;
   1.250 +            } elseif ((ord($string{$offset}) | 0x0F) == 0xEF) {
   1.251 +                // 1110bbbb 10bbbbbb 10bbbbbb
   1.252 +                $charval = ((ord($string{($offset + 0)}) & 0x0F) << 12) &
   1.253 +                           ((ord($string{($offset + 1)}) & 0x3F) <<  6) &
   1.254 +                            (ord($string{($offset + 2)}) & 0x3F);
   1.255 +                $offset += 3;
   1.256 +            } elseif ((ord($string{$offset}) | 0x1F) == 0xDF) {
   1.257 +                // 110bbbbb 10bbbbbb
   1.258 +                $charval = ((ord($string{($offset + 0)}) & 0x1F) <<  6) &
   1.259 +                            (ord($string{($offset + 1)}) & 0x3F);
   1.260 +                $offset += 2;
   1.261 +            } elseif ((ord($string{$offset}) | 0x7F) == 0x7F) {
   1.262 +                // 0bbbbbbb
   1.263 +                $charval = ord($string{$offset});
   1.264 +                $offset += 1;
   1.265 +            } else {
   1.266 +                // error? maybe throw some warning here?
   1.267 +                $charval = false;
   1.268 +                $offset += 1;
   1.269 +            }
   1.270 +            if ($charval !== false) {
   1.271 +                $newcharstring .= (($charval < 65536) ? getid3_lib::LittleEndian2String($charval, 2) : '?'."\x00");
   1.272 +            }
   1.273 +        }
   1.274 +        return $newcharstring;
   1.275 +    }
   1.276 +
   1.277 +    
   1.278 +    
   1.279 +    // UTF-8 => UTF-16
   1.280 +    public static function iconv_utf8_utf16($string) {
   1.281 +        return getid3_lib::iconv_utf8_utf16le($string, true);
   1.282 +    }
   1.283 +
   1.284 +    
   1.285 +    
   1.286 +    // UTF-16BE => ISO-8859-1
   1.287 +    public static function iconv_utf16be_iso88591($string) {
   1.288 +        if (substr($string, 0, 2) == "\xFE\xFF") {
   1.289 +            // strip BOM
   1.290 +            $string = substr($string, 2);
   1.291 +        }
   1.292 +        $newcharstring = '';
   1.293 +        for ($i = 0; $i < strlen($string); $i += 2) {
   1.294 +            $charval = getid3_lib::BigEndian2Int(substr($string, $i, 2));
   1.295 +            $newcharstring .= (($charval < 256) ? chr($charval) : '?');
   1.296 +        }
   1.297 +        return $newcharstring;
   1.298 +    }
   1.299 +
   1.300 +    
   1.301 +    
   1.302 +    // UTF-16BE => UTF-8
   1.303 +    public static function iconv_utf16be_utf8($string) {
   1.304 +        if (substr($string, 0, 2) == "\xFE\xFF") {
   1.305 +            // strip BOM
   1.306 +            $string = substr($string, 2);
   1.307 +        }
   1.308 +        $newcharstring = '';
   1.309 +        for ($i = 0; $i < strlen($string); $i += 2) {
   1.310 +            $charval = getid3_lib::BigEndian2Int(substr($string, $i, 2));
   1.311 +            $newcharstring .= getid3_iconv_replacement::iconv_int_utf8($charval);
   1.312 +        }
   1.313 +        return $newcharstring;
   1.314 +    }
   1.315 +    
   1.316 +    
   1.317 +    
   1.318 +    // UTF-16BE => UTF-16LE
   1.319 +    public static function iconv_utf16be_utf16le($string) {
   1.320 +        return getid3_iconv_replacement::iconv_utf8_utf16le(getid3_iconv_replacement::iconv_utf16be_utf8($string));
   1.321 +    }
   1.322 +    
   1.323 +    
   1.324 +    
   1.325 +    // UTF-16BE => UTF-16
   1.326 +    public static function iconv_utf16be_utf16($string) {
   1.327 +        return getid3_iconv_replacement::iconv_utf8_utf16(getid3_iconv_replacement::iconv_utf16be_utf8($string));
   1.328 +    }
   1.329 +    
   1.330 +    
   1.331 +    
   1.332 +    // UTF-16LE => ISO-8859-1
   1.333 +    public static function iconv_utf16le_iso88591($string) {
   1.334 +        if (substr($string, 0, 2) == "\xFF\xFE") {
   1.335 +            // strip BOM
   1.336 +            $string = substr($string, 2);
   1.337 +        }
   1.338 +        $newcharstring = '';
   1.339 +        for ($i = 0; $i < strlen($string); $i += 2) {
   1.340 +            $charval = getid3_lib::LittleEndian2Int(substr($string, $i, 2));
   1.341 +            $newcharstring .= (($charval < 256) ? chr($charval) : '?');
   1.342 +        }
   1.343 +        return $newcharstring;
   1.344 +    }
   1.345 +
   1.346 +    
   1.347 +    
   1.348 +    // UTF-16LE => UTF-8
   1.349 +    public static function iconv_utf16le_utf8($string) {
   1.350 +        if (substr($string, 0, 2) == "\xFF\xFE") {
   1.351 +            // strip BOM
   1.352 +            $string = substr($string, 2);
   1.353 +        }
   1.354 +        $newcharstring = '';
   1.355 +        for ($i = 0; $i < strlen($string); $i += 2) {
   1.356 +            $charval = getid3_lib::LittleEndian2Int(substr($string, $i, 2));
   1.357 +            $newcharstring .= getid3_iconv_replacement::iconv_int_utf8($charval);
   1.358 +        }
   1.359 +        return $newcharstring;
   1.360 +    }
   1.361 +
   1.362 +    
   1.363 +    
   1.364 +    // UTF-16LE => UTF-16BE
   1.365 +    public static function iconv_utf16le_utf16be($string) {
   1.366 +        return getid3_iconv_replacement::iconv_utf8_utf16be(getid3_iconv_replacement::iconv_utf16le_utf8($string));
   1.367 +    }
   1.368 +    
   1.369 +    
   1.370 +    
   1.371 +    // UTF-16LE => UTF-16
   1.372 +    public static function iconv_utf16le_utf16($string) {
   1.373 +        return getid3_iconv_replacement::iconv_utf8_utf16(getid3_iconv_replacement::iconv_utf16le_utf8($string));
   1.374 +    }
   1.375 +    
   1.376 +    
   1.377 +    
   1.378 +    // UTF-16 => ISO-8859-1
   1.379 +    public static function iconv_utf16_iso88591($string) {
   1.380 +        $bom = substr($string, 0, 2);
   1.381 +        if ($bom == "\xFE\xFF") {
   1.382 +            return getid3_lib::iconv_utf16be_iso88591(substr($string, 2));
   1.383 +        } elseif ($bom == "\xFF\xFE") {
   1.384 +            return getid3_lib::iconv_utf16le_iso88591(substr($string, 2));
   1.385 +        }
   1.386 +        return $string;
   1.387 +    }
   1.388 +
   1.389 +    
   1.390 +    
   1.391 +    // UTF-16 => UTF-8
   1.392 +    public static function iconv_utf16_utf8($string) {
   1.393 +        $bom = substr($string, 0, 2);
   1.394 +        if ($bom == "\xFE\xFF") {
   1.395 +            return getid3_iconv_replacement::iconv_utf16be_utf8(substr($string, 2));
   1.396 +        } elseif ($bom == "\xFF\xFE") {
   1.397 +            return getid3_iconv_replacement::iconv_utf16le_utf8(substr($string, 2));
   1.398 +        }
   1.399 +        return $string;
   1.400 +    }
   1.401 +    
   1.402 +    
   1.403 +    
   1.404 +    // UTF-16 => UTF-16BE
   1.405 +    public static function iconv_utf16_utf16be($string) {
   1.406 +        return getid3_iconv_replacement::iconv_utf8_utf16be(getid3_iconv_replacement::iconv_utf16_utf8($string));
   1.407 +    }
   1.408 +    
   1.409 +    
   1.410 +    
   1.411 +    // UTF-16 => UTF-16LE
   1.412 +    public static function iconv_utf16_utf16le($string) {
   1.413 +        return getid3_iconv_replacement::iconv_utf8_utf16le(getid3_iconv_replacement::iconv_utf16_utf8($string));
   1.414 +    }
   1.415 +
   1.416 +}
   1.417 +
   1.418 +?>
   1.419 \ No newline at end of file