]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/Users/GeneratePassword.php
Release 6.1.4
[Github/sugarcrm.git] / modules / Users / GeneratePassword.php
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-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  * 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 $new_pwd;
51
52         $mod_strings=return_module_language('','Users');
53         $res=$GLOBALS['sugar_config']['passwordsetting'];
54         $regexmail = "/^\w+(['\.\-\+]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+\$/";
55
56 ///////////////////////////////////////////////////
57 ///////  Retrieve user
58
59     $usr= new user();
60     if(isset( $_POST['username']) && isset($_POST['user_email'] )){
61         if ($_POST['username'] != '' && $_POST['user_email'] != ''){
62                 $usr_id=$usr->retrieve_user_id($_POST['username']);
63                 $usr->retrieve($usr_id);
64                 if ($usr->email1 !=  $_POST['user_email']){
65                     echo $mod_strings['ERR_PASSWORD_USERNAME_MISSMATCH'];
66                     return;
67             }
68             if ($usr->portal_only || $usr->is_group){
69                     echo $mod_strings['LBL_PROVIDE_USERNAME_AND_EMAIL'];
70                     return;
71             }
72         }
73         else
74         {
75                 echo  $mod_strings['LBL_PROVIDE_USERNAME_AND_EMAIL'];
76                 return;
77         }
78     }
79     else{
80         if (isset($_POST['userId']) && $_POST['userId'] != ''){
81             $usr->retrieve($_POST['userId']);
82         }
83         else{
84                 if(isset( $_POST['sugar_user_name']) && isset($_POST['sugar_user_name'] )){
85                                 $usr_id=$usr->retrieve_user_id($_POST['sugar_user_name']);
86                         $usr->retrieve($usr_id);
87                         }
88                 else{
89                         echo  $mod_strings['ERR_USER_INFO_NOT_FOUND'];
90                 return;
91                 }
92         }
93     }
94
95 ///////
96 ///////////////////////////////////////////////////
97
98 ///////////////////////////////////////////////////
99 ///////  Check email address
100
101         if (!preg_match($regexmail, $usr->emailAddress->getPrimaryAddress($usr))){
102                 echo $mod_strings['ERR_EMAIL_INCORRECT'];
103                 return;
104         }
105
106 ///////
107 ///////////////////////////////////////////////////
108
109
110         // if i need to generate a password (not a link)
111     if (!isset($_POST['link'])){
112             $charBKT='';
113             //chars to select from
114             $LOWERCASE = "abcdefghijklmnpqrstuvwxyz";
115             $NUMBER = "0123456789";
116             $UPPERCASE = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
117             $SPECIAL = '~!@#$%^&*()_+=-{}|'; 
118             $condition = 0;
119             $charBKT.=$UPPERCASE.$LOWERCASE.$NUMBER;
120             $password="";
121
122                 $lenght='6';
123             // Create random characters for the ones that doesnt have requirements
124             for ($i=0;$i<$lenght-$condition;$i++)  // loop and create password
125                $password = $password . substr ($charBKT, rand() % strlen($charBKT), 1);
126
127     }
128
129 ///////////////////////////////////////////////////
130 ///////  Create URL
131
132 // if i need to generate a link
133 if (isset($_POST['link']) && $_POST['link'] == '1'){
134         global $timedate;
135         $guid=create_guid();
136         $url=$GLOBALS['sugar_config']['site_url']."/index.php?entryPoint=Changenewpassword&guid=$guid";
137         $time_now=gmdate($GLOBALS['timedate']->get_db_date_time_format());
138         //$q2="UPDATE `users_password_link` SET `deleted` = '1' WHERE `username` = '".$_POST['username']."'";
139         //$usr->db->query($q2);
140         $q = "INSERT INTO users_password_link (id, username, date_generated) VALUES('".$guid."','".$_POST['username']."',' ".$time_now."' ) ";
141         $usr->db->query($q);
142 }
143 ///////
144 ///////////////////////////////////////////////////
145
146 ///////  Email creation
147         global $sugar_config, $current_user;
148     if (isset($_POST['link']) && $_POST['link'] == '1')
149         $emailTemp_id = $res['lostpasswordtmpl'];
150     else
151         $emailTemp_id = $res['generatepasswordtmpl'];
152
153     $emailTemp = new EmailTemplate();
154     $emailTemp->disable_row_level_security = true;
155     if ($emailTemp->retrieve($emailTemp_id) == ''){
156         echo $mod_strings['LBL_EMAIL_TEMPLATE_MISSING'];
157         $new_pwd='4';
158         return;}
159
160     //replace instance variables in email templates
161     $htmlBody = $emailTemp->body_html;
162     $body = $emailTemp->body;
163     if (isset($_POST['link']) && $_POST['link'] == '1'){
164         $htmlBody = str_replace('$contact_user_link_guid', $url, $htmlBody);
165         $body = str_replace('$contact_user_link_guid', $url, $body);
166     }
167     else{
168         $htmlBody = str_replace('$contact_user_user_hash', $password, $htmlBody);
169         $body = str_replace('$contact_user_user_hash', $password, $body);
170     }
171     $htmlBody = str_replace('$contact_user_user_name', $usr->user_name, $htmlBody);
172     $htmlBody = str_replace('$contact_user_pwd_last_changed', gmdate($GLOBALS['timedate']->get_db_date_time_format()), $htmlBody);
173     $body = str_replace('$contact_user_user_name', $usr->user_name, $body);
174     $body = str_replace('$contact_user_pwd_last_changed', gmdate($GLOBALS['timedate']->get_db_date_time_format()), $body);
175     $emailTemp->body_html = $htmlBody;
176     $emailTemp->body = $body;
177     require_once('include/SugarPHPMailer.php');
178
179     $itemail=$usr->emailAddress->getPrimaryAddress($usr);
180     //retrieve IT Admin Email
181     //_ppd( $emailTemp->body_html);
182     //retrieve email defaults
183     $emailObj = new Email();
184     $defaults = $emailObj->getSystemDefaultEmail();
185     $mail = new SugarPHPMailer();
186     $mail->setMailerForSystem();
187     //$mail->IsHTML(true);
188     $mail->From = $defaults['email'];
189     $mail->FromName = $defaults['name'];
190     $mail->ClearAllRecipients();
191     $mail->ClearReplyTos();
192     $mail->Subject=from_html($emailTemp->subject);
193     if($emailTemp->text_only != 1){
194         $mail->IsHTML(true);
195         $mail->Body=from_html($emailTemp->body_html);
196         $mail->AltBody=from_html($emailTemp->body);
197     }
198     else {
199         $mail->Body_html=from_html($emailTemp->body_html);
200         $mail->Body=from_html($emailTemp->body);
201     }
202     if($mail->Body == '' && $current_user->is_admin){
203         echo $app_strings['LBL_EMAIL_TEMPLATE_EDIT_PLAIN_TEXT'];
204         $new_pwd='4';
205         return;}
206     if($mail->Mailer == 'smtp' && $mail->Host ==''&& $current_user->is_admin){
207         echo $mod_strings['ERR_SERVER_SMTP_EMPTY'];
208         $new_pwd='4';
209         return;}
210
211     $mail->prepForOutbound();
212     $hasRecipients = false;
213
214     if (!empty($itemail)){
215         if($hasRecipients){
216             $mail->AddBCC($itemail);
217         }else{
218             $mail->AddAddress($itemail);
219         }
220         $hasRecipients = true;
221     }
222     $success = false;
223     if($hasRecipients){
224         $success = @$mail->Send();
225     }
226
227     //now create email
228     if($success){
229
230         $emailObj->team_id = 1;
231         $emailObj->to_addrs= '';
232         $emailObj->type= 'archived';
233         $emailObj->deleted = '0';
234         $emailObj->name = $mail->Subject ;
235         $emailObj->description = $mail->Body;
236         $emailObj->description_html =null;
237         $emailObj->from_addr = $mail->From;
238         $emailObj->parent_type = 'User';
239         $emailObj->date_sent =gmdate($GLOBALS['timedate']->get_db_date_time_format());
240         $emailObj->modified_user_id = '1';
241         $emailObj->created_by = '1';
242         $emailObj->status='sent';
243         $retId = $emailObj->save();
244         echo '1';
245         if (!isset($_POST['link'])){
246                 $user_hash = strtolower(md5($password));
247                 $usr->setPreference('loginexpiration','0');
248                 $usr->setPreference('lockout','');
249                 $usr->setPreference('loginfailed','0');
250                 $usr->savePreferencesToDB();
251                 //set new password
252                 $now=gmdate("Y-m-d H:i:s");
253                 $query = "UPDATE $usr->table_name SET user_hash='$user_hash', system_generated_password='1', pwd_last_changed='$now' where id='$usr->id'";
254                 $usr->db->query($query, true, "Error setting new password for $usr->user_name: ");
255                 echo $password;
256         }
257     }else{
258         $new_pwd='4';
259         if ($current_user->is_admin){
260                 $email_errors=$mod_strings['ERR_EMAIL_NOT_SENT_ADMIN'];
261                 if ($mail->Mailer == 'smtp')
262                         $email_errors.="\n-".$mod_strings['ERR_SMTP_URL_SMTP_PORT'];
263                 if ($mail->SMTPAuth)
264                         $email_errors.="\n-".$mod_strings['ERR_SMTP_USERNAME_SMTP_PASSWORD'];
265                 $email_errors.="\n-".$mod_strings['ERR_RECIPIENT_EMAIL'];
266                 $email_errors.="\n-".$mod_strings['ERR_SERVER_STATUS'];
267                 echo $email_errors;
268         }
269         else
270                 echo $mod_strings['LBL_EMAIL_NOT_SENT'];
271     }
272     return;
273
274 ?>