| 1 | #!/usr/local/bin/php |
---|
| 2 | <?php |
---|
| 3 | /* |
---|
| 4 | * This file is part of EC-CUBE |
---|
| 5 | * |
---|
| 6 | * Copyright(c) 2000-2010 LOCKON CO.,LTD. All Rights Reserved. |
---|
| 7 | * |
---|
| 8 | * http://www.lockon.co.jp/ |
---|
| 9 | * |
---|
| 10 | * This program is free software; you can redistribute it and/or |
---|
| 11 | * modify it under the terms of the GNU General Public License |
---|
| 12 | * as published by the Free Software Foundation; either version 2 |
---|
| 13 | * of the License, or (at your option) any later version. |
---|
| 14 | * |
---|
| 15 | * This program is distributed in the hope that it will be useful, |
---|
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 18 | * GNU General Public License for more details. |
---|
| 19 | * |
---|
| 20 | * You should have received a copy of the GNU General Public License |
---|
| 21 | * along with this program; if not, write to the Free Software |
---|
| 22 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
---|
| 23 | */ |
---|
| 24 | |
---|
| 25 | /** |
---|
| 26 | * ファイルのエンコーディングを $fromEncoding から $toEncoding へ変換します. |
---|
| 27 | * |
---|
| 28 | * @author Kentaro Ohkouchi<ohkouchi@loop-az.jp> |
---|
| 29 | * @since PHP4.3.0(cli) |
---|
| 30 | * @version $Id:convert.php 15079 2007-07-20 07:20:36Z nanasess $ |
---|
| 31 | */ |
---|
| 32 | |
---|
| 33 | /** |
---|
| 34 | * 変換したいファイルの拡張子をカンマ区切りで羅列. |
---|
| 35 | */ |
---|
| 36 | $includes = "php,inc,tpl,css,sql,js"; |
---|
| 37 | |
---|
| 38 | /** |
---|
| 39 | * 除外するファイル名をカンマ区切りで羅列. |
---|
| 40 | */ |
---|
| 41 | $excludes = "convert.php"; |
---|
| 42 | |
---|
| 43 | /** |
---|
| 44 | * 変換元エンコーディング. |
---|
| 45 | */ |
---|
| 46 | $fromEncoding = "EUC-JP"; |
---|
| 47 | |
---|
| 48 | /** |
---|
| 49 | * 変換先エンコーディング. |
---|
| 50 | */ |
---|
| 51 | $toEncoding = "UTF-8"; |
---|
| 52 | |
---|
| 53 | $includeArray = explode(',', $includes); |
---|
| 54 | $excludeArray = explode(',', $excludes); |
---|
| 55 | $fileArrays = listdirs('.'); |
---|
| 56 | |
---|
| 57 | foreach ($fileArrays as $path) { |
---|
| 58 | if (is_file($path)) { |
---|
| 59 | |
---|
| 60 | // ファイル名を取得 |
---|
| 61 | $fileName = pathinfo($path, PATHINFO_BASENAME); |
---|
| 62 | |
---|
| 63 | // 拡張子を取得 |
---|
| 64 | $suffix = pathinfo($path, PATHINFO_EXTENSION); |
---|
| 65 | |
---|
| 66 | // 除外ファイルをスキップ |
---|
| 67 | if (in_array($fileName, $excludeArray)) { |
---|
| 68 | echo "excludes by " . $path . "\n"; |
---|
| 69 | continue; |
---|
| 70 | } |
---|
| 71 | |
---|
| 72 | // 変換対象を順に処理 |
---|
| 73 | foreach ($includeArray as $include) { |
---|
| 74 | if ($suffix == $include) { |
---|
| 75 | |
---|
| 76 | // ファイル内容を取得し, エンコーディング変換 |
---|
| 77 | $contents = file_get_contents($path); |
---|
| 78 | $convertedContents = mb_convert_encoding($contents, |
---|
| 79 | $toEncoding, |
---|
| 80 | $fromEncoding); |
---|
| 81 | |
---|
| 82 | // 書き込みできるか? |
---|
| 83 | if (is_writable($path)) { |
---|
| 84 | |
---|
| 85 | // ファイルを書き出しモードで開く |
---|
| 86 | $handle = fopen($path, "w"); |
---|
| 87 | if (!$handle) { |
---|
| 88 | echo "Cannot open file (". $path . ")"; |
---|
| 89 | continue; |
---|
| 90 | } |
---|
| 91 | |
---|
| 92 | // コード変換した内容を書き込む |
---|
| 93 | if (fwrite($handle, $convertedContents) === false) { |
---|
| 94 | echo "Cannot write to file (" . $path . ")"; |
---|
| 95 | continue; |
---|
| 96 | } |
---|
| 97 | |
---|
| 98 | echo "converted " . $path . "\n"; |
---|
| 99 | // ファイルを閉じる |
---|
| 100 | fclose($handle); |
---|
| 101 | } else { |
---|
| 102 | |
---|
| 103 | echo "The file " . $filename . "is not writable"; |
---|
| 104 | } |
---|
| 105 | } |
---|
| 106 | } |
---|
| 107 | } |
---|
| 108 | } |
---|
| 109 | |
---|
| 110 | /** |
---|
| 111 | * $dir を再帰的に辿ってパス名を配列で返す. |
---|
| 112 | * |
---|
| 113 | * @param string 任意のパス名 |
---|
| 114 | * @return array $dir より下層に存在するパス名の配列 |
---|
| 115 | * @see http://www.php.net/glob |
---|
| 116 | */ |
---|
| 117 | function listdirs($dir) { |
---|
| 118 | static $alldirs = array(); |
---|
| 119 | $dirs = glob($dir . '/*'); |
---|
| 120 | if (is_array($dirs) && count($dirs) > 0) { |
---|
| 121 | foreach ($dirs as $d) $alldirs[] = $d; |
---|
| 122 | } |
---|
| 123 | if (is_array($dirs)) { |
---|
| 124 | foreach ($dirs as $dir) listdirs($dir); |
---|
| 125 | } |
---|
| 126 | return $alldirs; |
---|
| 127 | } |
---|
| 128 | ?> |
---|