public function savepostAction() { if ($datas = $this->getRequest()->getPost()) { if (!($customer = $this->getSession()->getCustomer())) { $customer = new Customer_Model_Customer(); } $isNew = !$customer->getId(); $isMobile = APPLICATION_TYPE == 'mobile'; try { if (!Zend_Validate::is($datas['email'], 'EmailAddress')) { throw new Exception($this->_('Please enter a valid email address')); } $dummy = new Customer_Model_Customer(); $dummy->find($datas['email'], 'email'); if ($isNew and $dummy->getId()) { throw new Exception($this->_('We are sorry but this address is already used.')); } if (!empty($datas['social_datas'])) { $social_ids = array(); foreach ($datas['social_datas'] as $type => $data) { if ($customer->findBySocialId($data['id'], $type)->getId()) { throw new Exception($this->_('We are sorry but the %s account is already linked to one of our customers', ucfirst($type))); } $social_ids[$type] = array('id' => $data['id']); } } $password = $customer->getPassword(); if (empty($datas['show_in_social_gaming'])) { $datas['show_in_social_gaming'] = 0; } $customer->setData($datas); $customer->setData('password', $password); if (isset($datas['id']) and $datas['id'] != $this->getSession()->getCustomer()->getId()) { throw new Exception($this->_('An error occurred while saving. Please try again later.')); } $formated_name = Core_Model_Lib_String::format($customer->getName(), true); $base_logo_path = $customer->getBaseImagePath() . '/' . $formated_name; if ($customer->getSocialPicture()) { $social_image = @file_get_contents($customer->getSocialPicture()); if ($social_image) { if (!is_dir($customer->getBaseImagePath())) { mkdir($customer->getBaseImagePath(), 0777); } $image_name = uniqid() . '.jpg'; $image = fopen($customer->getBaseImagePath() . '/' . $image_name, 'w'); fputs($image, $social_image); fclose($image); $customer->setImage('/' . $formated_name . '/' . $image_name); } else { $this->getSession()->addError($this->_('An error occurred while saving your picture. Please try againg later.')); } } if (empty($datas['password']) and $isNew) { throw new Exception($this->_('Please enter a password')); } if (!$isMobile and $datas['password'] != $datas['confirm_password']) { throw new Exception($this->_('Your password does not match the entered password.')); } if ($isNew and !$isMobile and $datas['email'] != $datas['confirm_email']) { throw new Exception($this->_("The old email address does not match the entered email address.")); } if (!$isNew and !empty($datas['old_password']) and !$customer->isSamePassword($datas['old_password'])) { throw new Exception($this->_("The old password does not match the entered password.")); } if (!empty($datas['password'])) { $customer->setPassword($datas['password']); } if (!empty($social_ids)) { $customer->setSocialDatas($social_ids); } $customer->save(); $this->getSession()->setCustomer($customer); if ($isNew) { $this->_sendNewAccountEmail($customer, $datas['password']); } if (!$isMobile) { $this->getSession()->addSuccess($this->_('Your account has been successfully saved')); // Retour des données (redirection vers la page en cours) $referer = !empty($datas['referer']) ? $datas['referer'] : $this->getRequest()->getHeader('referer'); $this->_redirect($referer); return $this; } foreach ($this->getRequest()->getParam('add_to_session', array()) as $key => $value) { $this->getSession()->{$key} = $value; } $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); } }