source: branches/version-2_5-dev/data/class/pages/admin/design/LC_Page_Admin_Design_MainEdit.php @ 18861

Revision 18861, 15.3 KB checked in by Seasoft, 14 years ago (diff)

#831(/html/user_data/*.php を上書きしない)実装

  • Property svn:eol-style set to LF
  • Property svn:keywords set to "Id Revision Date"
  • 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-2010 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_PATH . "pages/LC_Page.php");
26
27/**
28 * メイン編集 のページクラス.
29 *
30 * @package Page
31 * @author LOCKON CO.,LTD.
32 * @version $Id$
33 */
34class LC_Page_Admin_Design_MainEdit extends LC_Page {
35
36    // }}}
37    // {{{ functions
38
39    /**
40     * Page を初期化する.
41     *
42     * @return void
43     */
44    function init() {
45        parent::init();
46        $this->tpl_mainpage = 'design/main_edit.tpl';
47        $this->tpl_subnavi  = 'design/subnavi.tpl';
48        $this->user_URL     = USER_URL;
49        $this->text_row     = 13;
50        $this->tpl_subno = "main_edit";
51        $this->tpl_mainno = "design";
52        $this->tpl_subtitle = 'ページ詳細設定';
53    }
54
55    /**
56     * Page のプロセス.
57     *
58     * @return void
59     */
60    function process() {
61        $objView = new SC_AdminView();
62        $this->objLayout = new SC_Helper_PageLayout_Ex();
63
64        // 認証可否の判定
65        $objSess = new SC_Session();
66        SC_Utils_Ex::sfIsSuccess($objSess);
67
68        // ページ一覧を取得
69        $this->arrPageList = $this->objLayout->lfgetPageData();
70       
71        // ブロックIDを取得
72        if (isset($_POST['page_id'])) {
73            $page_id = $_POST['page_id'];
74        }else if (isset($_GET['page_id'])){
75            $page_id = $_GET['page_id'];
76        }else{
77            $page_id = '';
78        }
79
80        $this->page_id = $page_id;
81
82        // メッセージ表示
83        if (isset($_GET['msg']) && $_GET['msg'] == "on"){
84            $this->tpl_onload="alert('登録が完了しました。');";
85        }
86
87        // page_id が指定されている場合にはテンプレートデータの取得
88        if (is_numeric($page_id) and $page_id != '') {
89            $this->lfGetPageData($page_id, $objView);
90        }
91
92        if (!isset($_POST['mode'])) $_POST['mode'] = "";
93       
94        // プレビュー処理
95        if ($_POST['mode'] == 'preview') {
96            $this->lfPreviewPageData($page_id);
97            exit;
98        }
99
100        // データ登録処理
101        if ($_POST['mode'] == 'confirm') {
102            $this->lfConfirmPageData($page_id);
103        }
104
105        // データ削除処理 ベースデータでなければファイルを削除
106        if ($_POST['mode'] == 'delete' and !$this->objLayout->lfCheckBaseData($page_id)) {
107            $this->lfDeletePageData($page_id);
108            exit;
109        }
110
111        // 画面の表示
112        $objView->assignobj($this);
113        $objView->display(MAIN_FRAME);
114    }
115
116    /**
117     * デストラクタ.
118     *
119     * @return void
120     */
121    function destroy() {
122        parent::destroy();
123    }
124
125    /**
126     * ページデータを取得する.
127     *
128     * @param integer $page_id ページID
129     * @param object $objView ビューオブジェクト
130     * @return void
131     */
132    function lfGetPageData($page_id, $objView){
133        $arrPageData = $this->objLayout->lfgetPageData(" page_id = ? " , array($page_id));
134
135        if (strlen($arrPageData[0]['filename']) == 0) {
136            $this->arrErr['page_id_err'] = "※ 指定されたページは編集できません。";
137            // 画面の表示
138            $objView->assignobj($this);
139            $objView->display(MAIN_FRAME);
140            exit;
141        }
142
143        // テンプレートファイルが存在していれば読み込む
144        $tpl_file =  USER_TEMPLATE_PATH . "/" . TEMPLATE_NAME . "/" . $arrPageData[0]['filename'] . ".tpl";
145        if (file_exists($tpl_file)){
146            $arrPageData[0]['tpl_data'] = file_get_contents($tpl_file);
147        // 存在してなければ, 指定されたテンプレートのファイルを読み込む
148        } else {
149            $arrPageData[0]['tpl_data'] = file_get_contents(TEMPLATE_DIR . $arrPageData[0]['filename'] . ".tpl");
150        }
151
152        // チェックボックスの値変更
153        $arrPageData[0]['header_chk'] = SC_Utils_Ex::sfChangeCheckBox($arrPageData[0]['header_chk'], true);
154        $arrPageData[0]['footer_chk'] = SC_Utils_Ex::sfChangeCheckBox($arrPageData[0]['footer_chk'], true);
155
156        // ディレクトリを画面表示用に編集
157        $arrPageData[0]['directory'] = str_replace(USER_DIR, '', $arrPageData[0]['php_dir']);
158
159        $this->arrPageData = $arrPageData[0];
160    }
161
162    /**
163     * プレビュー画面を表示する.
164     *
165     * @param integer $page_id ページID
166     * @return void
167     */
168    function lfPreviewPageData($page_id){
169
170        $page_id_old = $page_id;
171        // プレビューの場合ページIDを0にセットする。
172        $page_id = "0";
173        $url = basename($_POST['url']);
174       
175        $tmpPost = $_POST;
176        $tmpPost['page_id'] = $page_id;
177        $tmpPost['url'] = $url;
178        $tmpPost['tpl_dir'] = USER_PATH . "templates/preview/";
179       
180        $arrPreData = $this->objLayout->lfgetPageData("page_id = ?" , array($page_id));
181       
182        // tplファイルの削除 (XXX: 処理の意図が不明。存在していると都合が悪いファイル?)
183        $del_tpl = USER_PATH . "templates/" . $arrPreData[0]['filename'] . '.tpl';
184        if (file_exists($del_tpl)){
185            unlink($del_tpl);
186        }
187
188        // DBへデータを更新する
189        $this->lfEntryPageData($tmpPost);
190
191        // TPLファイル作成
192        $preview_tpl = USER_PATH . "templates/preview/" . TEMPLATE_NAME . "/" . $url . '.tpl';
193        $this->lfCreateFile($preview_tpl, $_POST['tpl_data']);
194       
195        // blocposition を削除
196        $objQuery = new SC_Query();     // DB操作オブジェクト
197        $sql = 'delete from dtb_blocposition where page_id = 0';
198        $ret = $objQuery->query($sql);
199
200        if ($page_id_old != "") {
201            // 登録データを取得
202            $sql = "SELECT 0, target_id, bloc_id, bloc_row FROM dtb_blocposition WHERE page_id = ?";
203            $ret = $objQuery->getAll($sql,array($page_id_old));
204
205            if (count($ret) > 0) {
206
207                // blocposition を複製
208                $sql = " insert into dtb_blocposition (";
209                $sql .= "     page_id,";
210                $sql .= "     target_id,";
211                $sql .= "     bloc_id,";
212                $sql .= "     bloc_row";
213                $sql .= "     )values(?, ?, ?, ?)";
214
215                // 取得件数文INSERT実行
216                foreach($ret as $key => $val){
217                    $ret = $objQuery->query($sql,$val);
218                }
219            }
220        }
221        $_SESSION['preview'] = "ON";
222        $this->sendRedirect($this->getLocation(URL_DIR . "preview/" . DIR_INDEX_URL, array("filename" => $arrPageData[0]["filename"])));
223
224    }
225
226    /**
227     * データ登録処理.
228     *
229     * @param integer $page_id ページID
230     * @return void
231     */
232    function lfConfirmPageData($page_id){
233        // エラーチェック
234        $this->arrErr = $this->lfErrorCheck($_POST);
235
236        // エラーがなければ更新処理を行う
237        if (count($this->arrErr) == 0) {
238            // DBへデータを更新する
239            $this->lfEntryPageData($_POST);
240
241            // ベースデータでなければファイルを削除し、PHPファイルを作成する
242            if (!$this->objLayout->lfCheckBaseData($page_id)) {
243                // PHPファイル作成
244                $this->lfCreatePHPFile($_POST['url']);
245            }
246
247            // TPLファイル作成
248            $cre_tpl = USER_TEMPLATE_PATH . "/" . TEMPLATE_NAME . "/" . basename($_POST['url']) . '.tpl';
249            $this->lfCreateFile($cre_tpl, $_POST['tpl_data']);
250
251            // 新規作成の場合、
252            if ($page_id == '') {
253                // ページIDを取得する
254                $arrPageData = $this->objLayout->lfgetPageData(" url = ? AND page_id <> 0" , array(USER_DIR . $_POST['url'] . '.php'));
255                $page_id = $arrPageData[0]['page_id'];
256            }
257            $this->sendRedirect($this->getLocation("./main_edit.php",
258                                    array("page_id" => $page_id,
259                                          "msg"     => "on")));
260            exit;
261        } else {
262            // エラーがあれば入力時のデータを表示する
263            $this->arrPageData = $_POST;
264            $this->arrPageData['header_chk'] = SC_Utils_Ex::sfChangeCheckBox(SC_Utils_Ex::sfChangeCheckBox($_POST['header_chk']), true);
265            $this->arrPageData['footer_chk'] = SC_Utils_Ex::sfChangeCheckBox(SC_Utils_Ex::sfChangeCheckBox($_POST['footer_chk']), true);
266            $this->arrPageData['directory'] = '';
267            $this->arrPageData['filename'] = $_POST['url'];
268        }
269    }
270
271    /**
272     * ブロック情報を更新する.
273     *
274     * @param array $arrData 更新データ
275     * @return void
276     */
277    function lfEntryPageData($arrData){
278        $objQuery = new SC_Query();
279        $arrChk = array();          // 排他チェック用
280
281        // 更新データの変換
282        $sqlval = $this->lfGetUpdData($arrData);
283
284        // データが存在しているかチェックを行う
285        if($arrData['page_id'] !== ''){
286            $arrChk = $this->objLayout->lfgetPageData("page_id = ?", array($arrData['page_id']));
287        }
288
289        // page_id が空 若しくは データが存在していない場合にはINSERTを行う
290        if ($arrData['page_id'] === '' or !isset($arrChk[0])) {
291            $sqlval['page_id'] = $objQuery->nextVal('dtb_pagelayout_page_id');
292            $sqlval['create_date'] = 'now()';
293            $objQuery->insert('dtb_pagelayout', $sqlval);
294        }
295        // データが存在してる場合にはアップデートを行う
296        else {
297            $objQuery->update('dtb_pagelayout', $sqlval, 'page_id = ?', array($arrData['page_id']));
298        }
299    }
300
301    /**
302     * DBへ更新を行うデータを生成する.
303     *
304     * @param array $arrData 更新データ
305     * @return array 更新データ
306     */
307    function lfGetUpdData($arrData){
308        $arrUpdData = array(
309            'header_chk'    => SC_Utils_Ex::sfChangeCheckBox($arrData['header_chk']),   // ヘッダー使用
310            'footer_chk'    => SC_Utils_Ex::sfChangeCheckBox($arrData['footer_chk']),   // フッター使用
311            'update_url'    => $_SERVER['HTTP_REFERER'],                                // 更新URL
312            'update_date'   => 'now()',
313        );
314
315        // ベースデータの場合には変更しない。
316        if (!$this->objLayout->lfCheckBaseData($arrData['page_id'])) {
317            $arrUpdData['page_name']    = $arrData['page_name'] ;
318            $arrUpdData['url']          = USER_DIR . $arrData['url'] . '.php';
319            $arrUpdData['php_dir']      = dirname($arrUpdData['url']);
320            if ($arrUpdData['php_dir'] == '.') {
321                $arrUpdData['php_dir'] = '';
322            } else {
323                $arrUpdData['php_dir'] .= '/';
324            }
325            $arrUpdData['tpl_dir']      = substr(TPL_DIR, strlen(URL_DIR));
326            $arrUpdData['filename']     = basename($arrData['url']); // 拡張子を付加しない
327        }
328
329        return $arrUpdData;
330    }
331
332    /**
333     * ページデータを削除する.
334     *
335     * @param integer $page_id ページID
336     * @return void
337     */
338    function lfDeletePageData($page_id){
339        $this->objLayout->lfDelPageData($_POST['page_id']);
340        $this->sendRedirect($this->getLocation("./main_edit.php"));
341    }
342
343    /**
344     * 入力項目のエラーチェックを行う.
345     *
346     * @param array $arrData 入力データ
347     * @return array エラー情報
348     */
349    function lfErrorCheck($array) {
350        $objErr = new SC_CheckError($array);
351        $objErr->doFunc(array("名称", "page_name", STEXT_LEN), array("EXIST_CHECK", "SPTAB_CHECK", "MAX_LENGTH_CHECK"));
352        $objErr->doFunc(array("URL", "url", STEXT_LEN), array("EXIST_CHECK", "SPTAB_CHECK", "MAX_LENGTH_CHECK"));
353       
354        // URLチェック
355        $okUrl = true;
356        foreach (explode('/', $array['url']) as $url_part) {
357            if (!ereg( '^[a-zA-Z0-9:_~\.-]+$', $url_part)) {
358                $okUrl = false;
359            }
360            if ($url_part == '.' || $url_part == '..') {
361                $okUrl = false;
362            }
363        }
364        if (!$okUrl) {
365            $objErr->arrErr['url'] = "※ URLを正しく入力してください。<br />";
366        }
367       
368        // 同一のURLが存在している場合にはエラー
369        $params = array();
370       
371        $sqlWhere = 'url = ?';
372        $params[] = USER_DIR . $array['url'] . '.php';
373       
374        // プレビュー用のレコードは除外
375        $sqlWhere .= ' AND page_id <> 0';
376       
377        // 変更の場合、自身のレコードは除外
378        if (strlen($array['page_id']) != 0) {
379            $sqlWhere .= ' AND page_id <> ?';
380            $params[] = $array['page_id'];
381        }
382       
383        $arrChk = $this->objLayout->lfgetPageData($sqlWhere , $params);
384       
385        if (count($arrChk) >= 1) {
386            $objErr->arrErr['url'] = '※ 同じURLのデータが存在しています。別のURLを付けてください。<br />';
387        }
388       
389        return $objErr->arrErr;
390    }
391
392    /**
393     * ファイルを作成する.
394     *
395     * @param string $path テンプレートファイルのパス
396     * @param string $data テンプレートの内容
397     * @return void
398     */
399    function lfCreateFile($path, $data){
400
401        // ディレクトリが存在していなければ作成する
402        if (!is_dir(dirname($path))) {
403            mkdir(dirname($path));
404        }
405
406        // ファイル作成
407        $fp = fopen($path,"w");
408        fwrite($fp, $data); // FIXME いきなり POST はちょっと...
409        fclose($fp);
410    }
411
412    /**
413     * PHPファイルを作成する.
414     *
415     * @param string $path PHPファイルのパス
416     * @return void
417     */
418    function lfCreatePHPFile($url){
419
420        $path = USER_PATH . $url . ".php";
421
422        // カスタマイズを考慮し、上書きしない。(#831)
423        if (file_exists($path)) {
424            return;
425        }
426
427        // php保存先ディレクトリが存在していなければ作成する
428        if (!is_dir(dirname($path))) {
429            mkdir(dirname($path));
430        }
431
432        // ベースとなるPHPファイルの読み込み
433        if (file_exists(USER_DEF_PHP)){
434            $php_data = file_get_contents(USER_DEF_PHP);
435        }
436
437        // require.phpの場所を書き換える
438        $php_data = str_replace("###require###", str_repeat('../', substr_count($url, '/')) . '../require.php', $php_data);
439
440        // phpファイルの作成
441        $fp = fopen($path,"w");
442        fwrite($fp, $php_data);
443        fclose($fp);
444    }
445
446}
447?>
Note: See TracBrowser for help on using the repository browser.