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