/**
  * Hook: Page Top (Callback)
  **/
 public function hookTop()
 {
     // Load the context.
     $this->context = Context::getContext();
     // Only of the user is not logged in.
     if (!$this->context->customer->isLogged()) {
         // Check for callback arguments.
         if (Tools::getIsset('oa_action') === true and Tools::getIsset('connection_token') === true) {
             // Extract the callback arguments.
             $oa_action = trim(Tools::getValue('oa_action'));
             $connection_token = trim(Tools::getValue('connection_token'));
             //Verify arguments
             if ($oa_action == 'social_login' and strlen($connection_token) > 0) {
                 // Read the API credentials.
                 $api_key = Configuration::get('OASL_API_KEY');
                 $api_password = Configuration::get('OASL_API_PASSWORD');
                 $api_subdomain = Configuration::get('OASL_API_SUBDOMAIN');
                 // Read the API settings.
                 $api_handler = Configuration::get('OASL_API_HANDLER');
                 $api_handler = $api_handler == 'fsockopen' ? 'fsockopen' : 'curl';
                 $api_port = Configuration::get('OASL_API_PORT');
                 $api_port = $api_port == 80 ? 80 : 443;
                 // Set API resource uri.
                 $api_resource = ($api_port === 443 ? 'https' : 'http') . '://' . $api_subdomain . '.api.oneall.com/connections/' . $connection_token . '.json';
                 // Setup API parameters.
                 $api_params = array();
                 $api_params['api_key'] = $api_key;
                 $api_params['api_secret'] = $api_password;
                 // Retrieve connection details.
                 $result = oneall_social_login_tools::do_api_request($api_handler, $api_resource, $api_params, 15);
                 // Parse data.
                 $data = oneall_social_login_tools::extract_social_network_profile($result);
                 // Handle data.
                 if (is_array($data)) {
                     // Get the customer identifier for a given token.
                     $id_customer_tmp = oneall_social_login_tools::get_id_customer_for_user_token($data['user_token']);
                     // This customer already exists.
                     if (is_numeric($id_customer_tmp)) {
                         // Update the identity.
                         oneall_social_login_tools::update_identity_logins($data['identity_token']);
                         // Login this customer.
                         $id_customer = $id_customer_tmp;
                     } else {
                         // Account linking is enabled.
                         if (Configuration::get('OASL_LINK_ACCOUNT_DISABLE') != 1) {
                             // Account linking only works if the email address has been verified.
                             if (!empty($data['user_email']) && $data['user_email_is_verified'] === true) {
                                 // Try to read the existing customer account.
                                 if (($id_customer_tmp = oneall_social_login_tools::get_id_customer_for_email_address($data['user_email'])) !== false) {
                                     // Tie the user_token to the customer.
                                     if (oneall_social_login_tools::link_tokens_to_id_customer($id_customer_tmp, $data['user_token'], $data['identity_token'], $data['identity_provider']) === true) {
                                         // Update the identity.
                                         oneall_social_login_tools::update_identity_logins($data['identity_token']);
                                         // Login this customer.
                                         $id_customer = $id_customer_tmp;
                                     }
                                 }
                             }
                         }
                     }
                     // Create a user new account.
                     if (empty($id_customer)) {
                         // Notify the customer ?
                         $customer_email_notify = true;
                         // Redirection url.
                         $redirect_to = $this->context->link->getPageLink('oneallsociallogin', true, null, array('back' => oneall_social_login_tools::get_current_url()));
                         // How do we have to proceed?
                         switch (Configuration::get('OASL_DATA_HANDLING')) {
                             // Automatic Completion.
                             case 'auto':
                                 // Generate a random email if none is provided or if it's already taken.
                                 if (empty($data['user_email']) or oneall_social_login_tools::get_id_customer_for_email_address($data['user_email']) !== false) {
                                     // Generate a random email.
                                     $data['user_email'] = oneall_social_login_tools::generate_random_email_address();
                                     // But do not send notifications to this email
                                     $customer_email_notify = false;
                                 }
                                 // Generate a lastname if none is provided.
                                 if (empty($data['user_last_name'])) {
                                     $data['user_last_name'] = 'Doe';
                                 }
                                 // Generate a firstname if none is provided.
                                 if (empty($data['user_first_name'])) {
                                     $data['user_first_name'] = 'John';
                                 }
                                 break;
                                 //Ask for manual completion if any of the fields is empty or if the email is already taken.
                             //Ask for manual completion if any of the fields is empty or if the email is already taken.
                             case 'ask':
                                 if (empty($data['user_email']) or empty($data['user_first_name']) or empty($data['user_last_name']) or oneall_social_login_tools::get_id_customer_for_email_address($data['user_email']) !== false) {
                                     // Save the data in the session.
                                     $this->context->cookie->oasl_data = base64_encode(serialize($data));
                                     //Redirect to the request form
                                     header('Location: ' . $redirect_to);
                                     exit;
                                 }
                                 break;
                                 //Always verify the fields
                             //Always verify the fields
                             default:
                                 // Save the data in the session.
                                 $this->context->cookie->oasl_data = base64_encode(serialize($data));
                                 //Redirect to the request form
                                 header('Location: ' . $redirect_to);
                                 exit;
                                 break;
                         }
                         // Email flags.
                         $send_email_to_admin = Configuration::get('OASL_EMAIL_ADMIN_DISABLE') != 1 ? true : false;
                         $send_email_to_customer = ($customer_email_notify == true and Configuration::get('OASL_EMAIL_CUSTOMER_DISABLE') != 1);
                         // Create a new account.
                         $id_customer = oneall_social_login_tools::create_customer_from_data($data, $send_email_to_admin, $send_email_to_customer);
                     }
                     // Login.
                     if (!empty($id_customer) and oneall_social_login_tools::login_customer($id_customer)) {
                         //Remove the data (Should not be set here)
                         if (isset($this->context->cookie->oasl_data)) {
                             unset($this->context->cookie->oasl_data);
                         }
                         //A refresh is required to update the page
                         $back = trim(Tools::getValue('back'));
                         $back = !empty($back) ? $back : oneall_social_login_tools::get_current_url();
                         Tools::redirect($back);
                     }
                 }
             }
         }
     }
 }
     }
 } else {
     if (!oneall_social_login_tools::check_curl($api_connection_use_https)) {
         die('error_selected_handler_faulty');
     }
 }
 // Check subdomain format
 if (!preg_match("/^[a-z0-9\\-]+\$/i", $api_subdomain)) {
     die('error_subdomain_wrong_syntax');
 }
 // Domain
 $api_domain = $api_subdomain . '.api.oneall.com';
 // Connection to
 $api_resource_url = ($api_connection_use_https ? 'https' : 'http') . '://' . $api_domain . '/tools/ping.json';
 // Get connection details
 $result = oneall_social_login_tools::do_api_request($api_connection_handler, $api_resource_url, array('api_key' => $api_key, 'api_secret' => $api_secret), 15);
 // Parse result
 if (is_object($result) and property_exists($result, 'http_code') and property_exists($result, 'http_data')) {
     switch ($result->http_code) {
         // Success
         case 200:
             die('success');
             break;
             // Authentication Error
         // Authentication Error
         case 401:
             die('error_authentication_credentials_wrong');
             break;
             // Wrong Subdomain
         // Wrong Subdomain
         case 404:
 /**
  * Assign template vars related to page content
  */
 public function initContent()
 {
     parent::initContent();
     global $smarty;
     // Restore back value.
     $back = Tools::getValue('back');
     if (!empty($back)) {
         $this->context->smarty->assign('back', Tools::safeOutput($back));
     }
     //	Did an error occur?
     $have_error = true;
     // The cookie is required to proceed.
     if (isset($this->context->cookie->oasl_data)) {
         // Extract the data.
         $data = unserialize(base64_decode($this->context->cookie->oasl_data));
         // Check data format.
         if (is_array($data)) {
             $have_error = false;
             //Submit Button Clicked
             if (Tools::isSubmit('submit')) {
                 // Reset Errors.
                 $this->errors = array();
                 // Read fields.
                 $email = trim(Tools::getValue('oasl_email'));
                 $firstname = trim(Tools::getValue('oasl_firstname'));
                 $lastname = trim(Tools::getValue('oasl_lastname'));
                 $newsletter = intval(Tools::getValue('oasl_newsletter'));
                 // Make sure the firstname is not empty.
                 if (strlen($firstname) == 0) {
                     $this->errors[] = Tools::displayError('Please enter your first name');
                 } elseif (!Validate::isName($firstname)) {
                     $this->errors[] = Tools::displayError('Please enter a valid first name');
                 }
                 // Make sure the lastname is not empty.
                 if (strlen($lastname) == 0) {
                     $this->errors[] = Tools::displayError('Please enter your lastname');
                 } elseif (!Validate::isName($lastname)) {
                     $this->errors[] = Tools::displayError('Please enter a valid last name');
                 }
                 // Make sure the email address it is not empty.
                 if (strlen($email) == 0) {
                     $this->errors[] = Tools::displayError('Please enter your email address');
                 } elseif (!Validate::isEmail($email)) {
                     $this->errors[] = Tools::displayError('Please enter a valid email address');
                 } elseif (oneall_social_login_tools::get_id_customer_for_email_address($email) !== false) {
                     $this->errors[] = Tools::displayError('This email address is already taken');
                 }
                 // We are good to go.
                 if (count($this->errors) == 0) {
                     // Store the manually entered email fields.
                     $data['user_email'] = strtolower($email);
                     $data['user_first_name'] = ucwords(strtolower($firstname));
                     $data['user_last_name'] = ucwords(strtolower($lastname));
                     $data['user_newsletter'] = $newsletter == 1 ? 1 : 0;
                     // Email flags.
                     $send_email_to_admin = Configuration::get('OASL_EMAIL_ADMIN_DISABLE') != 1 ? true : false;
                     $send_email_to_customer = Configuration::get('OASL_EMAIL_CUSTOMER_DISABLE') != 1 ? true : false;
                     // Create a new account.
                     $id_customer = oneall_social_login_tools::create_customer_from_data($data, $send_email_to_admin, $send_email_to_customer);
                     // Login the customer.
                     if (!empty($id_customer) and oneall_social_login_tools::login_customer($id_customer)) {
                         //Remove the data
                         unset($this->context->cookie->oasl_data);
                         //A refresh is required to update the page
                         $back = trim(Tools::getValue('back'));
                         $back = !empty($back) ? $back : oneall_social_login_tools::get_current_url();
                         Tools::redirect($back);
                     }
                 }
             } else {
                 $smarty->assign('oasl_populate', 1);
                 $smarty->assign('oasl_email', isset($data['user_email']) ? $data['user_email'] : '');
                 $smarty->assign('oasl_first_name', isset($data['user_first_name']) ? $data['user_first_name'] : '');
                 $smarty->assign('oasl_last_name', isset($data['user_last_name']) ? $data['user_last_name'] : '');
                 $smarty->assign('oasl_newsletter', 1);
             }
             // Assign template vars.
             $smarty->assign('identity_provider', $data['identity_provider']);
             // Show our template.
             $this->setTemplate(_PS_THEME_DIR_ . 'oneallsociallogin.tpl');
         }
     }
     // We could not extract the data.
     if ($have_error) {
         Tools::redirect();
     }
 }
 /**
  * Assign template vars related to page content
  */
 public function preProcess()
 {
     // Global variables.
     global $cookie, $smarty;
     // Include our toolbox.
     require_once _PS_MODULE_DIR_ . 'oneallsociallogin/includes/tools.php';
     // Restore value,
     $back = Tools::getValue('back');
     if (!empty($back)) {
         $smarty->assign('back', Tools::safeOutput($back));
     }
     // Did an error occur?
     $have_error = true;
     // The cookie is required to proceed.
     if (isset($cookie->oasl_data)) {
         // Extract the data
         $data = @unserialize($cookie->oasl_data);
         //Check data format
         if (is_array($data)) {
             // Customer to login
             $id_customer = null;
             // Did an error occur?
             $have_error = false;
             // Get the customer identifier for a given token.
             $id_customer_tmp = oneall_social_login_tools::get_id_customer_for_user_token($data['user_token']);
             // This customer already exists.
             if (is_numeric($id_customer_tmp)) {
                 // Update the identity.
                 oneall_social_login_tools::update_identity_logins($data['identity_token']);
                 // Login this customer.
                 $id_customer = $id_customer_tmp;
             }
             // No need to do this if we have a customer
             if (empty($id_customer)) {
                 //Submit Button Clicked
                 if (Tools::isSubmit('submit')) {
                     // Reset Errors.
                     $this->errors = array();
                     // Read fields.
                     $email = trim(Tools::getValue('oasl_email'));
                     $firstname = trim(Tools::getValue('oasl_firstname'));
                     $lastname = trim(Tools::getValue('oasl_lastname'));
                     // Make sure it is not empty.
                     if (strlen($firstname) == 0) {
                         $this->errors[] = Tools::displayError('Please enter your first name');
                     } elseif (!Validate::isName($firstname)) {
                         $this->errors[] = Tools::displayError('Please enter a valid first name');
                     }
                     // Make sure it is not empty.
                     if (strlen($lastname) == 0) {
                         $this->errors[] = Tools::displayError('Please enter your lastname');
                     } elseif (!Validate::isName($lastname)) {
                         $this->errors[] = Tools::displayError('Please enter a valid last name');
                     }
                     // Make sure it is not empty.
                     if (strlen($email) == 0) {
                         $this->errors[] = Tools::displayError('Please enter your email address');
                     } elseif (!Validate::isEmail($email)) {
                         $this->errors[] = Tools::displayError('Please enter a valid email address');
                     } elseif (oneall_social_login_tools::get_id_customer_for_email_address($email) !== false) {
                         $this->errors[] = Tools::displayError('This email address is already taken');
                     }
                     // We are good to go.
                     if (count($this->errors) == 0) {
                         // Store the manually entered email fields.
                         $data['user_email'] = strtolower($email);
                         $data['user_first_name'] = ucwords(strtolower($firstname));
                         $data['user_last_name'] = ucwords(strtolower($lastname));
                         // Email flags.
                         $send_email_to_admin = Configuration::get('OASL_EMAIL_ADMIN_DISABLE') != 1 ? true : false;
                         $send_email_to_customer = Configuration::get('OASL_EMAIL_CUSTOMER_DISABLE') != 1 ? true : false;
                         // Create a new account.
                         $id_customer = oneall_social_login_tools::create_customer_from_data($data, $send_email_to_admin, $send_email_to_customer);
                     }
                 } else {
                     $smarty->assign('oasl_populate', 1);
                     $smarty->assign('oasl_email', isset($data['user_email']) ? $data['user_email'] : '');
                     $smarty->assign('oasl_first_name', isset($data['user_first_name']) ? $data['user_first_name'] : '');
                     $smarty->assign('oasl_last_name', isset($data['user_last_name']) ? $data['user_last_name'] : '');
                 }
             }
             // Login
             if (!empty($id_customer) and oneall_social_login_tools::login_customer($id_customer)) {
                 //Remove the data
                 unset($this->context->cookie->oasl_data);
                 //A refresh is required to update the page
                 $back = trim(Tools::getValue('back'));
                 $back = !empty($back) ? $back : oneall_social_login_tools::get_current_url();
                 Tools::redirectLink($back);
             }
             // Assign template vars.
             $smarty->assign('identity_provider', $data['identity_provider']);
             // Show our template.
             parent::preProcess();
         }
     }
     // We could not extract the data.
     if ($have_error) {
         Tools::redirect();
     }
 }