]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/Users/GeneratePassword.php
Merge pull request #100 from collinlee/master
[Github/sugarcrm.git] / modules / Users / GeneratePassword.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-2012 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  * Description:  TODO: To be written.
41  * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
42  * All Rights Reserved.
43  * Contributor(s): ______________________________________..
44  ********************************************************************************/
45
46     require_once('include/entryPoint.php');
47
48     require_once('modules/Users/language/en_us.lang.php');
49     global $app_strings;
50     global $sugar_config;
51     global $new_pwd;
52
53         $mod_strings=return_module_language('','Users');
54         $res=$GLOBALS['sugar_config']['passwordsetting'];
55         $regexmail = "/^\w+(['\.\-\+]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+\$/";
56
57 ///////////////////////////////////////////////////
58 ///////  Retrieve user
59 $username = '';
60 $useremail = '';
61 if(isset( $_POST['user_name'])){
62         $username = $_POST['user_name'];
63 }else if(isset( $_POST['username'])){
64         $username = $_POST['username'];
65 }
66
67 if(isset( $_POST['Users0emailAddress0'])){
68         $useremail = $_POST['Users0emailAddress0'];
69 }else if(isset( $_POST['user_email'])){
70         $useremail = $_POST['user_email'];
71 }
72
73     $usr= new user();
74     if(isset($username) && $username != '' && isset($useremail) && $useremail != '')
75     {
76         if ($username != '' && $useremail != ''){
77             $usr_id=$usr->retrieve_user_id($username);
78             $usr->retrieve($usr_id);
79             if ($usr->email1 !=  $useremail){
80                 echo $mod_strings['ERR_PASSWORD_USERNAME_MISSMATCH'];
81                 return;
82             }
83
84             if ($usr->portal_only || $usr->is_group){
85                     echo $mod_strings['LBL_PROVIDE_USERNAME_AND_EMAIL'];
86                     return;
87             }
88         }
89         else
90         {
91                 echo  $mod_strings['LBL_PROVIDE_USERNAME_AND_EMAIL'];
92                 return;
93         }
94     }
95     else{
96         if (isset($_POST['userId']) && $_POST['userId'] != ''){
97             $usr->retrieve($_POST['userId']);
98         }
99         else{
100                 if(isset( $_POST['sugar_user_name']) && isset($_POST['sugar_user_name'] )){
101                                 $usr_id=$usr->retrieve_user_id($_POST['sugar_user_name']);
102                         $usr->retrieve($usr_id);
103                         }
104                 else{
105                         echo  $mod_strings['ERR_USER_INFO_NOT_FOUND'];
106                 return;
107                 }
108         }
109     }
110
111 ///////
112 ///////////////////////////////////////////////////
113
114 ///////////////////////////////////////////////////
115 ///////  Check email address
116
117         if (!preg_match($regexmail, $usr->emailAddress->getPrimaryAddress($usr))){
118                 echo $mod_strings['ERR_EMAIL_INCORRECT'];
119                 return;
120         }
121
122 ///////
123 ///////////////////////////////////////////////////
124
125     // if i need to generate a password (not a link)
126     $password = !isset($_POST['link']) ? User::generatePassword() : '';
127
128 ///////////////////////////////////////////////////
129 ///////  Create URL
130
131 // if i need to generate a link
132 if (isset($_POST['link']) && $_POST['link'] == '1'){
133         global $timedate;
134         $guid=create_guid();
135         $url=$GLOBALS['sugar_config']['site_url']."/index.php?entryPoint=Changenewpassword&guid=$guid";
136         $time_now=TimeDate::getInstance()->nowDb();
137         //$q2="UPDATE `users_password_link` SET `deleted` = '1' WHERE `username` = '".$username."'";
138         //$usr->db->query($q2);
139         $q = "INSERT INTO users_password_link (id, username, date_generated) VALUES('".$guid."','".$username."','".$time_now."') ";
140         $usr->db->query($q);
141 }
142 ///////
143 ///////////////////////////////////////////////////
144
145 ///////  Email creation
146         global $current_user;
147     if (isset($_POST['link']) && $_POST['link'] == '1')
148         $emailTemp_id = $res['lostpasswordtmpl'];
149     else
150         $emailTemp_id = $res['generatepasswordtmpl'];
151
152     $additionalData = array(
153         'link' => isset($_POST['link']) && $_POST['link'] == '1',
154         'password' => $password
155     );
156     if (isset($url))
157     {
158         $additionalData['url'] = $url;
159     }
160     $result = $usr->sendEmailForPassword($emailTemp_id, $additionalData);
161     if ($result['status'] == false && $result['message'] != '')
162     {
163         echo $result['message'];
164         $new_pwd = '4';
165         return;
166     }
167     
168     if ($result['status'] == true)
169     {
170         echo '1';
171     } else {
172         $new_pwd='4';
173         if ($current_user->is_admin){
174                 $email_errors=$mod_strings['ERR_EMAIL_NOT_SENT_ADMIN'];
175                 if ($mail->Mailer == 'smtp')
176                         $email_errors.="\n-".$mod_strings['ERR_SMTP_URL_SMTP_PORT'];
177                 if ($mail->SMTPAuth)
178                         $email_errors.="\n-".$mod_strings['ERR_SMTP_USERNAME_SMTP_PASSWORD'];
179                 $email_errors.="\n-".$mod_strings['ERR_RECIPIENT_EMAIL'];
180                 $email_errors.="\n-".$mod_strings['ERR_SERVER_STATUS'];
181                 echo $email_errors;
182         }
183         else
184                 echo $mod_strings['LBL_EMAIL_NOT_SENT'];
185     }
186     return;
187
188 ?>