PageRenderTime 69ms CodeModel.GetById 23ms RepoModel.GetById 2ms app.codeStats 0ms

/catalog/controller/module/pp_login.php

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