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