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