| 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 | /** |
|---|
| 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 | if ($dh = opendir($dir)) { |
|---|
| 45 | $cnt = 0; |
|---|
| 46 | // 行末の/を取り除く |
|---|
| 47 | while (($file = readdir($dh)) !== false) $arrDir[] = $file; |
|---|
| 48 | $dir = ereg_replace("/$", "", $dir); |
|---|
| 49 | // アルファベットと数字でソート |
|---|
| 50 | natcasesort($arrDir); |
|---|
| 51 | foreach($arrDir as $file) { |
|---|
| 52 | // ./ と ../を除くファイルのみを取得 |
|---|
| 53 | if($file != "." && $file != "..") { |
|---|
| 54 | |
|---|
| 55 | $path = $dir."/".$file; |
|---|
| 56 | // SELECT内の見た目を整えるため指定文字数で切る |
|---|
| 57 | $file_name = SC_Utils_Ex::sfCutString($file, FILE_NAME_LEN); |
|---|
| 58 | $file_size = SC_Utils_Ex::sfCutString($this->sfGetDirSize($path), FILE_NAME_LEN); |
|---|
| 59 | $file_time = date("Y/m/d", filemtime($path)); |
|---|
| 60 | |
|---|
| 61 | // ディレクトリとファイルで格納配列を変える |
|---|
| 62 | if(is_dir($path)) { |
|---|
| 63 | $arrDirList[$cnt]['file_name'] = $file; |
|---|
| 64 | $arrDirList[$cnt]['file_path'] = $path; |
|---|
| 65 | $arrDirList[$cnt]['file_size'] = $file_size; |
|---|
| 66 | $arrDirList[$cnt]['file_time'] = $file_time; |
|---|
| 67 | $arrDirList[$cnt]['is_dir'] = true; |
|---|
| 68 | } else { |
|---|
| 69 | $arrFileList[$cnt]['file_name'] = $file; |
|---|
| 70 | $arrFileList[$cnt]['file_path'] = $path; |
|---|
| 71 | $arrFileList[$cnt]['file_size'] = $file_size; |
|---|
| 72 | $arrFileList[$cnt]['file_time'] = $file_time; |
|---|
| 73 | $arrFileList[$cnt]['is_dir'] = false; |
|---|
| 74 | } |
|---|
| 75 | $cnt++; |
|---|
| 76 | } |
|---|
| 77 | } |
|---|
| 78 | closedir($dh); |
|---|
| 79 | } |
|---|
| 80 | } |
|---|
| 81 | |
|---|
| 82 | // フォルダを先頭にしてマージ |
|---|
| 83 | return array_merge($arrDirList, $arrFileList); |
|---|
| 84 | } |
|---|
| 85 | |
|---|
| 86 | /** |
|---|
| 87 | * 指定したディレクトリのバイト数を取得する. |
|---|
| 88 | * |
|---|
| 89 | * @param string $dir ディレクトリ |
|---|
| 90 | * @return void |
|---|
| 91 | */ |
|---|
| 92 | function sfGetDirSize($dir) { |
|---|
| 93 | $bytes = 0; |
|---|
| 94 | if(file_exists($dir)) { |
|---|
| 95 | // ディレクトリの場合下層ファイルの総量を取得 |
|---|
| 96 | if (is_dir($dir)) { |
|---|
| 97 | $handle = opendir($dir); |
|---|
| 98 | while ($file = readdir($handle)) { |
|---|
| 99 | // 行末の/を取り除く |
|---|
| 100 | $dir = ereg_replace("/$", "", $dir); |
|---|
| 101 | $path = $dir."/".$file; |
|---|
| 102 | if ($file != '..' && $file != '.' && !is_dir($path)) { |
|---|
| 103 | $bytes += filesize($path); |
|---|
| 104 | } else if (is_dir($path) && $file != '..' && $file != '.') { |
|---|
| 105 | // 下層ファイルのバイト数を取得する為、再帰的に呼び出す。 |
|---|
| 106 | $bytes += $this->sfGetDirSize($path); |
|---|
| 107 | } |
|---|
| 108 | } |
|---|
| 109 | } else { |
|---|
| 110 | // ファイルの場合 |
|---|
| 111 | $bytes = filesize($dir); |
|---|
| 112 | } |
|---|
| 113 | } |
|---|
| 114 | // ディレクトリ(ファイル)が存在しない場合は0byteを返す |
|---|
| 115 | if($bytes == "") $bytes = 0; |
|---|
| 116 | |
|---|
| 117 | return $bytes; |
|---|
| 118 | } |
|---|
| 119 | |
|---|
| 120 | /** |
|---|
| 121 | * 指定したディレクトリ又はファイルを削除する. |
|---|
| 122 | * |
|---|
| 123 | * @param string $dir 削除するディレクトリ又はファイル |
|---|
| 124 | * @return void |
|---|
| 125 | */ |
|---|
| 126 | function sfDeleteDir($dir) { |
|---|
| 127 | $arrResult = array(); |
|---|
| 128 | if(file_exists($dir)) { |
|---|
| 129 | // ディレクトリかチェック |
|---|
| 130 | if (is_dir($dir)) { |
|---|
| 131 | if ($handle = opendir("$dir")) { |
|---|
| 132 | $cnt = 0; |
|---|
| 133 | while (false !== ($item = readdir($handle))) { |
|---|
| 134 | if ($item != "." && $item != "..") { |
|---|
| 135 | if (is_dir("$dir/$item")) { |
|---|
| 136 | $this->sfDeleteDir("$dir/$item"); |
|---|
| 137 | } else { |
|---|
| 138 | $arrResult[$cnt]['result'] = @unlink("$dir/$item"); |
|---|
| 139 | $arrResult[$cnt]['file_name'] = "$dir/$item"; |
|---|
| 140 | } |
|---|
| 141 | } |
|---|
| 142 | $cnt++; |
|---|
| 143 | } |
|---|
| 144 | } |
|---|
| 145 | closedir($handle); |
|---|
| 146 | $arrResult[$cnt]['result'] = @rmdir($dir); |
|---|
| 147 | $arrResult[$cnt]['file_name'] = "$dir/$item"; |
|---|
| 148 | } else { |
|---|
| 149 | // ファイル削除 |
|---|
| 150 | $arrResult[0]['result'] = @unlink("$dir"); |
|---|
| 151 | $arrResult[0]['file_name'] = "$dir"; |
|---|
| 152 | } |
|---|
| 153 | } |
|---|
| 154 | |
|---|
| 155 | return $arrResult; |
|---|
| 156 | } |
|---|
| 157 | |
|---|
| 158 | /** |
|---|
| 159 | * ツリー生成用配列取得(javascriptに渡す用). |
|---|
| 160 | * |
|---|
| 161 | * @param string $dir ディレクトリ |
|---|
| 162 | * @param string $tree_status 現在のツリーの状態開いているフォルダのパスを |
|---|
| 163 | * | 区切りで格納 |
|---|
| 164 | * @return array ツリー生成用の配列 |
|---|
| 165 | */ |
|---|
| 166 | function sfGetFileTree($dir, $tree_status) { |
|---|
| 167 | |
|---|
| 168 | $cnt = 0; |
|---|
| 169 | $arrTree = array(); |
|---|
| 170 | $default_rank = count(split('/', $dir)); |
|---|
| 171 | |
|---|
| 172 | // 文末の/を取り除く |
|---|
| 173 | $dir = ereg_replace("/$", "", $dir); |
|---|
| 174 | // 最上位層を格納(user_data/) |
|---|
| 175 | if($this->sfDirChildExists($dir)) { |
|---|
| 176 | $arrTree[$cnt]['type'] = "_parent"; |
|---|
| 177 | } else { |
|---|
| 178 | $arrTree[$cnt]['type'] = "_child"; |
|---|
| 179 | } |
|---|
| 180 | $arrTree[$cnt]['path'] = $dir; |
|---|
| 181 | $arrTree[$cnt]['rank'] = 0; |
|---|
| 182 | $arrTree[$cnt]['count'] = $cnt; |
|---|
| 183 | // 初期表示はオープン |
|---|
| 184 | if($_POST['mode'] != '') { |
|---|
| 185 | $arrTree[$cnt]['open'] = $this->lfIsFileOpen($dir, $tree_status); |
|---|
| 186 | } else { |
|---|
| 187 | $arrTree[$cnt]['open'] = true; |
|---|
| 188 | } |
|---|
| 189 | $cnt++; |
|---|
| 190 | |
|---|
| 191 | $this->sfGetFileTreeSub($dir, $default_rank, $cnt, $arrTree, $tree_status); |
|---|
| 192 | |
|---|
| 193 | return $arrTree; |
|---|
| 194 | } |
|---|
| 195 | |
|---|
| 196 | /** |
|---|
| 197 | * ツリー生成用配列取得(javascriptに渡す用). |
|---|
| 198 | * |
|---|
| 199 | * @param string $dir ディレクトリ |
|---|
| 200 | * @param string $default_rank デフォルトの階層 |
|---|
| 201 | * (/区切りで 0,1,2・・・とカウント) |
|---|
| 202 | * @param integer $cnt 連番 |
|---|
| 203 | * @param string $tree_status 現在のツリーの状態開いているフォルダのパスが |
|---|
| 204 | * | 区切りで格納 |
|---|
| 205 | * @return array ツリー生成用の配列 |
|---|
| 206 | */ |
|---|
| 207 | function sfGetFileTreeSub($dir, $default_rank, &$cnt, &$arrTree, $tree_status) { |
|---|
| 208 | |
|---|
| 209 | if(file_exists($dir)) { |
|---|
| 210 | if ($handle = opendir("$dir")) { |
|---|
| 211 | while (false !== ($item = readdir($handle))) $arrDir[] = $item; |
|---|
| 212 | // アルファベットと数字でソート |
|---|
| 213 | natcasesort($arrDir); |
|---|
| 214 | foreach($arrDir as $item) { |
|---|
| 215 | if ($item != "." && $item != "..") { |
|---|
| 216 | // 文末の/を取り除く |
|---|
| 217 | $dir = ereg_replace("/$", "", $dir); |
|---|
| 218 | $path = $dir."/".$item; |
|---|
| 219 | // ディレクトリのみ取得 |
|---|
| 220 | if (is_dir($path)) { |
|---|
| 221 | $arrTree[$cnt]['path'] = $path; |
|---|
| 222 | if($this->sfDirChildExists($path)) { |
|---|
| 223 | $arrTree[$cnt]['type'] = "_parent"; |
|---|
| 224 | } else { |
|---|
| 225 | $arrTree[$cnt]['type'] = "_child"; |
|---|
| 226 | } |
|---|
| 227 | |
|---|
| 228 | // 階層を割り出す |
|---|
| 229 | $arrCnt = split('/', $path); |
|---|
| 230 | $rank = count($arrCnt); |
|---|
| 231 | $arrTree[$cnt]['rank'] = $rank - $default_rank + 1; |
|---|
| 232 | $arrTree[$cnt]['count'] = $cnt; |
|---|
| 233 | // フォルダが開いているか |
|---|
| 234 | $arrTree[$cnt]['open'] = $this->lfIsFileOpen($path, $tree_status); |
|---|
| 235 | $cnt++; |
|---|
| 236 | // 下層ディレクトリ取得の為、再帰的に呼び出す |
|---|
| 237 | $this->sfGetFileTreeSub($path, $default_rank, $cnt, $arrTree, $tree_status); |
|---|
| 238 | } |
|---|
| 239 | } |
|---|
| 240 | } |
|---|
| 241 | } |
|---|
| 242 | closedir($handle); |
|---|
| 243 | } |
|---|
| 244 | } |
|---|
| 245 | |
|---|
| 246 | /** |
|---|
| 247 | * 指定したディレクトリ配下にファイルがあるかチェックする. |
|---|
| 248 | * |
|---|
| 249 | * @param string ディレクトリ |
|---|
| 250 | * @return bool ファイルが存在する場合 true |
|---|
| 251 | */ |
|---|
| 252 | function sfDirChildExists($dir) { |
|---|
| 253 | if(file_exists($dir)) { |
|---|
| 254 | if (is_dir($dir)) { |
|---|
| 255 | $handle = opendir($dir); |
|---|
| 256 | while ($file = readdir($handle)) { |
|---|
| 257 | // 行末の/を取り除く |
|---|
| 258 | $dir = ereg_replace("/$", "", $dir); |
|---|
| 259 | $path = $dir."/".$file; |
|---|
| 260 | if ($file != '..' && $file != '.' && is_dir($path)) { |
|---|
| 261 | return true; |
|---|
| 262 | } |
|---|
| 263 | } |
|---|
| 264 | } |
|---|
| 265 | } |
|---|
| 266 | |
|---|
| 267 | return false; |
|---|
| 268 | } |
|---|
| 269 | |
|---|
| 270 | /** |
|---|
| 271 | * 指定したファイルが前回開かれた状態にあったかチェックする. |
|---|
| 272 | * |
|---|
| 273 | * @param string $dir ディレクトリ |
|---|
| 274 | * @param string $tree_status 現在のツリーの状態開いているフォルダのパスが |
|---|
| 275 | * | 区切りで格納 |
|---|
| 276 | * @return bool 前回開かれた状態の場合 true |
|---|
| 277 | */ |
|---|
| 278 | function lfIsFileOpen($dir, $tree_status) { |
|---|
| 279 | $arrTreeStatus = split('\|', $tree_status); |
|---|
| 280 | if(in_array($dir, $arrTreeStatus)) { |
|---|
| 281 | return true; |
|---|
| 282 | } |
|---|
| 283 | |
|---|
| 284 | return false; |
|---|
| 285 | } |
|---|
| 286 | |
|---|
| 287 | /** |
|---|
| 288 | * ファイルのダウンロードを行う. |
|---|
| 289 | * |
|---|
| 290 | * @param string $file ファイルパス |
|---|
| 291 | * @return void |
|---|
| 292 | */ |
|---|
| 293 | function sfDownloadFile($file) { |
|---|
| 294 | // ファイルの場合はダウンロードさせる |
|---|
| 295 | Header("Content-disposition: attachment; filename=".basename($file)); |
|---|
| 296 | Header("Content-type: application/octet-stream; name=".basename($file)); |
|---|
| 297 | Header("Cache-Control: "); |
|---|
| 298 | Header("Pragma: "); |
|---|
| 299 | echo ($this->sfReadFile($file)); |
|---|
| 300 | } |
|---|
| 301 | |
|---|
| 302 | /** |
|---|
| 303 | * ファイル作成を行う. |
|---|
| 304 | * |
|---|
| 305 | * @param string $file ファイルパス |
|---|
| 306 | * @param integer $mode パーミッション |
|---|
| 307 | * @return bool ファイル作成に成功した場合 true |
|---|
| 308 | */ |
|---|
| 309 | function sfCreateFile($file, $mode = "") { |
|---|
| 310 | // 行末の/を取り除く |
|---|
| 311 | if($mode != "") { |
|---|
| 312 | $ret = @mkdir($file, $mode); |
|---|
| 313 | } else { |
|---|
| 314 | $ret = @mkdir($file); |
|---|
| 315 | } |
|---|
| 316 | |
|---|
| 317 | return $ret; |
|---|
| 318 | } |
|---|
| 319 | |
|---|
| 320 | /** |
|---|
| 321 | * ファイル読込を行う. |
|---|
| 322 | * |
|---|
| 323 | * @param string ファイルパス |
|---|
| 324 | * @return string ファイルの内容 |
|---|
| 325 | */ |
|---|
| 326 | function sfReadFile($filename) { |
|---|
| 327 | $str = ""; |
|---|
| 328 | // バイナリモードでオープン |
|---|
| 329 | $fp = @fopen($filename, "rb" ); |
|---|
| 330 | //ファイル内容を全て変数に読み込む |
|---|
| 331 | if($fp) { |
|---|
| 332 | $str = @fread($fp, filesize($filename)+1); |
|---|
| 333 | } |
|---|
| 334 | @fclose($fp); |
|---|
| 335 | |
|---|
| 336 | return $str; |
|---|
| 337 | } |
|---|
| 338 | } |
|---|
| 339 | ?> |
|---|