source: branches/version-2_13-dev/data/class/pages/admin/contents/LC_Page_Admin_Contents_FileManager.php @ 22856

Revision 22856, 18.0 KB checked in by Seasoft, 11 years ago (diff)

#2043 (typo修正・ソース整形・ソースコメントの改善 for 2.13.0)

  • 主に空白・空白行の調整。もう少し整えたいが、一旦現状コミット。
  • 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
24require_once CLASS_EX_REALDIR . 'page_extends/admin/LC_Page_Admin_Ex.php';
25
26/**
27 * ファイル管理 のページクラス.
28 *
29 * @package Page
30 * @author LOCKON CO.,LTD.
31 * @version $Id$
32 */
33class LC_Page_Admin_Contents_FileManager extends LC_Page_Admin_Ex
34{
35    /**
36     * Page を初期化する.
37     *
38     * @return void
39     */
40    function init()
41    {
42        parent::init();
43        $this->tpl_mainpage = 'contents/file_manager.tpl';
44        $this->tpl_mainno = 'contents';
45        $this->tpl_subno = 'file';
46        $this->tpl_maintitle = 'コンテンツ管理';
47        $this->tpl_subtitle = 'ファイル管理';
48
49    }
50
51    /**
52     * Page のプロセス.
53     *
54     * @return void
55     */
56    function process()
57    {
58        $this->action();
59        $this->sendResponse();
60    }
61
62    /**
63     * Page のアクション.
64     *
65     * @return void
66     */
67    function action()
68    {
69        // フォーム操作クラス
70        $objFormParam = new SC_FormParam_Ex();
71        // パラメーター情報の初期化
72        $this->lfInitParam($objFormParam);
73        $objFormParam->setParam($this->createSetParam($_POST));
74        $objFormParam->convParam();
75
76        // ファイル管理クラス
77        $objUpFile = new SC_UploadFile_Ex($objFormParam->getValue('now_dir'), $objFormParam->getValue('now_dir'));
78        // ファイル情報の初期化
79        $this->lfInitFile($objUpFile);
80
81        // ファイル操作クラス
82        $objFileManager = new SC_Helper_FileManager_Ex();
83
84        switch ($this->getMode()) {
85            // フォルダ移動
86            case 'move':
87                $objFormParam = new SC_FormParam_Ex();
88                $this->lfInitParamModeMove($objFormParam);
89                $objFormParam->setParam($this->createSetParam($_POST));
90                $objFormParam->convParam();
91
92                $this->arrErr = $objFormParam->checkError();
93                if (SC_Utils_Ex::isBlank($this->arrErr)) {
94                    $now_dir = $this->lfCheckSelectDir($objFormParam, $objFormParam->getValue('tree_select_file'));
95                    $objFormParam->setValue('now_dir', $now_dir);
96                }
97                break;
98
99            // ファイル表示
100            case 'view':
101                $objFormParam = new SC_FormParam_Ex();
102                $this->lfInitParamModeView($objFormParam);
103                $objFormParam->setParam($this->createSetParam($_POST));
104                $objFormParam->convParam();
105
106                $this->arrErr = $objFormParam->checkError();
107                if (SC_Utils_Ex::isBlank($this->arrErr)) {
108                    if ($this->tryView($objFormParam)) {
109                        $pattern = '/' . preg_quote($objFormParam->getValue('top_dir'), '/') . '/';
110                        $file_url = htmlspecialchars(preg_replace($pattern, '', $objFormParam->getValue('select_file')));
111                        $tpl_onload = "win02('./file_view.php?file=". $file_url ."', 'user_data', '600', '400');";
112                        $this->setTplOnLoad($tpl_onload);
113                    }
114                }
115                break;
116
117            // ファイルダウンロード
118            case 'download':
119                $objFormParam = new SC_FormParam_Ex();
120                $this->lfInitParamModeView($objFormParam);
121                $objFormParam->setParam($this->createSetParam($_POST));
122                $objFormParam->convParam();
123
124                $this->arrErr = $objFormParam->checkError();
125                if (SC_Utils_Ex::isBlank($this->arrErr)) {
126                    if (is_dir($objFormParam->getValue('select_file'))) {
127                        $disp_error = '※ ディレクトリをダウンロードすることは出来ません。<br/>';
128                        $this->setDispError('select_file', $disp_error);
129                    } else {
130                        // ファイルダウンロード
131                        $objFileManager->sfDownloadFile($objFormParam->getValue('select_file'));
132                        SC_Response_Ex::actionExit();
133                    }
134                }
135                break;
136            // ファイル削除
137            case 'delete':
138                $objFormParam = new SC_FormParam_Ex();
139                $this->lfInitParamModeView($objFormParam);
140                $objFormParam->setParam($this->createSetParam($_POST));
141                $objFormParam->convParam();
142
143                $this->arrErr = $objFormParam->checkError();
144                if (SC_Utils_Ex::isBlank($this->arrErr)) {
145                    $objFileManager->deleteFile($objFormParam->getValue('select_file'));
146                }
147                break;
148            // ファイル作成
149            case 'create':
150                $objFormParam = new SC_FormParam_Ex();
151                $this->lfInitParamModeCreate($objFormParam);
152                $objFormParam->setParam($this->createSetParam($_POST));
153                $objFormParam->convParam();
154
155                $this->arrErr = $objFormParam->checkError();
156                if (SC_Utils_Ex::isBlank($this->arrErr)) {
157                    if (!$this->tryCreateDir($objFileManager, $objFormParam)) {
158                        $disp_error = '※ '.htmlspecialchars($objFormParam->getValue('create_file'), ENT_QUOTES).'の作成に失敗しました。<br/>';
159                        $this->setDispError('create_file', $disp_error);
160                    } else {
161                        $tpl_onload = "alert('フォルダを作成しました。');";
162                        $this->setTplOnLoad($tpl_onload);
163                    }
164                }
165                break;
166            // ファイルアップロード
167            case 'upload':
168                // 画像保存処理
169                $ret = $objUpFile->makeTempFile('upload_file', false);
170                if (SC_Utils_Ex::isBlank($ret)) {
171                    $tpl_onload = "alert('ファイルをアップロードしました。');";
172                    $this->setTplOnLoad($tpl_onload);
173                } else {
174                    $this->setDispError('upload_file', $ret);
175                }
176                break;
177            // 初期表示
178            default:
179                break;
180        }
181
182        // 値をテンプレートに渡す
183        $this->arrParam = $objFormParam->getHashArray();
184        // 現在の階層がルートディレクトリかどうかテンプレートに渡す
185        $this->setIsTopDir($objFormParam);
186        // 現在の階層より一つ上の階層をテンプレートに渡す
187        $this->setParentDir($objFormParam);
188        // 現在いる階層(表示用)をテンプレートに渡す
189        $this->setDispPath($objFormParam);
190        // 現在のディレクトリ配下のファイル一覧を取得
191        $this->arrFileList = $objFileManager->sfGetFileList($objFormParam->getValue('now_dir'));
192        // 現在の階層のディレクトリをテンプレートに渡す
193        $this->setDispParam('tpl_now_file', $objFormParam->getValue('now_dir'));
194        // ディレクトリツリー表示
195        $this->setDispTree($objFileManager, $objFormParam);
196
197    }
198
199    /**
200     * デストラクタ.
201     *
202     * @return void
203     */
204    function destroy()
205    {
206        parent::destroy();
207    }
208
209    /**
210     * 初期化を行う.
211     *
212     * @param SC_FormParam $objFormParam SC_FormParamインスタンス
213     * @return void
214     */
215    function lfInitParam(&$objFormParam)
216    {
217        // 共通定義
218        $this->lfInitParamCommon($objFormParam);
219    }
220
221    /**
222     * ディレクトリ移動時、パラメーター定義
223     *
224     * @param SC_FormParam $objFormParam SC_FormParam インスタンス
225     * @return void
226     */
227    function lfInitParamModeMove(&$objFormParam)
228    {
229        // 共通定義
230        $this->lfInitParamCommon($objFormParam);
231        $objFormParam->addParam('選択ファイル', 'select_file', MTEXT_LEN, 'a', array());
232    }
233
234    /**
235     * ファイル表示時、パラメーター定義
236     *
237     * @param SC_FormParam $objFormParam SC_FormParam インスタンス
238     * @return void
239     */
240    function lfInitParamModeView(&$objFormParam)
241    {
242        // 共通定義
243        $this->lfInitParamCommon($objFormParam);
244        $objFormParam->addParam('選択ファイル', 'select_file', MTEXT_LEN, 'a', array('SELECT_CHECK'));
245    }
246
247    /**
248     * ファイル表示時、パラメーター定義
249     *
250     * @param SC_FormParam $objFormParam SC_FormParam インスタンス
251     * @return void
252     */
253    function lfInitParamModeCreate(&$objFormParam)
254    {
255        // 共通定義
256        $this->lfInitParamCommon($objFormParam);
257        $objFormParam->addParam('選択ファイル', 'select_file', MTEXT_LEN, 'a', array());
258        $objFormParam->addParam('作成ファイル名', 'create_file', MTEXT_LEN, 'a', array('EXIST_CHECK', 'FILE_NAME_CHECK_BY_NOUPLOAD'));
259    }
260
261    /**
262     * ファイル表示時、パラメーター定義
263     *
264     * @param SC_FormParam $objFormParam SC_FormParam インスタンス
265     * @return void
266     */
267    function lfInitParamCommon(&$objFormParam)
268    {
269        $objFormParam->addParam('ルートディレクトリ', 'top_dir', MTEXT_LEN, 'a', array());
270        $objFormParam->addParam('現在の階層ディレクトリ', 'now_dir', MTEXT_LEN, 'a', array());
271        $objFormParam->addParam('現在の階層ファイル', 'now_file', MTEXT_LEN, 'a', array());
272        $objFormParam->addParam('ツリー選択状態', 'tree_status', MTEXT_LEN, 'a', array());
273        $objFormParam->addParam('ツリー選択ディレクトリ', 'tree_select_file', MTEXT_LEN, 'a', array());
274    }
275
276    /*
277     * ファイル情報の初期化
278     *
279     * @param object $objUpFile SC_UploadFileインスタンス
280     * @return void
281     */
282    function lfInitFile(&$objUpFile)
283    {
284        $objUpFile->addFile('ファイル', 'upload_file', array(), FILE_SIZE, true, 0, 0, false);
285    }
286
287    /**
288     * テンプレートに渡す値を整形する
289     *
290     * @param array $arrVal $_POST
291     * @return array $setParam テンプレートに渡す値
292     */
293    function createSetParam($arrVal)
294    {
295        $setParam = $arrVal;
296        // Windowsの場合は, ディレクトリの区切り文字を\から/に変換する
297        $setParam['top_dir'] = (strpos(PHP_OS, 'WIN') === false) ? USER_REALDIR : str_replace('\\', '/', USER_REALDIR);
298        // 初期表示はルートディレクトリ(user_data/)を表示
299        if (SC_Utils_Ex::isBlank($this->getMode())) {
300            $setParam['now_dir'] = $setParam['top_dir'];
301        }
302
303        return $setParam;
304    }
305
306    /**
307     * テンプレートに値を渡す
308     *
309     * @param string $key キー名
310     * @param string $val 値
311     * @return void
312     */
313    function setDispParam($key, $val)
314    {
315        $this->$key = $val;
316    }
317
318    /**
319     * ディレクトリを作成
320     *
321     * @param object $objFileManager SC_Helper_FileManager_Exインスタンス
322     * @param SC_FormParam $objFormParam SC_FormParamインスタンス
323     * @return boolean ディレクトリ作成できたかどうか
324     */
325    function tryCreateDir($objFileManager, $objFormParam)
326    {
327        $create_dir_flg = false;
328        $create_dir = rtrim($objFormParam->getValue('now_dir'), '/');
329        // ファイル作成
330        if ($objFileManager->sfCreateFile($create_dir.'/'.$objFormParam->getValue('create_file'), 0755)) {
331            $create_dir_flg = true;
332        }
333
334        return $create_dir_flg;
335    }
336
337    /**
338     * ファイル表示を行う
339     *
340     * @param SC_FormParam $objFormParam SC_FormParamインスタンス
341     * @return boolean ファイル表示するかどうか
342     */
343    function tryView(&$objFormParam)
344    {
345        $view_flg = false;
346        $now_dir = $this->lfCheckSelectDir($objFormParam, dirname($objFormParam->getValue('select_file')));
347        $objFormParam->setValue('now_dir', $now_dir);
348        if (!strpos($objFormParam->getValue('select_file'), $objFormParam->getValue('top_dir'))) {
349            $view_flg = true;
350        }
351
352        return $view_flg;
353    }
354
355    /**
356     * 現在の階層の一つ上の階層のディレクトリをテンプレートに渡す
357     *
358     * @param SC_FormParam $objFormParam SC_FormParamインスタンス
359     * @return void
360     */
361    function setParentDir($objFormParam)
362    {
363        $parent_dir = $this->lfGetParentDir($objFormParam->getValue('now_dir'));
364        $this->setDispParam('tpl_parent_dir', $parent_dir);
365    }
366
367    /**
368     * 現在の階層のパスをテンプレートに渡す
369     *
370     * @param SC_FormParam $objFormParam SC_FormParamインスタンス
371     * @return void
372     */
373    function setDispPath($objFormParam)
374    {
375        $tpl_now_dir = '';
376        // Windows 環境で DIRECTORY_SEPARATOR が JavaScript に渡るとエスケープ文字と勘違いするので置換
377        $html_realdir = str_replace(DIRECTORY_SEPARATOR, '/', HTML_REALDIR);
378        $arrNowDir = preg_split('/\//', str_replace($html_realdir, '', $objFormParam->getValue('now_dir')));
379        $this->setDispParam('tpl_now_dir', SC_Utils_Ex::jsonEncode($arrNowDir));
380        $this->setDispParam('tpl_file_path', $html_realdir);
381    }
382
383    /**
384     * エラーを表示用の配列に格納
385     *
386     * @param string $key キー名
387     * @param string $value エラー内容
388     * @return void
389     */
390    function setDispError($key, $value)
391    {
392        // 既にエラーがある場合は、処理しない
393        if (SC_Utils_Ex::isBlank($this->arrErr[$key])) {
394            $this->arrErr[$key] = $value;
395        }
396    }
397
398    /**
399     * javascriptをテンプレートに渡す
400     *
401     * @param string $tpl_onload javascript
402     * @return void
403     */
404    function setTplOnLoad($tpl_onload)
405    {
406        $this->tpl_onload .= $tpl_onload;
407    }
408
409    /*
410     * 選択ディレクトリがUSER_REALDIR以下かチェック
411     *
412     * @param object $objFormParam SC_FormParamインスタンス
413     * @param string $dir ディレクトリ
414     * @return string $select_dir 選択ディレクトリ
415     */
416    function lfCheckSelectDir($objFormParam, $dir)
417    {
418        $select_dir = '';
419        $top_dir = $objFormParam->getValue('top_dir');
420        // USER_REALDIR以下の場合
421        if (preg_match("@^\Q". $top_dir. "\E@", $dir) > 0) {
422            // 相対パスがある場合、USER_REALDIRを返す.
423            if (preg_match("@\Q..\E@", $dir) > 0) {
424                $select_dir = $top_dir;
425            // 相対パスがない場合、そのままディレクトリパスを返す.
426            } else {
427                $select_dir= $dir;
428            }
429        // USER_REALDIR以下でない場合、USER_REALDIRを返す.
430        } else {
431            $select_dir = $top_dir;
432        }
433
434        return $select_dir;
435    }
436
437    /**
438     * 親ディレクトリ取得
439     *
440     * @param string $dir 現在いるディレクトリ
441     * @return string $parent_dir 親ディレクトリ
442     */
443    function lfGetParentDir($dir)
444    {
445        $parent_dir = '';
446        $dir = rtrim($dir, '/');
447        $arrDir = explode('/', $dir);
448        array_pop($arrDir);
449        foreach ($arrDir as $val) {
450            $parent_dir .= "$val/";
451        }
452        $parent_dir = rtrim($parent_dir, '/');
453
454        return $parent_dir;
455    }
456
457    /**
458     * ディレクトリツリー生成
459     *
460     * @param object $objFileManager SC_Helper_FileManager_Exインスタンス
461     * @param SC_FormParam $objFormParam SC_FormParamインスタンス
462     * @return void
463     */
464    function setDispTree($objFileManager, $objFormParam)
465    {
466        $tpl_onload = '';
467        // ツリーを表示する divタグid, ツリー配列変数名, 現在ディレクトリ, 選択ツリーhidden名, ツリー状態hidden名, mode hidden名
468        $now_dir = $objFormParam->getValue('now_dir');
469        $treeView = "fnTreeView('tree', arrTree, '$now_dir', 'tree_select_file', 'tree_status', 'move');";
470        if (!empty($this->tpl_onload)) {
471            $tpl_onload .= $treeView;
472        } else {
473            $tpl_onload = $treeView;
474        }
475        $this->setTplOnLoad($tpl_onload);
476
477        $tpl_javascript = '';
478        $arrTree = $objFileManager->sfGetFileTree($objFormParam->getValue('top_dir'), $objFormParam->getValue('tree_status'));
479        $tpl_javascript .= "arrTree = new Array();\n";
480        foreach ($arrTree as $arrVal) {
481            $tpl_javascript .= 'arrTree['.$arrVal['count'].'] = new Array('.$arrVal['count'].", '".$arrVal['type']."', '".$arrVal['path']."', ".$arrVal['rank'].',';
482            if ($arrVal['open']) {
483                $tpl_javascript .= "true);\n";
484            } else {
485                $tpl_javascript .= "false);\n";
486            }
487        }
488        $this->setDispParam('tpl_javascript', $tpl_javascript);
489    }
490
491    /**
492     * 現在の階層がルートディレクトリかどうかテンプレートに渡す
493     *
494     * @param object $objFormParam SC_FormParamインスタンス
495     * @return void
496     */
497    function setIsTopDir($objFormParam)
498    {
499        // トップディレクトリか調査
500        $is_top_dir = false;
501        // 末尾の/をとる
502        $top_dir_check = rtrim($objFormParam->getValue('top_dir'), '/');
503        $now_dir_check = rtrim($objFormParam->getValue('now_dir'), '/');
504        if ($top_dir_check == $now_dir_check) {
505            $is_top_dir = true;
506        }
507        $this->setDispParam('tpl_is_top_dir', $is_top_dir);
508    }
509
510}
Note: See TracBrowser for help on using the repository browser.