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