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

Revision 20538, 17.8 KB checked in by Seasoft, 13 years ago (diff)

#627(ソース整形・ソースコメントの改善)
#628(未使用処理・定義などの削除)

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