/**
  * connect() method check if token is valid or not, and either redirect on FB interface or log the customer by creating his account if necessary
  *
  * @param array $aParams
  * @return string
  */
 public function connect(array $aParams = null)
 {
     // use case - not FB code returned - redirect
     if (empty($aParams['code']) || empty($aParams['access_token'])) {
         // redirect on FB interface
         $this->redirect();
     } else {
         // test token
         // $oFBUser = BT_FPCModuleTools::jsonDecode(BT_FPCModuleTools::fileGetContent('https://graph.facebook.com/me?access_token=' . $aParams['access_token']));
         $oFBUser = BT_FPCModuleTools::jsonDecode(BT_FPCModuleTools::fileGetContent('https://graph.facebook.com/me?fields=id,name,last_name,first_name,email&access_token=' . $aParams['access_token']));
         // only if social user id exist
         if (!empty($oFBUser->id)) {
             // set create status
             $bCreateStatus = true;
             $bCreatePs = false;
             $bCreateSocial = false;
             // set FB data
             $this->oUser->id = $oFBUser->id;
             $this->oUser->customerId = 0;
             $this->oUser->first_name = $oFBUser->first_name;
             $this->oUser->last_name = $oFBUser->last_name;
             $this->oUser->email = $oFBUser->email;
             // set birthday
             if (!empty($oFBUser->birthday)) {
                 $aBirthday = explode('/', $oFBUser->birthday);
                 // format date for PS customer table
                 $this->oUser->birthday = $aBirthday[2] . '-' . $aBirthday[0] . '-' . $aBirthday[1];
             }
             // set gender
             if (!empty($oFBUser->gender)) {
                 // get gender ID from PS
                 $this->oUser->gender = parent::getGender($oFBUser->gender);
             }
             // use case - customer is already logged and ask him account association
             if (($iCustomerId = self::$oSession->get('iCustomerId')) !== null) {
                 // get customer id
                 $this->oUser->customerId = $iCustomerId;
                 // delete customer ID session
                 self::$oSession->delete('iCustomerId');
                 // use case - social account not exists
                 if (!parent::existSocialAccount($this->oUser->id)) {
                     $bCreateSocial = true;
                 } else {
                     $aSocialData = parent::getSocialData($oFBUser->id);
                     // get customer data of old account
                     $aCustomerData = parent::getCustomerData($aSocialData[0]['CNT_CUST_ID']);
                     throw new BT_FacebookException(FacebookPsConnect::$oModule->l('This Facebook account has already been linked to a customer account on our shop. The e-mail address of this account on our shop is', 'facebook-connector_class') . ' : "' . $aCustomerData[0]['email'] . '". ' . FacebookPsConnect::$oModule->l('Please contact the merchant to warn him', 'facebook-connector_class') . '.', 520);
                 }
             } else {
                 // test if user already exist in social table
                 $bCreateSocial = !parent::existSocialAccount($this->oUser->id);
                 // test if user already exist in PS table
                 $bCreatePs = !parent::existPsAccount($this->oUser->email);
                 // use case - social account exist
                 if (empty($bCreateSocial)) {
                     // use case - create new PS account and have to delete old social account
                     if (!empty($bCreatePs)) {
                         $iCustomerId = parent::getCustomerId($this->oUser->id);
                         // use case - PS customer account exists too
                         if (parent::existPsAccount($iCustomerId, 'id')) {
                             $bCreateSocial = false;
                             $bCreatePs = false;
                         } else {
                             parent::deleteSocialAccount($this->oUser->id);
                             $bCreateSocial = true;
                         }
                     }
                 } elseif (!$bCreatePs) {
                     $this->oUser->customerId = parent::getCustomerIdByMail($this->oUser->email);
                 }
             }
             // use case - if one of 2 accounts has to be created at least
             if (!empty($bCreatePs) || !empty($bCreateSocial)) {
                 // create customer in 1 or 2 tables
                 $bCreateStatus = parent::createCustomer($bCreatePs, $bCreateSocial);
             }
             // use case  - create status valid
             if ($bCreateStatus) {
                 // load customer
                 parent::loadCustomer($this->oUser->id);
                 // get data session if exists
                 $sData = self::$oSession->get('data');
                 // delete session
                 self::$oSession->delete('data');
                 return $this->login($sData);
             } else {
                 throw new BT_FacebookException(FacebookPsConnect::$oModule->l('Internal server error. Account creation processing is unavailable', 'facebook-connector_class'), 521);
             }
         } else {
             throw new BT_FacebookException(FacebookPsConnect::$oModule->l('The token is not valid. You may be a victim of cross-site request forgery or the connect method to the Facebook URL with HTTPS is not allowed. Please contact the merchant to warn him', 'facebook-connector_class'), 522);
         }
     }
 }
 /**
  * connect() method  check if token is valid or not, and either redirect on Twitter interface or log the customer by creating his account if necessary
  *
  * @param array $aParams
  * @return string
  */
 public function connect(array $aParams = null)
 {
     // detect if callback requested
     if (empty($aParams['oauth_token']) && empty($aParams['oauth_token_secret'])) {
         // redirect on Twitter interface
         $this->redirect();
     } else {
         // set new twitter oauth with token
         $this->getTwitterOAuth($this->consumer_key, $this->consumer_secret, $aParams['oauth_token'], $aParams['oauth_token_secret']);
         // verify credentials
         $oTwitterAccount = $this->oTwitterOAuth->get('account/verify_credentials');
         if (!empty($oTwitterAccount->errors)) {
             throw new BT_TwitterException(FacebookPsConnect::$oModule->l('Authentication failed', 'twitter-connect_class'), 532);
         }
         // set create status
         $bCreateStatus = true;
         $bCreatePs = false;
         $bCreateSocial = false;
         // set data
         $this->oUser->id = $oTwitterAccount->id;
         $this->oUser->email = 'twitter' . $oTwitterAccount->id . '@twitter.com';
         $this->oUser->customerId = 0;
         // get last name
         $aName = explode(' ', $oTwitterAccount->name);
         // manage last name with figure
         $aName = preg_replace('`(.*?)(\\d+)(.*?)`', '$1', $aName);
         if (empty($aName[0])) {
             $aName[0] = "generic name";
         }
         // set name
         if (count($aName) != 1) {
             $this->oUser->first_name = $aName[0];
             $this->oUser->last_name = $aName[1];
         } else {
             $this->oUser->first_name = $aName[0];
             $this->oUser->last_name = $aName[0];
         }
         // test if user already exist in social table
         $bCreateSocial = !parent::existSocialAccount($this->oUser->id);
         // use case - social account exist
         if (empty($bCreateSocial)) {
             // get PS customer ID
             $iParentId = parent::getCustomerId($this->oUser->id);
             if (!empty($iParentId)) {
                 // get PS customer data
                 $aCustomerData = parent::getCustomerData($iParentId);
                 // if exists set existing customer e-mail address
                 if (!empty($aCustomerData[0]['email'])) {
                     $this->oUser->email = $aCustomerData[0]['email'];
                 }
             }
         }
         // test if user already exist in PS table
         $bCreatePs = !parent::existPsAccount($this->oUser->email);
         // use case - social account exist
         if (empty($bCreateSocial)) {
             // use case - create new PS account and have to delete old social account
             if (!empty($bCreatePs)) {
                 parent::deleteSocialAccount($this->oUser->id);
                 $bCreateSocial = true;
             }
         } elseif (empty($bCreatePs)) {
             $this->oUser->customerId = parent::getCustomerIdByMail($this->oUser->email);
         }
         // use case - if one of 2 accounts has to be created at least
         if (!empty($bCreatePs) || !empty($bCreateSocial)) {
             // create customer in 1 or 2 tables
             $bCreateStatus = parent::createCustomer($bCreatePs, $bCreateSocial);
         }
         // use case  - create status valid
         if ($bCreateStatus) {
             // load customer
             parent::loadCustomer($this->oUser->id);
             return $this->login();
         } else {
             throw new BT_TwitterException(FacebookPsConnect::$oModule->l('Internal server error. Account creation processing is unavailable', 'twitter-connector_class'), 533);
         }
     }
 }