]> git.mxchange.org Git - friendica.git/blob - mod/lostpass.php
Bugfix: The user id wasn't fetched correctly.
[friendica.git] / mod / lostpass.php
1 <?php
2
3 require_once('include/email.php');
4 require_once('include/enotify.php');
5 require_once('include/text.php');
6
7 function lostpass_post(&$a) {
8
9         $loginame = notags(trim($_POST['login-name']));
10         if(! $loginame)
11                 goaway(z_root());
12
13         $r = q("SELECT * FROM `user` WHERE ( `email` = '%s' OR `nickname` = '%s' ) AND `verified` = 1 AND `blocked` = 0 LIMIT 1",
14                 dbesc($loginame),
15                 dbesc($loginame)
16         );
17
18         if(! count($r)) {
19                 notice( t('No valid account found.') . EOL);
20                 goaway(z_root());
21         }
22
23         $uid = $r[0]['uid'];
24         $username = $r[0]['username'];
25         $email = $r[0]['email'];
26
27         $new_password = autoname(12) . mt_rand(100,9999);
28         $new_password_encoded = hash('whirlpool',$new_password);
29
30         $r = q("UPDATE `user` SET `pwdreset` = '%s' WHERE `uid` = %d",
31                 dbesc($new_password_encoded),
32                 intval($uid)
33         );
34         if($r)
35                 info( t('Password reset request issued. Check your email.') . EOL);
36
37
38         $sitename = $a->config['sitename'];
39         $siteurl = $a->get_baseurl();
40         $resetlink = $a->get_baseurl() . '/lostpass?verify=' . $new_password;
41
42         $preamble = deindent(t('
43                 Dear %1$s,
44                         A request was recently received at "%2$s" to reset your account
45                 password. In order to confirm this request, please select the verification link
46                 below or paste it into your web browser address bar.
47
48                 If you did NOT request this change, please DO NOT follow the link
49                 provided and ignore and/or delete this email.
50
51                 Your password will not be changed unless we can verify that you
52                 issued this request.'));
53         $body = deindent(t('
54                 Follow this link to verify your identity:
55
56                 %1$s
57
58                 You will then receive a follow-up message containing the new password.
59                 You may change that password from your account settings page after logging in.
60
61                 The login details are as follows:
62
63                 Site Location:  %2$s
64                 Login Name:     %3$s'));
65
66         $preamble = sprintf($preamble, $username, $sitename);
67         $body = sprintf($body, $resetlink, $siteurl, $email);
68
69         notification(array(
70                 'type' => "SYSTEM_EMAIL",
71                 'to_email' => $email,
72                 'subject'=> sprintf( t('Password reset requested at %s'),$sitename),
73                 'preamble'=> $preamble,
74                 'body' => $body));
75
76         goaway(z_root());
77
78 }
79
80
81 function lostpass_content(&$a) {
82
83
84         if(x($_GET,'verify')) {
85                 $verify = $_GET['verify'];
86                 $hash = hash('whirlpool', $verify);
87
88                 $r = q("SELECT * FROM `user` WHERE `pwdreset` = '%s' LIMIT 1",
89                         dbesc($hash)
90                 );
91                 if(! count($r)) {
92                         $o =  t("Request could not be verified. \x28You may have previously submitted it.\x29 Password reset failed.");
93                         return $o;
94                 }
95                 $uid = $r[0]['uid'];
96                 $username = $r[0]['username'];
97                 $email = $r[0]['email'];
98
99                 $new_password = autoname(6) . mt_rand(100,9999);
100                 $new_password_encoded = hash('whirlpool',$new_password);
101
102                 $r = q("UPDATE `user` SET `password` = '%s', `pwdreset` = ''  WHERE `uid` = %d",
103                         dbesc($new_password_encoded),
104                         intval($uid)
105                 );
106                 if($r) {
107                         $tpl = get_markup_template('pwdreset.tpl');
108                         $o .= replace_macros($tpl,array(
109                                 '$lbl1' => t('Password Reset'),
110                                 '$lbl2' => t('Your password has been reset as requested.'),
111                                 '$lbl3' => t('Your new password is'),
112                                 '$lbl4' => t('Save or copy your new password - and then'),
113                                 '$lbl5' => '<a href="' . $a->get_baseurl() . '">' . t('click here to login') . '</a>.',
114                                 '$lbl6' => t('Your password may be changed from the <em>Settings</em> page after successful login.'),
115                                 '$newpass' => $new_password,
116                                 '$baseurl' => $a->get_baseurl()
117
118                         ));
119                                 info("Your password has been reset." . EOL);
120
121
122                         $sitename = $a->config['sitename'];
123                         $siteurl = $a->get_baseurl();
124                         // $username, $email, $new_password
125                         $preamble = deindent(t('
126                                 Dear %1$s,
127                                         Your password has been changed as requested. Please retain this
128                                 information for your records (or change your password immediately to
129                                 something that you will remember).
130                         '));
131                         $body = deindent(t('
132                                 Your login details are as follows:
133
134                                 Site Location:  %1$s
135                                 Login Name:     %2$s
136                                 Password:       %3$s
137
138                                 You may change that password from your account settings page after logging in.
139                         '));
140
141                         $preamble = sprintf($preamble, $username);
142                         $body = sprintf($body, $siteurl, $email, $new_password);
143
144                         notification(array(
145                                 'type' => "SYSTEM_EMAIL",
146                                 'to_email' => $email,
147                                 'subject'=> sprintf( t('Your password has been changed at %s'),$sitename),
148                                 'preamble'=> $preamble,
149                                 'body' => $body));
150
151                         return $o;
152                 }
153
154         }
155         else {
156                 $tpl = get_markup_template('lostpass.tpl');
157
158                 $o .= replace_macros($tpl,array(
159                         '$title' => t('Forgot your Password?'),
160                         '$desc' => t('Enter your email address and submit to have your password reset. Then check your email for further instructions.'),
161                         '$name' => t('Nickname or Email: '),
162                         '$submit' => t('Reset')
163                 ));
164
165                 return $o;
166         }
167
168 }