public function saveAction()
 {
     if ($data = $this->getRequest()->getPost()) {
         try {
             $customer = new Customer_Model_Customer();
             if (!empty($data['customer_id'])) {
                 $customer->find($data['customer_id']);
                 if (!$customer->getId() || $customer->getAppId() != $this->getApplication()->getId()) {
                     throw new Exception($this->_("An error occurred while saving. Please try again later."));
                 }
             }
             $isNew = !$customer->getId();
             $errors = array();
             if (empty($data['civility'])) {
                 $errors[] = $this->_("the gender");
             }
             if (empty($data['firstname'])) {
                 $errors[] = $this->_("the first name");
             }
             if (empty($data['lastname'])) {
                 $errors[] = $this->_("the last name");
             }
             if (empty($data['email'])) {
                 $errors[] = $this->_("the email address");
             }
             if ($isNew and empty($data['password'])) {
                 $errors[] = $this->_("the password");
             }
             if (!empty($errors)) {
                 $message = array($this->_("Please fill in the following fields:"));
                 foreach ($errors as $error) {
                     $message[] = $error;
                 }
                 $message = join('<br />- ', $message);
                 throw new Exception($message);
             }
             if (!empty($data['email']) and !Zend_Validate::is($data['email'], 'emailAddress')) {
                 throw new Exception($this->_("Please enter a valid email address"));
             }
             $data['show_in_social_gaming'] = (int) (!empty($data['show_in_social_gaming']));
             $data['can_access_locked_features'] = (int) (!empty($data['can_access_locked_features']));
             if ($isNew) {
                 $data['app_id'] = $this->getApplication()->getId();
             }
             if (isset($data['password']) and empty($data['password'])) {
                 unset($data['password']);
             }
             $customer->setData($data);
             if (!empty($data['password'])) {
                 $customer->setPassword($data['password']);
             }
             $customer->save();
             $this->getSession()->addSuccess($this->_("Info successfully saved"));
             $html = array("success" => 1);
         } catch (Exception $e) {
             $html = array("error" => 1, "message" => $e->getMessage(), 'message_button' => 1, 'message_loader' => 1);
         }
         $this->getResponse()->setBody(Zend_Json::encode($html))->sendResponse();
         die;
     }
 }
 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);
     }
 }