-
Notifications
You must be signed in to change notification settings - Fork 0
/
language_check_and_clean.php
executable file
·179 lines (156 loc) · 4.73 KB
/
language_check_and_clean.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
172
173
174
175
176
177
178
179
#!/usr/bin/php
<?php
// CodeIgniter language check&clean v1.0
define('BASEPATH', TRUE);
define('LANG_DIR', './system/language');
define('LANG_DEFAULT', 'english');
define('LANG_CANSAVE', 'polish');
define('EXT_OLD', '.bak');
define('COL_ERR', "\033[31m");
define('COL_INF', "\033[36m");
define('COL_DEF', "\033[0m");
define('EOL', "\n");
define('SLASH', '/');
define('TPL_HEADER', "<?php
/**
* CodeIgniter Polish language pack
*
* @package CodeIgniter
* @author [email protected] (originally [email protected])
* @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
* @link https://github.com/patwork/CodeIgniter-PL
* @since Version 1.0
*/
defined('BASEPATH') OR exit('No direct script access allowed');".EOL.EOL);
define('TPL_FOOTER', EOL."/* End of file %s */".EOL."/* Location: %s */".EOL);
define('TPL_LINE', "\$lang['%s'] %s= '%s';".EOL);
// ==========================================================================
function cleanstring($str)
{
return trim(preg_replace('/\s\s+/', ' ', str_replace("'", "\\'", $str)));
}
// ==========================================================================
function loadlang($filename)
{
$lang = array();
include $filename;
if (empty($lang)) {
echo COL_ERR.'! '.$filename.' is empty or damaged'.COL_DEF.EOL;
}
return $lang;
}
// ==========================================================================
function savelang($default, $check, $filename)
{
$maxkeylen = 0;
foreach($default as $key => $val) {
if (strlen($key) > $maxkeylen) {
$maxkeylen = strlen($key);
}
}
$tmpname = tempnam('.', '.clean');
$tmp = fopen($tmpname, 'w');
fwrite($tmp, TPL_HEADER);
foreach($default as $key => $val) {
fwrite($tmp, sprintf(TPL_LINE, $key, str_repeat(' ', $maxkeylen - strlen($key)), cleanstring($check[$key])));
}
fwrite($tmp, sprintf(TPL_FOOTER, basename($filename), $filename));
fclose($tmp);
$oldname = $filename.EXT_OLD;
if (file_exists($oldname)) {
unlink($oldname);
}
rename($filename, $oldname);
rename($tmpname, $filename);
chmod($filename, fileperms($oldname));
}
// ==========================================================================
function comparelang($default, $check)
{
$status = TRUE;
foreach($default as $key => $val) {
if (!isset($check[$key])) {
echo COL_ERR.'! missing translation of "'.$key.'" :: '.$val.COL_DEF.EOL;
$status = FALSE;
} else {
unset($check[$key]);
}
}
if (!empty($check)) {
foreach($check as $key => $val) {
echo COL_ERR.'! obsolete translation of "'.$key.'" :: '.$val.COL_DEF.EOL;
}
$status = FALSE;
}
return $status;
}
// ==========================================================================
function checkfile($dir, $filename)
{
echo '* '.$filename.EOL;
if (!file_exists(LANG_DIR.SLASH.LANG_DEFAULT.SLASH.$filename)) {
echo COL_ERR.'! missing '.LANG_DEFAULT.' language file '.$filename.COL_DEF.EOL;
return;
}
$lang_default = loadlang(LANG_DIR.SLASH.LANG_DEFAULT.SLASH.$filename);
$lang_check = loadlang(LANG_DIR.SLASH.$dir.SLASH.$filename);
if (empty($lang_default) || empty($lang_check)) {
return;
}
if (comparelang($lang_default, $lang_check) && $dir === LANG_CANSAVE) {
savelang($lang_default, $lang_check, LANG_DIR.SLASH.$dir.SLASH.$filename);
}
}
// ==========================================================================
function checklang($dir, $defaults)
{
echo COL_INF.'* checking '.$dir.' language...'.COL_DEF.EOL;
$dh = opendir(LANG_DIR.SLASH.$dir);
while(($filename = readdir($dh)) !== FALSE) {
if (strtolower(substr($filename, -4)) === '.php') {
unset($defaults[$filename]);
checkfile($dir, $filename);
}
}
closedir($dh);
if (!empty($defaults)) {
foreach($defaults as $key => $val) {
echo COL_ERR.'! missing translation of file '.$key.COL_DEF.EOL;
}
}
}
// ==========================================================================
function runme()
{
if (!is_dir(LANG_DIR)) {
echo COL_ERR.'! directory '.LANG_DIR.' not found!'.COL_DEF.EOL;
return 1;
}
if (!is_dir(LANG_DIR.SLASH.LANG_DEFAULT)) {
echo COL_ERR.'! '.LANG_DEFAULT.' language directory not found!'.COL_DEF.EOL;
return 2;
}
$defaults = array();
$dh = opendir(LANG_DIR.SLASH.LANG_DEFAULT);
while(($filename = readdir($dh)) !== FALSE) {
if (strtolower(substr($filename, -4)) === '.php') {
$defaults[$filename] = TRUE;
}
}
closedir($dh);
if (empty($defaults)) {
echo COL_ERR.'! '.LANG_DEFAULT.' language directory is empty!'.COL_DEF.EOL;
return 3;
}
$dh = opendir(LANG_DIR);
while(($dirname = readdir($dh)) !== FALSE) {
if (is_dir(LANG_DIR.SLASH.$dirname) && $dirname[0] != '.' && $dirname != LANG_DEFAULT) {
checklang($dirname, $defaults);
}
}
closedir($dh);
return 0;
}
// ==========================================================================
exit(runme());
// EoF