public function loginwithfacebookAction() { if ($access_token = $this->getRequest()->getParam('token')) { try { // Réinitialise la connexion $this->getSession()->resetInstance(); // Récupération des données du compte Facebook $graph_url = "https://graph.facebook.com/v2.0/me?access_token=" . $access_token; $user = json_decode(file_get_contents($graph_url)); if (!$user instanceof stdClass or !$user->id) { throw new Exception($this->_('An error occurred while connecting to your Facebook account. Please try again later')); } // Récupère le user_id $user_id = $user->id; // Charge le client à partir du user_id $customer = new Customer_Model_Customer(); $customer->findBySocialId($user_id, 'facebook'); // Si le client n'a pas de compte if (!$customer->getId()) { // Charge le client à partir de l'adresse email afin d'associer les 2 comptes ensemble if ($user->email) { $customer->find(array('email' => $user->email)); } // Si l'email n'existe pas en base, on crée le client if (!$customer->getId()) { // Préparation des données du client $customer->setData(array('civility' => $user->gender == 'male' ? 'm' : 'mme', 'firstname' => $user->first_name, 'lastname' => $user->last_name, 'email' => $user->email)); // Ajoute un mot de passe par défaut $customer->setPassword(uniqid()); // Récupèration de l'image de Facebook $social_image = @file_get_contents("http://graph.facebook.com/v2.0/{$user_id}/picture?type=large"); if ($social_image) { $formated_name = Core_Model_Lib_String::format($customer->getName(), true); $image_path = $customer->getBaseImagePath() . '/' . $formated_name; // Créer le dossier du client s'il n'existe pas if (!is_dir($customer->getBaseImagePath())) { mkdir($image_path, 0777); } // Créer l'image sur le serveur $image_name = uniqid() . '.jpg'; $image = fopen($image_path . '/' . $image_name, 'w'); fputs($image, $social_image); fclose($image); // Redimensionne l'image Thumbnailer_CreateThumb::createThumbnail($image_path . '/' . $image_name, $image_path . '/' . $image_name, 150, 150, 'jpg', true); // Affecte l'image au client $customer->setImage('/' . $formated_name . '/' . $image_name); } } } // Affecte les données du réseau social au client $customer->setSocialData('facebook', array('id' => $user_id, 'datas' => $access_token)); // Sauvegarde du nouveau client $customer->save(); // Connexion du client $this->getSession()->setCustomer($customer); $html = array('success' => 1, 'customer_id' => $customer->getId()); } catch (Exception $e) { $html = array('error' => 1, 'message' => $e->getMessage()); } $this->_sendHtml($html); } }
public function loginwithfacebookAction() { if ($access_token = $this->getRequest()->getParam('token')) { try { // Reset session $this->getSession()->resetInstance(); // Fetch data from Facebook $graph_url = "https://graph.facebook.com/me?fields=id,name,email,first_name,last_name&access_token=" . $access_token; $user = json_decode(file_get_contents($graph_url)); if (!$user instanceof stdClass or !$user->id) { throw new Exception($this->_('An error occurred while connecting to your Facebook account. Please try again later')); } // Retrieve the user_id $user_id = $user->id; // Retrieve the current app ID $app_id = $this->getApplication()->getId(); // Load the customer from the user_id $customer = new Customer_Model_Customer(); $customer->findBySocialId($user_id, 'facebook', $app_id); // If the customer doesn't exist if (!$customer->getId()) { // Load the customer based on the email address in order to link the 2 accounts together if ($user->email) { $customer->find(array("email" => $user->email, "app_id" => $app_id)); } // If the email doesn't exist, create the account if (!$customer->getId()) { // Préparation des données du client $customer->setData(array("app_id" => $app_id, "firstname" => $user->first_name, "lastname" => $user->last_name, "email" => $user->email)); // Add a default password $customer->setPassword(uniqid()); // Retrieve its picture from Facebook $social_image = @file_get_contents("http://graph.facebook.com/{$user_id}/picture?type=large"); if ($social_image) { $formated_name = Core_Model_Lib_String::format($customer->getName(), true); $image_path = $customer->getBaseImagePath() . '/' . $formated_name; // Create customer's folder if (!is_dir($customer->getBaseImagePath())) { mkdir($image_path, 0777); } // Store the picture on the server $image_name = uniqid() . '.jpg'; $image = fopen($image_path . '/' . $image_name, 'w'); fputs($image, $social_image); fclose($image); // Resize the image Thumbnailer_CreateThumb::createThumbnail($image_path . '/' . $image_name, $image_path . '/' . $image_name, 150, 150, 'jpg', true); // Set the image to the customer $customer->setImage('/' . $formated_name . '/' . $image_name); } } } // Set the social data to the customer $customer->setSocialData('facebook', array('id' => $user_id, 'datas' => $access_token)); // Save the customer $customer->save(); // Log-in the customer $this->getSession()->setCustomer($customer); $html = array('success' => 1, 'customer_id' => $customer->getId(), 'can_access_locked_features' => $customer->canAccessLockedFeatures()); } catch (Exception $e) { $html = array('error' => 1, 'message' => $e->getMessage()); } $this->_sendHtml($html); } }