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

Revision 21514, 17.5 KB checked in by Seasoft, 12 years ago (diff)

#1625 (typo修正・ソース整形・ソースコメントの改善)

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