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