]> git.mxchange.org Git - friendica.git/blob - mod/lostpass.php
Merge remote-tracking branch 'upstream/develop' into rewrites/coding-convention
[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(App $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 (! dbm::is_result($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         $resetlink = App::get_baseurl() . '/lostpass?verify=' . $new_password;
40
41         $preamble = deindent(t('
42                 Dear %1$s,
43                         A request was recently received at "%2$s" to reset your account
44                 password. In order to confirm this request, please select the verification link
45                 below or paste it into your web browser address bar.
46
47                 If you did NOT request this change, please DO NOT follow the link
48                 provided and ignore and/or delete this email.
49
50                 Your password will not be changed unless we can verify that you
51                 issued this request.'));
52         $body = deindent(t('
53                 Follow this link to verify your identity:
54
55                 %1$s
56
57                 You will then receive a follow-up message containing the new password.
58                 You may change that password from your account settings page after logging in.
59
60                 The login details are as follows:
61
62                 Site Location:  %2$s
63                 Login Name:     %3$s'));
64
65         $preamble = sprintf($preamble, $username, $sitename);
66         $body = sprintf($body, $resetlink, App::get_baseurl(), $email);
67
68         notification(array(
69                 'type' => "SYSTEM_EMAIL",
70                 'to_email' => $email,
71                 'subject'=> sprintf( t('Password reset requested at %s'),$sitename),
72                 'preamble'=> $preamble,
73                 'body' => $body));
74
75         goaway(z_root());
76
77 }
78
79
80 function lostpass_content(App $a) {
81
82         if (x($_GET,'verify')) {
83                 $verify = $_GET['verify'];
84                 $hash = hash('whirlpool', $verify);
85
86                 $r = q("SELECT * FROM `user` WHERE `pwdreset` = '%s' LIMIT 1",
87                         dbesc($hash)
88                 );
89                 if (! dbm::is_result($r)) {
90                         $o =  t("Request could not be verified. \x28You may have previously submitted it.\x29 Password reset failed.");
91                         return $o;
92                 }
93                 $uid = $r[0]['uid'];
94                 $username = $r[0]['username'];
95                 $email = $r[0]['email'];
96
97                 $new_password = autoname(6) . mt_rand(100,9999);
98                 $new_password_encoded = hash('whirlpool',$new_password);
99
100                 $r = q("UPDATE `user` SET `password` = '%s', `pwdreset` = ''  WHERE `uid` = %d",
101                         dbesc($new_password_encoded),
102                         intval($uid)
103                 );
104
105                 /// @TODO Is dbm::is_result() okay here?
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="' . App::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' => App::get_baseurl()
117
118                         ));
119                                 info("Your password has been reset." . EOL);
120
121
122                         $sitename = $a->config['sitename'];
123                         // $username, $email, $new_password
124                         $preamble = deindent(t('
125                                 Dear %1$s,
126                                         Your password has been changed as requested. Please retain this
127                                 information for your records (or change your password immediately to
128                                 something that you will remember).
129                         '));
130                         $body = deindent(t('
131                                 Your login details are as follows:
132
133                                 Site Location:  %1$s
134                                 Login Name:     %2$s
135                                 Password:       %3$s
136
137                                 You may change that password from your account settings page after logging in.
138                         '));
139
140                         $preamble = sprintf($preamble, $username);
141                         $body = sprintf($body, App::get_baseurl(), $email, $new_password);
142
143                         notification(array(
144                                 'type' => "SYSTEM_EMAIL",
145                                 'to_email' => $email,
146                                 'subject'=> sprintf( t('Your password has been changed at %s'),$sitename),
147                                 'preamble'=> $preamble,
148                                 'body' => $body));
149
150                         return $o;
151                 }
152
153         }
154         else {
155                 $tpl = get_markup_template('lostpass.tpl');
156
157                 $o .= replace_macros($tpl,array(
158                         '$title' => t('Forgot your Password?'),
159                         '$desc' => t('Enter your email address and submit to have your password reset. Then check your email for further instructions.'),
160                         '$name' => t('Nickname or Email: '),
161                         '$submit' => t('Reset')
162                 ));
163
164                 return $o;
165         }
166
167 }