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

Revision 23124, 18.0 KB checked in by kimoto, 11 years ago (diff)

#2043 typo修正・ソース整形・ソースコメントの改善 for 2.13.0
PHP4的な書き方の修正

  • 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    public 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    public function process()
56    {
57        $this->action();
58        $this->sendResponse();
59    }
60
61    /**
62     * Page のアクション.
63     *
64     * @return void
65     */
66    public 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     * @param  SC_FormParam $objFormParam SC_FormParamインスタンス
201     * @return void
202     */
203    public function lfInitParam(&$objFormParam)
204    {
205        // 共通定義
206        $this->lfInitParamCommon($objFormParam);
207    }
208
209    /**
210     * ディレクトリ移動時、パラメーター定義
211     *
212     * @param  SC_FormParam $objFormParam SC_FormParam インスタンス
213     * @return void
214     */
215    public function lfInitParamModeMove(&$objFormParam)
216    {
217        // 共通定義
218        $this->lfInitParamCommon($objFormParam);
219        $objFormParam->addParam('選択ファイル', 'select_file', MTEXT_LEN, 'a', array());
220    }
221
222    /**
223     * ファイル表示時、パラメーター定義
224     *
225     * @param  SC_FormParam $objFormParam SC_FormParam インスタンス
226     * @return void
227     */
228    public function lfInitParamModeView(&$objFormParam)
229    {
230        // 共通定義
231        $this->lfInitParamCommon($objFormParam);
232        $objFormParam->addParam('選択ファイル', 'select_file', MTEXT_LEN, 'a', array('SELECT_CHECK'));
233    }
234
235    /**
236     * ファイル表示時、パラメーター定義
237     *
238     * @param  SC_FormParam $objFormParam SC_FormParam インスタンス
239     * @return void
240     */
241    public function lfInitParamModeCreate(&$objFormParam)
242    {
243        // 共通定義
244        $this->lfInitParamCommon($objFormParam);
245        $objFormParam->addParam('選択ファイル', 'select_file', MTEXT_LEN, 'a', array());
246        $objFormParam->addParam('作成ファイル名', 'create_file', MTEXT_LEN, 'a', array('EXIST_CHECK', 'FILE_NAME_CHECK_BY_NOUPLOAD'));
247    }
248
249    /**
250     * ファイル表示時、パラメーター定義
251     *
252     * @param  SC_FormParam $objFormParam SC_FormParam インスタンス
253     * @return void
254     */
255    public function lfInitParamCommon(&$objFormParam)
256    {
257        $objFormParam->addParam('ルートディレクトリ', 'top_dir', MTEXT_LEN, 'a', array());
258        $objFormParam->addParam('現在の階層ディレクトリ', 'now_dir', MTEXT_LEN, 'a', array());
259        $objFormParam->addParam('現在の階層ファイル', 'now_file', MTEXT_LEN, 'a', array());
260        $objFormParam->addParam('ツリー選択状態', 'tree_status', MTEXT_LEN, 'a', array());
261        $objFormParam->addParam('ツリー選択ディレクトリ', 'tree_select_file', MTEXT_LEN, 'a', array());
262    }
263
264    /*
265     * ファイル情報の初期化
266     *
267     * @param  object $objUpFile SC_UploadFileインスタンス
268     * @return void
269     */
270    public function lfInitFile(&$objUpFile)
271    {
272        $objUpFile->addFile('ファイル', 'upload_file', array(), FILE_SIZE, true, 0, 0, false);
273    }
274
275    /**
276     * テンプレートに渡す値を整形する
277     *
278     * @param  array $arrVal $_POST
279     * @return array $setParam テンプレートに渡す値
280     */
281    public function createSetParam($arrVal)
282    {
283        $setParam = $arrVal;
284        // Windowsの場合は, ディレクトリの区切り文字を\から/に変換する
285        $setParam['top_dir'] = (strpos(PHP_OS, 'WIN') === false) ? USER_REALDIR : str_replace('\\', '/', USER_REALDIR);
286        // 初期表示はルートディレクトリ(user_data/)を表示
287        if (SC_Utils_Ex::isBlank($this->getMode())) {
288            $setParam['now_dir'] = $setParam['top_dir'];
289        }
290
291        return $setParam;
292    }
293
294    /**
295     * テンプレートに値を渡す
296     *
297     * @param  string $key キー名
298     * @param  string $val 値
299     * @return void
300     */
301    public function setDispParam($key, $val)
302    {
303        $this->$key = $val;
304    }
305
306    /**
307     * ディレクトリを作成
308     *
309     * @param  object       $objFileManager SC_Helper_FileManager_Exインスタンス
310     * @param  SC_FormParam $objFormParam   SC_FormParamインスタンス
311     * @return boolean      ディレクトリ作成できたかどうか
312     */
313    public function tryCreateDir($objFileManager, $objFormParam)
314    {
315        $create_dir_flg = false;
316        $create_dir = rtrim($objFormParam->getValue('now_dir'), '/');
317        // ファイル作成
318        if ($objFileManager->sfCreateFile($create_dir.'/'.$objFormParam->getValue('create_file'), 0755)) {
319            $create_dir_flg = true;
320        }
321
322        return $create_dir_flg;
323    }
324
325    /**
326     * ファイル表示を行う
327     *
328     * @param  SC_FormParam $objFormParam SC_FormParamインスタンス
329     * @return boolean      ファイル表示するかどうか
330     */
331    public function tryView(&$objFormParam)
332    {
333        $view_flg = false;
334        $now_dir = $this->lfCheckSelectDir($objFormParam, dirname($objFormParam->getValue('select_file')));
335        $objFormParam->setValue('now_dir', $now_dir);
336        if (!strpos($objFormParam->getValue('select_file'), $objFormParam->getValue('top_dir'))) {
337            $view_flg = true;
338        }
339
340        return $view_flg;
341    }
342
343    /**
344     * 現在の階層の一つ上の階層のディレクトリをテンプレートに渡す
345     *
346     * @param  SC_FormParam $objFormParam SC_FormParamインスタンス
347     * @return void
348     */
349    public function setParentDir($objFormParam)
350    {
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    public function setDispPath($objFormParam)
362    {
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    public function setDispError($key, $value)
378    {
379        // 既にエラーがある場合は、処理しない
380        if (SC_Utils_Ex::isBlank($this->arrErr[$key])) {
381            $this->arrErr[$key] = $value;
382        }
383    }
384
385    /**
386     * javascriptをテンプレートに渡す
387     *
388     * @param  string $tpl_onload javascript
389     * @return void
390     */
391    public function setTplOnLoad($tpl_onload)
392    {
393        $this->tpl_onload .= $tpl_onload;
394    }
395
396    /*
397     * 選択ディレクトリがUSER_REALDIR以下かチェック
398     *
399     * @param  object $objFormParam SC_FormParamインスタンス
400     * @param  string $dir          ディレクトリ
401     * @return string $select_dir 選択ディレクトリ
402     */
403    public function lfCheckSelectDir($objFormParam, $dir)
404    {
405        $select_dir = '';
406        $top_dir = $objFormParam->getValue('top_dir');
407        // USER_REALDIR以下の場合
408        if (preg_match("@^\Q". $top_dir. "\E@", $dir) > 0) {
409            // 相対パスがある場合、USER_REALDIRを返す.
410            if (preg_match("@\Q..\E@", $dir) > 0) {
411                $select_dir = $top_dir;
412            // 相対パスがない場合、そのままディレクトリパスを返す.
413            } else {
414                $select_dir= $dir;
415            }
416        // USER_REALDIR以下でない場合、USER_REALDIRを返す.
417        } else {
418            $select_dir = $top_dir;
419        }
420
421        return $select_dir;
422    }
423
424    /**
425     * 親ディレクトリ取得
426     *
427     * @param  string $dir 現在いるディレクトリ
428     * @return string $parent_dir 親ディレクトリ
429     */
430    public function lfGetParentDir($dir)
431    {
432        $parent_dir = '';
433        $dir = rtrim($dir, '/');
434        $arrDir = explode('/', $dir);
435        array_pop($arrDir);
436        foreach ($arrDir as $val) {
437            $parent_dir .= "$val/";
438        }
439        $parent_dir = rtrim($parent_dir, '/');
440
441        return $parent_dir;
442    }
443
444    /**
445     * ディレクトリツリー生成
446     *
447     * @param  object       $objFileManager SC_Helper_FileManager_Exインスタンス
448     * @param  SC_FormParam $objFormParam   SC_FormParamインスタンス
449     * @return void
450     */
451    public function setDispTree($objFileManager, $objFormParam)
452    {
453        $tpl_onload = '';
454        // ツリーを表示する divタグid, ツリー配列変数名, 現在ディレクトリ, 選択ツリーhidden名, ツリー状態hidden名, mode hidden名
455        $now_dir = $objFormParam->getValue('now_dir');
456        $treeView = "fnTreeView('tree', arrTree, '$now_dir', 'tree_select_file', 'tree_status', 'move');";
457        if (!empty($this->tpl_onload)) {
458            $tpl_onload .= $treeView;
459        } else {
460            $tpl_onload = $treeView;
461        }
462        $this->setTplOnLoad($tpl_onload);
463
464        $tpl_javascript = '';
465        $arrTree = $objFileManager->sfGetFileTree($objFormParam->getValue('top_dir'), $objFormParam->getValue('tree_status'));
466        $tpl_javascript .= "arrTree = new Array();\n";
467        foreach ($arrTree as $arrVal) {
468            $tpl_javascript .= 'arrTree['.$arrVal['count'].'] = new Array('.$arrVal['count'].", '".$arrVal['type']."', '".$arrVal['path']."', ".$arrVal['rank'].',';
469            if ($arrVal['open']) {
470                $tpl_javascript .= "true);\n";
471            } else {
472                $tpl_javascript .= "false);\n";
473            }
474        }
475        $this->setDispParam('tpl_javascript', $tpl_javascript);
476    }
477
478    /**
479     * 現在の階層がルートディレクトリかどうかテンプレートに渡す
480     *
481     * @param  object $objFormParam SC_FormParamインスタンス
482     * @return void
483     */
484    public function setIsTopDir($objFormParam)
485    {
486        // トップディレクトリか調査
487        $is_top_dir = false;
488        // 末尾の/をとる
489        $top_dir_check = rtrim($objFormParam->getValue('top_dir'), '/');
490        $now_dir_check = rtrim($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}
Note: See TracBrowser for help on using the repository browser.