]> git.mxchange.org Git - friendica.git/blob - mod/lostpass.php
Merge branch 'develop' into rewrites/dbm_is_result
[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
83         if(x($_GET,'verify')) {
84                 $verify = $_GET['verify'];
85                 $hash = hash('whirlpool', $verify);
86
87                 $r = q("SELECT * FROM `user` WHERE `pwdreset` = '%s' LIMIT 1",
88                         dbesc($hash)
89                 );
90                 if (! dbm::is_result($r)) {
91                         $o =  t("Request could not be verified. \x28You may have previously submitted it.\x29 Password reset failed.");
92                         return $o;
93                 }
94                 $uid = $r[0]['uid'];
95                 $username = $r[0]['username'];
96                 $email = $r[0]['email'];
97
98                 $new_password = autoname(6) . mt_rand(100,9999);
99                 $new_password_encoded = hash('whirlpool',$new_password);
100
101                 $r = q("UPDATE `user` SET `password` = '%s', `pwdreset` = ''  WHERE `uid` = %d",
102                         dbesc($new_password_encoded),
103                         intval($uid)
104                 );
105
106                 /// @TODO Is dbm::is_result() okay here?
107                 if ($r) {
108                         $tpl = get_markup_template('pwdreset.tpl');
109                         $o .= replace_macros($tpl,array(
110                                 '$lbl1' => t('Password Reset'),
111                                 '$lbl2' => t('Your password has been reset as requested.'),
112                                 '$lbl3' => t('Your new password is'),
113                                 '$lbl4' => t('Save or copy your new password - and then'),
114                                 '$lbl5' => '<a href="' . App::get_baseurl() . '">' . t('click here to login') . '</a>.',
115                                 '$lbl6' => t('Your password may be changed from the <em>Settings</em> page after successful login.'),
116                                 '$newpass' => $new_password,
117                                 '$baseurl' => App::get_baseurl()
118
119                         ));
120                                 info("Your password has been reset." . EOL);
121
122
123                         $sitename = $a->config['sitename'];
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, App::get_baseurl(), $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 }