CodeSwitch.class.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2009 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: liu21st <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. namespace Org\Util;
  12. class CodeSwitch {
  13. // 错误信息
  14. static private $error = array();
  15. // 提示信息
  16. static private $info = array();
  17. // 记录错误
  18. static private function error($msg) {
  19. self::$error[] = $msg;
  20. }
  21. // 记录信息
  22. static private function info($info) {
  23. self::$info[] = $info;
  24. }
  25. /**
  26. * 编码转换函数,对整个文件进行编码转换
  27. * 支持以下转换
  28. * GB2312、UTF-8 WITH BOM转换为UTF-8
  29. * UTF-8、UTF-8 WITH BOM转换为GB2312
  30. * @access public
  31. * @param string $filename 文件名
  32. * @param string $out_charset 转换后的文件编码,与iconv使用的参数一致
  33. * @return void
  34. */
  35. static function DetectAndSwitch($filename,$out_charset) {
  36. $fpr = fopen($filename,"r");
  37. $char1 = fread($fpr,1);
  38. $char2 = fread($fpr,1);
  39. $char3 = fread($fpr,1);
  40. $originEncoding = "";
  41. if($char1==chr(239) && $char2==chr(187) && $char3==chr(191))//UTF-8 WITH BOM
  42. $originEncoding = "UTF-8 WITH BOM";
  43. elseif($char1==chr(255) && $char2==chr(254))//UNICODE LE
  44. {
  45. self::error("不支持从UNICODE LE转换到UTF-8或GB编码");
  46. fclose($fpr);
  47. return;
  48. }elseif($char1==chr(254) && $char2==chr(255)){//UNICODE BE
  49. self::error("不支持从UNICODE BE转换到UTF-8或GB编码");
  50. fclose($fpr);
  51. return;
  52. }else{//没有文件头,可能是GB或UTF-8
  53. if(rewind($fpr)===false){//回到文件开始部分,准备逐字节读取判断编码
  54. self::error($filename."文件指针后移失败");
  55. fclose($fpr);
  56. return;
  57. }
  58. while(!feof($fpr)){
  59. $char = fread($fpr,1);
  60. //对于英文,GB和UTF-8都是单字节的ASCII码小于128的值
  61. if(ord($char)<128)
  62. continue;
  63. //对于汉字GB编码第一个字节是110*****第二个字节是10******(有特例,比如联字)
  64. //UTF-8编码第一个字节是1110****第二个字节是10******第三个字节是10******
  65. //按位与出来结果要跟上面非星号相同,所以应该先判断UTF-8
  66. //因为使用GB的掩码按位与,UTF-8的111得出来的也是110,所以要先判断UTF-8
  67. if((ord($char)&224)==224) {
  68. //第一个字节判断通过
  69. $char = fread($fpr,1);
  70. if((ord($char)&128)==128) {
  71. //第二个字节判断通过
  72. $char = fread($fpr,1);
  73. if((ord($char)&128)==128) {
  74. $originEncoding = "UTF-8";
  75. break;
  76. }
  77. }
  78. }
  79. if((ord($char)&192)==192) {
  80. //第一个字节判断通过
  81. $char = fread($fpr,1);
  82. if((ord($char)&128)==128) {
  83. //第二个字节判断通过
  84. $originEncoding = "GB2312";
  85. break;
  86. }
  87. }
  88. }
  89. }
  90. if(strtoupper($out_charset)==$originEncoding) {
  91. self::info("文件".$filename."转码检查完成,原始文件编码".$originEncoding);
  92. fclose($fpr);
  93. }else {
  94. //文件需要转码
  95. $originContent = "";
  96. if($originEncoding == "UTF-8 WITH BOM") {
  97. //跳过三个字节,把后面的内容复制一遍得到utf-8的内容
  98. fseek($fpr,3);
  99. $originContent = fread($fpr,filesize($filename)-3);
  100. fclose($fpr);
  101. }elseif(rewind($fpr)!=false){//不管是UTF-8还是GB2312,回到文件开始部分,读取内容
  102. $originContent = fread($fpr,filesize($filename));
  103. fclose($fpr);
  104. }else{
  105. self::error("文件编码不正确或指针后移失败");
  106. fclose($fpr);
  107. return;
  108. }
  109. //转码并保存文件
  110. $content = iconv(str_replace(" WITH BOM","",$originEncoding),strtoupper($out_charset),$originContent);
  111. $fpw = fopen($filename,"w");
  112. fwrite($fpw,$content);
  113. fclose($fpw);
  114. if($originEncoding!="")
  115. self::info("对文件".$filename."转码完成,原始文件编码".$originEncoding.",转换后文件编码".strtoupper($out_charset));
  116. elseif($originEncoding=="")
  117. self::info("文件".$filename."中没有出现中文,但是可以断定不是带BOM的UTF-8编码,没有进行编码转换,不影响使用");
  118. }
  119. }
  120. /**
  121. * 目录遍历函数
  122. * @access public
  123. * @param string $path 要遍历的目录名
  124. * @param string $mode 遍历模式,一般取FILES,这样只返回带路径的文件名
  125. * @param array $file_types 文件后缀过滤数组
  126. * @param int $maxdepth 遍历深度,-1表示遍历到最底层
  127. * @return void
  128. */
  129. static function searchdir($path,$mode = "FULL",$file_types = array(".html",".php"),$maxdepth = -1,$d = 0) {
  130. if(substr($path,strlen($path)-1) != '/')
  131. $path .= '/';
  132. $dirlist = array();
  133. if($mode != "FILES")
  134. $dirlist[] = $path;
  135. if($handle = @opendir($path)) {
  136. while(false !== ($file = readdir($handle)))
  137. {
  138. if($file != '.' && $file != '..')
  139. {
  140. $file = $path.$file ;
  141. if(!is_dir($file))
  142. {
  143. if($mode != "DIRS")
  144. {
  145. $extension = "";
  146. $extpos = strrpos($file, '.');
  147. if($extpos!==false)
  148. $extension = substr($file,$extpos,strlen($file)-$extpos);
  149. $extension=strtolower($extension);
  150. if(in_array($extension, $file_types))
  151. $dirlist[] = $file;
  152. }
  153. }
  154. elseif($d >= 0 && ($d < $maxdepth || $maxdepth < 0))
  155. {
  156. $result = self::searchdir($file.'/',$mode,$file_types,$maxdepth,$d + 1) ;
  157. $dirlist = array_merge($dirlist,$result);
  158. }
  159. }
  160. }
  161. closedir ( $handle ) ;
  162. }
  163. if($d == 0)
  164. natcasesort($dirlist);
  165. return($dirlist) ;
  166. }
  167. /**
  168. * 对整个项目目录中的PHP和HTML文件行进编码转换
  169. * @access public
  170. * @param string $app 要遍历的项目路径
  171. * @param string $mode 遍历模式,一般取FILES,这样只返回带路径的文件名
  172. * @param array $file_types 文件后缀过滤数组
  173. * @return void
  174. */
  175. static function CodingSwitch($app = "./",$charset='UTF-8',$mode = "FILES",$file_types = array(".html",".php")) {
  176. self::info("注意: 程序使用的文件编码检测算法可能对某些特殊字符不适用");
  177. $filearr = self::searchdir($app,$mode,$file_types);
  178. foreach($filearr as $file)
  179. self::DetectAndSwitch($file,$charset);
  180. }
  181. static public function getError() {
  182. return self::$error;
  183. }
  184. static public function getInfo() {
  185. return self::$info;
  186. }
  187. }