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