source: branches/version-2_12-dev/data/class/helper/SC_Helper_FileManager.php @ 21866

Revision 21866, 15.6 KB checked in by h_yoshimoto, 12 years ago (diff)

#1831 Copyrightを更新を誤っているので戻します

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