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

Revision 21757, 17.9 KB checked in by shutta, 12 years ago (diff)

#1612 不要な関数の削除
SC_UtilsとSC_Helper_FileManager間の重複している(もしくは同機能の)関数の整理。

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