shop.balmet.com

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

pp_login.php (7714B)


      1 <?php
      2 class ControllerExtensionModulePPLogin extends Controller {
      3 	private $error = array();
      4 
      5 	public function index() {
      6 		if (!$this->customer->isLogged()) {
      7 			$data['client_id'] = $this->config->get('module_pp_login_client_id');
      8 			$data['return_url'] = $this->url->link('extension/module/pp_login/login', '', true);
      9 
     10 			if ($this->config->get('module_pp_login_sandbox')) {
     11 				$data['sandbox'] = 'sandbox';
     12 			} else {
     13 				$data['sandbox'] = '';
     14 			}
     15 
     16 			if ($this->config->get('module_pp_login_button_colour') == 'grey') {
     17 				$data['button_colour'] = 'neutral';
     18 			} else {
     19 				$data['button_colour'] = '';
     20 			}
     21 
     22 			$locale = $this->config->get('module_pp_login_locale');
     23 
     24 			$this->load->model('localisation/language');
     25 
     26 			$languages = $this->model_localisation_language->getLanguages();
     27 
     28 			foreach ($languages as $language) {
     29 				if ($language['status'] && ($language['code'] == $this->session->data['language']) && isset($locale[$language['language_id']])) {
     30 					$data['locale'] = $locale[$language['language_id']];
     31 				}
     32 			}
     33 
     34 			if (!isset($data['locale'])) {
     35 				$data['locale'] = 'en-gb';
     36 			}
     37 
     38 			$scopes = array(
     39 				'profile',
     40 				'email',
     41 				'address',
     42 				'phone'
     43 			);
     44 
     45 			if ($this->config->get('module_pp_login_seamless')) {
     46 				$scopes[] = 'https://uri.paypal.com/services/expresscheckout';
     47 			}
     48 
     49 			$data['scopes'] = implode(' ', $scopes);
     50 
     51 			return $this->load->view('extension/module/pp_login', $data);
     52 		}
     53 	}
     54 
     55 	public function login() {
     56 		$this->load->model('extension/module/pp_login');
     57 		$this->load->model('account/customer');
     58 		$this->load->model('account/customer_group');
     59 
     60 		if ($this->customer->isLogged()) {
     61 			echo '<script type="text/javascript">window.opener.location = "' . $this->url->link('account/account', '', true) . '"; window.close();</script>';
     62 		}
     63 
     64 		if (!isset($this->request->get['code'])) {
     65 			if (isset($this->request->get['error']) && isset($this->request->get['error_description'])) {
     66 				$this->model_extension_module_pp_login->log('No code returned. Error: ' . $this->request->get['error'] . ', Error Description: ' . $this->request->get['error_description']);
     67 			}
     68 
     69 			echo '<script type="text/javascript">window.opener.location = "' . $this->url->link('account/login', '', true) . '"; window.close();</script>';
     70 		} else {
     71 			$tokens = $this->model_extension_module_pp_login->getTokens($this->request->get['code']);
     72 		}
     73 
     74 		if (isset($tokens->access_token) && !isset($tokens->error)) {
     75 			$user = $this->model_extension_module_pp_login->getUserInfo($tokens->access_token);
     76 		}
     77 
     78 		if (isset($user)) {
     79 			$customer_info = $this->model_account_customer->getCustomerByEmail($user->email);
     80 
     81 			if ($customer_info) {
     82 				if ($this->validate($user->email)) {
     83 					$this->completeLogin($customer_info['customer_id'], $customer_info['email'], $tokens->access_token);
     84 				} else {
     85 					$this->model_extension_module_pp_login->log('Could not login to - ID: ' . $customer_info['customer_id'] . ', Email: ' . $customer_info['email']);
     86 					echo '<script type="text/javascript">window.opener.location = "' . $this->url->link('account/login', '', true) . '"; window.close();</script>';
     87 				}
     88 			} else {
     89 				$country = $this->db->query("SELECT `country_id` FROM `" . DB_PREFIX . "country` WHERE iso_code_2 = '" . $this->db->escape($user->address->country) . "'");
     90 
     91 				if ($country->num_rows) {
     92 					$country_id = $country->row['country_id'];
     93 
     94 					$zone = $this->db->query("SELECT `zone_id` FROM `" . DB_PREFIX . "zone` WHERE country_id = '" . (int)$country_id . "' AND name = '" . $this->db->escape($user->address->region) . "'");
     95 
     96 					if ($zone->num_rows) {
     97 						$zone_id = $zone->row['zone_id'];
     98 					} else {
     99 						$zone_id = 0;
    100 					}
    101 				} else {
    102 					$country_id = 0;
    103 					$zone_id = 0;
    104 				}
    105 
    106 				if ($this->config->get('module_pp_login_customer_group_id')) {
    107 					$customer_group_id = $this->config->get('module_pp_login_customer_group_id');
    108 				} else {
    109 					$customer_group_id = $this->config->get('config_customer_group_id');
    110 				}
    111 
    112 				$data = array(
    113 					'customer_group_id' => (int)$customer_group_id,
    114 					'firstname'         => $user->given_name,
    115 					'lastname'          => $user->family_name,
    116 					'email'             => $user->email,
    117 					'telephone'         => $user->phone_number,
    118 					'password'          => uniqid(rand(), true),
    119 					'company'           => '',
    120 					'address_1'         => $user->address->street_address,
    121 					'address_2'         => '',
    122 					'city'              => $user->address->locality,
    123 					'postcode'          => $user->address->postal_code,
    124 					'country_id'        => (int)$country_id,
    125 					'zone_id'           => (int)$zone_id,
    126 				);
    127 
    128 				$customer_id = $this->model_account_customer->addCustomer($data);
    129 
    130 				$this->model_extension_module_pp_login->log('Customer ID date_added: ' . $customer_id);
    131 
    132 				if ($this->validate($user->email)) {
    133 					$this->completeLogin($customer_id, $user->email, $tokens->access_token);
    134 				} else {
    135 					$this->model_extension_module_pp_login->log('Could not login to - ID: ' . $customer_id . ', Email: ' . $user->email);
    136 					echo '<script type="text/javascript">window.opener.location = "' . $this->url->link('account/login', '', true) . '"; window.close();</script>';
    137 				}
    138 			}
    139 		}
    140 	}
    141 
    142 	public function logout() {
    143 		if (isset($this->session->data['pp_login'])) {
    144 			unset($this->session->data['pp_login']);
    145 		}
    146 	}
    147 
    148 	protected function completeLogin($customer_id, $email, $access_token) {
    149 		unset($this->session->data['guest']);
    150 
    151 		// Default Shipping Address
    152 		$this->load->model('account/address');
    153 
    154 		if ($this->config->get('config_tax_customer') == 'payment') {
    155 			$this->session->data['payment_address'] = $this->model_account_address->getAddress($this->customer->getAddressId());
    156 		}
    157 
    158 		if ($this->config->get('config_tax_customer') == 'shipping') {
    159 			$this->session->data['shipping_address'] = $this->model_account_address->getAddress($this->customer->getAddressId());
    160 		}
    161 
    162 		if ($this->config->get('module_pp_login_seamless')) {
    163 			$this->session->data['pp_login']['seamless']['customer_id'] = $this->customer->getId();
    164 			$this->session->data['pp_login']['seamless']['access_token'] = $access_token;
    165 		} else {
    166 			if (isset($this->session->data['pp_login']['seamless'])) {
    167 				unset($this->session->data['pp_login']['seamless']);
    168 			}
    169 		}
    170 
    171 		$this->model_extension_module_pp_login->log('Customer logged in - ID: ' . $customer_id . ', Email: ' . $email);
    172 		echo '<script type="text/javascript">window.opener.location = "' . $this->url->link('account/account', '', true) . '"; window.close();</script>';
    173 	}
    174 
    175 	protected function validate($email) {
    176 		// Check how many login attempts have been made.
    177 		$login_info = $this->model_account_customer->getLoginAttempts($email);
    178 
    179 		if ($login_info && ($login_info['total'] >= $this->config->get('config_login_attempts')) && strtotime('-1 hour') < strtotime($login_info['date_modified'])) {
    180 			$this->error['warning'] = $this->language->get('error_attempts');
    181 		}
    182 
    183 		// Check if customer has been approved.
    184 		$customer_info = $this->model_account_customer->getCustomerByEmail($email);
    185 
    186 		if ($customer_info && !$customer_info['status']) {
    187 			$this->error['warning'] = $this->language->get('error_approved');
    188 		}
    189 
    190 		if (!$this->error) {
    191 			if (!$this->customer->login($email, '', true)) {
    192 				$this->error['warning'] = $this->language->get('error_login');
    193 
    194 				$this->model_account_customer->addLoginAttempt($email);
    195 			} else {
    196 				$this->model_account_customer->deleteLoginAttempts($email);
    197 			}
    198 		}
    199 
    200 		return !$this->error;
    201 	}
    202 }