]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/PasswordReset.php
private
[SourceForge/phpwiki.git] / lib / plugin / PasswordReset.php
1 <?php
2
3 /**
4  * Copyright (C) 2006 $ThePhpWikiProgrammingTeam
5  *
6  * This file is part of PhpWiki.
7  *
8  * PhpWiki is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * PhpWiki is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License along
19  * with PhpWiki; if not, write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21  */
22
23 /**
24  * 1. User forgot password but has email in the prefs.
25  *    => action=email&user=username will send the password per email in plaintext.
26  *
27  *    If no email is stored, because user might not exist,
28  *    => "No e-mail stored for user %s.
29  *        You need to ask an Administrator to reset this password."
30  *       Problem: How to contact Admin? Present a link to ADMIN_USER
31  *
32  *    If no email exists but is not verified,
33  *    => "Warning: This users email address is unverified!"
34  *
35  * 2. Admin may reset any users password, with verification.
36  *    => action=reset&user=username
37  */
38 class WikiPlugin_PasswordReset
39     extends WikiPlugin
40 {
41     function getDescription()
42     {
43         return _("Allow admin to reset any users password, allow user to request his password by e-mail.");
44     }
45
46     function getDefaultArguments()
47     {
48         return array('user' => '');
49     }
50
51     /* reset password, verified */
52     private function doReset($userid)
53     {
54
55         $user = WikiUser($userid);
56         $prefs = $user->getPreferences();
57         $prefs->set('passwd', '');
58         if ($user->setPreferences($prefs)) {
59             $alert = new Alert(_("Message"),
60                 fmt("The password for user %s has been deleted.", $userid));
61         } else {
62             $alert = new Alert(_("Error"),
63                 fmt("The password for user %s could not be deleted.", $userid));
64         }
65         $alert->show();
66     }
67
68     private function doEmail(&$request, $userid)
69     {
70
71         $thisuser = WikiUser($userid);
72         $prefs = $thisuser->getPreferences();
73         $email = $prefs->get('email');
74         $passwd = $prefs->get('passwd'); // plain?
75         $from = $request->_user->getId() . '@' . $request->get('REMOTE_HOST');
76         if (mail($email,
77             "[" . WIKI_NAME . "] PasswortReset",
78             "PasswortReset requested by $from\r\n" .
79                 "Password for " . WIKI_NAME . ": $passwd",
80             "From: $from")
81         )
82             $alert = new Alert(_("Message"),
83                 fmt("E-mail sent to the stored e-mail address for user %s", $userid));
84         else
85             $alert = new Alert(_("Error"),
86                 fmt("Error sending e-mail with password for user %s.", $userid));
87         $alert->show();
88     }
89
90     private function doForm(&$request, $userid = '', $header = '', $footer = '')
91     {
92         if (!$header) {
93             $header = HTML::p(_("Reset password of user: "),
94                 HTML::raw('&nbsp;'),
95                 HTML::input(array('type' => 'text',
96                     'name' => "user",
97                     'value' => $userid))
98             );
99         }
100         if (!$footer) {
101             $isadmin = $request->_user->isAdmin();
102             $footer = HTML::p(Button('submit:admin_reset[reset]',
103                     $isadmin ? _("Yes") : _("Send e-mail"),
104                     $isadmin ? 'wikiadmin' : 'button'),
105                 HTML::raw('&nbsp;'),
106                 Button('submit:admin_reset[cancel]', _("Cancel"), 'button'));
107         }
108         return HTML::form(array('action' => $request->getPostURL(),
109                 'method' => 'post'),
110             $header,
111             HiddenInputs($request->getArgs(), false, array('admin_reset', 'user')),
112             ENABLE_PAGEPERM ? '' : HiddenInputs(array('require_authority_for_post' => WIKIAUTH_ADMIN)),
113             $footer);
114     }
115
116     function run($dbi, $argstr, &$request, $basepage)
117     {
118         $args = $this->getArgs($argstr, $request);
119         if (isa($request, 'MockRequest'))
120             return '';
121
122         $user =& $request->_user;
123         $post_args = $request->getArg('admin_reset');
124         $userid = $args['user'];
125         if (!$userid) $userid = $request->getArg('user');
126         $isadmin = $user->isAdmin();
127         if ($request->isPost()) {
128             @$reset = $post_args['reset'];
129             if (empty($reset))
130                 return $this->doForm($request, $userid);
131             if (!$userid) {
132                 $alert = new Alert(_("Warning:"),
133                     _("You need to specify the userid!"));
134                 $alert->show();
135                 return $this->doForm($request);
136             }
137             if ($userid and !empty($post_args['verify'])) {
138                 if ($user->isAdmin()) {
139                     $this->doReset($userid);
140                     return '';
141                 } else {
142                     $this->doEmail($request, $userid);
143                     return '';
144                 }
145             } elseif (empty($post_args['verify'])) {
146                 //TODO: verify should check if the user exists, his prefs can be read/safed
147                 //      and the email is verified, even if admin.
148                 $buttons = HTML::p(Button('submit:admin_reset[reset]',
149                         $isadmin ? _("Yes") : _("Send e-mail"),
150                         $isadmin ? 'wikiadmin' : 'button'),
151                     HTML::raw('&nbsp;'),
152                     Button('submit:admin_reset[cancel]', _("Cancel"), 'button'));
153                 $header = HTML::strong("Verify");
154                 if (!$user->isAdmin()) {
155                     // check for email
156                     if ($userid == $user->UserName() and $user->isAuthenticated()) {
157                         $alert = new Alert(_("Already logged in"),
158                             HTML(fmt("Changing passwords is done at "), WikiLink(_("UserPreferences"))));
159                         $alert->show();
160                         return '';
161                     }
162                     $thisuser = WikiUser($userid);
163                     $prefs = $thisuser->getPreferences();
164                     $email = $prefs->get('email');
165                     if (!$email) {
166                         $alert = new Alert(_("Error"),
167                             HTML(fmt("No e-mail stored for user %s.", $userid),
168                                 HTML::br(),
169                                 fmt("You need to ask an Administrator to reset this password. See below: "),
170                                 HTML::br(), WikiLink(ADMIN_USER)));
171                         $alert->show();
172                         return '';
173                     }
174                     $verified = $thisuser->_prefs->_prefs['email']->getraw('emailVerified');
175                     if (!$verified)
176                         $header->pushContent(HTML::br(), "Warning: This users email address is unverified!");
177                 }
178                 return $this->doForm($request, $userid,
179                     $header,
180                     HTML(HTML::hr(),
181                         fmt("Do you really want to reset the password of user %s?", $userid),
182                         $isadmin ? '' : _("An e-mail will be sent."),
183                         HiddenInputs(array('admin_reset[verify]' => 1, 'user' => $userid)),
184                         $buttons));
185             } else { // verify ok, but no userid
186                 return $this->doForm($request, $userid);
187             }
188         } else {
189             return $this->doForm($request, $userid);
190         }
191     }
192 }
193
194 // Local Variables:
195 // mode: php
196 // tab-width: 8
197 // c-basic-offset: 4
198 // c-hanging-comment-ender-p: nil
199 // indent-tabs-mode: nil
200 // End: