source: branches/version-2_13-dev/data/class/SC_Customer.php @ 23352

Revision 23352, 12.9 KB checked in by shutta, 10 years ago (diff)

#2513 (Session Fixation対策)
ログイン後には、セッションIDを新しいIDに更新するよう修正。

  • 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/*  [名称] SC_Customer
25 *  [概要] 会員管理クラス
26 */
27class SC_Customer
28{
29    /** 会員情報 */
30    public $customer_data;
31
32    public function getCustomerDataFromEmailPass($pass, $email, $mobile = false)
33    {
34        // 小文字に変換
35        $email = strtolower($email);
36        $sql_mobile = $mobile ? ' OR email_mobile = ?' : '';
37        $arrValues = array($email);
38        if ($mobile) {
39            $arrValues[] = $email;
40        }
41        // 本登録された会員のみ
42        $sql = 'SELECT * FROM dtb_customer WHERE (email = ?' . $sql_mobile . ') AND del_flg = 0 AND status = 2';
43        $objQuery =& SC_Query_Ex::getSingletonInstance();
44        $result = $objQuery->getAll($sql, $arrValues);
45        if (empty($result)) {
46            return false;
47        } else {
48            $data = $result[0];
49        }
50
51        // パスワードが合っていれば会員情報をcustomer_dataにセットしてtrueを返す
52        if (SC_Utils_Ex::sfIsMatchHashPassword($pass, $data['password'], $data['salt'])) {
53            $this->customer_data = $data;
54            $this->startSession();
55
56            return true;
57        }
58
59        return false;
60    }
61
62    /**
63     * 携帯端末IDが一致する会員が存在するかどうかをチェックする。
64     * FIXME
65     * @return boolean 該当する会員が存在する場合は true、それ以外の場合
66     *                 は false を返す。
67     */
68    public function checkMobilePhoneId()
69    {
70        //docomo用にデータを取り出す。
71        if (SC_MobileUserAgent_Ex::getCarrier() == 'docomo') {
72            if ($_SESSION['mobile']['phone_id'] == '' && strlen($_SESSION['mobile']['phone_id']) == 0) {
73                $_SESSION['mobile']['phone_id'] = SC_MobileUserAgent_Ex::getId();
74            }
75        }
76        if (!isset($_SESSION['mobile']['phone_id']) || $_SESSION['mobile']['phone_id'] === false) {
77            return false;
78        }
79
80        // 携帯端末IDが一致し、本登録された会員を検索する。
81        $objQuery =& SC_Query_Ex::getSingletonInstance();
82        $exists = $objQuery->exists('dtb_customer', 'mobile_phone_id = ? AND del_flg = 0 AND status = 2', array($_SESSION['mobile']['phone_id']));
83
84        return $exists;
85    }
86
87    /**
88     * 携帯端末IDを使用して会員を検索し、パスワードの照合を行う。
89     * パスワードが合っている場合は会員情報を取得する。
90     *
91     * @param  string  $pass パスワード
92     * @return boolean 該当する会員が存在し、パスワードが合っている場合は true、
93     *                 それ以外の場合は false を返す。
94     */
95    public function getCustomerDataFromMobilePhoneIdPass($pass)
96    {
97        //docomo用にデータを取り出す。
98        if (SC_MobileUserAgent_Ex::getCarrier() == 'docomo') {
99            if ($_SESSION['mobile']['phone_id'] == '' && strlen($_SESSION['mobile']['phone_id']) == 0) {
100                $_SESSION['mobile']['phone_id'] = SC_MobileUserAgent_Ex::getId();
101            }
102        }
103        if (!isset($_SESSION['mobile']['phone_id']) || $_SESSION['mobile']['phone_id'] === false) {
104            return false;
105        }
106
107        // 携帯端末IDが一致し、本登録された会員を検索する。
108        $sql = 'SELECT * FROM dtb_customer WHERE mobile_phone_id = ? AND del_flg = 0 AND status = 2';
109        $objQuery =& SC_Query_Ex::getSingletonInstance();
110        @list($data) = $objQuery->getAll($sql, array($_SESSION['mobile']['phone_id']));
111
112        // パスワードが合っている場合は、会員情報をcustomer_dataに格納してtrueを返す。
113        if (SC_Utils_Ex::sfIsMatchHashPassword($pass, $data['password'], $data['salt'])) {
114            $this->customer_data = $data;
115            $this->startSession();
116
117            return true;
118        }
119
120        return false;
121    }
122
123    /**
124     * 携帯端末IDを登録する。
125     *
126     * @return void
127     */
128    public function updateMobilePhoneId()
129    {
130        if (!isset($_SESSION['mobile']['phone_id']) || $_SESSION['mobile']['phone_id'] === false) {
131            return;
132        }
133
134        if ($this->customer_data['mobile_phone_id'] == $_SESSION['mobile']['phone_id']) {
135            return;
136        }
137
138        $objQuery =& SC_Query_Ex::getSingletonInstance();
139        $sqlval = array('mobile_phone_id' => $_SESSION['mobile']['phone_id']);
140        $where = 'customer_id = ? AND del_flg = 0 AND status = 2';
141        $objQuery->update('dtb_customer', $sqlval, $where, array($this->customer_data['customer_id']));
142
143        $this->customer_data['mobile_phone_id'] = $_SESSION['mobile']['phone_id'];
144    }
145
146    // パスワードを確認せずにログイン
147    public function setLogin($email)
148    {
149        // 本登録された会員のみ
150        $sql = 'SELECT * FROM dtb_customer WHERE (email = ? OR email_mobile = ?) AND del_flg = 0 AND status = 2';
151        $objQuery =& SC_Query_Ex::getSingletonInstance();
152        $result = $objQuery->getAll($sql, array($email, $email));
153        $data = isset($result[0]) ? $result[0] : '';
154        $this->customer_data = $data;
155        $this->startSession();
156    }
157
158    // セッション情報を最新の情報に更新する
159    public function updateSession()
160    {
161        $sql = 'SELECT * FROM dtb_customer WHERE customer_id = ? AND del_flg = 0';
162        $customer_id = $this->getValue('customer_id');
163        $objQuery =& SC_Query_Ex::getSingletonInstance();
164        $arrRet = $objQuery->getAll($sql, array($customer_id));
165        $this->customer_data = isset($arrRet[0]) ? $arrRet[0] : '';
166        $_SESSION['customer'] = $this->customer_data;
167    }
168
169    // ログイン情報をセッションに登録し、ログに書き込む
170    public function startSession()
171    {
172        $_SESSION['customer'] = $this->customer_data;
173        // セッション情報の保存
174        GC_Utils_Ex::gfPrintLog('access : user='.$this->customer_data['customer_id'] ."\t".'ip='. $this->getRemoteHost(), CUSTOMER_LOG_REALFILE, false);
175    }
176
177    // ログアウト $_SESSION['customer']を解放し、ログに書き込む
178    public function EndSession()
179    {
180        // セッション情報破棄の前にcustomer_idを保存
181        $customer_id = $_SESSION['customer']['customer_id'];
182
183        // $_SESSION['customer']の解放
184        unset($_SESSION['customer']);
185        // セッションの配送情報を全て破棄する
186        SC_Helper_Purchase_Ex::unsetAllShippingTemp(true);
187        // トランザクショントークンの破棄
188        SC_Helper_Session_Ex::destroyToken();
189        $objSiteSess = new SC_SiteSession_Ex();
190        $objSiteSess->unsetUniqId();
191
192        // ログに記録する
193        $log = sprintf("logout : user=%d\tip=%s",
194            $customer_id, $this->getRemoteHost());
195        GC_Utils_Ex::gfPrintLog($log, CUSTOMER_LOG_REALFILE, false);
196    }
197
198    // ログインに成功しているか判定する。
199    public function isLoginSuccess($dont_check_email_mobile = false)
200    {
201        // ログイン時のメールアドレスとDBのメールアドレスが一致している場合
202        if (isset($_SESSION['customer']['customer_id'])
203            && SC_Utils_Ex::sfIsInt($_SESSION['customer']['customer_id'])
204        ) {
205            $objQuery =& SC_Query_Ex::getSingletonInstance();
206            $email = $objQuery->get('email', 'dtb_customer', 'customer_id = ?', array($_SESSION['customer']['customer_id']));
207            if ($email == $_SESSION['customer']['email']) {
208                // モバイルサイトの場合は携帯のメールアドレスが登録されていることもチェックする。
209                // ただし $dont_check_email_mobile が true の場合はチェックしない。
210                if (SC_Display_Ex::detectDevice() == DEVICE_TYPE_MOBILE && !$dont_check_email_mobile) {
211                    $email_mobile = $objQuery->get('email_mobile', 'dtb_customer', 'customer_id = ?', array($_SESSION['customer']['customer_id']));
212
213                    return isset($email_mobile);
214                }
215
216                return true;
217            }
218        }
219
220        return false;
221    }
222
223    // パラメーターの取得
224    public function getValue($keyname)
225    {
226        // ポイントはリアルタイム表示
227        if ($keyname == 'point') {
228            $objQuery =& SC_Query_Ex::getSingletonInstance();
229            $point = $objQuery->get('point', 'dtb_customer', 'customer_id = ?', array($_SESSION['customer']['customer_id']));
230            $_SESSION['customer']['point'] = $point;
231
232            return $point;
233        } else {
234            return isset($_SESSION['customer'][$keyname]) ? $_SESSION['customer'][$keyname] : '';
235        }
236    }
237
238    // パラメーターのセット
239    public function setValue($keyname, $val)
240    {
241        $_SESSION['customer'][$keyname] = $val;
242    }
243
244    // パラメーターがNULLかどうかの判定
245    public function hasValue($keyname)
246    {
247        if (isset($_SESSION['customer'][$keyname])) {
248            return !SC_Utils_Ex::isBlank($_SESSION['customer'][$keyname]);
249        }
250
251        return false;
252    }
253
254    // 誕生日月であるかどうかの判定
255    public function isBirthMonth()
256    {
257        if (isset($_SESSION['customer']['birth'])) {
258            $arrRet = preg_split('|[- :/]|', $_SESSION['customer']['birth']);
259            $birth_month = intval($arrRet[1]);
260            $now_month = intval(date('m'));
261
262            if ($birth_month == $now_month) {
263                return true;
264            }
265        }
266
267        return false;
268    }
269
270    /**
271     * $_SERVER['REMOTE_HOST'] または $_SERVER['REMOTE_ADDR'] を返す.
272     *
273     * $_SERVER['REMOTE_HOST'] が取得できない場合は $_SERVER['REMOTE_ADDR']
274     * を返す.
275     *
276     * @return string $_SERVER['REMOTE_HOST'] 又は $_SERVER['REMOTE_ADDR']の文字列
277     */
278    public function getRemoteHost()
279    {
280        if (!empty($_SERVER['REMOTE_HOST'])) {
281            return $_SERVER['REMOTE_HOST'];
282        } elseif (!empty($_SERVER['REMOTE_ADDR'])) {
283            return $_SERVER['REMOTE_ADDR'];
284        } else {
285            return '';
286        }
287    }
288
289    //受注関連の会員情報を更新
290    public function updateOrderSummary($customer_id)
291    {
292        $objQuery =& SC_Query_Ex::getSingletonInstance();
293        $arrOrderSummary =  $objQuery->getRow('SUM( payment_total) as buy_total, COUNT(order_id) as buy_times,MAX( create_date) as last_buy_date, MIN(create_date) as first_buy_date','dtb_order','customer_id = ? AND del_flg = 0 AND status <> ?',array($customer_id,ORDER_CANCEL));
294        $objQuery->update('dtb_customer',$arrOrderSummary,'customer_id = ?',array($customer_id));
295    }
296
297    /**
298     * ログインを実行する.
299     *
300     * ログインを実行し, 成功した場合はユーザー情報をセッションに格納し,
301     * true を返す.
302     * モバイル端末の場合は, 携帯端末IDを保存する.
303     * ログインに失敗した場合は, false を返す.
304     *
305     * @param  string  $login_email ログインメールアドレス
306     * @param  string  $login_pass  ログインパスワード
307     * @return boolean ログインに成功した場合 true; 失敗した場合 false
308     */
309    public function doLogin($login_email, $login_pass)
310    {
311        switch (SC_Display_Ex::detectDevice()) {
312            case DEVICE_TYPE_MOBILE:
313                if (!$this->getCustomerDataFromMobilePhoneIdPass($login_pass) &&
314                    !$this->getCustomerDataFromEmailPass($login_pass, $login_email, true)
315                ) {
316                    return false;
317                } else {
318                    // Session Fixation対策
319                    SC_Session_Ex::regenerateSID();
320
321                    $this->updateMobilePhoneId();
322
323                    return true;
324                }
325                break;
326
327            case DEVICE_TYPE_SMARTPHONE:
328            case DEVICE_TYPE_PC:
329            default:
330                if (!$this->getCustomerDataFromEmailPass($login_pass, $login_email)) {
331                    return false;
332                } else {
333                    // Session Fixation対策
334                    SC_Session_Ex::regenerateSID();
335
336                    return true;
337                }
338                break;
339        }
340    }
341}
Note: See TracBrowser for help on using the repository browser.