PageRenderTime 56ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/joomla/plugins/authentication/gmail/gmail.php

https://gitlab.com/ricardosanchez/prueba
PHP | 208 lines | 141 code | 25 blank | 42 comment | 22 complexity | a8c0f49e7fe7b844c6d3f168e7c5cde6 MD5 | raw file
  1. <?php
  2. /**
  3. * @package Joomla.Plugin
  4. * @subpackage Authentication.gmail
  5. *
  6. * @copyright Copyright (C) 2005 - 2015 Open Source Matters, Inc. All rights reserved.
  7. * @license GNU General Public License version 2 or later; see LICENSE.txt
  8. */
  9. defined('_JEXEC') or die;
  10. /**
  11. * GMail Authentication Plugin
  12. *
  13. * @since 1.5
  14. */
  15. class PlgAuthenticationGMail extends JPlugin
  16. {
  17. /**
  18. * This method should handle any authentication and report back to the subject
  19. *
  20. * @param array $credentials Array holding the user credentials
  21. * @param array $options Array of extra options
  22. * @param object &$response Authentication response object
  23. *
  24. * @return boolean
  25. *
  26. * @since 1.5
  27. */
  28. public function onUserAuthenticate($credentials, $options, &$response)
  29. {
  30. // Load plugin language
  31. $this->loadLanguage();
  32. // No backend authentication
  33. if (JFactory::getApplication()->isAdmin() && !$this->params->get('backendLogin', 0))
  34. {
  35. return;
  36. }
  37. $success = 0;
  38. // Check if we have curl or not
  39. if (function_exists('curl_init'))
  40. {
  41. // Check if we have a username and password
  42. if (strlen($credentials['username']) && strlen($credentials['password']))
  43. {
  44. $blacklist = explode(',', $this->params->get('user_blacklist', ''));
  45. // Check if the username isn't blacklisted
  46. if (!in_array($credentials['username'], $blacklist))
  47. {
  48. $suffix = $this->params->get('suffix', '');
  49. $applysuffix = $this->params->get('applysuffix', 0);
  50. $offset = strpos($credentials['username'], '@');
  51. // Check if we want to do suffix stuff, typically for Google Apps for Your Domain
  52. if ($suffix && $applysuffix)
  53. {
  54. if ($applysuffix == 1 && $offset === false)
  55. {
  56. // Apply suffix if missing
  57. $credentials['username'] .= '@' . $suffix;
  58. }
  59. elseif ($applysuffix == 2)
  60. {
  61. // Always use suffix
  62. if ($offset)
  63. {
  64. // If we already have an @, get rid of it and replace it
  65. $credentials['username'] = substr($credentials['username'], 0, $offset);
  66. }
  67. $credentials['username'] .= '@' . $suffix;
  68. }
  69. }
  70. $curl = curl_init('https://mail.google.com/mail/feed/atom');
  71. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  72. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, $this->params->get('verifypeer', 1));
  73. curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
  74. curl_setopt($curl, CURLOPT_USERPWD, $credentials['username'] . ':' . $credentials['password']);
  75. curl_exec($curl);
  76. $code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
  77. switch ($code)
  78. {
  79. case 200 :
  80. $message = JText::_('JGLOBAL_AUTH_ACCESS_GRANTED');
  81. $success = 1;
  82. break;
  83. case 401 :
  84. $message = JText::_('JGLOBAL_AUTH_ACCESS_DENIED');
  85. break;
  86. default :
  87. $message = JText::_('JGLOBAL_AUTH_UNKNOWN_ACCESS_DENIED');
  88. break;
  89. }
  90. }
  91. else
  92. {
  93. // The username is black listed
  94. $message = JText::_('JGLOBAL_AUTH_USER_BLACKLISTED');
  95. }
  96. }
  97. else
  98. {
  99. $message = JText::_('JGLOBAL_AUTH_USER_BLACKLISTED');
  100. }
  101. }
  102. else
  103. {
  104. $message = JText::_('JGLOBAL_AUTH_CURL_NOT_INSTALLED');
  105. }
  106. $response->type = 'GMail';
  107. if ($success)
  108. {
  109. if (strpos($credentials['username'], '@') === false)
  110. {
  111. if ($suffix)
  112. {
  113. // If there is a suffix then we want to apply it
  114. $email = $credentials['username'] . '@' . $suffix;
  115. }
  116. else
  117. {
  118. // If there isn't a suffix just use the default gmail one
  119. $email = $credentials['username'] . '@gmail.com';
  120. }
  121. }
  122. else
  123. {
  124. // The username looks like an email address (probably is) so use that
  125. $email = $credentials['username'];
  126. }
  127. // Extra security checks with existing local accounts
  128. $db = JFactory::getDbo();
  129. $localUsernameChecks = array(strstr($email, '@', true), $email);
  130. $query = $db->getQuery(true)
  131. ->select('id, activation, username, email, block')
  132. ->from('#__users')
  133. ->where('username IN(' . implode(',', array_map(array($db, 'quote'), $localUsernameChecks)) . ')'
  134. . ' OR email = ' . $db->quote($email)
  135. );
  136. $db->setQuery($query);
  137. if ($localUsers = $db->loadObjectList())
  138. {
  139. foreach ($localUsers as $localUser)
  140. {
  141. // Local user exists with same username but different email address
  142. if ($email != $localUser->email)
  143. {
  144. $response->status = JAuthentication::STATUS_FAILURE;
  145. $response->error_message = JText::sprintf('JGLOBAL_AUTH_FAILED', JText::_('PLG_GMAIL_ERROR_LOCAL_USERNAME_CONFLICT'));
  146. return;
  147. }
  148. else
  149. {
  150. // Existing user disabled locally
  151. if ($localUser->block || !empty($localUser->activation))
  152. {
  153. $response->status = JAuthentication::STATUS_FAILURE;
  154. $response->error_message = JText::_('JGLOBAL_AUTH_ACCESS_DENIED');
  155. return;
  156. }
  157. // We will always keep the local username for existing accounts
  158. $credentials['username'] = $localUser->username;
  159. break;
  160. }
  161. }
  162. }
  163. elseif (JFactory::getApplication()->isAdmin())
  164. // We wont' allow backend access without local account
  165. {
  166. $response->status = JAuthentication::STATUS_FAILURE;
  167. $response->error_message = JText::_('JERROR_LOGIN_DENIED');
  168. return;
  169. }
  170. $response->status = JAuthentication::STATUS_SUCCESS;
  171. $response->error_message = '';
  172. $response->email = $email;
  173. // Reset the username to what we ended up using
  174. $response->username = $credentials['username'];
  175. $response->fullname = $credentials['username'];
  176. }
  177. else
  178. {
  179. $response->status = JAuthentication::STATUS_FAILURE;
  180. $response->error_message = JText::sprintf('JGLOBAL_AUTH_FAILED', $message);
  181. }
  182. }
  183. }