/**
  * run() method execute hook
  *
  * @param array $aParams
  * @return array
  */
 public function run(array $aParams)
 {
     $aAssign = array();
     try {
         // get connector
         $oConnector = BT_BaseConnector::get($this->_sConnectorType, $aParams);
         // first connection
         if (empty($aParams['activecallback'])) {
             $mContent = $oConnector->connect();
         } else {
             // exec callback for some connectors which need to use redirection and callback
             $mContent = $oConnector->callback($aParams);
         }
         $aAssign['content'] = $mContent;
     } catch (BT_ConnectorException $e) {
         $aAssign['aErrors'][] = array('msg' => $e->getMessage(), 'code' => $e->getCode());
         $aAssign['sErrorInclude'] = BT_FPCModuleTools::getTemplatePath(_FPC_PATH_TPL_NAME . _FPC_TPL_ERROR);
     }
     return $aAssign;
 }
 /**
  * get() method instantiate matched connector object
  *
  * @param string $sConnectorType
  * @param array $aParams
  * @return obj connector abstract type
  */
 public static function get($sConnectorType, array $aParams = null)
 {
     // if valid connector
     if (in_array($sConnectorType, array_keys($GLOBALS[_FPC_MODULE_NAME . '_CONNECTORS']))) {
         // set module URI
         if (!empty($aParams['sURI'])) {
             self::$sModuleURI = $aParams['sURI'];
         }
         // include
         require_once $sConnectorType . '-connect_class.php';
         require_once _FPC_PATH_LIB_COMMON . 'session.class.php';
         // get session object
         self::$oSession = BT_FpcSession::create(array('sPrefix' => _FPC_MODULE_NAME . '_'));
         // check if back URI is set
         if (!empty($aParams['back'])) {
             // delete first
             self::$oSession->delete('back');
             self::$oSession->set('back', $aParams['back']);
         }
         // set class name
         $sClassName = 'BT_' . ucfirst($sConnectorType) . 'Connect';
         // get connector name
         self::$sName = $sConnectorType;
         return new $sClassName($aParams);
     } else {
         throw new BT_ConnectorException(FacebookPsConnect::$oModule->l('Internal server error => invalid connector', 'base-connector_class'), 520);
     }
 }
 /**
  * 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);
         }
     }
 }
 /**
  * _displayAccount() method displays fancybox if customer do not use a social connector to link his PS account
  *
  * @category hook collection
  * @uses
  *
  * @param array $aParams
  * @return array
  */
 private function _displayAccount()
 {
     $aAssign = array('iCustomerId' => $this->iCustomerLogged, 'bUseJqueryUI' => true);
     $aAssign['bDisplay'] = false;
     // if one of connectors is active at least
     if (self::$bConnectorsActive) {
         require_once _FPC_PATH_LIB . 'module-dao_class.php';
         // include abstract connector
         require_once _FPC_PATH_LIB_CONNECTOR . 'base-connector_class.php';
         if (FacebookPsConnect::$aConfiguration[_FPC_MODULE_NAME . '_DISPLAY_FB_POPIN'] && !empty($GLOBALS[_FPC_MODULE_NAME . '_CONNECTORS']['facebook']['data']['activeConnector'])) {
             // set
             $bSocialCustomerExist = false;
             // loop on each connector to check if social account already exists - if not, display FB popin account association
             foreach ($GLOBALS[_FPC_MODULE_NAME . '_CONNECTORS'] as $sName => $aConnector) {
                 if (!empty($GLOBALS[_FPC_MODULE_NAME . '_CONNECTORS'][$sName]['data'])) {
                     // get connector options
                     $aParams = $GLOBALS[_FPC_MODULE_NAME . '_CONNECTORS'][$sName]['data'];
                     // get connector
                     $oConnector = BT_BaseConnector::get($sName, $aParams);
                     // check if customer is already logged from FB connector
                     if ($oConnector->existSocialAccount($aAssign['iCustomerId'], 'ps')) {
                         $bSocialCustomerExist = true;
                     }
                     unset($oConnector);
                 }
             }
             if (!BT_FPCModuleDao::existCustomerAssociationStatus(FacebookPsConnect::$iShopId, $this->iCustomerLogged) && empty($bSocialCustomerExist)) {
                 $aAssign['bDisplay'] = true;
                 $aAssign['bSocialCustomerExist'] = true;
                 $aAssign['sConnectorButtonFacebook'] = BT_FPCModuleTools::getTemplatePath(_FPC_PATH_TPL_NAME . _FPC_TPL_HOOK_PATH . _FPC_TPL_BUTTON_FB);
                 $aAssign['sModuleURI'] = $this->sModuleURI;
                 $aAssign['bFriendlyPermission'] = $GLOBALS[_FPC_MODULE_NAME . '_CONNECTORS']['facebook']['data']['permissions'];
                 $aAssign['sBackUri'] = self::$sCurrentURI;
             }
         }
         // get connector options
         $aParams = $GLOBALS[_FPC_MODULE_NAME . '_CONNECTORS']['twitter']['data'];
         // test if twitter is already configured
         if (!empty($aParams)) {
             // get connector
             $oConnector = BT_BaseConnector::get('twitter', $aParams);
             // check if customer is already logged from FB connector
             if ($oConnector->existSocialAccount($aAssign['iCustomerId'], 'ps') && strstr(FacebookPsConnect::$oCookie->email, 'twitter.com')) {
                 $aAssign['iCustomerId'] = md5(_FPC_MODULE_NAME . 'twitter' . $aAssign['iCustomerId']);
                 $aAssign['sConnector'] = 'twitter';
                 $aAssign['bTwitterCustomerExist'] = true;
                 $aAssign['bDisplay'] = true;
             }
             unset($oConnector);
         }
         // use case - data sent for collecting
         $sRequestData = Tools::getValue('data');
         if (!empty($sRequestData)) {
             $aRequestData = unserialize(gzuncompress(urldecode(base64_decode($sRequestData))));
             if (!empty($aRequestData)) {
                 if (empty($aRequestData['ci'])) {
                     $aRequestData['ci'] = md5('collect' . FacebookPsConnect::$oCookie->id_customer);
                     if (!empty($aRequestData['cn']) && !empty($aRequestData['ca']) && !empty($aRequestData['ct']) && !empty($aRequestData['oi'])) {
                         // execute social collect method
                         $sReturn = FacebookPsConnect::$oModule->HookSocialCollector(base64_decode($aRequestData['cn']), $aRequestData);
                         if (!empty($sReturn)) {
                             // get collect data array
                             $oResponse = BT_FPCModuleTools::jsonDecode($sReturn);
                             if (!empty($oResponse->status)) {
                                 $aAssign['bCustomerCollect'] = true;
                             }
                         }
                     }
                 }
             }
         }
     }
     $aAssign['sModuleURI'] = _FPC_MODULE_URL . 'ws-' . _FPC_MODULE_SET_NAME . '.php';
     return array('tpl' => _FPC_TPL_HOOK_PATH . _FPC_TPL_CONNECTOR_ACCOUNT, 'assign' => $aAssign);
 }
 /**
  * connect() method check if token is valid or not, and either redirect on Paypal 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['access_token'])) {
         $this->redirect();
     } else {
         // get profile data
         $oPaypalAccount = $this->getProfile($aParams);
         // set create status
         $bCreateStatus = true;
         $bCreatePs = false;
         $bCreateSocial = false;
         // set ID from email because there is no ID returned by Paypal
         $this->oUser->id = md5($oPaypalAccount->email);
         $this->oUser->email = $oPaypalAccount->email;
         $this->oUser->customerId = 0;
         // set name
         $this->oUser->first_name = $oPaypalAccount->given_name;
         $this->oUser->last_name = $oPaypalAccount->family_name;
         // set birthday
         if (!empty($oPaypalAccount->birthday)) {
             // format date for PS customer table
             $this->oUser->birthday = $oPaypalAccount->birthday;
         }
         // set customer address
         if (is_object($oPaypalAccount->address) && !empty($oPaypalAccount->address)) {
             // set address data
             $this->oUser->address->id_country = parent::getCountryID($oPaypalAccount->address->country);
             $this->oUser->address->id_state = !empty($oPaypalAccount->address->region) ? parent::getStateId($this->oUser->address->id_country, $oPaypalAccount->address->region) : parent::$aRequiredAddressFields['id_state'];
             $this->oUser->address->id_manufacturer = parent::$aRequiredAddressFields['id_manufacturer'];
             $this->oUser->address->id_supplier = parent::$aRequiredAddressFields['id_supplier'];
             $this->oUser->address->id_warehouse = parent::$aRequiredAddressFields['id_warehouse'];
             $this->oUser->address->alias = FacebookPsConnect::$oModule->l('My address', 'paypal-connect_class');
             $this->oUser->address->company = '';
             $this->oUser->address->lastname = $this->oUser->last_name;
             $this->oUser->address->firstname = $this->oUser->first_name;
             // get explode street address
             $aStreet = parent::concatAddress($oPaypalAccount->address->street_address);
             $this->oUser->address->address1 = $aStreet['line1'];
             $this->oUser->address->address2 = $aStreet['line2'];
             $this->oUser->address->postcode = $oPaypalAccount->address->postal_code;
             $this->oUser->address->city = $oPaypalAccount->address->locality;
             $this->oUser->address->other = parent::$aRequiredAddressFields['other'];
             $this->oUser->address->phone = !empty($oPaypalAccount->phone_number) ? $oPaypalAccount->phone_number : parent::$aRequiredAddressFields['phone'];
             $this->oUser->address->phone_mobile = !empty($oPaypalAccount->phone_number) ? $oPaypalAccount->phone_number : parent::$aRequiredAddressFields['phone_mobile'];
             $this->oUser->address->vat_number = parent::$aRequiredAddressFields['vat_number'];
             $this->oUser->address->dni = parent::$aRequiredAddressFields['dni'];
             $this->oUser->address->date_add = date('Y-m-d H:i:s', time());
             $this->oUser->address->date_upd = date('Y-m-d H:i:s', time());
             $this->oUser->address->active = parent::$aRequiredAddressFields['active'];
             $this->oUser->address->deleted = parent::$aRequiredAddressFields['deleted'];
             unset($sStreetLine1);
             unset($sStreetLine2);
         }
         // 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 (!$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();
         }
     }
 }
 /**
  * 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);
         }
     }
 }
 /**
  * connect() method check if code is valid or not, and either redirect on Google interface or log the customer by creating his account if necessary
  *
  * @param array $aParams
  * @return string
  */
 public function connect(array $aParams = null)
 {
     // Instantiate Google objects
     $this->getGoogleObj();
     if (empty($aParams['code'])) {
         // Redirect to Google Interface
         $this->redirect();
     } else {
         $this->oGoogleClient->authenticate($aParams['code']);
         self::$oSession->set('code', $this->oGoogleClient->getAccessToken());
         $this->oGoogleClient->setAccessToken(self::$oSession->get('code'));
         // get response
         $response = $this->oGoogleClient->getAccessToken(self::$oSession->get('code'));
         // use case - valid response
         if ($response) {
             // set create status
             $bCreateStatus = true;
             $bCreatePs = false;
             $bCreateSocial = false;
             // get user info
             $aGoogleAccount = $this->oGoogleOAuth->userinfo->get();
             // set data
             $this->oUser->id = $aGoogleAccount['id'];
             $this->oUser->email = $aGoogleAccount['email'];
             $this->oUser->customerId = 0;
             // get last name
             $aName = explode(' ', $aGoogleAccount['name']);
             // set name
             $this->oUser->first_name = $aName[0];
             $this->oUser->last_name = $aName[1];
             // set birthday
             if (!empty($aGoogleAccount['birthday'])) {
                 // format date for PS customer table
                 $this->oUser->birthday = $aGoogleAccount['birthday'];
             }
             // 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 (!$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_GoogleException(FacebookPsConnect::$oModule->l('Internal server error. Account creation processing is unavailable', 'google-connector_class'), 542);
             }
         } else {
             throw new BT_GoogleException(FacebookPsConnect::$oModule->l('Unable to get access token', 'google-connect_class'), 543);
         }
     }
 }