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

Revision 22857, 18.0 KB checked in by Seasoft, 11 years ago (diff)

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

  • 主に空白・空白行の調整。
  • 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-2013 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
24require_once CLASS_EX_REALDIR . 'page_extends/admin/LC_Page_Admin_Ex.php';
25
26/**
27 * ファイル管理 のページクラス.
28 *
29 * @package Page
30 * @author LOCKON CO.,LTD.
31 * @version $Id$
32 */
33class LC_Page_Admin_Contents_FileManager extends LC_Page_Admin_Ex
34{
35    /**
36     * Page を初期化する.
37     *
38     * @return void
39     */
40    function init()
41    {
42        parent::init();
43        $this->tpl_mainpage = 'contents/file_manager.tpl';
44        $this->tpl_mainno = 'contents';
45        $this->tpl_subno = 'file';
46        $this->tpl_maintitle = 'コンテンツ管理';
47        $this->tpl_subtitle = 'ファイル管理';
48    }
49
50    /**
51     * Page のプロセス.
52     *
53     * @return void
54     */
55    function process()
56    {
57        $this->action();
58        $this->sendResponse();
59    }
60
61    /**
62     * Page のアクション.
63     *
64     * @return void
65     */
66    function action()
67    {
68        // フォーム操作クラス
69        $objFormParam = new SC_FormParam_Ex();
70        // パラメーター情報の初期化
71        $this->lfInitParam($objFormParam);
72        $objFormParam->setParam($this->createSetParam($_POST));
73        $objFormParam->convParam();
74
75        // ファイル管理クラス
76        $objUpFile = new SC_UploadFile_Ex($objFormParam->getValue('now_dir'), $objFormParam->getValue('now_dir'));
77        // ファイル情報の初期化
78        $this->lfInitFile($objUpFile);
79
80        // ファイル操作クラス
81        $objFileManager = new SC_Helper_FileManager_Ex();
82
83        switch ($this->getMode()) {
84            // フォルダ移動
85            case 'move':
86                $objFormParam = new SC_FormParam_Ex();
87                $this->lfInitParamModeMove($objFormParam);
88                $objFormParam->setParam($this->createSetParam($_POST));
89                $objFormParam->convParam();
90
91                $this->arrErr = $objFormParam->checkError();
92                if (SC_Utils_Ex::isBlank($this->arrErr)) {
93                    $now_dir = $this->lfCheckSelectDir($objFormParam, $objFormParam->getValue('tree_select_file'));
94                    $objFormParam->setValue('now_dir', $now_dir);
95                }
96                break;
97
98            // ファイル表示
99            case 'view':
100                $objFormParam = new SC_FormParam_Ex();
101                $this->lfInitParamModeView($objFormParam);
102                $objFormParam->setParam($this->createSetParam($_POST));
103                $objFormParam->convParam();
104
105                $this->arrErr = $objFormParam->checkError();
106                if (SC_Utils_Ex::isBlank($this->arrErr)) {
107                    if ($this->tryView($objFormParam)) {
108                        $pattern = '/' . preg_quote($objFormParam->getValue('top_dir'), '/') . '/';
109                        $file_url = htmlspecialchars(preg_replace($pattern, '', $objFormParam->getValue('select_file')));
110                        $tpl_onload = "win02('./file_view.php?file=". $file_url ."', 'user_data', '600', '400');";
111                        $this->setTplOnLoad($tpl_onload);
112                    }
113                }
114                break;
115
116            // ファイルダウンロード
117            case 'download':
118                $objFormParam = new SC_FormParam_Ex();
119                $this->lfInitParamModeView($objFormParam);
120                $objFormParam->setParam($this->createSetParam($_POST));
121                $objFormParam->convParam();
122
123                $this->arrErr = $objFormParam->checkError();
124                if (SC_Utils_Ex::isBlank($this->arrErr)) {
125                    if (is_dir($objFormParam->getValue('select_file'))) {
126                        $disp_error = '※ ディレクトリをダウンロードすることは出来ません。<br/>';
127                        $this->setDispError('select_file', $disp_error);
128                    } else {
129                        // ファイルダウンロード
130                        $objFileManager->sfDownloadFile($objFormParam->getValue('select_file'));
131                        SC_Response_Ex::actionExit();
132                    }
133                }
134                break;
135            // ファイル削除
136            case 'delete':
137                $objFormParam = new SC_FormParam_Ex();
138                $this->lfInitParamModeView($objFormParam);
139                $objFormParam->setParam($this->createSetParam($_POST));
140                $objFormParam->convParam();
141
142                $this->arrErr = $objFormParam->checkError();
143                if (SC_Utils_Ex::isBlank($this->arrErr)) {
144                    $objFileManager->deleteFile($objFormParam->getValue('select_file'));
145                }
146                break;
147            // ファイル作成
148            case 'create':
149                $objFormParam = new SC_FormParam_Ex();
150                $this->lfInitParamModeCreate($objFormParam);
151                $objFormParam->setParam($this->createSetParam($_POST));
152                $objFormParam->convParam();
153
154                $this->arrErr = $objFormParam->checkError();
155                if (SC_Utils_Ex::isBlank($this->arrErr)) {
156                    if (!$this->tryCreateDir($objFileManager, $objFormParam)) {
157                        $disp_error = '※ '.htmlspecialchars($objFormParam->getValue('create_file'), ENT_QUOTES).'の作成に失敗しました。<br/>';
158                        $this->setDispError('create_file', $disp_error);
159                    } else {
160                        $tpl_onload = "alert('フォルダを作成しました。');";
161                        $this->setTplOnLoad($tpl_onload);
162                    }
163                }
164                break;
165            // ファイルアップロード
166            case 'upload':
167                // 画像保存処理
168                $ret = $objUpFile->makeTempFile('upload_file', false);
169                if (SC_Utils_Ex::isBlank($ret)) {
170                    $tpl_onload = "alert('ファイルをアップロードしました。');";
171                    $this->setTplOnLoad($tpl_onload);
172                } else {
173                    $this->setDispError('upload_file', $ret);
174                }
175                break;
176            // 初期表示
177            default:
178                break;
179        }
180
181        // 値をテンプレートに渡す
182        $this->arrParam = $objFormParam->getHashArray();
183        // 現在の階層がルートディレクトリかどうかテンプレートに渡す
184        $this->setIsTopDir($objFormParam);
185        // 現在の階層より一つ上の階層をテンプレートに渡す
186        $this->setParentDir($objFormParam);
187        // 現在いる階層(表示用)をテンプレートに渡す
188        $this->setDispPath($objFormParam);
189        // 現在のディレクトリ配下のファイル一覧を取得
190        $this->arrFileList = $objFileManager->sfGetFileList($objFormParam->getValue('now_dir'));
191        // 現在の階層のディレクトリをテンプレートに渡す
192        $this->setDispParam('tpl_now_file', $objFormParam->getValue('now_dir'));
193        // ディレクトリツリー表示
194        $this->setDispTree($objFileManager, $objFormParam);
195    }
196
197    /**
198     * デストラクタ.
199     *
200     * @return void
201     */
202    function destroy()
203    {
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        // 共通定義
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        // 共通定義
228        $this->lfInitParamCommon($objFormParam);
229        $objFormParam->addParam('選択ファイル', 'select_file', MTEXT_LEN, 'a', array());
230    }
231
232    /**
233     * ファイル表示時、パラメーター定義
234     *
235     * @param SC_FormParam $objFormParam SC_FormParam インスタンス
236     * @return void
237     */
238    function lfInitParamModeView(&$objFormParam)
239    {
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        // 共通定義
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    {
267        $objFormParam->addParam('ルートディレクトリ', 'top_dir', MTEXT_LEN, 'a', array());
268        $objFormParam->addParam('現在の階層ディレクトリ', 'now_dir', MTEXT_LEN, 'a', array());
269        $objFormParam->addParam('現在の階層ファイル', 'now_file', MTEXT_LEN, 'a', array());
270        $objFormParam->addParam('ツリー選択状態', 'tree_status', MTEXT_LEN, 'a', array());
271        $objFormParam->addParam('ツリー選択ディレクトリ', 'tree_select_file', MTEXT_LEN, 'a', array());
272    }
273
274    /*
275     * ファイル情報の初期化
276     *
277     * @param object $objUpFile SC_UploadFileインスタンス
278     * @return void
279     */
280    function lfInitFile(&$objUpFile)
281    {
282        $objUpFile->addFile('ファイル', 'upload_file', array(), FILE_SIZE, true, 0, 0, false);
283    }
284
285    /**
286     * テンプレートに渡す値を整形する
287     *
288     * @param array $arrVal $_POST
289     * @return array $setParam テンプレートに渡す値
290     */
291    function createSetParam($arrVal)
292    {
293        $setParam = $arrVal;
294        // Windowsの場合は, ディレクトリの区切り文字を\から/に変換する
295        $setParam['top_dir'] = (strpos(PHP_OS, 'WIN') === false) ? USER_REALDIR : str_replace('\\', '/', USER_REALDIR);
296        // 初期表示はルートディレクトリ(user_data/)を表示
297        if (SC_Utils_Ex::isBlank($this->getMode())) {
298            $setParam['now_dir'] = $setParam['top_dir'];
299        }
300
301        return $setParam;
302    }
303
304    /**
305     * テンプレートに値を渡す
306     *
307     * @param string $key キー名
308     * @param string $val 値
309     * @return void
310     */
311    function setDispParam($key, $val)
312    {
313        $this->$key = $val;
314    }
315
316    /**
317     * ディレクトリを作成
318     *
319     * @param object $objFileManager SC_Helper_FileManager_Exインスタンス
320     * @param SC_FormParam $objFormParam SC_FormParamインスタンス
321     * @return boolean ディレクトリ作成できたかどうか
322     */
323    function tryCreateDir($objFileManager, $objFormParam)
324    {
325        $create_dir_flg = false;
326        $create_dir = rtrim($objFormParam->getValue('now_dir'), '/');
327        // ファイル作成
328        if ($objFileManager->sfCreateFile($create_dir.'/'.$objFormParam->getValue('create_file'), 0755)) {
329            $create_dir_flg = true;
330        }
331
332        return $create_dir_flg;
333    }
334
335    /**
336     * ファイル表示を行う
337     *
338     * @param SC_FormParam $objFormParam SC_FormParamインスタンス
339     * @return boolean ファイル表示するかどうか
340     */
341    function tryView(&$objFormParam)
342    {
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
350        return $view_flg;
351    }
352
353    /**
354     * 現在の階層の一つ上の階層のディレクトリをテンプレートに渡す
355     *
356     * @param SC_FormParam $objFormParam SC_FormParamインスタンス
357     * @return void
358     */
359    function setParentDir($objFormParam)
360    {
361        $parent_dir = $this->lfGetParentDir($objFormParam->getValue('now_dir'));
362        $this->setDispParam('tpl_parent_dir', $parent_dir);
363    }
364
365    /**
366     * 現在の階層のパスをテンプレートに渡す
367     *
368     * @param SC_FormParam $objFormParam SC_FormParamインスタンス
369     * @return void
370     */
371    function setDispPath($objFormParam)
372    {
373        $tpl_now_dir = '';
374        // Windows 環境で DIRECTORY_SEPARATOR が JavaScript に渡るとエスケープ文字と勘違いするので置換
375        $html_realdir = str_replace(DIRECTORY_SEPARATOR, '/', HTML_REALDIR);
376        $arrNowDir = preg_split('/\//', str_replace($html_realdir, '', $objFormParam->getValue('now_dir')));
377        $this->setDispParam('tpl_now_dir', SC_Utils_Ex::jsonEncode($arrNowDir));
378        $this->setDispParam('tpl_file_path', $html_realdir);
379    }
380
381    /**
382     * エラーを表示用の配列に格納
383     *
384     * @param string $key キー名
385     * @param string $value エラー内容
386     * @return void
387     */
388    function setDispError($key, $value)
389    {
390        // 既にエラーがある場合は、処理しない
391        if (SC_Utils_Ex::isBlank($this->arrErr[$key])) {
392            $this->arrErr[$key] = $value;
393        }
394    }
395
396    /**
397     * javascriptをテンプレートに渡す
398     *
399     * @param string $tpl_onload javascript
400     * @return void
401     */
402    function setTplOnLoad($tpl_onload)
403    {
404        $this->tpl_onload .= $tpl_onload;
405    }
406
407    /*
408     * 選択ディレクトリがUSER_REALDIR以下かチェック
409     *
410     * @param object $objFormParam SC_FormParamインスタンス
411     * @param string $dir ディレクトリ
412     * @return string $select_dir 選択ディレクトリ
413     */
414    function lfCheckSelectDir($objFormParam, $dir)
415    {
416        $select_dir = '';
417        $top_dir = $objFormParam->getValue('top_dir');
418        // USER_REALDIR以下の場合
419        if (preg_match("@^\Q". $top_dir. "\E@", $dir) > 0) {
420            // 相対パスがある場合、USER_REALDIRを返す.
421            if (preg_match("@\Q..\E@", $dir) > 0) {
422                $select_dir = $top_dir;
423            // 相対パスがない場合、そのままディレクトリパスを返す.
424            } else {
425                $select_dir= $dir;
426            }
427        // USER_REALDIR以下でない場合、USER_REALDIRを返す.
428        } else {
429            $select_dir = $top_dir;
430        }
431
432        return $select_dir;
433    }
434
435    /**
436     * 親ディレクトリ取得
437     *
438     * @param string $dir 現在いるディレクトリ
439     * @return string $parent_dir 親ディレクトリ
440     */
441    function lfGetParentDir($dir)
442    {
443        $parent_dir = '';
444        $dir = rtrim($dir, '/');
445        $arrDir = explode('/', $dir);
446        array_pop($arrDir);
447        foreach ($arrDir as $val) {
448            $parent_dir .= "$val/";
449        }
450        $parent_dir = rtrim($parent_dir, '/');
451
452        return $parent_dir;
453    }
454
455    /**
456     * ディレクトリツリー生成
457     *
458     * @param object $objFileManager SC_Helper_FileManager_Exインスタンス
459     * @param SC_FormParam $objFormParam SC_FormParamインスタンス
460     * @return void
461     */
462    function setDispTree($objFileManager, $objFormParam)
463    {
464        $tpl_onload = '';
465        // ツリーを表示する divタグid, ツリー配列変数名, 現在ディレクトリ, 選択ツリーhidden名, ツリー状態hidden名, mode hidden名
466        $now_dir = $objFormParam->getValue('now_dir');
467        $treeView = "fnTreeView('tree', arrTree, '$now_dir', 'tree_select_file', 'tree_status', 'move');";
468        if (!empty($this->tpl_onload)) {
469            $tpl_onload .= $treeView;
470        } else {
471            $tpl_onload = $treeView;
472        }
473        $this->setTplOnLoad($tpl_onload);
474
475        $tpl_javascript = '';
476        $arrTree = $objFileManager->sfGetFileTree($objFormParam->getValue('top_dir'), $objFormParam->getValue('tree_status'));
477        $tpl_javascript .= "arrTree = new Array();\n";
478        foreach ($arrTree as $arrVal) {
479            $tpl_javascript .= 'arrTree['.$arrVal['count'].'] = new Array('.$arrVal['count'].", '".$arrVal['type']."', '".$arrVal['path']."', ".$arrVal['rank'].',';
480            if ($arrVal['open']) {
481                $tpl_javascript .= "true);\n";
482            } else {
483                $tpl_javascript .= "false);\n";
484            }
485        }
486        $this->setDispParam('tpl_javascript', $tpl_javascript);
487    }
488
489    /**
490     * 現在の階層がルートディレクトリかどうかテンプレートに渡す
491     *
492     * @param object $objFormParam SC_FormParamインスタンス
493     * @return void
494     */
495    function setIsTopDir($objFormParam)
496    {
497        // トップディレクトリか調査
498        $is_top_dir = false;
499        // 末尾の/をとる
500        $top_dir_check = rtrim($objFormParam->getValue('top_dir'), '/');
501        $now_dir_check = rtrim($objFormParam->getValue('now_dir'), '/');
502        if ($top_dir_check == $now_dir_check) {
503            $is_top_dir = true;
504        }
505        $this->setDispParam('tpl_is_top_dir', $is_top_dir);
506    }
507}
Note: See TracBrowser for help on using the repository browser.