Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

file 173 lines (158 sloc) 3.846 kb
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 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
<?php

require_once '../SimplePieAutoloader.php';

function normalize_character_set($charset)
{
return strtolower(preg_replace('/(?:[^a-zA-Z0-9]+|([^0-9])0+)/', '\1', $charset));
}

function build_character_set_list()
{
$file = new SimplePie_File('http://www.iana.org/assignments/character-sets');
if (!$file->success && !($file->method & SIMPLEPIE_FILE_SOURCE_REMOTE === 0 || ($file->status_code === 200 || $file->status_code > 206 && $file->status_code < 300)))
{
return false;
}
else
{
$data = explode("\n", $file->body);
unset($file);

foreach ($data as $line)
{
// New character set
if (preg_match('/^Name:\s+(\S+)/', $line, $match))
{
// If we already have one, push it on to the array
if (isset($aliases))
{
foreach ($aliases as &$alias)
{
$alias = normalize_character_set($alias);
}
$charsets[$preferred] = array_unique($aliases);
natsort($charsets[$preferred]);
}

$aliases = array($match[1]);
$preferred = $match[1];
}
// Another alias
elseif (preg_match('/^Alias:\s+(\S+)(\s+\(preferred MIME name\))?\s*$/', $line, $match))
{
if ($match[1] !== 'None')
{
$aliases[] = $match[1];
if (isset($match[2]))
{
$preferred = $match[1];
}
}
}
}

// Compatibility replacements
// From http://www.whatwg.org/specs/web-apps/current-work/multipage/parsing.html#misinterpreted-for-compatibility
$compat = array(
'EUC-KR' => 'windows-949',
'GB2312' => 'GBK',
'GB_2312-80' => 'GBK',
'ISO-8859-1' => 'windows-1252',
'ISO-8859-9' => 'windows-1254',
'ISO-8859-11' => 'windows-874',
'KS_C_5601-1987' => 'windows-949',
'Shift_JIS' => 'Windows-31J',
'TIS-620' => 'windows-874',
//'US-ASCII' => 'windows-1252',
);

foreach ($compat as $real => $replace)
{
if (isset($charsets[$real]) && isset($charsets[$replace]))
{
$charsets[$replace] = array_merge($charsets[$replace], $charsets[$real]);
unset($charsets[$real]);
}
elseif (isset($charsets[$real]))
{
$charsets[$replace] = $charsets[$real];
$charsets[$replace][] = normalize_character_set($replace);
unset($charsets[$real]);
}
else
{
$charsets[$replace][] = normalize_character_set($real);
}
$charsets[$replace] = array_unique($charsets[$replace]);
natsort($charsets[$replace]);
}

// Sort it
uksort($charsets, 'strnatcasecmp');

// Check that nothing matches more than one
$all = call_user_func_array('array_merge', $charsets);
$all_count = array_count_values($all);
if (max($all_count) > 1)
{
echo "Duplicated charsets:\n";
foreach ($all_count as $charset => $count)
{
if ($count > 1)
{
echo "$charset\n";
}
}
}

// And we're done!
return $charsets;
}
}

function charset($charset)
{
$normalized_charset = normalize_character_set($charset);
if ($charsets = build_character_set_list())
{
foreach ($charsets as $preferred => $aliases)
{
if (in_array($normalized_charset, $aliases))
{
return $preferred;
}
}
return $charset;
}
else
{
return false;
}
}

function build_function()
{
if ($charsets = build_character_set_list())
{
$return = <<<EOF
public static function encoding(\$charset)
{
// Normalization from UTS #22
switch (strtolower(preg_replace('/(?:[^a-zA-Z0-9]+|([^0-9])0+)/', '\\1', \$charset)))
{

EOF;
foreach ($charsets as $preferred => $aliases)
{
foreach ($aliases as $alias)
{
$return .= "\t\tcase " . var_export($alias, true) . ":\n";
}
$return .= "\t\t\treturn " . var_export($preferred, true) . ";\n\n";
}
$return .= <<<EOF
default:
return \$charset;
}
}
EOF;
return $return;
}
else
{
return false;
}
}

if (php_sapi_name() === 'cli' && realpath($_SERVER['argv'][0]) === __FILE__)
{
echo build_function();
}

?>
Something went wrong with that request. Please try again.