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-2011 SugarCRM Inc.
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.
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
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
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.
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.
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 ********************************************************************************/
42 * This file is where the user authentication occurs. No redirection should happen in this file.
45 require_once('modules/Users/authentication/SugarAuthenticate/SugarAuthenticateUser.php');
46 class EmailAuthenticateUser extends SugarAuthenticateUser {
47 var $passwordLength = 4;
51 * this is called when a user logs in
54 * @param STRING $password
57 function loadUserOnLogin($name, $password) {
61 $GLOBALS['log']->debug("Starting user load for ". $name);
62 if(empty($name) || empty($password)) return false;
64 if(empty($_SESSION['lastUserId'])){
65 $user_hash = SugarAuthenticate::encodePassword($password);
66 $user_id = $this->authenticateUser($name, $user_hash);
68 $GLOBALS['log']->fatal('SECURITY: User authentication for '.$name.' failed');
73 if(empty($_SESSION['emailAuthToken'])){
74 $_SESSION['lastUserId'] = $user_id;
75 $_SESSION['lastUserName'] = $name;
76 $_SESSION['emailAuthToken'] = '';
77 for($i = 0; $i < $this->passwordLength; $i++){
78 $_SESSION['emailAuthToken'] .= chr(mt_rand(48,90));
80 $_SESSION['emailAuthToken'] = str_replace(array('<', '>'), array('#', '@'), $_SESSION['emailAuthToken']);
81 $_SESSION['login_error'] = 'Please Enter Your User Name and Emailed Session Token';
82 $this->sendEmailPassword($user_id, $_SESSION['emailAuthToken']);
85 if(strcmp($name, $_SESSION['lastUserName']) == 0 && strcmp($password, $_SESSION['emailAuthToken']) == 0){
86 $this->loadUserOnSession($_SESSION['lastUserId']);
87 unset($_SESSION['lastUserId']);
88 unset($_SESSION['lastUserName']);
89 unset($_SESSION['emailAuthToken']);
95 $_SESSION['login_error'] = 'Please Enter Your User Name and Emailed Session Token';
101 * Sends the users password to the email address or sends
103 * @param unknown_type $user_id
104 * @param unknown_type $password
106 function sendEmailPassword($user_id, $password){
108 $result = $GLOBALS['db']->query("SELECT email1, email2, first_name, last_name FROM users WHERE id='$user_id'");
109 $row = $GLOBALS['db']->fetchByAssoc($result);
111 global $sugar_config;
112 if(empty($row['email1']) && empty($row['email2'])){
114 $_SESSION['login_error'] = 'Please contact an administrator to setup up your email address associated to this account';
118 require_once("include/SugarPHPMailer.php");
120 $OBCharset = $locale->getPrecedentPreference('default_email_charset');
121 $notify_mail = new SugarPHPMailer();
122 $notify_mail->CharSet = $sugar_config['default_charset'];
123 $notify_mail->AddAddress(((!empty($row['email1']))?$row['email1']: $row['email2']),$locale->translateCharsetMIME(trim($row['first_name'] . ' ' . $row['last_name']), 'UTF-8', $OBCharset));
125 if (empty($_SESSION['authenticated_user_language'])) {
126 $current_language = $sugar_config['default_language'];
129 $current_language = $_SESSION['authenticated_user_language'];
131 $mail_settings = new Administration();
132 $mail_settings->retrieveSettings('mail');
135 $notify_mail->Subject = 'Sugar Token';
136 $notify_mail->Body = 'Your sugar session authentication token is: ' . $password;
137 if ($mail_settings->settings['mail_sendtype'] == "SMTP") {
138 $notify_mail->Mailer = "smtp";
139 $notify_mail->Host = $mail_settings->settings['mail_smtpserver'];
140 $notify_mail->Port = $mail_settings->settings['mail_smtpport'];
141 if ($mail_settings->settings['mail_smtpauth_req']) {
142 $notify_mail->SMTPAuth = TRUE;
143 $notify_mail->Username = $mail_settings->settings['mail_smtpuser'];
144 $notify_mail->Password = $mail_settings->settings['mail_smtppass'];
148 $notify_mail->From = 'no-reply@sugarcrm.com';
149 $notify_mail->FromName = 'Sugar Authentication';
151 if(!$notify_mail->Send()) {
152 $GLOBALS['log']->warn("Notifications: error sending e-mail (method: {$notify_mail->Mailer}), (error: {$notify_mail->ErrorInfo})");
155 $GLOBALS['log']->info("Notifications: e-mail successfully sent");