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

Revision 22790, 15.3 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
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_Design_MainEdit extends LC_Page_Admin_Ex
35{
36
37    // }}}
38    // {{{ functions
39
40    /**
41     * Page を初期化する.
42     *
43     * @return void
44     */
45    function init()
46    {
47        parent::init();
48        $this->tpl_mainpage = 'design/main_edit.tpl';
49        $this->text_row     = 13;
50        $this->tpl_subno = 'main_edit';
51        $this->tpl_mainno = 'design';
52        $this->tpl_maintitle = 'デザイン管理';
53        $this->tpl_subtitle = 'ページ詳細設定';
54        $masterData = new SC_DB_MasterData_Ex();
55        $this->arrDeviceType = $masterData->getMasterData('mtb_device_type');
56    }
57
58    /**
59     * Page のプロセス.
60     *
61     * @return void
62     */
63    function process()
64    {
65        $this->action();
66        $this->sendResponse();
67    }
68
69    /**
70     * Page のアクション.
71     *
72     * @return void
73     */
74    function action()
75    {
76
77        $objLayout = new SC_Helper_PageLayout_Ex();
78        $objFormParam = new SC_FormParam_Ex();
79        $this->lfInitParam($objFormParam);
80        $objFormParam->setParam($_REQUEST);
81        $objFormParam->convParam();
82        $this->arrErr = $objFormParam->checkError();
83        $is_error = (!SC_Utils_Ex::isBlank($this->arrErr));
84
85        $this->device_type_id = $objFormParam->getValue('device_type_id', DEVICE_TYPE_PC);
86        $this->page_id = $objFormParam->getValue('page_id');
87
88        switch ($this->getMode()) {
89            // 削除
90            case 'delete':
91                if (!$is_error) {
92                    if ($objLayout->isEditablePage($this->device_type_id, $this->page_id)) {
93                        $objLayout->lfDelPageData($this->page_id, $this->device_type_id);
94
95                        SC_Response_Ex::reload(array('device_type_id' => $this->device_type_id,
96                                                     'msg' => 'on'), true);
97                        SC_Response_Ex::actionExit();
98                    }
99                }
100                break;
101
102            // 登録/編集
103            case 'confirm':
104                if (!$is_error) {
105                    $this->arrErr = $this->lfCheckError($objFormParam, $this->arrErr);
106                    if (SC_Utils_Ex::isBlank($this->arrErr)) {
107                        $result = $this->doRegister($objFormParam, $objLayout);
108                        if ($result !== false) {
109                            $arrQueryString = array(
110                                'device_type_id' => $this->device_type_id,
111                                'page_id' => $result,
112                                'msg' => 'on',
113                            );
114                            SC_Response_Ex::reload($arrQueryString, true);
115                            SC_Response_Ex::actionExit();
116                        }
117                    }
118                }
119                break;
120
121            default:
122                if (isset($_GET['msg']) && $_GET['msg'] == 'on') {
123                    $this->tpl_onload = "alert('登録が完了しました。');";
124                }
125                break;
126        }
127
128        if (!$is_error) {
129            $this->arrPageList = $objLayout->getPageProperties($this->device_type_id, null);
130            // page_id が指定されている場合にはテンプレートデータの取得
131            if (!SC_Utils_Ex::isBlank($this->page_id)) {
132                $arrPageData = $this->getTplMainpage($this->device_type_id, $this->page_id, $objLayout);
133                $objFormParam->setParam($arrPageData);
134            }
135        } else {
136            // 画面にエラー表示しないため, ログ出力
137            GC_Utils_Ex::gfPrintLog('Error: ' . print_r($this->arrErr, true));
138        }
139        $this->tpl_subtitle = $this->arrDeviceType[$this->device_type_id] . '>' . $this->tpl_subtitle;
140        $this->arrForm = $objFormParam->getFormParamList();
141
142    }
143
144    /**
145     * デストラクタ.
146     *
147     * @return void
148     */
149    function destroy()
150    {
151        parent::destroy();
152    }
153
154    /**
155     * パラメーター情報の初期化
156     *
157     * XXX URL のフィールドは, 実際は filename なので注意
158     *
159     * @param object $objFormParam SC_FormParamインスタンス
160     * @return void
161     */
162    function lfInitParam(&$objFormParam)
163    {
164        $objFormParam->addParam('ページID', 'page_id', INT_LEN, 'n', array('NUM_CHECK', 'MAX_LENGTH_CHECK'));
165        $objFormParam->addParam('端末種別ID', 'device_type_id', INT_LEN, 'n', array('NUM_CHECK', 'MAX_LENGTH_CHECK'));
166        $objFormParam->addParam('名称', 'page_name', STEXT_LEN, 'KVa', array('SPTAB_CHECK', 'MAX_LENGTH_CHECK'));
167        $objFormParam->addParam('URL', 'filename', STEXT_LEN, 'a', array('SPTAB_CHECK', 'MAX_LENGTH_CHECK'));
168        $objFormParam->addParam('ヘッダチェック', 'header_chk', INT_LEN, 'n', array('NUM_CHECK', 'MAX_LENGTH_CHECK'));
169        $objFormParam->addParam('フッタチェック', 'footer_chk', INT_LEN, 'n', array('NUM_CHECK', 'MAX_LENGTH_CHECK'));
170        $objFormParam->addParam('修正フラグ', 'edit_flg', INT_LEN, 'n', array('NUM_CHECK', 'MAX_LENGTH_CHECK'));
171        $objFormParam->addParam('TPLデータ', 'tpl_data');
172        $objFormParam->addParam('meta タグ:author', 'author', MTEXT_LEN, 'KVa', array('MAX_LENGTH_CHECK'));
173        $objFormParam->addParam('meta タグ:description', 'description', MTEXT_LEN, 'KVa', array('MAX_LENGTH_CHECK'));
174        $objFormParam->addParam('meta タグ:keyword', 'keyword', MTEXT_LEN, 'KVa', array('MAX_LENGTH_CHECK'));
175        $objFormParam->addParam('meta タグ:robots', 'meta_robots', MTEXT_LEN, 'KVa', array('MAX_LENGTH_CHECK'));
176    }
177
178    /**
179     * ページデータを取得する.
180     *
181     * @param integer $device_type_id 端末種別ID
182     * @param integer $page_id ページID
183     * @param SC_Helper_PageLayout $objLayout SC_Helper_PageLayout インスタンス
184     * @return array ページデータの配列
185     */
186    function getTplMainpage($device_type_id, $page_id, &$objLayout)
187    {
188        $arrPageData = $objLayout->getPageProperties($device_type_id, $page_id);
189
190        $templatePath = $objLayout->getTemplatePath($device_type_id);
191        $filename = $templatePath . $arrPageData[0]['filename'] . '.tpl';
192        if (file_exists($filename)) {
193            $arrPageData[0]['tpl_data'] = file_get_contents($filename);
194        }
195        // ファイル名を画面表示用に加工しておく
196        $arrPageData[0]['filename'] = preg_replace('|^' . preg_quote(USER_DIR) . '|', '', $arrPageData[0]['filename']);
197        return $arrPageData[0];
198    }
199
200    /**
201     * 登録を実行する.
202     *
203     * ファイルの作成に失敗した場合は, エラーメッセージを出力し,
204     * データベースをロールバックする.
205     *
206     * @param SC_FormParam $objFormParam SC_FormParam インスタンス
207     * @param SC_Helper_PageLayout $objLayout SC_Helper_PageLayout インスタンス
208     * @return integer|boolean 登録が成功した場合, 登録したページID;
209     *                         失敗した場合 false
210     */
211    function doRegister(&$objFormParam, &$objLayout)
212    {
213        $filename = $objFormParam->getValue('filename');
214        $arrParams['device_type_id'] = $objFormParam->getValue('device_type_id');
215        $arrParams['page_id'] = $objFormParam->getValue('page_id');
216        $arrParams['header_chk'] = intval($objFormParam->getValue('header_chk')) === 1 ? 1 : 2;
217        $arrParams['footer_chk'] = intval($objFormParam->getValue('footer_chk')) === 1 ? 1 : 2;
218        $arrParams['tpl_data'] = $objFormParam->getValue('tpl_data');
219        $arrParams['page_name'] = $objFormParam->getValue('page_name');
220        $arrParams['url'] = USER_DIR . $filename . '.php';
221        $arrParams['filename'] = USER_DIR . $filename;
222        $arrParams['author']        = $objFormParam->getValue('author');
223        $arrParams['description']   = $objFormParam->getValue('description');
224        $arrParams['keyword']       = $objFormParam->getValue('keyword');
225        $arrParams['meta_robots']   = $objFormParam->getValue('meta_robots');
226
227        $objQuery =& SC_Query_Ex::getSingletonInstance();
228        $objQuery->begin();
229
230        $page_id = $this->registerPage($arrParams, $objLayout);
231
232        /*
233         * 新規登録時
234         * or 編集可能な既存ページ編集時かつ, PHP ファイルが存在しない場合に,
235         * PHP ファイルを作成する.
236         */
237        if (SC_Utils_Ex::isBlank($arrParams['page_id'])
238            || $objLayout->isEditablePage($arrParams['device_type_id'], $arrParams['page_id'])) {
239            if (!$this->createPHPFile($filename)) {
240                $this->arrErr['err'] = '※ PHPファイルの作成に失敗しました<br />';
241                $objQuery->rollback();
242                return false;
243            }
244            // 新規登録時のみ $page_id を代入
245            $arrParams['page_id'] = $page_id;
246        }
247
248        if ($objLayout->isEditablePage($arrParams['device_type_id'], $page_id)) {
249            $tpl_path = $objLayout->getTemplatePath($arrParams['device_type_id']) . $arrParams['filename'] . '.tpl';
250        } else {
251            $tpl_path = $objLayout->getTemplatePath($arrParams['device_type_id']) . $filename . '.tpl';
252        }
253
254        if (!SC_Helper_FileManager_Ex::sfWriteFile($tpl_path, $arrParams['tpl_data'])) {
255            $this->arrErr['err'] = '※ TPLファイルの書き込みに失敗しました<br />';
256            $objQuery->rollback();
257            return false;
258        }
259
260        $objQuery->commit();
261        return $arrParams['page_id'];
262    }
263
264    /**
265     * 入力内容をデータベースに登録する.
266     *
267     * @param array $arrParams フォームパラメーターの配列
268     * @param SC_Helper_PageLayout $objLayout SC_Helper_PageLayout インスタンス
269     * @return integer ページID
270     */
271    function registerPage($arrParams, &$objLayout)
272    {
273        $objQuery =& SC_Query_Ex::getSingletonInstance();
274
275        // ページIDが空の場合は新規登録
276        $is_new = SC_Utils_Ex::isBlank($arrParams['page_id']);
277        // 既存ページの存在チェック
278        if (!$is_new) {
279            $arrExists = $objLayout->getPageProperties($arrParams['device_type_id'], $arrParams['page_id']);
280        }
281
282        $table = 'dtb_pagelayout';
283        $arrValues = $objQuery->extractOnlyColsOf($table, $arrParams);
284        $arrValues['update_url'] = $_SERVER['HTTP_REFERER'];
285        $arrValues['update_date'] = 'CURRENT_TIMESTAMP';
286
287        // 新規登録
288        if ($is_new || SC_Utils_Ex::isBlank($arrExists)) {
289            $objQuery->setOrder('');
290            $arrValues['page_id'] = 1 + $objQuery->max('page_id', $table, 'device_type_id = ?',
291                                                       array($arrValues['device_type_id']));
292            $arrValues['create_date'] = 'CURRENT_TIMESTAMP';
293            $objQuery->insert($table, $arrValues);
294        }
295        // 更新
296        else {
297            // 編集不可ページは更新しない
298            if (!$objLayout->isEditablePage($arrValues['device_type_id'], $arrValues['page_id'])) {
299                unset($arrValues['page_name']);
300                unset($arrValues['filename']);
301                unset($arrValues['url']);
302            }
303
304            $objQuery->update($table, $arrValues, 'page_id = ? AND device_type_id = ?',
305                              array($arrValues['page_id'], $arrValues['device_type_id']));
306        }
307        return $arrValues['page_id'];
308    }
309
310    /**
311     * エラーチェックを行う.
312     *
313     * @param SC_FormParam $objFormParam SC_FormParam インスタンス
314     * @return array エラーメッセージの配列
315     */
316    function lfCheckError(&$objFormParam, &$arrErr)
317    {
318        $arrParams = $objFormParam->getHashArray();
319        $objErr = new SC_CheckError_Ex($arrParams);
320        $objErr->arrErr =& $arrErr;
321        $objErr->doFunc(array('名称', 'page_name', STEXT_LEN), array('EXIST_CHECK', 'SPTAB_CHECK', 'MAX_LENGTH_CHECK'));
322        $objErr->doFunc(array('URL', 'filename', STEXT_LEN), array('EXIST_CHECK', 'SPTAB_CHECK', 'MAX_LENGTH_CHECK'));
323
324        /*
325         * URL チェック
326         * ここでチェックするのは, パスのみなので SC_CheckError::URL_CHECK()
327         * は使用しない
328         */
329        $valid_url = true;
330        foreach (explode('/', $arrParams['filename']) as $val) {
331            if (!preg_match('/^[a-zA-Z0-9:_~\.\-]+$/', $val)) {
332                $valid_url = false;
333            }
334            if ($val == '.' || $val == '..') {
335                $valid_url = false;
336            }
337        }
338        if (!$valid_url) {
339            $objErr->arrErr['filename'] = '※ URLを正しく入力してください。<br />';
340        }
341        // 同一URLの存在チェック
342        $where = 'page_id <> 0 AND device_type_id = ? AND filename = ?';
343        $arrValues = array($arrParams['device_type_id'], USER_DIR . $arrParams['filename']);
344        // 変更の場合は自 URL を除外
345        if (!SC_Utils_Ex::isBlank($arrParams['page_id'])) {
346            $where .= ' AND page_id <> ?';
347            $arrValues[] = $arrParams['page_id'];
348        }
349
350        $objQuery =& SC_Query_Ex::getSingletonInstance();
351        $exists = $objQuery->exists('dtb_pagelayout', $where, $arrValues);
352        if ($exists) {
353            $objErr->arrErr['filename'] = '※ 同じURLのデータが存在しています。別のURLを入力してください。<br />';
354        }
355        return $objErr->arrErr;
356    }
357
358    /**
359     * PHP ファイルを生成する.
360     *
361     * 既に同名の PHP ファイルが存在する場合は何もせず true を返す.(#831)
362     *
363     * @param string $filename フォームパラメーターの filename
364     * @return boolean 作成に成功した場合 true
365     */
366    function createPHPFile($filename)
367    {
368        $path = USER_REALDIR . $filename . '.php';
369
370        if (file_exists($path)) {
371            return true;
372        }
373
374        if (file_exists(USER_DEF_PHP_REALFILE)) {
375            $php_contents = file_get_contents(USER_DEF_PHP_REALFILE);
376        } else {
377            return false;
378        }
379
380        // require.php の PATH を書き換える
381        $defaultStrings = "exit; // Don't rewrite. This line is rewritten by EC-CUBE.";
382        $replaceStrings = "require_once '" . str_repeat('../', substr_count($filename, '/')) . "../require.php';";
383        $php_contents = str_replace($defaultStrings, $replaceStrings, $php_contents);
384
385        return SC_Helper_FileManager_Ex::sfWriteFile($path, $php_contents);
386    }
387}
Note: See TracBrowser for help on using the repository browser.