4 * Copyright (C) 2006 $ThePhpWikiProgrammingTeam
6 * This file is part of PhpWiki.
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.
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.
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.
24 * 1. User forgot password but has email in the prefs.
25 * => action=email&user=username will send the password per email in plaintext.
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
32 * If no email exists but is not verified,
33 * => "Warning: This users email address is unverified!"
35 * 2. Admin may reset any users password, with verification.
36 * => action=reset&user=username
38 class WikiPlugin_PasswordReset
41 function getDescription()
43 return _("Allow admin to reset any users password, allow user to request his password by e-mail.");
46 function getDefaultArguments()
48 return array('user' => '');
51 /* reset password, verified */
52 private function doReset($userid)
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));
62 $alert = new Alert(_("Error"),
63 fmt("The password for user %s could not be deleted.", $userid));
68 private function doEmail(&$request, $userid)
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');
77 "[" . WIKI_NAME . "] PasswortReset",
78 "PasswortReset requested by $from\r\n" .
79 "Password for " . WIKI_NAME . ": $passwd",
82 $alert = new Alert(_("Message"),
83 fmt("E-mail sent to the stored e-mail address for user %s", $userid));
85 $alert = new Alert(_("Error"),
86 fmt("Error sending e-mail with password for user %s.", $userid));
90 private function doForm(&$request, $userid = '', $header = '', $footer = '')
93 $header = HTML::p(_("Reset password of user: "),
95 HTML::input(array('type' => 'text',
101 $isadmin = $request->_user->isAdmin();
102 $footer = HTML::p(Button('submit:admin_reset[reset]',
103 $isadmin ? _("Yes") : _("Send e-mail"),
104 $isadmin ? 'wikiadmin' : 'button'),
106 Button('submit:admin_reset[cancel]', _("Cancel"), 'button'));
108 return HTML::form(array('action' => $request->getPostURL(),
111 HiddenInputs($request->getArgs(), false, array('admin_reset', 'user')),
112 ENABLE_PAGEPERM ? '' : HiddenInputs(array('require_authority_for_post' => WIKIAUTH_ADMIN)),
116 function run($dbi, $argstr, &$request, $basepage)
118 $args = $this->getArgs($argstr, $request);
119 if (isa($request, 'MockRequest'))
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'];
130 return $this->doForm($request, $userid);
132 $alert = new Alert(_("Warning:"),
133 _("You need to specify the userid!"));
135 return $this->doForm($request);
137 if ($userid and !empty($post_args['verify'])) {
138 if ($user->isAdmin()) {
139 $this->doReset($userid);
142 $this->doEmail($request, $userid);
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'),
152 Button('submit:admin_reset[cancel]', _("Cancel"), 'button'));
153 $header = HTML::strong("Verify");
154 if (!$user->isAdmin()) {
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"))));
162 $thisuser = WikiUser($userid);
163 $prefs = $thisuser->getPreferences();
164 $email = $prefs->get('email');
166 $alert = new Alert(_("Error"),
167 HTML(fmt("No e-mail stored for user %s.", $userid),
169 fmt("You need to ask an Administrator to reset this password. See below: "),
170 HTML::br(), WikiLink(ADMIN_USER)));
174 $verified = $thisuser->_prefs->_prefs['email']->getraw('emailVerified');
176 $header->pushContent(HTML::br(), "Warning: This users email address is unverified!");
178 return $this->doForm($request, $userid,
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)),
185 } else { // verify ok, but no userid
186 return $this->doForm($request, $userid);
189 return $this->doForm($request, $userid);
198 // c-hanging-comment-ender-p: nil
199 // indent-tabs-mode: nil