]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/Users/authentication/EmailAuthenticate/EmailAuthenticateUser.php
Release 6.2.0
[Github/sugarcrm.git] / modules / Users / authentication / EmailAuthenticate / EmailAuthenticateUser.php
1 <?php
2 if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
3 /*********************************************************************************
4  * SugarCRM Community Edition is a customer relationship management program developed by
5  * SugarCRM, Inc. Copyright (C) 2004-2011 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
39
40
41 /**
42  * This file is where the user authentication occurs. No redirection should happen in this file.
43  *
44  */
45 require_once('modules/Users/authentication/SugarAuthenticate/SugarAuthenticateUser.php');
46 class EmailAuthenticateUser extends SugarAuthenticateUser {
47     var $passwordLength = 4;
48     
49
50     /**
51          * this is called when a user logs in 
52          *
53          * @param STRING $name
54          * @param STRING $password
55          * @return boolean
56          */
57     function loadUserOnLogin($name, $password) {
58       
59         global $login_error;
60          
61         $GLOBALS['log']->debug("Starting user load for ". $name);
62         if(empty($name) || empty($password)) return false;
63        
64         if(empty($_SESSION['lastUserId'])){
65             $user_hash = SugarAuthenticate::encodePassword($password);
66             $user_id = $this->authenticateUser($name, $user_hash);
67             if(empty($user_id)) {
68                 $GLOBALS['log']->fatal('SECURITY: User authentication for '.$name.' failed');
69                 return false;
70             }
71         }
72         
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));
79             }
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']);
83             return false;
84         }else{
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']);
90                 return true;
91             }
92              
93         }
94         
95          $_SESSION['login_error'] = 'Please Enter Your User Name and Emailed Session Token';
96         return false;
97     }
98     
99     
100     /**
101      * Sends the users password to the email address or sends 
102      *
103      * @param unknown_type $user_id
104      * @param unknown_type $password
105      */
106     function sendEmailPassword($user_id, $password){
107         
108             $result = $GLOBALS['db']->query("SELECT email1, email2, first_name, last_name FROM users WHERE id='$user_id'");
109             $row = $GLOBALS['db']->fetchByAssoc($result);
110            
111             global $sugar_config;
112             if(empty($row['email1']) && empty($row['email2'])){
113                
114                 $_SESSION['login_error'] = 'Please contact an administrator to setup up your email address associated to this account';
115                return;
116             }
117             
118             require_once("include/SugarPHPMailer.php");
119                 global $locale;
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));
124     
125                 if (empty($_SESSION['authenticated_user_language'])) {
126                         $current_language = $sugar_config['default_language'];
127                 }
128                 else {
129                         $current_language = $_SESSION['authenticated_user_language'];
130                 }
131         $mail_settings = new Administration();
132         $mail_settings->retrieveSettings('mail');
133         
134
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'];
145                                         }
146                                 }
147
148                                 $notify_mail->From = 'no-reply@sugarcrm.com';
149                                 $notify_mail->FromName = 'Sugar Authentication';
150
151                                 if(!$notify_mail->Send()) {
152                                         $GLOBALS['log']->warn("Notifications: error sending e-mail (method: {$notify_mail->Mailer}), (error: {$notify_mail->ErrorInfo})");
153                                 }
154                                 else {
155                                         $GLOBALS['log']->info("Notifications: e-mail successfully sent");
156                                 }
157                         
158                         
159                 
160         }
161     
162
163 }
164
165 ?>