-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUTF8_konversio.php
executable file
·171 lines (128 loc) · 6.37 KB
/
UTF8_konversio.php
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
<?php
// Kutsutaanko CLI:stä
$php_cli = FALSE;
if (php_sapi_name() == 'cli' or isset($editil_cli)) {
$php_cli = TRUE;
}
if (!$php_cli) {
echo "Voidaan ajaa vain komentoriviltä!!!\n";
die;
}
if (!`which recode`) {
echo "Tarvitaan recode -ohjelma!\n";
die;
}
$vain_recode = FALSE;
if (isset($argv[1]) and $argv[1] != '') {
$vain_recode = TRUE;
}
// Pupeasennuksen root
$pupe_root_polku = dirname(__FILE__);
require_once "inc/functions.inc";
// Konvertoidaan kaikki filet:
$files = listdir($pupe_root_polku);
$finfo1 = finfo_open(FILEINFO_MIME_TYPE);
$finfo2 = finfo_open(FILEINFO_MIME_ENCODING);
echo "Konvertoidaan Pupesoft UTF8 merkistöön";
foreach ($files as $file) {
if (strpos($file, "UTF8_konversio.php") !== FALSE or strpos($file, "import.class.php") !== FALSE) {
continue;
}
if (!is_writable($file)) {
continue;
}
$mime = finfo_file($finfo1, $file);
$encd = finfo_file($finfo2, $file);
if (substr($mime, 0, 4) == "text" and $encd != "utf-8" and $encd != "us-ascii") {
$mitenmeni = system("recode ISO_8859-1..UTF8 $file");
// echo "$file, $encd\n";
echo ".";
}
if ($vain_recode) {
continue;
}
$koodi = file_get_contents($file);
if (substr($koodi, 0, 5) == "<?php" or substr($koodi, 0, 14) == "#!/usr/bin/php") {
// Otetaan käyttöön prime ja doubleprime
$koodi = str_replace('{DOULEPRIME}', '″', $koodi);
$koodi = str_replace('{PRIME}', '′', $koodi);
// Vaihdetaan encoding
$koodi = str_replace('ln = "<?xml version=\"1.0\" encoding=\"ISO-8859-15\"?>', 'ln = "<?xml version=\"1.0\" encoding=\"utf-8\"?>', $koodi);
$koodi = str_replace("= '<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>';", "= '<?xml version=\"1.0\" encoding=\"utf-8\"?>';", $koodi);
// Muutetaan iconv-funkkarin parameja
$koodi = str_replace('iconv("ISO-8859-1", "Windows-1252"', 'iconv("UTF-8", "Windows-1252"', $koodi);
// PREG REPLACE --> {}-substringit
// Find: (\$[a-z0-9_]+?)\{([0-9]*)\}
// Replace: substr($1, $2, 1)
$koodi = preg_replace('/(\$[a-z0-9_]+?)\{([0-9]*)\}/i', 'substr($1, $2, 1)', $koodi);
// Etsitään laikki mysqli_set_charset-koodit
// Find: mysqli_set_charset\(["'][a-z0-9]*["']
// Replace: mysqli_set_charset("utf8"
$koodi = preg_replace('/mysqli_set_charset\(["\'][a-z0-9]*["\']/', 'mysqli_set_charset("utf8"', $koodi);
// Find: charset=iso-8859-15
// Find: charset=iso-8859-1
// Replace: charset=utf-8
$koodi = preg_replace('/charset=iso-8859-15?/i', 'charset=utf-8', $koodi);
$koodi = preg_replace('/charset=\\\\"iso-8859-15?\\\\"/i', 'charset=\"utf-8\"', $koodi);
$koodi = preg_replace('/charset = "iso-8859-15?"/i', 'charset = "utf-8"', $koodi);
// Find: (drawText.*?)\'LATIN1\'
// Replace: $1
$koodi = preg_replace('/(drawText.*?)\'LATIN1\'/', '$1\'UTF-8\'', $koodi);
// Find: (mb_encode_mimeheader\(.*?")ISO-8859-1"
// Replace: $1
$koodi = preg_replace('/(mb_encode_mimeheader\(.*?")ISO-8859-1"/', '$1UTF-8"', $koodi);
// Find: mysqli_query($XXXlink, "set group_concat_max_len=1000000");
// Replace: mysqli_query($XXXlink, "set collation_connection=\"utf8_unicode_ci\", collation_database=\"utf8_unicode_ci\", group_concat_max_len=1000000");
$koodi = preg_replace('/mysqli_query\($link, "set group_concat_max_len=1000000/', 'mysqli_query($link, "set collation_connection=\"utf8_unicode_ci\", collation_database=\"utf8_unicode_ci\", group_concat_max_len=1000000', $koodi);
// Vaihdetaan string-funkkarit mb_string-funkkareiks
if (strpos($file, "phppdflib.class.php") === FALSE) {
$rivit = explode("\n", $koodi);
$php = TRUE;
$jsc = FALSE;
foreach ($rivit as $rivinro => &$rivi) {
if (stripos($rivi, "<?php") !== FALSE and stripos($rivi, "?>") === FALSE and !preg_match('/<\?xml.*?\?>/i', $rivi)) {
$php = TRUE;
}
if (stripos($rivi, "<script") !== FALSE) {
$jsc = TRUE;
}
if (stripos($rivi, "<?php") === FALSE and stripos($rivi, "?>") !== FALSE and !preg_match('/<\?xml.*?\?>/i', $rivi)) {
$php = FALSE;
}
if (stripos($rivi, "</script") !== FALSE) {
$jsc = FALSE;
}
if ($php and !$jsc and stripos($rivi, "NO_MB_OVERLOAD") === FALSE) {
$rivi = preg_replace('/utf8_encode\(([^\)]+?)\)/', '$1', $rivi);
$rivi = preg_replace('/utf8_decode\(([^\)]+?)\)/', '$1', $rivi);
$rivi = preg_replace('/([^_])strlen ?\(/', '$1mb_strlen(', $rivi);
$rivi = preg_replace('/([^_])strpos ?\(/', '$1mb_strpos(', $rivi);
$rivi = preg_replace('/([^_])strrpos ?\(/', '$1mb_strrpos(', $rivi);
$rivi = preg_replace('/([^_])substr ?\(/', '$1mb_substr(', $rivi);
$rivi = preg_replace('/([^_])strtolower ?\(/', '$1mb_strtolower(', $rivi);
$rivi = preg_replace('/([^_])strtoupper ?\(/', '$1mb_strtoupper(', $rivi);
$rivi = preg_replace('/([^_])stripos ?\(/', '$1mb_stripos(', $rivi);
$rivi = preg_replace('/([^_])strripos ?\(/', '$1mb_strripos(', $rivi);
$rivi = preg_replace('/([^_])strstr ?\(/', '$1mb_strstr(', $rivi);
$rivi = preg_replace('/([^_])stristr ?\(/', '$1mb_stristr(', $rivi);
$rivi = preg_replace('/([^_])strrchr ?\(/', '$1mb_strrchr(', $rivi);
$rivi = preg_replace('/([^_])substr_count ?\(/', '$1mb_substr_count(', $rivi);
$rivi = preg_replace('/([^_])ereg ?\(/', '$1mb_ereg(', $rivi);
$rivi = preg_replace('/([^_])eregi ?\(/', '$1mb_eregi(', $rivi);
$rivi = preg_replace('/([^_])ereg_replace ?\(/', '$1mb_ereg_replace(', $rivi);
$rivi = preg_replace('/([^_])eregi_replace ?\(/', '$1mb_eregi_replace(', $rivi);
$rivi = preg_replace('/([^_])split ?\(/', '$1mb_split(', $rivi);
$rivi = str_replace('ÖÖÖÖÖÖÖÖÖÖÖÖÖÖÖÖÖÖÖÖÖÖ', 'ZZZZZZZZZZZZZZZZZZZZZZ', $rivi);
// Meidän omat multibytefunkkarit
$rivi = preg_replace('/([^_])str_pad ?\(/', '$1mb_str_pad(', $rivi);
$rivi = preg_replace('/([^_])str_split ?\(/', '$1mb_str_split(', $rivi);
}
}
$koodi = implode("\n", $rivit);
}
file_put_contents($file, $koodi);
}
}
finfo_close($finfo1);
finfo_close($finfo2);
echo ". Valmis!\n";