| 1 | <?php |
|---|
| 2 | /* |
|---|
| 3 | * This file is part of EC-CUBE |
|---|
| 4 | * |
|---|
| 5 | * Copyright(c) 2000-2007 LOCKON CO.,LTD. All Rights Reserved. |
|---|
| 6 | * |
|---|
| 7 | * http://www.lockon.co.jp/ |
|---|
| 8 | * |
|---|
| 9 | * This program is free software; you can redistribute it and/or |
|---|
| 10 | * modify it under the terms of the GNU General Public License |
|---|
| 11 | * as published by the Free Software Foundation; either version 2 |
|---|
| 12 | * of the License, or (at your option) any later version. |
|---|
| 13 | * |
|---|
| 14 | * This program is distributed in the hope that it will be useful, |
|---|
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 17 | * GNU General Public License for more details. |
|---|
| 18 | * |
|---|
| 19 | * You should have received a copy of the GNU General Public License |
|---|
| 20 | * along with this program; if not, write to the Free Software |
|---|
| 21 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|---|
| 22 | */ |
|---|
| 23 | |
|---|
| 24 | // {{{ requires |
|---|
| 25 | require_once(dirname(__FILE__) . '/../../module/Tar.php'); |
|---|
| 26 | |
|---|
| 27 | /** |
|---|
| 28 | * ファイル管理 のヘルパークラス. |
|---|
| 29 | * |
|---|
| 30 | * @package Page |
|---|
| 31 | * @author LOCKON CO.,LTD. |
|---|
| 32 | * @version $Id$ |
|---|
| 33 | */ |
|---|
| 34 | class SC_Helper_FileManager { |
|---|
| 35 | |
|---|
| 36 | /** |
|---|
| 37 | * 指定パス配下のディレクトリ取得する. |
|---|
| 38 | * |
|---|
| 39 | * @param string $dir 取得するディレクトリパス |
|---|
| 40 | * @return void |
|---|
| 41 | */ |
|---|
| 42 | function sfGetFileList($dir) { |
|---|
| 43 | $arrFileList = array(); |
|---|
| 44 | $arrDirList = array(); |
|---|
| 45 | |
|---|
| 46 | if (is_dir($dir)) { |
|---|
| 47 | if ($dh = opendir($dir)) { |
|---|
| 48 | $cnt = 0; |
|---|
| 49 | // 行末の/を取り除く |
|---|
| 50 | while (($file = readdir($dh)) !== false) $arrDir[] = $file; |
|---|
| 51 | $dir = ereg_replace("/$", "", $dir); |
|---|
| 52 | // アルファベットと数字でソート |
|---|
| 53 | natcasesort($arrDir); |
|---|
| 54 | foreach($arrDir as $file) { |
|---|
| 55 | // ./ と ../を除くファイルのみを取得 |
|---|
| 56 | if($file != "." && $file != "..") { |
|---|
| 57 | |
|---|
| 58 | $path = $dir."/".$file; |
|---|
| 59 | // SELECT内の見た目を整えるため指定文字数で切る |
|---|
| 60 | $file_name = SC_Utils_Ex::sfCutString($file, FILE_NAME_LEN); |
|---|
| 61 | $file_size = SC_Utils_Ex::sfCutString($this->sfGetDirSize($path), FILE_NAME_LEN); |
|---|
| 62 | $file_time = date("Y/m/d", filemtime($path)); |
|---|
| 63 | |
|---|
| 64 | // ディレクトリとファイルで格納配列を変える |
|---|
| 65 | if(is_dir($path)) { |
|---|
| 66 | $arrDirList[$cnt]['file_name'] = $file; |
|---|
| 67 | $arrDirList[$cnt]['file_path'] = $path; |
|---|
| 68 | $arrDirList[$cnt]['file_size'] = $file_size; |
|---|
| 69 | $arrDirList[$cnt]['file_time'] = $file_time; |
|---|
| 70 | $arrDirList[$cnt]['is_dir'] = true; |
|---|
| 71 | } else { |
|---|
| 72 | $arrFileList[$cnt]['file_name'] = $file; |
|---|
| 73 | $arrFileList[$cnt]['file_path'] = $path; |
|---|
| 74 | $arrFileList[$cnt]['file_size'] = $file_size; |
|---|
| 75 | $arrFileList[$cnt]['file_time'] = $file_time; |
|---|
| 76 | $arrFileList[$cnt]['is_dir'] = false; |
|---|
| 77 | } |
|---|
| 78 | $cnt++; |
|---|
| 79 | } |
|---|
| 80 | } |
|---|
| 81 | closedir($dh); |
|---|
| 82 | } |
|---|
| 83 | } |
|---|
| 84 | |
|---|
| 85 | // フォルダを先頭にしてマージ |
|---|
| 86 | return array_merge($arrDirList, $arrFileList); |
|---|
| 87 | } |
|---|
| 88 | |
|---|
| 89 | /** |
|---|
| 90 | * 指定したディレクトリのバイト数を取得する. |
|---|
| 91 | * |
|---|
| 92 | * @param string $dir ディレクトリ |
|---|
| 93 | * @return void |
|---|
| 94 | */ |
|---|
| 95 | function sfGetDirSize($dir) { |
|---|
| 96 | $bytes = 0; |
|---|
| 97 | if(file_exists($dir)) { |
|---|
| 98 | // ディレクトリの場合下層ファイルの総量を取得 |
|---|
| 99 | if (is_dir($dir)) { |
|---|
| 100 | $handle = opendir($dir); |
|---|
| 101 | while ($file = readdir($handle)) { |
|---|
| 102 | // 行末の/を取り除く |
|---|
| 103 | $dir = ereg_replace("/$", "", $dir); |
|---|
| 104 | $path = $dir."/".$file; |
|---|
| 105 | if ($file != '..' && $file != '.' && !is_dir($path)) { |
|---|
| 106 | $bytes += filesize($path); |
|---|
| 107 | } else if (is_dir($path) && $file != '..' && $file != '.') { |
|---|
| 108 | // 下層ファイルのバイト数を取得する為、再帰的に呼び出す。 |
|---|
| 109 | $bytes += $this->sfGetDirSize($path); |
|---|
| 110 | } |
|---|
| 111 | } |
|---|
| 112 | } else { |
|---|
| 113 | // ファイルの場合 |
|---|
| 114 | $bytes = filesize($dir); |
|---|
| 115 | } |
|---|
| 116 | } |
|---|
| 117 | // ディレクトリ(ファイル)が存在しない場合は0byteを返す |
|---|
| 118 | if($bytes == "") $bytes = 0; |
|---|
| 119 | |
|---|
| 120 | return $bytes; |
|---|
| 121 | } |
|---|
| 122 | |
|---|
| 123 | /** |
|---|
| 124 | * 指定したディレクトリ又はファイルを削除する. |
|---|
| 125 | * |
|---|
| 126 | * @param string $dir 削除するディレクトリ又はファイル |
|---|
| 127 | * @return void |
|---|
| 128 | */ |
|---|
| 129 | function sfDeleteDir($dir) { |
|---|
| 130 | $arrResult = array(); |
|---|
| 131 | if(file_exists($dir)) { |
|---|
| 132 | // ディレクトリかチェック |
|---|
| 133 | if (is_dir($dir)) { |
|---|
| 134 | if ($handle = opendir("$dir")) { |
|---|
| 135 | $cnt = 0; |
|---|
| 136 | while (false !== ($item = readdir($handle))) { |
|---|
| 137 | if ($item != "." && $item != "..") { |
|---|
| 138 | if (is_dir("$dir/$item")) { |
|---|
| 139 | $this->sfDeleteDir("$dir/$item"); |
|---|
| 140 | } else { |
|---|
| 141 | $arrResult[$cnt]['result'] = @unlink("$dir/$item"); |
|---|
| 142 | $arrResult[$cnt]['file_name'] = "$dir/$item"; |
|---|
| 143 | } |
|---|
| 144 | } |
|---|
| 145 | $cnt++; |
|---|
| 146 | } |
|---|
| 147 | } |
|---|
| 148 | closedir($handle); |
|---|
| 149 | $arrResult[$cnt]['result'] = @rmdir($dir); |
|---|
| 150 | $arrResult[$cnt]['file_name'] = "$dir/$item"; |
|---|
| 151 | } else { |
|---|
| 152 | // ファイル削除 |
|---|
| 153 | $arrResult[0]['result'] = @unlink("$dir"); |
|---|
| 154 | $arrResult[0]['file_name'] = "$dir"; |
|---|
| 155 | } |
|---|
| 156 | } |
|---|
| 157 | |
|---|
| 158 | return $arrResult; |
|---|
| 159 | } |
|---|
| 160 | |
|---|
| 161 | /** |
|---|
| 162 | * ツリー生成用配列取得(javascriptに渡す用). |
|---|
| 163 | * |
|---|
| 164 | * @param string $dir ディレクトリ |
|---|
| 165 | * @param string $tree_status 現在のツリーの状態開いているフォルダのパスを |
|---|
| 166 | * | 区切りで格納 |
|---|
| 167 | * @return array ツリー生成用の配列 |
|---|
| 168 | */ |
|---|
| 169 | function sfGetFileTree($dir, $tree_status) { |
|---|
| 170 | |
|---|
| 171 | $cnt = 0; |
|---|
| 172 | $arrTree = array(); |
|---|
| 173 | $default_rank = count(split('/', $dir)); |
|---|
| 174 | |
|---|
| 175 | // 文末の/を取り除く |
|---|
| 176 | $dir = ereg_replace("/$", "", $dir); |
|---|
| 177 | // 最上位層を格納(user_data/) |
|---|
| 178 | if($this->sfDirChildExists($dir)) { |
|---|
| 179 | $arrTree[$cnt]['type'] = "_parent"; |
|---|
| 180 | } else { |
|---|
| 181 | $arrTree[$cnt]['type'] = "_child"; |
|---|
| 182 | } |
|---|
| 183 | $arrTree[$cnt]['path'] = $dir; |
|---|
| 184 | $arrTree[$cnt]['rank'] = 0; |
|---|
| 185 | $arrTree[$cnt]['count'] = $cnt; |
|---|
| 186 | // 初期表示はオープン |
|---|
| 187 | if($_POST['mode'] != '') { |
|---|
| 188 | $arrTree[$cnt]['open'] = $this->lfIsFileOpen($dir, $tree_status); |
|---|
| 189 | } else { |
|---|
| 190 | $arrTree[$cnt]['open'] = true; |
|---|
| 191 | } |
|---|
| 192 | $cnt++; |
|---|
| 193 | |
|---|
| 194 | $this->sfGetFileTreeSub($dir, $default_rank, $cnt, $arrTree, $tree_status); |
|---|
| 195 | |
|---|
| 196 | return $arrTree; |
|---|
| 197 | } |
|---|
| 198 | |
|---|
| 199 | /** |
|---|
| 200 | * ツリー生成用配列取得(javascriptに渡す用). |
|---|
| 201 | * |
|---|
| 202 | * @param string $dir ディレクトリ |
|---|
| 203 | * @param string $default_rank デフォルトの階層 |
|---|
| 204 | * (/区切りで 0,1,2・・・とカウント) |
|---|
| 205 | * @param integer $cnt 連番 |
|---|
| 206 | * @param string $tree_status 現在のツリーの状態開いているフォルダのパスが |
|---|
| 207 | * | 区切りで格納 |
|---|
| 208 | * @return array ツリー生成用の配列 |
|---|
| 209 | */ |
|---|
| 210 | function sfGetFileTreeSub($dir, $default_rank, &$cnt, &$arrTree, $tree_status) { |
|---|
| 211 | |
|---|
| 212 | if(file_exists($dir)) { |
|---|
| 213 | if ($handle = opendir("$dir")) { |
|---|
| 214 | while (false !== ($item = readdir($handle))) $arrDir[] = $item; |
|---|
| 215 | // アルファベットと数字でソート |
|---|
| 216 | natcasesort($arrDir); |
|---|
| 217 | foreach($arrDir as $item) { |
|---|
| 218 | if ($item != "." && $item != "..") { |
|---|
| 219 | // 文末の/を取り除く |
|---|
| 220 | $dir = ereg_replace("/$", "", $dir); |
|---|
| 221 | $path = $dir."/".$item; |
|---|
| 222 | // ディレクトリのみ取得 |
|---|
| 223 | if (is_dir($path)) { |
|---|
| 224 | $arrTree[$cnt]['path'] = $path; |
|---|
| 225 | if($this->sfDirChildExists($path)) { |
|---|
| 226 | $arrTree[$cnt]['type'] = "_parent"; |
|---|
| 227 | } else { |
|---|
| 228 | $arrTree[$cnt]['type'] = "_child"; |
|---|
| 229 | } |
|---|
| 230 | |
|---|
| 231 | // 階層を割り出す |
|---|
| 232 | $arrCnt = split('/', $path); |
|---|
| 233 | $rank = count($arrCnt); |
|---|
| 234 | $arrTree[$cnt]['rank'] = $rank - $default_rank + 1; |
|---|
| 235 | $arrTree[$cnt]['count'] = $cnt; |
|---|
| 236 | // フォルダが開いているか |
|---|
| 237 | $arrTree[$cnt]['open'] = $this->lfIsFileOpen($path, $tree_status); |
|---|
| 238 | $cnt++; |
|---|
| 239 | // 下層ディレクトリ取得の為、再帰的に呼び出す |
|---|
| 240 | $this->sfGetFileTreeSub($path, $default_rank, $cnt, $arrTree, $tree_status); |
|---|
| 241 | } |
|---|
| 242 | } |
|---|
| 243 | } |
|---|
| 244 | } |
|---|
| 245 | closedir($handle); |
|---|
| 246 | } |
|---|
| 247 | } |
|---|
| 248 | |
|---|
| 249 | /** |
|---|
| 250 | * 指定したディレクトリ配下にファイルがあるかチェックする. |
|---|
| 251 | * |
|---|
| 252 | * @param string ディレクトリ |
|---|
| 253 | * @return bool ファイルが存在する場合 true |
|---|
| 254 | */ |
|---|
| 255 | function sfDirChildExists($dir) { |
|---|
| 256 | if(file_exists($dir)) { |
|---|
| 257 | if (is_dir($dir)) { |
|---|
| 258 | $handle = opendir($dir); |
|---|
| 259 | while ($file = readdir($handle)) { |
|---|
| 260 | // 行末の/を取り除く |
|---|
| 261 | $dir = ereg_replace("/$", "", $dir); |
|---|
| 262 | $path = $dir."/".$file; |
|---|
| 263 | if ($file != '..' && $file != '.' && is_dir($path)) { |
|---|
| 264 | return true; |
|---|
| 265 | } |
|---|
| 266 | } |
|---|
| 267 | } |
|---|
| 268 | } |
|---|
| 269 | |
|---|
| 270 | return false; |
|---|
| 271 | } |
|---|
| 272 | |
|---|
| 273 | /** |
|---|
| 274 | * 指定したファイルが前回開かれた状態にあったかチェックする. |
|---|
| 275 | * |
|---|
| 276 | * @param string $dir ディレクトリ |
|---|
| 277 | * @param string $tree_status 現在のツリーの状態開いているフォルダのパスが |
|---|
| 278 | * | 区切りで格納 |
|---|
| 279 | * @return bool 前回開かれた状態の場合 true |
|---|
| 280 | */ |
|---|
| 281 | function lfIsFileOpen($dir, $tree_status) { |
|---|
| 282 | $arrTreeStatus = split('\|', $tree_status); |
|---|
| 283 | if(in_array($dir, $arrTreeStatus)) { |
|---|
| 284 | return true; |
|---|
| 285 | } |
|---|
| 286 | |
|---|
| 287 | return false; |
|---|
| 288 | } |
|---|
| 289 | |
|---|
| 290 | /** |
|---|
| 291 | * ファイルのダウンロードを行う. |
|---|
| 292 | * |
|---|
| 293 | * @param string $file ファイルパス |
|---|
| 294 | * @return void |
|---|
| 295 | */ |
|---|
| 296 | function sfDownloadFile($file) { |
|---|
| 297 | // ファイルの場合はダウンロードさせる |
|---|
| 298 | Header("Content-disposition: attachment; filename=".basename($file)); |
|---|
| 299 | Header("Content-type: application/octet-stream; name=".basename($file)); |
|---|
| 300 | Header("Cache-Control: "); |
|---|
| 301 | Header("Pragma: "); |
|---|
| 302 | echo ($this->sfReadFile($file)); |
|---|
| 303 | } |
|---|
| 304 | |
|---|
| 305 | /** |
|---|
| 306 | * ファイル作成を行う. |
|---|
| 307 | * |
|---|
| 308 | * @param string $file ファイルパス |
|---|
| 309 | * @param integer $mode パーミッション |
|---|
| 310 | * @return bool ファイル作成に成功した場合 true |
|---|
| 311 | */ |
|---|
| 312 | function sfCreateFile($file, $mode = "") { |
|---|
| 313 | // 行末の/を取り除く |
|---|
| 314 | if($mode != "") { |
|---|
| 315 | $ret = @mkdir($file, $mode); |
|---|
| 316 | } else { |
|---|
| 317 | $ret = @mkdir($file); |
|---|
| 318 | } |
|---|
| 319 | |
|---|
| 320 | return $ret; |
|---|
| 321 | } |
|---|
| 322 | |
|---|
| 323 | /** |
|---|
| 324 | * ファイル読込を行う. |
|---|
| 325 | * |
|---|
| 326 | * @param string ファイルパス |
|---|
| 327 | * @return string ファイルの内容 |
|---|
| 328 | */ |
|---|
| 329 | function sfReadFile($filename) { |
|---|
| 330 | $str = ""; |
|---|
| 331 | // バイナリモードでオープン |
|---|
| 332 | $fp = @fopen($filename, "rb" ); |
|---|
| 333 | //ファイル内容を全て変数に読み込む |
|---|
| 334 | if($fp) { |
|---|
| 335 | $str = @fread($fp, filesize($filename)+1); |
|---|
| 336 | } |
|---|
| 337 | @fclose($fp); |
|---|
| 338 | |
|---|
| 339 | return $str; |
|---|
| 340 | } |
|---|
| 341 | |
|---|
| 342 | /** |
|---|
| 343 | * ユーザが作成したファイルをアーカイブしダウンロードさせる |
|---|
| 344 | * TODO 要リファクタリング |
|---|
| 345 | * @param void |
|---|
| 346 | * @return void |
|---|
| 347 | */ |
|---|
| 348 | function downloadArchiveFiles($dir) { |
|---|
| 349 | $debug_message = ""; |
|---|
| 350 | // ダウンロードされるファイル名 |
|---|
| 351 | $dlFileName = 'tpl_package_' . date('YmdHis') . '.tar.gz'; |
|---|
| 352 | |
|---|
| 353 | // ファイル一覧取得 |
|---|
| 354 | $arrFileHash = SC_Utils::sfGetFileList($dir); |
|---|
| 355 | foreach($arrFileHash as $val) { |
|---|
| 356 | $arrFileList[] = $val['file_name']; |
|---|
| 357 | $debug_message.= "圧縮:".$val['file_name']."\n"; |
|---|
| 358 | } |
|---|
| 359 | GC_Utils::gfDebugLog($debug_message); |
|---|
| 360 | |
|---|
| 361 | // ディレクトリを移動 |
|---|
| 362 | chdir($dir); |
|---|
| 363 | // 圧縮をおこなう |
|---|
| 364 | $tar = new Archive_Tar($dlFileName, true); |
|---|
| 365 | $tar->create($arrFileList); |
|---|
| 366 | |
|---|
| 367 | // ダウンロード用HTTPヘッダ出力 |
|---|
| 368 | header("Content-disposition: attachment; filename=${dlFileName}"); |
|---|
| 369 | header("Content-type: application/octet-stream; name=${dlFileName}"); |
|---|
| 370 | header("Content-Length: " . filesize($dlFileName)); |
|---|
| 371 | readfile($dlFileName); |
|---|
| 372 | unlink($dir . "/" . $dlFileName); |
|---|
| 373 | exit; |
|---|
| 374 | } |
|---|
| 375 | |
|---|
| 376 | /** |
|---|
| 377 | * tarアーカイブを解凍する. |
|---|
| 378 | * |
|---|
| 379 | * @param string $path アーカイブパス |
|---|
| 380 | * @return string Archive_Tar::extractModify()のエラー |
|---|
| 381 | */ |
|---|
| 382 | function unpackFile($path) { |
|---|
| 383 | // 圧縮フラグTRUEはgzip解凍をおこなう |
|---|
| 384 | $tar = new Archive_Tar($path, true); |
|---|
| 385 | |
|---|
| 386 | $dir = dirname($path); |
|---|
| 387 | $file_name = basename($path); |
|---|
| 388 | |
|---|
| 389 | // 拡張子を切り取る |
|---|
| 390 | $unpacking_name = preg_replace("/(\.tar|\.tar\.gz)$/", "", $file_name); |
|---|
| 391 | |
|---|
| 392 | // 指定されたフォルダ内に解凍する |
|---|
| 393 | $tar->extractModify($dir. "/", $unpacking_name); |
|---|
| 394 | GC_Utils_Ex::gfPrintLog("解凍:" . $dir."/".$file_name."->".$dir."/".$unpacking_name); |
|---|
| 395 | |
|---|
| 396 | // フォルダ削除 |
|---|
| 397 | SC_Utils::sfDelFile($dir . "/" . $unpacking_name); |
|---|
| 398 | // 圧縮ファイル削除 |
|---|
| 399 | unlink($path); |
|---|
| 400 | } |
|---|
| 401 | } |
|---|
| 402 | ?> |
|---|