name = 'AuthenticationFacebook'; $this->tab = 'other'; $this->version = '1.0.0'; $this->author = 'Roman Stadnyk'; $this->need_instance = 0; $this->ps_versions_compliancy = array('min' => '1.6', 'max' => _PS_VERSION_); $this->bootstrap = true; parent::__construct(); $this->displayName = $this->l('AuthenticationFacebook'); $this->description = $this->l('Fast sign in with Facebook.'); } public function install() { return (parent::install() && $this->registerHook('rightColumn')); } public function hookRightColumn($params) { $this->context->smarty->assign('textTest', "true" ); //AuthenticationFacebook::authentication(); return $this->display(__FILE__, 'authenticationFacebook.tpl'); } public static function authentication() { $site_url = $_SERVER['HTTP_HOST']; $client_id = '********'; // Client ID $client_secret = '******'; // Client secret $redirect_uri = 'http://'.$site_url.'/....../'; // Redirect URIs $url = 'https://www.facebook.com/dialog/oauth'; $params = array( 'client_id' => $client_id, 'redirect_uri' => $redirect_uri, 'response_type' => 'code', 'scope' => 'email' ); echo $link = ''; if (isset($_GET['code'])) { $result = false; $params = array( 'client_id' => $client_id, 'redirect_uri' => $redirect_uri, 'client_secret' => $client_secret, 'code' => $_GET['code'] ); $url = 'https://graph.facebook.com/v2.9/oauth/access_token'; $tokenInfo = null; $tokenInfo = file_get_contents($url . '?' . http_build_query($params)); $var = json_decode($tokenInfo, true); if (count($tokenInfo) > 0 && isset($var['access_token'])) { $params = array('access_token' => $var['access_token'], "fields"=>"email,first_name,last_name,locale,gender,birthday,location"); $userInfo = json_decode(file_get_contents('https://graph.facebook.com/me' . '?' . urldecode(http_build_query($params))), true); if (isset($userInfo['id'])) { $sql = "SELECT * FROM `" . _DB_PREFIX_ . "user_facebook` WHERE `id_facebook` = '" . $userInfo['id'] . "'"; $row_customer = Db::getInstance ()->GetRow ($sql); // The user account has been found! if (!empty ($row_customer ['id_customer'])) { //var_dump($userInfo); $email = Db::getInstance ()->getValue('SELECT `email` FROM `' . _DB_PREFIX_ . 'customer` WHERE `id_customer` = "'.$row_customer ['id_customer'].'"'); //перевіряємо чи не змінив свою електронну скриньку на фейсбуці наш відвідувач, якщо змінив то ми міняємо її і в себе if($email == $userInfo['email']) { AuthenticationFacebook::login_customer($row_customer ['id_customer']); } else { Db::getInstance()->Execute("UPDATE `"._DB_PREFIX_."customer` SET `email`='".$userInfo['email']."'"); AuthenticationFacebook::login_customer($row_customer ['id_customer']); } } else { $pass = md5(rand(10000, 100000)); $zero = 0; $one = 1; $two = 2; $three = 3; if($userInfo['locale'] == 'ru_RU') $lang = 1; if($userInfo['locale'] == 'uk_UA') $lang = 2; if($userInfo['locale'] != 'uk_UA' and $userInfo['locale'] != 'ru_RU') $lang = 3; if($userInfo['gender'] == 'male') $gender = 0; if($userInfo['gender'] == 'female') $gender = 1; if($userInfo['gender'] != 'male' and $userInfo['gender'] != 'female') $gender = 0; $dateDefault = date("Y-m-d H:i:s"); $birthday = date("Y-m-d", strtotime($userInfo['birthday'])); $ip_registration = $_SERVER["REMOTE_ADDR"]; $secure_key = md5(rand(10000, 100000)); Db::getInstance()->autoExecute(_DB_PREFIX_.'customer', array( 'id_shop_group' => (int)$zero, 'id_shop' => (int)$one, 'id_gender' => (int)$gender, 'id_default_group' => (int)$three, 'id_lang' => (int)$lang, 'id_risk' => (int)$zero, 'firstname' => pSQL($userInfo['first_name']), 'lastname' => pSQL($userInfo['last_name']), 'email' => pSQL($userInfo['email']), 'passwd' => pSQL($pass), 'last_passwd_gen' => pSQL($dateDefault), 'birthday' => pSQL($birthday), 'newsletter' => (int)($one), 'ip_registration_newsletter' => pSQL($ip_registration), 'newsletter_date_add' => pSQL($dateDefault), 'optin' => (int)($one), 'outstanding_allow_amount' => (int)($zero), 'show_public_prices' => (int)($zero), 'max_payment_days' => (int)($zero), 'secure_key' => pSQL($secure_key), 'active' => (int)$one, 'is_guest' => (int)$zero, 'deleted' => (int)$zero, 'date_add' => pSQL($dateDefault), 'date_upd' => pSQL($dateDefault), 'id_organization' => (int)($zero), 'sms_requests' => (int)($zero), 'email_requests' => (int)($zero) ), 'INSERT'); //echo Db::getInstance()->Insert_ID();// - id останього запису в базу // Add new user in table user_facebook. Db::getInstance()->autoExecute(_DB_PREFIX_.'user_facebook', array( 'id_customer' => (int)Db::getInstance()->Insert_ID(), 'id_facebook' => $userInfo['id'] ), 'INSERT'); $id = Db::getInstance ()->getValue('SELECT `id_customer` FROM `' . _DB_PREFIX_ . 'user_facebook` WHERE `id_facebook` = "'.$userInfo['id'].'"'); AuthenticationFacebook::login_customer($id); } } } } } public static function login_customer($id_customer) { $id = Db::getInstance ()->getValue('SELECT `id_customer` FROM `' . _DB_PREFIX_ . 'customer` WHERE `id_customer` = "'.(int)$id_customer.'"'); $customer = new Customer($id); //var_dump($customer); if (Validate::isLoadedObject($customer)) { $context = Context::getContext(); $context->cookie->id_customer = (int)$customer->id; $context->cookie->customer_firstname = $customer->firstname; $context->cookie->customer_lastname = $customer->lastname; $context->cookie->is_guest = $customer->isGuest(); $context->cookie->passwd = $customer->passwd; $context->cookie->email = $customer->email; $context->cookie->logged = true; $context->cookie->write(); } header("Location: http://......."); /* Redirect browser */ } } ?>