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

Revision 22567, 15.7 KB checked in by shutta, 11 years ago (diff)

#2043 (typo修正・ソース整形・ソースコメントの改善 for 2.12.4)
Zend Framework PHP 標準コーディング規約のコーディングスタイルへ準拠。
classおよびfunctionの開始波括弧「{」のスタイルを修正。

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