| 1 | <?php |
|---|
| 2 | /* |
|---|
| 3 | * This file is part of EC-CUBE |
|---|
| 4 | * |
|---|
| 5 | * Copyright(c) 2000-2013 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 | /** |
|---|
| 25 | * ファイル管理 のヘルパークラス. |
|---|
| 26 | * |
|---|
| 27 | * @package Page |
|---|
| 28 | * @author LOCKON CO.,LTD. |
|---|
| 29 | * @version $Id$ |
|---|
| 30 | */ |
|---|
| 31 | class SC_Helper_FileManager { |
|---|
| 32 | |
|---|
| 33 | /** |
|---|
| 34 | * 指定パス配下のディレクトリ取得する. |
|---|
| 35 | * |
|---|
| 36 | * @param string $dir 取得するディレクトリパス |
|---|
| 37 | * @return void |
|---|
| 38 | */ |
|---|
| 39 | function sfGetFileList($dir) { |
|---|
| 40 | $arrFileList = array(); |
|---|
| 41 | $arrDirList = array(); |
|---|
| 42 | |
|---|
| 43 | if (is_dir($dir)) { |
|---|
| 44 | $dh = opendir($dir); |
|---|
| 45 | if ($dh) { |
|---|
| 46 | $cnt = 0; |
|---|
| 47 | $arrDir = array(); |
|---|
| 48 | // 行末の/を取り除く |
|---|
| 49 | while (($file = readdir($dh)) !== false) $arrDir[] = $file; |
|---|
| 50 | $dir = rtrim($dir, '/'); |
|---|
| 51 | // アルファベットと数字でソート |
|---|
| 52 | natcasesort($arrDir); |
|---|
| 53 | foreach ($arrDir as $file) { |
|---|
| 54 | // ./ と ../を除くファイルのみを取得 |
|---|
| 55 | if ($file != '.' && $file != '..') { |
|---|
| 56 | |
|---|
| 57 | $path = $dir.'/'.$file; |
|---|
| 58 | // SELECT内の見た目を整えるため指定文字数で切る |
|---|
| 59 | $file_size = SC_Utils_Ex::sfCutString(SC_Helper_FileManager::sfGetDirSize($path), FILE_NAME_LEN); |
|---|
| 60 | $file_time = date('Y/m/d', filemtime($path)); |
|---|
| 61 | |
|---|
| 62 | // ディレクトリとファイルで格納配列を変える |
|---|
| 63 | if (is_dir($path)) { |
|---|
| 64 | $arrDirList[$cnt]['file_name'] = $file; |
|---|
| 65 | $arrDirList[$cnt]['file_path'] = $path; |
|---|
| 66 | $arrDirList[$cnt]['file_size'] = $file_size; |
|---|
| 67 | $arrDirList[$cnt]['file_time'] = $file_time; |
|---|
| 68 | $arrDirList[$cnt]['is_dir'] = true; |
|---|
| 69 | } else { |
|---|
| 70 | $arrFileList[$cnt]['file_name'] = $file; |
|---|
| 71 | $arrFileList[$cnt]['file_path'] = $path; |
|---|
| 72 | $arrFileList[$cnt]['file_size'] = $file_size; |
|---|
| 73 | $arrFileList[$cnt]['file_time'] = $file_time; |
|---|
| 74 | $arrFileList[$cnt]['is_dir'] = false; |
|---|
| 75 | } |
|---|
| 76 | $cnt++; |
|---|
| 77 | } |
|---|
| 78 | } |
|---|
| 79 | closedir($dh); |
|---|
| 80 | } |
|---|
| 81 | } |
|---|
| 82 | |
|---|
| 83 | // フォルダを先頭にしてマージ |
|---|
| 84 | return array_merge($arrDirList, $arrFileList); |
|---|
| 85 | } |
|---|
| 86 | |
|---|
| 87 | /** |
|---|
| 88 | * 指定したディレクトリのバイト数を取得する. |
|---|
| 89 | * |
|---|
| 90 | * @param string $dir ディレクトリ |
|---|
| 91 | * @return void |
|---|
| 92 | */ |
|---|
| 93 | function sfGetDirSize($dir) { |
|---|
| 94 | $bytes = 0; |
|---|
| 95 | if (file_exists($dir)) { |
|---|
| 96 | // ディレクトリの場合下層ファイルの総量を取得 |
|---|
| 97 | if (is_dir($dir)) { |
|---|
| 98 | $handle = opendir($dir); |
|---|
| 99 | while ($file = readdir($handle)) { |
|---|
| 100 | // 行末の/を取り除く |
|---|
| 101 | $dir = rtrim($dir, '/'); |
|---|
| 102 | $path = $dir.'/'.$file; |
|---|
| 103 | if ($file != '..' && $file != '.' && !is_dir($path)) { |
|---|
| 104 | $bytes += filesize($path); |
|---|
| 105 | } else if (is_dir($path) && $file != '..' && $file != '.') { |
|---|
| 106 | // 下層ファイルのバイト数を取得する為、再帰的に呼び出す。 |
|---|
| 107 | $bytes += SC_Helper_FileManager::sfGetDirSize($path); |
|---|
| 108 | } |
|---|
| 109 | } |
|---|
| 110 | } else { |
|---|
| 111 | // ファイルの場合 |
|---|
| 112 | $bytes = filesize($dir); |
|---|
| 113 | } |
|---|
| 114 | } |
|---|
| 115 | // ディレクトリ(ファイル)が存在しない場合は0byteを返す |
|---|
| 116 | if ($bytes == '') { |
|---|
| 117 | $bytes = 0; |
|---|
| 118 | } |
|---|
| 119 | |
|---|
| 120 | return $bytes; |
|---|
| 121 | } |
|---|
| 122 | |
|---|
| 123 | /** |
|---|
| 124 | * ツリー生成用配列取得(javascriptに渡す用). |
|---|
| 125 | * |
|---|
| 126 | * @param string $dir ディレクトリ |
|---|
| 127 | * @param string $tree_status 現在のツリーの状態開いているフォルダのパスを |
|---|
| 128 | * | 区切りで格納 |
|---|
| 129 | * @return array ツリー生成用の配列 |
|---|
| 130 | */ |
|---|
| 131 | function sfGetFileTree($dir, $tree_status) { |
|---|
| 132 | |
|---|
| 133 | $cnt = 0; |
|---|
| 134 | $arrTree = array(); |
|---|
| 135 | $default_rank = count(explode('/', $dir)); |
|---|
| 136 | |
|---|
| 137 | // 文末の/を取り除く |
|---|
| 138 | $dir = rtrim($dir, '/'); |
|---|
| 139 | // 最上位層を格納(user_data/) |
|---|
| 140 | if ($this->sfDirChildExists($dir)) { |
|---|
| 141 | $arrTree[$cnt]['type'] = '_parent'; |
|---|
| 142 | } else { |
|---|
| 143 | $arrTree[$cnt]['type'] = '_child'; |
|---|
| 144 | } |
|---|
| 145 | $arrTree[$cnt]['path'] = $dir; |
|---|
| 146 | $arrTree[$cnt]['rank'] = 0; |
|---|
| 147 | $arrTree[$cnt]['count'] = $cnt; |
|---|
| 148 | // 初期表示はオープン |
|---|
| 149 | if ($_POST['mode'] != '') { |
|---|
| 150 | $arrTree[$cnt]['open'] = $this->lfIsFileOpen($dir, $tree_status); |
|---|
| 151 | } else { |
|---|
| 152 | $arrTree[$cnt]['open'] = true; |
|---|
| 153 | } |
|---|
| 154 | $cnt++; |
|---|
| 155 | |
|---|
| 156 | $this->sfGetFileTreeSub($dir, $default_rank, $cnt, $arrTree, $tree_status); |
|---|
| 157 | |
|---|
| 158 | return $arrTree; |
|---|
| 159 | } |
|---|
| 160 | |
|---|
| 161 | /** |
|---|
| 162 | * ツリー生成用配列取得(javascriptに渡す用). |
|---|
| 163 | * |
|---|
| 164 | * @param string $dir ディレクトリ |
|---|
| 165 | * @param string $default_rank デフォルトの階層 |
|---|
| 166 | * (/区切りで 0,1,2・・・とカウント) |
|---|
| 167 | * @param integer $cnt 連番 |
|---|
| 168 | * @param string $tree_status 現在のツリーの状態開いているフォルダのパスが |
|---|
| 169 | * | 区切りで格納 |
|---|
| 170 | * @return array ツリー生成用の配列 |
|---|
| 171 | */ |
|---|
| 172 | function sfGetFileTreeSub($dir, $default_rank, &$cnt, &$arrTree, $tree_status) { |
|---|
| 173 | |
|---|
| 174 | if (file_exists($dir)) { |
|---|
| 175 | $handle = opendir($dir); |
|---|
| 176 | if ($handle) { |
|---|
| 177 | $arrDir = array(); |
|---|
| 178 | while (false !== ($item = readdir($handle))) $arrDir[] = $item; |
|---|
| 179 | // アルファベットと数字でソート |
|---|
| 180 | natcasesort($arrDir); |
|---|
| 181 | foreach ($arrDir as $item) { |
|---|
| 182 | if ($item != '.' && $item != '..') { |
|---|
| 183 | // 文末の/を取り除く |
|---|
| 184 | $dir = rtrim($dir, '/'); |
|---|
| 185 | $path = $dir.'/'.$item; |
|---|
| 186 | // ディレクトリのみ取得 |
|---|
| 187 | if (is_dir($path)) { |
|---|
| 188 | $arrTree[$cnt]['path'] = $path; |
|---|
| 189 | if ($this->sfDirChildExists($path)) { |
|---|
| 190 | $arrTree[$cnt]['type'] = '_parent'; |
|---|
| 191 | } else { |
|---|
| 192 | $arrTree[$cnt]['type'] = '_child'; |
|---|
| 193 | } |
|---|
| 194 | |
|---|
| 195 | // 階層を割り出す |
|---|
| 196 | $arrCnt = explode('/', $path); |
|---|
| 197 | $rank = count($arrCnt); |
|---|
| 198 | $arrTree[$cnt]['rank'] = $rank - $default_rank + 1; |
|---|
| 199 | $arrTree[$cnt]['count'] = $cnt; |
|---|
| 200 | // フォルダが開いているか |
|---|
| 201 | $arrTree[$cnt]['open'] = $this->lfIsFileOpen($path, $tree_status); |
|---|
| 202 | $cnt++; |
|---|
| 203 | // 下層ディレクトリ取得の為、再帰的に呼び出す |
|---|
| 204 | $this->sfGetFileTreeSub($path, $default_rank, $cnt, $arrTree, $tree_status); |
|---|
| 205 | } |
|---|
| 206 | } |
|---|
| 207 | } |
|---|
| 208 | } |
|---|
| 209 | closedir($handle); |
|---|
| 210 | } |
|---|
| 211 | } |
|---|
| 212 | |
|---|
| 213 | /** |
|---|
| 214 | * 指定したディレクトリ配下にファイルがあるかチェックする. |
|---|
| 215 | * |
|---|
| 216 | * @param string ディレクトリ |
|---|
| 217 | * @return bool ファイルが存在する場合 true |
|---|
| 218 | */ |
|---|
| 219 | function sfDirChildExists($dir) { |
|---|
| 220 | if (file_exists($dir)) { |
|---|
| 221 | if (is_dir($dir)) { |
|---|
| 222 | $handle = opendir($dir); |
|---|
| 223 | while ($file = readdir($handle)) { |
|---|
| 224 | // 行末の/を取り除く |
|---|
| 225 | $dir = rtrim($dir, '/'); |
|---|
| 226 | $path = $dir.'/'.$file; |
|---|
| 227 | if ($file != '..' && $file != '.' && is_dir($path)) { |
|---|
| 228 | return true; |
|---|
| 229 | } |
|---|
| 230 | } |
|---|
| 231 | } |
|---|
| 232 | } |
|---|
| 233 | |
|---|
| 234 | return false; |
|---|
| 235 | } |
|---|
| 236 | |
|---|
| 237 | /** |
|---|
| 238 | * 指定したファイルが前回開かれた状態にあったかチェックする. |
|---|
| 239 | * |
|---|
| 240 | * @param string $dir ディレクトリ |
|---|
| 241 | * @param string $tree_status 現在のツリーの状態開いているフォルダのパスが |
|---|
| 242 | * | 区切りで格納 |
|---|
| 243 | * @return bool 前回開かれた状態の場合 true |
|---|
| 244 | */ |
|---|
| 245 | function lfIsFileOpen($dir, $tree_status) { |
|---|
| 246 | $arrTreeStatus = explode('|', $tree_status); |
|---|
| 247 | if (in_array($dir, $arrTreeStatus)) { |
|---|
| 248 | return true; |
|---|
| 249 | } |
|---|
| 250 | |
|---|
| 251 | return false; |
|---|
| 252 | } |
|---|
| 253 | |
|---|
| 254 | /** |
|---|
| 255 | * ファイルのダウンロードを行う. |
|---|
| 256 | * |
|---|
| 257 | * @param string $file ファイルパス |
|---|
| 258 | * @return void |
|---|
| 259 | */ |
|---|
| 260 | function sfDownloadFile($file) { |
|---|
| 261 | // ファイルの場合はダウンロードさせる |
|---|
| 262 | Header('Content-disposition: attachment; filename='.basename($file)); |
|---|
| 263 | Header('Content-type: application/octet-stream; name='.basename($file)); |
|---|
| 264 | Header('Cache-Control: '); |
|---|
| 265 | Header('Pragma: '); |
|---|
| 266 | echo ($this->sfReadFile($file)); |
|---|
| 267 | } |
|---|
| 268 | |
|---|
| 269 | /** |
|---|
| 270 | * ファイル作成を行う. |
|---|
| 271 | * |
|---|
| 272 | * @param string $file ファイルパス |
|---|
| 273 | * @param integer $mode パーミッション |
|---|
| 274 | * @return bool ファイル作成に成功した場合 true |
|---|
| 275 | */ |
|---|
| 276 | function sfCreateFile($file, $mode = '') { |
|---|
| 277 | // 行末の/を取り除く |
|---|
| 278 | if ($mode != '') { |
|---|
| 279 | $ret = @mkdir($file, $mode); |
|---|
| 280 | } else { |
|---|
| 281 | $ret = @mkdir($file); |
|---|
| 282 | } |
|---|
| 283 | |
|---|
| 284 | return $ret; |
|---|
| 285 | } |
|---|
| 286 | |
|---|
| 287 | /** |
|---|
| 288 | * ファイル読込を行う. |
|---|
| 289 | * |
|---|
| 290 | * @param string ファイルパス |
|---|
| 291 | * @return string ファイルの内容 |
|---|
| 292 | */ |
|---|
| 293 | function sfReadFile($filename) { |
|---|
| 294 | $str = ''; |
|---|
| 295 | // バイナリモードでオープン |
|---|
| 296 | $fp = @fopen($filename, 'rb'); |
|---|
| 297 | //ファイル内容を全て変数に読み込む |
|---|
| 298 | if ($fp) { |
|---|
| 299 | $str = @fread($fp, filesize($filename)+1); |
|---|
| 300 | } |
|---|
| 301 | @fclose($fp); |
|---|
| 302 | |
|---|
| 303 | return $str; |
|---|
| 304 | } |
|---|
| 305 | |
|---|
| 306 | /** |
|---|
| 307 | * ファイル書込を行う. |
|---|
| 308 | * |
|---|
| 309 | * @param string $filename ファイルパス |
|---|
| 310 | * @param string $value 書き込み内容 |
|---|
| 311 | * @return boolean ファイルの書き込みに成功した場合 true |
|---|
| 312 | */ |
|---|
| 313 | function sfWriteFile($filename, $value) { |
|---|
| 314 | if (!is_dir(dirname($filename))) { |
|---|
| 315 | SC_Utils_Ex::recursiveMkdir(dirname($filename), 0777); |
|---|
| 316 | } |
|---|
| 317 | $fp = fopen($filename,'w'); |
|---|
| 318 | if ($fp === false) { |
|---|
| 319 | return false; |
|---|
| 320 | } |
|---|
| 321 | if (fwrite($fp, $value) === false) { |
|---|
| 322 | return false; |
|---|
| 323 | } |
|---|
| 324 | return fclose($fp);; |
|---|
| 325 | } |
|---|
| 326 | |
|---|
| 327 | /** |
|---|
| 328 | * ユーザが作成したファイルをアーカイブしダウンロードさせる |
|---|
| 329 | * TODO 要リファクタリング |
|---|
| 330 | * @param string $dir アーカイブを行なうディレクトリ |
|---|
| 331 | * @param string $template_code テンプレートコード |
|---|
| 332 | * @return boolean 成功した場合 true; 失敗した場合 false |
|---|
| 333 | */ |
|---|
| 334 | function downloadArchiveFiles($dir, $template_code) { |
|---|
| 335 | // ダウンロードされるファイル名 |
|---|
| 336 | $dlFileName = 'tpl_package_' . $template_code . '_' . date('YmdHis') . '.tar.gz'; |
|---|
| 337 | |
|---|
| 338 | $debug_message = $dir . ' から ' . $dlFileName . " を作成します...\n"; |
|---|
| 339 | // ファイル一覧取得 |
|---|
| 340 | $arrFileHash = SC_Helper_FileManager_Ex::sfGetFileList($dir); |
|---|
| 341 | $arrFileList = array(); |
|---|
| 342 | foreach ($arrFileHash as $val) { |
|---|
| 343 | $arrFileList[] = $val['file_name']; |
|---|
| 344 | $debug_message.= '圧縮:'.$val['file_name']."\n"; |
|---|
| 345 | } |
|---|
| 346 | GC_Utils_Ex::gfPrintLog($debug_message); |
|---|
| 347 | |
|---|
| 348 | // ディレクトリを移動 |
|---|
| 349 | chdir($dir); |
|---|
| 350 | // 圧縮をおこなう |
|---|
| 351 | $tar = new Archive_Tar($dlFileName, true); |
|---|
| 352 | if ($tar->create($arrFileList)) { |
|---|
| 353 | // ダウンロード用HTTPヘッダ出力 |
|---|
| 354 | header("Content-disposition: attachment; filename=${dlFileName}"); |
|---|
| 355 | header("Content-type: application/octet-stream; name=${dlFileName}"); |
|---|
| 356 | header('Cache-Control: '); |
|---|
| 357 | header('Pragma: '); |
|---|
| 358 | readfile($dlFileName); |
|---|
| 359 | unlink($dir . '/' . $dlFileName); |
|---|
| 360 | return true; |
|---|
| 361 | } else { |
|---|
| 362 | return false; |
|---|
| 363 | } |
|---|
| 364 | } |
|---|
| 365 | |
|---|
| 366 | /** |
|---|
| 367 | * tarアーカイブを解凍する. |
|---|
| 368 | * |
|---|
| 369 | * @param string $path アーカイブパス |
|---|
| 370 | * @return boolean Archive_Tar::extractModify()のエラー |
|---|
| 371 | */ |
|---|
| 372 | function unpackFile($path) { |
|---|
| 373 | // 圧縮フラグTRUEはgzip解凍をおこなう |
|---|
| 374 | $tar = new Archive_Tar($path, true); |
|---|
| 375 | |
|---|
| 376 | $dir = dirname($path); |
|---|
| 377 | $file_name = basename($path); |
|---|
| 378 | |
|---|
| 379 | // 拡張子を切り取る |
|---|
| 380 | $unpacking_name = preg_replace("/(\.tar|\.tar\.gz)$/", '', $file_name); |
|---|
| 381 | |
|---|
| 382 | // 指定されたフォルダ内に解凍する |
|---|
| 383 | $result = $tar->extractModify($dir. '/', $unpacking_name); |
|---|
| 384 | GC_Utils_Ex::gfPrintLog('解凍:' . $dir.'/'.$file_name.'->'.$dir.'/'.$unpacking_name); |
|---|
| 385 | |
|---|
| 386 | // フォルダ削除 |
|---|
| 387 | SC_Helper_FileManager_Ex::deleteFile($dir . '/' . $unpacking_name); |
|---|
| 388 | // 圧縮ファイル削除 |
|---|
| 389 | unlink($path); |
|---|
| 390 | return $result; |
|---|
| 391 | } |
|---|
| 392 | |
|---|
| 393 | /** |
|---|
| 394 | * 指定されたパスの配下を再帰的に削除. |
|---|
| 395 | * |
|---|
| 396 | * @param string $path 削除対象のディレクトリまたはファイルのパス |
|---|
| 397 | * @param boolean $del_myself $pathそのものを削除するか. true なら削除する. |
|---|
| 398 | * @return void |
|---|
| 399 | */ |
|---|
| 400 | function deleteFile($path, $del_myself = true) { |
|---|
| 401 | $flg = false; |
|---|
| 402 | // 対象が存在するかを検証. |
|---|
| 403 | if (file_exists($path) === false) { |
|---|
| 404 | GC_Utils_Ex::gfPrintLog($path . ' が存在しません.'); |
|---|
| 405 | } elseif (is_dir($path)) { |
|---|
| 406 | // ディレクトリが指定された場合 |
|---|
| 407 | $handle = opendir($path); |
|---|
| 408 | if (!$handle) { |
|---|
| 409 | GC_Utils_Ex::gfPrintLog($path . ' が開けませんでした.'); |
|---|
| 410 | } |
|---|
| 411 | while (($item = readdir($handle)) !== false) { |
|---|
| 412 | if ($item === '.' || $item === '..') continue; |
|---|
| 413 | $cur_path = $path . '/' . $item; |
|---|
| 414 | if (is_dir($cur_path)) { |
|---|
| 415 | // ディレクトリの場合、再帰処理 |
|---|
| 416 | $flg = SC_Helper_FileManager_Ex::deleteFile($cur_path); |
|---|
| 417 | } else { |
|---|
| 418 | // ファイルの場合、unlink |
|---|
| 419 | $flg = @unlink($cur_path); |
|---|
| 420 | } |
|---|
| 421 | } |
|---|
| 422 | closedir($handle); |
|---|
| 423 | // ディレクトリを削除 |
|---|
| 424 | GC_Utils_Ex::gfPrintLog($path . ' を削除します.'); |
|---|
| 425 | if ($del_myself) { |
|---|
| 426 | $flg = @rmdir($path); |
|---|
| 427 | } |
|---|
| 428 | } else { |
|---|
| 429 | // ファイルが指定された場合. |
|---|
| 430 | GC_Utils_Ex::gfPrintLog($path . ' を削除します.'); |
|---|
| 431 | $flg = @unlink($path); |
|---|
| 432 | } |
|---|
| 433 | return $flg; |
|---|
| 434 | } |
|---|
| 435 | } |
|---|