source: branches/version-2_12-dev/data/class/SC_Customer.php @ 22062

Revision 22062, 14.1 KB checked in by pineray, 11 years ago (diff)

ログイン処理をまとめる

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