PageRenderTime 54ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/modules/Users/authentication/EmailAuthenticate/EmailAuthenticateUser.php

https://github.com/yinhm/sugarcrm
PHP | 164 lines | 85 code | 28 blank | 51 comment | 22 complexity | 1fb65f7799d038da1b41f4f4ba15d9cb MD5 | raw file
Possible License(s): LGPL-2.1, MPL-2.0-no-copyleft-exception
  1. <?php
  2. if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
  3. /*********************************************************************************
  4. * SugarCRM is a customer relationship management program developed by
  5. * SugarCRM, Inc. Copyright (C) 2004-2010 SugarCRM Inc.
  6. *
  7. * This program is free software; you can redistribute it and/or modify it under
  8. * the terms of the GNU Affero General Public License version 3 as published by the
  9. * Free Software Foundation with the addition of the following permission added
  10. * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
  11. * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
  12. * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
  13. *
  14. * This program is distributed in the hope that it will be useful, but WITHOUT
  15. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  16. * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
  17. * details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License along with
  20. * this program; if not, see http://www.gnu.org/licenses or write to the Free
  21. * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  22. * 02110-1301 USA.
  23. *
  24. * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
  25. * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
  26. *
  27. * The interactive user interfaces in modified source and object code versions
  28. * of this program must display Appropriate Legal Notices, as required under
  29. * Section 5 of the GNU Affero General Public License version 3.
  30. *
  31. * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
  32. * these Appropriate Legal Notices must retain the display of the "Powered by
  33. * SugarCRM" logo. If the display of the logo is not reasonably feasible for
  34. * technical reasons, the Appropriate Legal Notices must display the words
  35. * "Powered by SugarCRM".
  36. ********************************************************************************/
  37. /**
  38. * This file is where the user authentication occurs. No redirection should happen in this file.
  39. *
  40. */
  41. require_once('modules/Users/authentication/SugarAuthenticate/SugarAuthenticateUser.php');
  42. class EmailAuthenticateUser extends SugarAuthenticateUser {
  43. var $passwordLength = 4;
  44. /**
  45. * this is called when a user logs in
  46. *
  47. * @param STRING $name
  48. * @param STRING $password
  49. * @return boolean
  50. */
  51. function loadUserOnLogin($name, $password) {
  52. global $login_error;
  53. $GLOBALS['log']->debug("Starting user load for ". $name);
  54. if(empty($name) || empty($password)) return false;
  55. if(empty($_SESSION['lastUserId'])){
  56. $user_hash = SugarAuthenticate::encodePassword($password);
  57. $user_id = $this->authenticateUser($name, $user_hash);
  58. if(empty($user_id)) {
  59. $GLOBALS['log']->fatal('SECURITY: User authentication for '.$name.' failed');
  60. return false;
  61. }
  62. }
  63. if(empty($_SESSION['emailAuthToken'])){
  64. $_SESSION['lastUserId'] = $user_id;
  65. $_SESSION['lastUserName'] = $name;
  66. $_SESSION['emailAuthToken'] = '';
  67. for($i = 0; $i < $this->passwordLength; $i++){
  68. $_SESSION['emailAuthToken'] .= chr(mt_rand(48,90));
  69. }
  70. $_SESSION['emailAuthToken'] = str_replace(array('<', '>'), array('#', '@'), $_SESSION['emailAuthToken']);
  71. $_SESSION['login_error'] = 'Please Enter Your User Name and Emailed Session Token';
  72. $this->sendEmailPassword($user_id, $_SESSION['emailAuthToken']);
  73. return false;
  74. }else{
  75. if(strcmp($name, $_SESSION['lastUserName']) == 0 && strcmp($password, $_SESSION['emailAuthToken']) == 0){
  76. $this->loadUserOnSession($_SESSION['lastUserId']);
  77. unset($_SESSION['lastUserId']);
  78. unset($_SESSION['lastUserName']);
  79. unset($_SESSION['emailAuthToken']);
  80. return true;
  81. }
  82. }
  83. $_SESSION['login_error'] = 'Please Enter Your User Name and Emailed Session Token';
  84. return false;
  85. }
  86. /**
  87. * Sends the users password to the email address or sends
  88. *
  89. * @param unknown_type $user_id
  90. * @param unknown_type $password
  91. */
  92. function sendEmailPassword($user_id, $password){
  93. $result = $GLOBALS['db']->query("SELECT email1, email2, first_name, last_name FROM users WHERE id='$user_id'");
  94. $row = $GLOBALS['db']->fetchByAssoc($result);
  95. global $sugar_config;
  96. if(empty($row['email1']) && empty($row['email2'])){
  97. $_SESSION['login_error'] = 'Please contact an administrator to setup up your email address associated to this account';
  98. return;
  99. }
  100. require_once("include/SugarPHPMailer.php");
  101. global $locale;
  102. $OBCharset = $locale->getPrecedentPreference('default_email_charset');
  103. $notify_mail = new SugarPHPMailer();
  104. $notify_mail->CharSet = $sugar_config['default_charset'];
  105. $notify_mail->AddAddress(((!empty($row['email1']))?$row['email1']: $row['email2']),$locale->translateCharsetMIME(trim($row['first_name'] . ' ' . $row['last_name']), 'UTF-8', $OBCharset));
  106. if (empty($_SESSION['authenticated_user_language'])) {
  107. $current_language = $sugar_config['default_language'];
  108. }
  109. else {
  110. $current_language = $_SESSION['authenticated_user_language'];
  111. }
  112. $mail_settings = new Administration();
  113. $mail_settings->retrieveSettings('mail');
  114. $notify_mail->Subject = 'Sugar Token';
  115. $notify_mail->Body = 'Your sugar session authentication token is: ' . $password;
  116. if ($mail_settings->settings['mail_sendtype'] == "SMTP") {
  117. $notify_mail->Mailer = "smtp";
  118. $notify_mail->Host = $mail_settings->settings['mail_smtpserver'];
  119. $notify_mail->Port = $mail_settings->settings['mail_smtpport'];
  120. if ($mail_settings->settings['mail_smtpauth_req']) {
  121. $notify_mail->SMTPAuth = TRUE;
  122. $notify_mail->Username = $mail_settings->settings['mail_smtpuser'];
  123. $notify_mail->Password = $mail_settings->settings['mail_smtppass'];
  124. }
  125. }
  126. $notify_mail->From = 'no-reply@sugarcrm.com';
  127. $notify_mail->FromName = 'Sugar Authentication';
  128. if(!$notify_mail->Send()) {
  129. $GLOBALS['log']->warn("Notifications: error sending e-mail (method: {$notify_mail->Mailer}), (error: {$notify_mail->ErrorInfo})");
  130. }
  131. else {
  132. $GLOBALS['log']->info("Notifications: e-mail successfully sent");
  133. }
  134. }
  135. }
  136. ?>