예제 #1
0
 public function login_weibo()
 {
     include_once DIR_SYSTEM . 'weibo/config.php';
     include_once DIR_SYSTEM . 'weibo/saetv2.ex.class.php';
     $o = new SaeTOAuthV2(WB_AKEY, WB_SKEY);
     if (isset($_GET['code'])) {
         $keys = array();
         $keys['code'] = $_GET['code'];
         $keys['redirect_uri'] = WB_CALLBACK_URL;
         try {
             $token = $o->getAccessToken('code', $keys);
         } catch (OAuthException $e) {
         }
     }
     if (isset($token) && !empty($token)) {
         $this->session->data['token'] = $token;
         setcookie('weibojs_' . $o->client_id, http_build_query($token));
         $c = new SaeTClientV2(WB_AKEY, WB_SKEY, $_SESSION['token']['access_token']);
         $ms = $c->home_timeline();
         // done
         $uid_get = $c->get_uid();
         $email_get = $c->get_email();
         $uid = $uid_get['uid'];
         if (isset($email_get['error']) || !$email_get['email']) {
             $email = "";
         } else {
             $email = "&email=" . $email_get['email'];
         }
         $user_message = $c->show_user_by_id($uid);
         //根据ID获取用户等基本信息
         $uname = isset($user_message['screen_name']) ? $user_message['screen_name'] : '';
         $oauthuid = $uid;
         $sex = $user_message['gender'];
         $face = $user_message['avatar_large'];
         $row = $this->db->query("SELECT * FROM " . DB_PREFIX . "customer WHERE `from` = 'weibo' AND oauthuid= '" . $uid . "'");
         //判定用户是否存在
         if (count($row->row) == "0") {
             $ip = $this->request->server['REMOTE_ADDR'];
             $info = file_get_contents('http://ip.taobao.com/service/getIpInfo.php?ip=' . $ip);
             $info = json_decode($info, true);
             if ($info['code'] == 0) {
                 $country = $info['data']['country'];
             } else {
                 $country = '';
             }
             $this->db->query("INSERT INTO " . DB_PREFIX . "customer SET  firstname = '" . $this->db->escape($uname) . "', sex = '" . $this->db->escape($sex) . "', salt = '" . $this->db->escape($salt = substr(md5(uniqid(rand(), true)), 0, 9)) . "', password = '******', customer_group_id = 1, ip = '" . $this->db->escape($ip) . "', status = '1', tname= '" . $this->db->escape($uname) . "', approved = 1, `from` = 'weibo', `oauthuid` = '" . $oauthuid . "', `face` = '" . $face . "', regtime = UNIX_TIMESTAMP(NOW()), date_added = NOW(),logintime= UNIX_TIMESTAMP(NOW()),country='" . $this->db->escape($country) . "' ");
             $row2 = $this->db->query("SELECT customer_id FROM " . DB_PREFIX . "customer WHERE `from` = 'weibo' AND oauthuid= '" . $oauthuid . "'");
             //SQL 范围 FROM=weibo  oauthuid
             $this->session->data['customer_id'] = $row2->row['customer_id'];
             $this->jumpurl("/index.php?route=account/reg_mail&code=微博登陆" . $email);
         } else {
             $cid = $row->row['customer_id'];
             $this->session->data['customer_id'] = $cid;
             if ($row->row['email'] == "") {
                 $this->jumpurl("/index.php?route=account/reg_mail&code=微博登陆" . $email);
             } else {
                 unset($this->session->data['guest']);
                 $this->db->query("UPDATE " . DB_PREFIX . "customer SET logintime = UNIX_TIMESTAMP(NOW()) WHERE `from` = 'weibo' AND  oauthuid= '" . $uid . "' AND oauthuid!='' LIMIT 1");
                 $this->load->model('log/customer_login');
                 $this->model_log_customer_login->addInfo($cid, 'weibo');
                 //添加日志
             }
             if (isset($_COOKIE['taobao_id']) && !empty($_COOKIE['taobao_id'])) {
                 //未登录并下单
                 $this->jumpurl('/order-snatch.html');
             } else {
                 $this->jumpurl("/index.php");
             }
         }
     } else {
         print "<script language='javascript'>alert('登录超时,請重试!');</script>";
         $this->jumpurl("index.php");
     }
 }