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

Revision 21743, 17.9 KB checked in by AMUAMU, 11 years ago (diff)

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