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

Revision 20208, 17.8 KB checked in by kishida, 12 years ago (diff)

#1016 コンテンツ管理リファクタリング

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