forked from yiisoft/yii
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtf8Command.php
196 lines (182 loc) · 4.41 KB
/
Utf8Command.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
<?php
/**
* Utf8Command class file.
*
* @author Qiang Xue <[email protected]>
* @link http://www.yiiframework.com/
* @copyright Copyright © 2008-2012 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
/**
* Utf8Command will help you to make sure files are encoded properly.
*
* @author Qiang Xue <[email protected]>
* @package system.build
* @since 1.1.11
*/
class Utf8Command extends CConsoleCommand
{
public function getHelp()
{
return <<<EOD
USAGE
yiic utf8 <action> <file>
DESCRIPTION
This command can detect and remove UTF-8 BOM headers. It also supports
detection of wrong file encodings (non UTF-8).
PARAMETERS
* action: required, the name of the action to execute. The following
actions are available:
- checkbom: checks for UTF-8 BOM header
- fixbom: removes UTF-8 BOM header
- checkencoding: checks for correct UTF-8 encoding
* file: optional, the file to process. If not set, all (!) translation files
will be processed.
EOD;
}
public function run($args)
{
if(!isset($args[0]))
$this->usageError("Please specify a valid action");
if(!in_array($args[0],array('checkbom','fixbom','checkencoding')))
$this->usageError("Invalid action '{$args[0]}' specified");
if(isset($args[1]) && !file_exists($args[1]))
$this->usageError("File '{$args[1]}' does not exist");
if('checkbom'===$args[0])
{
if(isset($args[1]))
{
if($this->checkBom($args[1]))
echo "UTF-8 BOM header detected";
else
echo "File seems to be clean";
}
else
{
$affectedFiles='';
foreach($this->findTranslationFiles() as $file)
{
if($this->checkBom($file))
$affectedFiles.="{$file}\r\n";
}
if(empty($affectedFiles))
echo "All files seem to be clean";
else
echo "Detected UTF-8 BOM header in the following files:\r\n".trim($affectedFiles);
}
}
elseif('fixbom'===$args[0])
{
if(isset($args[1]))
{
if(!$this->checkBom($args[1]))
echo "Nothing to fix, no UTF-8 BOM header detected";
else
{
$this->fixBom($args[1]);
echo "UTF-8 BOM header removed";
}
}
else
{
$affectedFiles='';
foreach($this->findTranslationFiles() as $file)
{
if($this->checkBom($file))
{
$affectedFiles.="{$file}\r\n";
$this->fixBom($file);
}
}
if(empty($affectedFiles))
echo "Nothing to fix, all files seem to be clean";
else
echo "Removed UTF-8 BOM header from the following files:\r\n".trim($affectedFiles);
}
}
elseif('checkencoding'===$args[0])
{
if(isset($args[1]))
{
if($this->checkEncoding($args[1]))
echo "File does have a correct UTF-8 encoding";
else
echo "Wrong encoding detected";
}
else
{
$affectedFiles='';
foreach($this->findTranslationFiles() as $file)
{
if(!$this->checkEncoding($file))
$affectedFiles.="{$file}\r\n";
}
if(empty($affectedFiles))
echo "All files seem to have the correct UTF-8 encoding";
else
echo "Wrong encoding of the following files detected:\r\n".trim($affectedFiles);
}
}
}
public function checkBom($file)
{
$data=file_get_contents($file,false,null,0,3);
return bin2hex($data)==='efbbbf';
}
public function fixBom($file)
{
$data=file_get_contents($file,false,null,3);
file_put_contents($file,$data);
}
public function checkEncoding($file)
{
$data=file_get_contents($file);
return preg_match('/./u',$data) && $this->is_utf8($data);
}
public function findTranslationFiles()
{
return CFileHelper::findFiles(
dirname(Yii::app()->basePath),
array(
'fileTypes' => array('txt', 'php'),
'exclude' => array(
'/index.php',
'/members.txt',
'/blog/source',
'/css',
'/framework',
'/guide/source',
'/protected',
'/requirements/views/source',
'/views/source',
),
)
);
}
/** php.net/manual/de/function.mb-detect-encoding.php#85294 */
public function is_utf8($str) {
$c=0; $b=0;
$bits=0;
$len=strlen($str);
for($i=0; $i<$len; $i++){
$c=ord($str[$i]);
if($c > 128){
if(($c >= 254)) return false;
elseif($c >= 252) $bits=6;
elseif($c >= 248) $bits=5;
elseif($c >= 240) $bits=4;
elseif($c >= 224) $bits=3;
elseif($c >= 192) $bits=2;
else return false;
if(($i+$bits) > $len) return false;
while($bits > 1){
$i++;
$b=ord($str[$i]);
if($b < 128 || $b > 191) return false;
$bits--;
}
}
}
return true;
}
}