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()
{
$client_id = ''; // Client ID
$client_secret = ''; // Client secret
$redirect_uri = ''; // Redirect URIs
$url = 'https://www.facebook.com/dialog/oauth';
$params = array(
'client_id' => $client_id,
'redirect_uri' => $redirect_uri,
'response_type' => 'code',
'scope' => 'email'
);
$module_url = $_SERVER['HTTP_HOST'];
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");
$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']))
{
//echo $row_customer ['id_customer']."
";
var_dump($userInfo);
AuthenticationFacebook::login_customer($row_customer ['id_customer']);
} else {
echo "new user added";
}
}
}
}
}
public static function login_customer ($id_customer)
{
// Make sure that that the customers exists.
$sql = "SELECT * FROM `" . _DB_PREFIX_ . "customer` WHERE `id_customer` = '".$id_customer."'";
$result = Db::getInstance ()->GetRow ($sql);
$customer = new Customer($result ['id_customer']);
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();
}
}
}
?>