| 1 | <?php |
|---|
| 2 | /* |
|---|
| 3 | * Copyright(c) 2000-2007 LOCKON CO.,LTD. All Rights Reserved. |
|---|
| 4 | * |
|---|
| 5 | * http://www.lockon.co.jp/ |
|---|
| 6 | */ |
|---|
| 7 | |
|---|
| 8 | // {{{ requires |
|---|
| 9 | require_once(CLASS_PATH . "batch/SC_Batch.php"); |
|---|
| 10 | |
|---|
| 11 | /** |
|---|
| 12 | * アップデート機能 のバッチクラス. |
|---|
| 13 | * |
|---|
| 14 | * XXX Singleton にするべき... |
|---|
| 15 | * |
|---|
| 16 | * @package Batch |
|---|
| 17 | * @author LOCKON CO.,LTD. |
|---|
| 18 | * @version $Id$ |
|---|
| 19 | */ |
|---|
| 20 | class SC_Batch_Update extends SC_Batch { |
|---|
| 21 | |
|---|
| 22 | /** |
|---|
| 23 | * 変換したいファイルの拡張子をカンマ区切りで羅列. |
|---|
| 24 | */ |
|---|
| 25 | var $includes = "php,inc,tpl,css,sql,js"; |
|---|
| 26 | |
|---|
| 27 | /** |
|---|
| 28 | * 除外するファイル名をカンマ区切りで羅列. |
|---|
| 29 | */ |
|---|
| 30 | var $excludes = "distinfo.php,update.php"; |
|---|
| 31 | |
|---|
| 32 | /** |
|---|
| 33 | * バッチ処理を実行する. |
|---|
| 34 | * |
|---|
| 35 | * @param string $target アップデータファイルのディレクトリパス |
|---|
| 36 | * @return void |
|---|
| 37 | */ |
|---|
| 38 | function execute($target = ".") { |
|---|
| 39 | $arrLog = array(); |
|---|
| 40 | $includeArray = explode(',', $this->includes); |
|---|
| 41 | $excludeArray = explode(',', $this->excludes); |
|---|
| 42 | $fileArrays = $this->listdirs($target); |
|---|
| 43 | |
|---|
| 44 | foreach ($fileArrays as $path) { |
|---|
| 45 | if (is_file($path)) { |
|---|
| 46 | |
|---|
| 47 | // ファイル名を取得 |
|---|
| 48 | $fileName = pathinfo($path, PATHINFO_BASENAME); |
|---|
| 49 | |
|---|
| 50 | // 拡張子を取得 |
|---|
| 51 | $suffix = pathinfo($path, PATHINFO_EXTENSION); |
|---|
| 52 | |
|---|
| 53 | // distinfo の変数定義 |
|---|
| 54 | $distinfo = isset($distinfo) ? $distinfo : ""; |
|---|
| 55 | |
|---|
| 56 | // distinfo.php を読み込む |
|---|
| 57 | if ($fileName == "distinfo.php") { |
|---|
| 58 | include_once($path); |
|---|
| 59 | } |
|---|
| 60 | |
|---|
| 61 | // 除外ファイルをスキップ |
|---|
| 62 | if (in_array($fileName, $excludeArray)) { |
|---|
| 63 | $arrLog[] = "excludes by " . $path . "\n"; |
|---|
| 64 | continue; |
|---|
| 65 | } |
|---|
| 66 | |
|---|
| 67 | // sha1 を取得 |
|---|
| 68 | $sha1 = sha1_file($path); |
|---|
| 69 | |
|---|
| 70 | $arrLog[] = $sha1 . " => " . $path . "\n"; |
|---|
| 71 | |
|---|
| 72 | |
|---|
| 73 | // 変換対象を順に処理 |
|---|
| 74 | foreach ($includeArray as $include) { |
|---|
| 75 | if ($suffix == $include) { |
|---|
| 76 | |
|---|
| 77 | // ファイル内容を取得 |
|---|
| 78 | $contents = file_get_contents($path); |
|---|
| 79 | |
|---|
| 80 | // 書き出し先を取得 |
|---|
| 81 | if (!empty($distinfo[$sha1])) { |
|---|
| 82 | $out = $distinfo[$sha1]; |
|---|
| 83 | } else { |
|---|
| 84 | $arrLog[] = "ハッシュ値が一致しないため, コピー先が取得できません."; |
|---|
| 85 | die(); |
|---|
| 86 | } |
|---|
| 87 | |
|---|
| 88 | // ファイルを書き出しモードで開く |
|---|
| 89 | $handle = @fopen($out, "w"); |
|---|
| 90 | if (!$handle) { |
|---|
| 91 | // ディレクトリ作成を行ってリトライ |
|---|
| 92 | $this->mkdir_p($out); |
|---|
| 93 | $handle = @fopen($out, "w"); |
|---|
| 94 | if (!$handle) { |
|---|
| 95 | $arrLog[] = "Cannot open file (". $out . ")\n"; |
|---|
| 96 | continue; |
|---|
| 97 | } |
|---|
| 98 | } |
|---|
| 99 | |
|---|
| 100 | // 取得した内容を書き込む |
|---|
| 101 | if (fwrite($handle, $contents) === false) { |
|---|
| 102 | $arrLog[] = "Cannot write to file (" . $out . ")\n"; |
|---|
| 103 | continue; |
|---|
| 104 | } |
|---|
| 105 | |
|---|
| 106 | $arrLog[] = "copyed " . $out . "\n"; |
|---|
| 107 | // ファイルを閉じる |
|---|
| 108 | fclose($handle); |
|---|
| 109 | } |
|---|
| 110 | } |
|---|
| 111 | } |
|---|
| 112 | } |
|---|
| 113 | $arrLog[] = "Finished Successful!\n"; |
|---|
| 114 | return $arrLog; |
|---|
| 115 | } |
|---|
| 116 | |
|---|
| 117 | /** |
|---|
| 118 | * $dir を再帰的に辿ってパス名を配列で返す. |
|---|
| 119 | * |
|---|
| 120 | * @param string 任意のパス名 |
|---|
| 121 | * @return array $dir より下層に存在するパス名の配列 |
|---|
| 122 | * @see http://www.php.net/glob |
|---|
| 123 | */ |
|---|
| 124 | function listdirs($dir) { |
|---|
| 125 | static $alldirs = array(); |
|---|
| 126 | $dirs = glob($dir . '/*'); |
|---|
| 127 | if (count($dirs) > 0) { |
|---|
| 128 | foreach ($dirs as $d) $alldirs[] = $d; |
|---|
| 129 | } |
|---|
| 130 | foreach ($dirs as $dir) $this->listdirs($dir); |
|---|
| 131 | return $alldirs; |
|---|
| 132 | } |
|---|
| 133 | |
|---|
| 134 | /** |
|---|
| 135 | * mkdir -p |
|---|
| 136 | * |
|---|
| 137 | * @param string $path 絶対パス |
|---|
| 138 | */ |
|---|
| 139 | function mkdir_p($path){ |
|---|
| 140 | $path = dirname($path); |
|---|
| 141 | $path = str_replace ('\\', '/', $path); |
|---|
| 142 | |
|---|
| 143 | $arrDirs = explode("/", $path); |
|---|
| 144 | $dir = ''; |
|---|
| 145 | |
|---|
| 146 | foreach($arrDirs as $n){ |
|---|
| 147 | $dir .= $n . '/'; |
|---|
| 148 | if(!file_exists($dir)) { |
|---|
| 149 | if (!@mkdir($dir)) return; |
|---|
| 150 | } |
|---|
| 151 | } |
|---|
| 152 | } |
|---|
| 153 | } |
|---|
| 154 | ?> |
|---|