/**
  * OAuth callback
  */
 public function callback($datasource = null)
 {
     $this->Twitter->setTwitterSource($datasource);
     // 正当な返り値かチェック
     if (!$this->Twitter->isRequested()) {
         $this->Twitter->deleteAuthorizeCookie();
         $this->flash(__d('twitter_kit', 'Authorization failure.'), '/', 5);
         return;
     }
     // $tokenを取得
     $token = $this->Twitter->getAccessToken();
     if (is_string($token)) {
         $this->flash(__d('twitter_kit', 'Authorization Error: %s', $token), '/', 5);
         return;
     }
     if (class_exists('TwitterUser') || (true || App::uses('TwitterUser', 'Model')) && class_exists('TwitterUser')) {
         /* @var $model TwitterUser */
         $model = ClassRegistry::init('TwitterUser');
     } else {
         /* @var $model TwitterKitUser */
         $model = ClassRegistry::init('TwitterKit.TwitterKitUser');
     }
     // 保存データの作成
     $data = $model->createSaveDataByToken($token);
     if (!$model->save($data)) {
         $this->flash(__d('twitter_kit', 'The user could not be saved'), array('plugin' => 'twitter_kit', 'controller' => 'users', 'action' => 'login'), 5);
         return;
     }
     $this->Auth->login($data);
     // Redirect
     if (ini_get('session.referer_check') && env('HTTP_REFERER')) {
         $this->flash(__d('twiter_kit', 'Redirect to %s', Router::url($this->Auth->redirect(), true) . ini_get('session.referer_check')), $this->Auth->redirect(), 0);
         return;
     }
     $this->redirect($this->Auth->redirect());
 }
 /**
  * OAuth callback
  */
 public function callback($datasource = null)
 {
     $this->Twitter->setTwitterSource($datasource);
     // 正当な返り値かチェック
     if (empty($this->params['url']['oauth_token']) || empty($this->params['url']['oauth_verifier'])) {
         $this->Twitter->deleteAuthorizeCookie();
         $this->flash(__d('twitter_kit', 'Authorization failure.', true), '/', 5);
         return;
     }
     // $tokenを取得
     $token = $this->Twitter->getAccessToken();
     if (is_string($token)) {
         $this->flash(__d('twitter_kit', 'Authorization Error: ', true) . $token, '/', 5);
         return;
     }
     if (ClassRegistry::isKeySet('TwitterUser')) {
         /* @var $model TwitterUser */
         $model = ClassRegistry::init('TwitterUser');
     } else {
         /* @var $model TwitterKitUser */
         $model = ClassRegistry::init('TwitterKit.TwitterKitUser');
     }
     // 保存データの作成
     $data = $model->createSaveDataByToken($token);
     if (!$model->save($data)) {
         $this->flash(__d('twitter_kit', 'The user could not be saved', true), array('plugin' => 'twitter_kit', 'controller' => 'users', 'action' => 'login'), 5);
         return;
     }
     $this->Auth->login($data);
     // Redirect
     if (ini_get('session.referer_check') && env('HTTP_REFERER')) {
         $this->flash(sprintf(__d('twiter_kit', 'Redirect to %s', true), Router::url($this->Auth->redirect(), true) . ini_get('session.referer_check')), $this->Auth->redirect(), 0);
         return;
     }
     $this->redirect($this->Auth->redirect());
 }
Example #3
0
 /**
  * OAuth callback
  */
 public function callback($datasource = null)
 {
     $this->Twitter->setTwitterSource($datasource);
     // 正当な返り値かチェック
     if (!$this->Twitter->isRequested()) {
         $this->Twitter->deleteAuthorizeCookie();
         $this->flash(__d('twitter_kit', 'Authorization failure.'), '/', 5);
         return;
     }
     // $tokenを取得
     $token = $this->Twitter->getAccessToken();
     if (is_string($token)) {
         $this->flash(__d('twitter_kit', 'Authorization Error: %s', $token), '/', 5);
         return;
     }
     if (class_exists('TwitterUser') || (true || App::uses('TwitterUser', 'Model')) && class_exists('TwitterUser')) {
         /* @var $model TwitterUser */
         $model = ClassRegistry::init('TwitterUser');
     } else {
         /* @var $model TwitterKitUser */
         //$model = ClassRegistry::init('TwitterKit.TwitterKitUser');
         $model = ClassRegistry::init('User');
     }
     $create_flg = false;
     //既に登録済みか?
     $tmp = $model->findTwitterUser($token['user_id']);
     if (!empty($tmp)) {
         $data = $model->updateSaveDataByToken($tmp, $token);
     } else {
         // 保存データの作成
         $data = $model->createSaveDataByToken($token);
         $model->create();
         $create_flg = true;
     }
     if (!$model->save($data, false)) {
         $this->flash(__d('twitter_kit', 'The user could not be saved'), array('plugin' => 'twitter_kit', 'controller' => 'users', 'action' => 'login'), 5);
         return;
     }
     if ($create_flg == true) {
         $data['User']['id'] = $model->getLastInsertId();
         //新規会員登録の際
         $ds = $this->Twitter->getTwitterSource();
         $ds->setToken($data['User']);
         //プロフィール画像の取得
         $params = array();
         $params['id'] = $data['User']['twitter_user_id'];
         $result = $ds->users_show($data['User']['twitter_user_id']);
         if (!empty($result['profile_image_url'])) {
             $image_url = str_replace("_normal", "", $result['profile_image_url']);
             $image_data = file_get_contents($image_url);
             //MIMEタイプの取得
             $finfo = finfo_open(FILEINFO_MIME_TYPE);
             $mime_type = finfo_buffer($finfo, $image_data);
             finfo_close($finfo);
             //出力
             switch ($mime_type) {
                 case 'image/gif':
                     $ext = "gif";
                     break;
                 case 'image/png':
                     $ext = "png";
                     break;
                 case 'image/jpg':
                 case 'image/jpeg':
                     $ext = "jpg";
                     break;
             }
             $file_name = md5(date("YmdHis") . $data['User']['id']) . "." . $ext;
             file_put_contents(IMAGES . 'profileImg/' . $file_name, $image_data);
             //profile_imgを更新
             $model->id = $data['User']['id'];
             $model->saveField('profile_img', $file_name);
             $data['User']['profile_img'] = $file_name;
         }
         //メール送信
         $email = new CakeEmail('smtp');
         $email->to('*****@*****.**');
         $email->subject('twitterから会員登録がありました');
         $email->emailFormat('text');
         $email->template('regist');
         $postData['User']['nickname'] = $data['User']['nickname'];
         $email->viewVars(compact('postData'));
         $email->send();
     }
     $this->Auth->login($data['User']);
     // Redirect
     if (ini_get('session.referer_check') && env('HTTP_REFERER')) {
         $this->flash(__d('twiter_kit', 'Redirect to %s', Router::url($this->Auth->redirect(), true) . ini_get('session.referer_check')), $this->Auth->redirect(), 0);
         return;
     }
     //ログイン履歴を保存
     $this->LoginHistory->saveLoginHistory($data['User']['id'], $this->request);
     //$this->redirect($this->Auth->redirect());
     if ($create_flg == true) {
         $this->redirect("/users/edit");
     } else {
         $this->redirect("/");
     }
 }