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

Revision 21689, 18.6 KB checked in by h_yoshimoto, 12 years ago (diff)

#1692 インスタンスを呼び出す処理を統一

  • 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// {{{ requires
25require_once CLASS_EX_REALDIR . 'page_extends/admin/LC_Page_Admin_Ex.php';
26
27/**
28 * ファイル管理 のページクラス.
29 *
30 * @package Page
31 * @author LOCKON CO.,LTD.
32 * @version $Id$
33 */
34class LC_Page_Admin_Contents_FileManager extends LC_Page_Admin_Ex {
35
36    // }}}
37    // {{{ functions
38
39    /**
40     * Page を初期化する.
41     *
42     * @return void
43     */
44    function init() {
45        parent::init();
46        $this->tpl_mainpage = 'contents/file_manager.tpl';
47        $this->tpl_mainno = 'contents';
48        $this->tpl_subno = 'file';
49        $this->tpl_maintitle = 'コンテンツ管理';
50        $this->tpl_subtitle = 'ファイル管理';
51
52    }
53
54    /**
55     * Page のプロセス.
56     *
57     * @return void
58     */
59    function process() {
60        $this->action();
61        $this->sendResponse();
62    }
63
64    /**
65     * Page のアクション.
66     *
67     * @return void
68     */
69    function action() {
70        // フックポイント.
71        $objPlugin = SC_Helper_Plugin_Ex::getSingletonInstance($this->plugin_activate_flg);
72        $objPlugin->doAction('lc_page_admin_contents_filemanager_action_start', array($this));
73
74        // フォーム操作クラス
75        $objFormParam = new SC_FormParam_Ex();
76        // パラメーター情報の初期化
77        $this->lfInitParam($objFormParam);
78        $objFormParam->setParam($this->createSetParam($_POST));
79        $objFormParam->convParam();
80
81        // ファイル管理クラス
82        $objUpFile = new SC_UploadFile_Ex($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_Ex();
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_Ex();
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_Ex();
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                        $disp_error = '※ ディレクトリをダウンロードすることは出来ません。<br/>';
132                        $this->setDispError('select_file', $disp_error);
133                    } else {
134                        // フックポイント.
135                        $objPlugin = SC_Helper_Plugin_Ex::getSingletonInstance($this->plugin_activate_flg);
136                        $objPlugin->doAction('lc_page_admin_contents_filemanager_action_download', array($this));
137
138                        // ファイルダウンロード
139                        $objFileManager->sfDownloadFile($objFormParam->getValue('select_file'));
140                        exit;
141                    }
142                }
143                break;
144            // ファイル削除
145            case 'delete':
146                $objFormParam = new SC_FormParam_Ex();
147                $this->lfInitParamModeView($objFormParam);
148                $objFormParam->setParam($this->createSetParam($_POST));
149                $objFormParam->convParam();
150
151                $this->arrErr = $objFormParam->checkError();
152                if (SC_Utils_Ex::isBlank($this->arrErr)) {
153                    $objFileManager->sfDeleteDir($objFormParam->getValue('select_file'));
154                }
155                break;
156            // ファイル作成
157            case 'create':
158                $objFormParam = new SC_FormParam_Ex();
159                $this->lfInitParamModeCreate($objFormParam);
160                $objFormParam->setParam($this->createSetParam($_POST));
161                $objFormParam->convParam();
162
163                $this->arrErr = $objFormParam->checkError();
164                if (SC_Utils_Ex::isBlank($this->arrErr)) {
165                    if (!$this->tryCreateDir($objFileManager, $objFormParam)) {
166                        $disp_error = '※ '.htmlspecialchars($objFormParam->getValue('create_file'), ENT_QUOTES).'の作成に失敗しました。<br/>';
167                        $this->setDispError('create_file', $disp_error);
168                    } else {
169                        $tpl_onload = "alert('フォルダを作成しました。');";
170                        $this->setTplOnLoad($tpl_onload);
171                    }
172                }
173                break;
174            // ファイルアップロード
175            case 'upload':
176                // 画像保存処理
177                $ret = $objUpFile->makeTempFile('upload_file', false);
178                if (SC_Utils_Ex::isBlank($ret)) {
179                    $tpl_onload = "alert('ファイルをアップロードしました。');";
180                    $this->setTplOnLoad($tpl_onload);
181                } else {
182                    $this->setDispError('upload_file', $ret);
183                }
184                break;
185            // 初期表示
186            default:
187                break;
188        }
189
190        // 値をテンプレートに渡す
191        $this->arrParam = $objFormParam->getHashArray();
192        // 現在の階層がルートディレクトリかどうかテンプレートに渡す
193        $this->setIsTopDir($objFormParam);
194        // 現在の階層より一つ上の階層をテンプレートに渡す
195        $this->setParentDir($objFormParam);
196        // 現在いる階層(表示用)をテンプレートに渡す
197        $this->setDispPath($objFormParam);
198        // 現在のディレクトリ配下のファイル一覧を取得
199        $this->arrFileList = $objFileManager->sfGetFileList($objFormParam->getValue('now_dir'));
200        // 現在の階層のディレクトリをテンプレートに渡す
201        $this->setDispParam('tpl_now_file', $objFormParam->getValue('now_dir'));
202        // ディレクトリツリー表示
203        $this->setDispTree($objFileManager, $objFormParam);
204
205        // フックポイント.
206        $objPlugin = SC_Helper_Plugin_Ex::getSingletonInstance($this->plugin_activate_flg);
207        $objPlugin->doAction('lc_page_admin_contents_filemanager_action_end', array($this));
208    }
209
210    /**
211     * デストラクタ.
212     *
213     * @return void
214     */
215    function destroy() {
216        parent::destroy();
217    }
218
219    /**
220     * 初期化を行う.
221     *
222     * @param SC_FormParam $objFormParam SC_FormParamインスタンス
223     * @return void
224     */
225    function lfInitParam(&$objFormParam) {
226        // 共通定義
227        $this->lfInitParamCommon($objFormParam);
228    }
229
230    /**
231     * ディレクトリ移動時、パラメーター定義
232     *
233     * @param SC_FormParam $objFormParam SC_FormParam インスタンス
234     * @return void
235     */
236    function lfInitParamModeMove(&$objFormParam) {
237        // 共通定義
238        $this->lfInitParamCommon($objFormParam);
239        $objFormParam->addParam('選択ファイル', 'select_file', MTEXT_LEN, 'a', array());
240    }
241
242    /**
243     * ファイル表示時、パラメーター定義
244     *
245     * @param SC_FormParam $objFormParam SC_FormParam インスタンス
246     * @return void
247     */
248    function lfInitParamModeView(&$objFormParam) {
249        // 共通定義
250        $this->lfInitParamCommon($objFormParam);
251        $objFormParam->addParam('選択ファイル', 'select_file', MTEXT_LEN, 'a', array('SELECT_CHECK'));
252    }
253
254    /**
255     * ファイル表示時、パラメーター定義
256     *
257     * @param SC_FormParam $objFormParam SC_FormParam インスタンス
258     * @return void
259     */
260    function lfInitParamModeCreate(&$objFormParam) {
261        // 共通定義
262        $this->lfInitParamCommon($objFormParam);
263        $objFormParam->addParam('選択ファイル', 'select_file', MTEXT_LEN, 'a', array());
264        $objFormParam->addParam('作成ファイル名', 'create_file', MTEXT_LEN, 'a', array('EXIST_CHECK', 'FILE_NAME_CHECK_BY_NOUPLOAD'));
265    }
266
267    /**
268     * ファイル表示時、パラメーター定義
269     *
270     * @param SC_FormParam $objFormParam SC_FormParam インスタンス
271     * @return void
272     */
273    function lfInitParamCommon(&$objFormParam) {
274        $objFormParam->addParam('ルートディレクトリ', 'top_dir', MTEXT_LEN, 'a', array());
275        $objFormParam->addParam('現在の階層ディレクトリ', 'now_dir', MTEXT_LEN, 'a', array());
276        $objFormParam->addParam('現在の階層ファイル', 'now_file', MTEXT_LEN, 'a', array());
277        $objFormParam->addParam('ツリー選択状態', 'tree_status', MTEXT_LEN, 'a', array());
278        $objFormParam->addParam('ツリー選択ディレクトリ', 'tree_select_file', MTEXT_LEN, 'a', array());
279    }
280
281    /*
282     * ファイル情報の初期化
283     *
284     * @param object $objUpFile SC_UploadFileインスタンス
285     * @return void
286     */
287    function lfInitFile(&$objUpFile) {
288        $objUpFile->addFile('ファイル', 'upload_file', array(), FILE_SIZE, true, 0, 0, false);
289    }
290
291    /**
292     * テンプレートに渡す値を整形する
293     *
294     * @param array $_POST $_POST
295     * @return array $setParam テンプレートに渡す値
296     */
297    function createSetParam($_POST) {
298        $setParam = $_POST;
299        // Windowsの場合は, ディレクトリの区切り文字を\から/に変換する
300        $setParam['top_dir'] = (strpos(PHP_OS, 'WIN') === false) ? USER_REALDIR : str_replace('\\', '/', USER_REALDIR);
301        // 初期表示はルートディレクトリ(user_data/)を表示
302        if (SC_Utils_Ex::isBlank($this->getMode())) {
303            $setParam['now_dir'] = $setParam['top_dir'];
304        }
305        return $setParam;
306    }
307
308    /**
309     * テンプレートに値を渡す
310     *
311     * @param string $key キー名
312     * @param string $val 値
313     * @return void
314     */
315    function setDispParam($key, $val) {
316        $this->$key = $val;
317    }
318
319    /**
320     * ディレクトリを作成
321     *
322     * @param object $objFileManager SC_Helper_FileManager_Exインスタンス
323     * @param SC_FormParam $objFormParam SC_FormParamインスタンス
324     * @return boolean ディレクトリ作成できたかどうか
325     */
326    function tryCreateDir($objFileManager, $objFormParam) {
327        $create_dir_flg = false;
328        $create_dir = ereg_replace("/$", '', $objFormParam->getValue('now_dir'));
329        // ファイル作成
330        if ($objFileManager->sfCreateFile($create_dir.'/'.$objFormParam->getValue('create_file'), 0755)) {
331            $create_dir_flg = true;
332        }
333        return $create_dir_flg;
334    }
335
336    /**
337     * ファイル表示を行う
338     *
339     * @param SC_FormParam $objFormParam SC_FormParamインスタンス
340     * @return boolean ファイル表示するかどうか
341     */
342    function tryView(&$objFormParam) {
343        $view_flg = false;
344        $now_dir = $this->lfCheckSelectDir($objFormParam, dirname($objFormParam->getValue('select_file')));
345        $objFormParam->setValue('now_dir', $now_dir);
346        if (!strpos($objFormParam->getValue('select_file'), $objFormParam->getValue('top_dir'))) {
347            $view_flg = true;
348        }
349        return $view_flg;
350    }
351
352    /**
353     * 現在の階層の一つ上の階層のディレクトリをテンプレートに渡す
354     *
355     * @param SC_FormParam $objFormParam SC_FormParamインスタンス
356     * @return void
357     */
358    function setParentDir($objFormParam) {
359        $parent_dir = $this->lfGetParentDir($objFormParam->getValue('now_dir'));
360        $this->setDispParam('tpl_parent_dir', $parent_dir);
361    }
362
363    /**
364     * 現在の階層のパスをテンプレートに渡す
365     *
366     * @param SC_FormParam $objFormParam SC_FormParamインスタンス
367     * @return void
368     */
369    function setDispPath($objFormParam) {
370        $tpl_now_dir = '';
371        // Windows 環境で DIRECTORY_SEPARATOR が JavaScript に渡るとエスケープ文字と勘違いするので置換
372        $html_realdir = str_replace(DIRECTORY_SEPARATOR, '/', HTML_REALDIR);
373        $arrNowDir = preg_split('/\//', str_replace($html_realdir, '', $objFormParam->getValue('now_dir')));
374        $this->setDispParam('tpl_now_dir', SC_Utils_Ex::jsonEncode($arrNowDir));
375        $this->setDispParam('tpl_file_path', $html_realdir);
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 = explode('/', $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) {
492            $is_top_dir = true;
493        }
494        $this->setDispParam('tpl_is_top_dir', $is_top_dir);
495    }
496
497}
Note: See TracBrowser for help on using the repository browser.