source: branches/version-2_13-dev/data/class/SC_SessionFactory.php @ 23605

Revision 23605, 7.1 KB checked in by kimoto, 10 years ago (diff)

#2448 typo修正・ソース整形・ソースコメントの改善 for 2.13.3

Scrutinizer Auto-Fixes

This patch was automatically generated as part of the following inspection:
 https://scrutinizer-ci.com/g/nobuhiko/EC-CUBE/inspections/d8722894-69a6-4b1b-898d-43618035c60d

Enabled analysis tools:

  • PHP Analyzer
  • PHP PDepend
  • PHP Similarity Analyzer
  • PHP Change Tracking Analyzer
  • 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-2014 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/**
25 * セッションの初期化処理を抽象化するファクトリークラス.
26 *
27 * このクラスはセッションの維持方法を管理するクラスです.
28 * 他のセッション管理クラスとは若干異なります.
29 *
30 * EC-CUBE2.1.1ベータ版から、
31 * 管理画面>基本情報>パラメーター管理で、セッションの維持方法を
32 * ・Cookieを使用する場合
33 * ・リクエストパラメーターを使用する場合
34 * の2種類が選択できますが、どちらの設定であっても下記のように呼び出すことで
35 * 適切にセッションを開始することができます.
36 *
37 * $sessionFactory = SC_SessionFactory::getInstance()
38 * $sessionFactory->initSession();
39 *
40 * @package SC_Session
41 * @author LOCKON CO.,LTD.
42 * @version $Id$
43 */
44class SC_SessionFactory
45{
46    /**
47     * パラメーター管理で設定したセッション維持設定に従って適切なオブジェクトを返す.
48     *
49     * @return SC_SessionFactory
50     */
51    public static function getInstance()
52    {
53        $type = defined('SESSION_KEEP_METHOD')
54            ? SESSION_KEEP_METHOD
55            : '';
56
57        switch ($type) {
58            // セッションの維持にリクエストパラメーターを使用する
59            case 'useRequest':
60                $session = new SC_SessionFactory_UseRequest_Ex;
61                SC_Display_Ex::detectDevice() == DEVICE_TYPE_MOBILE
62                    ? $session->setState('mobile')
63                    : $session->setState('pc');
64                break;
65
66            // クッキーを使用する
67            case 'useCookie':
68            default:
69                // モバイルの場合はSC_SessionFactory_UseRequestを使用する
70                if (SC_Display_Ex::detectDevice() == DEVICE_TYPE_MOBILE) {
71                    $session = new SC_SessionFactory_UseRequest_Ex;
72                    $session->setState('mobile');
73                } else {
74                    $session = new SC_SessionFactory_UseCookie_Ex;
75                }
76                break;
77        }
78
79        return $session;
80    }
81
82    /**
83     * セッションの初期化を行う.
84     *
85     */
86    public function initSession()
87    {
88        session_set_save_handler(array(&$this, 'sfSessOpen'),
89            array(&$this, 'sfSessClose'),
90            array(&$this, 'sfSessRead'),
91            array(&$this, 'sfSessWrite'),
92            array(&$this, 'sfSessDestroy'),
93            array(&$this, 'sfSessGc'));
94
95        // 通常よりも早い段階(オブジェクトが破棄される前)でセッションデータを書き込んでセッションを終了する
96        // XXX APC による MDB2 の破棄タイミングによる不具合を回避する目的
97        register_shutdown_function('session_write_close');
98    }
99
100    /**
101     * Cookieを使用するかどうかを返す.
102     *
103     * @return boolean|null
104     */
105    public function useCookie()
106    {
107    }
108
109    /**
110     * セッションを開始する.
111     *
112     * @param  string $save_path    セッションを保存するパス(使用しない)
113     * @param  string $session_name セッション名(使用しない)
114     * @return bool   セッションが正常に開始された場合 true
115     */
116    public function sfSessOpen($save_path, $session_name)
117    {
118        return true;
119    }
120
121    /**
122     * セッションを閉じる.
123     *
124     * @return bool セッションが正常に終了した場合 true
125     */
126    public function sfSessClose()
127    {
128        return true;
129    }
130
131    /**
132     * セッションのデータをDBから読み込む.
133     *
134     * @param  string $id セッションID
135     * @return string セッションデータの値
136     */
137    public function sfSessRead($id)
138    {
139        $objQuery =& SC_Query_Ex::getSingletonInstance();
140        $arrRet = $objQuery->select('sess_data', 'dtb_session', 'sess_id = ?', array($id));
141        if (empty($arrRet)) {
142            return '';
143        } else {
144            return $arrRet[0]['sess_data'];
145        }
146    }
147
148    /**
149     * セッションのデータをDBに書き込む.
150     *
151     * @param  string $id        セッションID
152     * @param  string $sess_data セッションデータの値
153     * @return bool   セッションの書き込みに成功した場合 true
154     */
155    public function sfSessWrite($id, $sess_data)
156    {
157        $objQuery =& SC_Query_Ex::getSingletonInstance();
158        $exists = $objQuery->exists('dtb_session', 'sess_id = ?', array($id));
159        $sqlval = array();
160        if ($exists) {
161            // レコード更新
162            $sqlval['sess_data'] = $sess_data;
163            $sqlval['update_date'] = 'CURRENT_TIMESTAMP';
164            $objQuery->update('dtb_session', $sqlval, 'sess_id = ?', array($id));
165        } else {
166            // セッションデータがある場合は、レコード作成
167            if (strlen($sess_data) > 0) {
168                $sqlval['sess_id'] = $id;
169                $sqlval['sess_data'] = $sess_data;
170                $sqlval['update_date'] = 'CURRENT_TIMESTAMP';
171                $sqlval['create_date'] = 'CURRENT_TIMESTAMP';
172                $objQuery->insert('dtb_session', $sqlval);
173            }
174        }
175
176        return true;
177    }
178
179    // セッション破棄
180
181    /**
182     * セッションを破棄する.
183     *
184     * @param  string $id セッションID
185     * @return bool   セッションを正常に破棄した場合 true
186     */
187    public function sfSessDestroy($id)
188    {
189        $objQuery =& SC_Query_Ex::getSingletonInstance();
190        $objQuery->delete('dtb_session', 'sess_id = ?', array($id));
191
192        return true;
193    }
194
195    /**
196     * ガーベジコレクションを実行する.
197     *
198     * 引数 $maxlifetime の代りに 定数 MAX_LIFETIME を使用する.
199     *
200     * @param integer $maxlifetime セッションの有効期限(使用しない)
201     * @return bool
202     */
203    public function sfSessGc($maxlifetime)
204    {
205        // MAX_LIFETIME以上更新されていないセッションを削除する。
206        $objQuery =& SC_Query_Ex::getSingletonInstance();
207        $limit = date("Y-m-d H:i:s", time() - MAX_LIFETIME);
208        $where = "update_date < '". $limit . "' ";
209        $objQuery->delete('dtb_session', $where);
210
211        return true;
212    }
213}
214/*
215 * Local variables:
216 * coding: utf-8
217 * End:
218 */
Note: See TracBrowser for help on using the repository browser.