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
43 return _("PasswordReset");
46 function getDescription()
48 return _("Allow admin to reset any users password, allow user to request his password by e-mail.");
51 function getDefaultArguments()
53 return array('user' => '');
56 /* reset password, verified */
57 function doReset($userid)
60 $user = WikiUser($userid);
61 $prefs = $user->getPreferences();
62 $prefs->set('passwd', '');
63 if ($user->setPreferences($prefs)) {
64 $alert = new Alert(_("Message"),
65 fmt("The password for user %s has been deleted.", $userid));
67 $alert = new Alert(_("Error"),
68 fmt("The password for user %s could not be deleted.", $userid));
73 function doEmail(&$request, $userid)
76 $thisuser = WikiUser($userid);
77 $prefs = $thisuser->getPreferences();
78 $email = $prefs->get('email');
79 $passwd = $prefs->get('passwd'); // plain?
80 $from = $request->_user->getId() . '@' . $request->get('REMOTE_HOST');
82 "[" . WIKI_NAME . "] PasswortReset",
83 "PasswortReset requested by $from\r\n" .
84 "Password for " . WIKI_NAME . ": $passwd",
87 $alert = new Alert(_("Message"),
88 fmt("E-mail sent to the stored e-mail address for user %s", $userid));
90 $alert = new Alert(_("Error"),
91 fmt("Error sending e-mail with password for user %s.", $userid));
95 function doForm(&$request, $userid = '', $header = '', $footer = '')
97 $post_args = $request->getArg('admin_reset');
99 $header = HTML::p(_("Reset password of user: "),
101 HTML::input(array('type' => 'text',
107 $isadmin = $request->_user->isAdmin();
108 $footer = HTML::p(Button('submit:admin_reset[reset]',
109 $isadmin ? _("Yes") : _("Send e-mail"),
110 $isadmin ? 'wikiadmin' : 'button'),
112 Button('submit:admin_reset[cancel]', _("Cancel"), 'button'));
114 return HTML::form(array('action' => $request->getPostURL(),
117 HiddenInputs($request->getArgs(), false, array('admin_reset', 'user')),
118 ENABLE_PAGEPERM ? '' : HiddenInputs(array('require_authority_for_post' => WIKIAUTH_ADMIN)),
122 function run($dbi, $argstr, &$request, $basepage)
124 $args = $this->getArgs($argstr, $request);
125 if (isa($request, 'MockRequest'))
128 $user =& $request->_user;
129 $post_args = $request->getArg('admin_reset');
130 $userid = $args['user'];
131 if (!$userid) $userid = $request->getArg('user');
132 $isadmin = $user->isAdmin();
133 if ($request->isPost()) {
134 @$reset = $post_args['reset'];
136 return $this->doForm($request, $userid);
138 $alert = new Alert(_("Warning:"),
139 _("You need to specify the userid!"));
141 return $this->doForm($request);
143 if ($userid and !empty($post_args['verify'])) {
144 if ($user->isAdmin()) {
145 return $this->doReset($userid);
147 return $this->doEmail($request, $userid);
149 } elseif (empty($post_args['verify'])) {
150 //TODO: verify should check if the user exists, his prefs can be read/safed
151 // and the email is verified, even if admin.
152 $buttons = HTML::p(Button('submit:admin_reset[reset]',
153 $isadmin ? _("Yes") : _("Send e-mail"),
154 $isadmin ? 'wikiadmin' : 'button'),
156 Button('submit:admin_reset[cancel]', _("Cancel"), 'button'));
157 $header = HTML::strong("Verify");
158 if (!$user->isAdmin()) {
160 if ($userid == $user->UserName() and $user->isAuthenticated()) {
161 $alert = new Alert(_("Already logged in"),
162 HTML(fmt("Changing passwords is done at "), WikiLink(_("UserPreferences"))));
166 $thisuser = WikiUser($userid);
167 $prefs = $thisuser->getPreferences();
168 $email = $prefs->get('email');
170 $alert = new Alert(_("Error"),
171 HTML(fmt("No e-mail stored for user %s.", $userid),
173 fmt("You need to ask an Administrator to reset this password. See below: "),
174 HTML::br(), WikiLink(ADMIN_USER)));
178 $verified = $thisuser->_prefs->_prefs['email']->getraw('emailVerified');
180 $header->pushContent(HTML::br(), "Warning: This users email address is unverified!");
182 return $this->doForm($request, $userid,
185 fmt("Do you really want to reset the password of user %s?", $userid),
186 $isadmin ? '' : _("An e-mail will be sent."),
187 HiddenInputs(array('admin_reset[verify]' => 1, 'user' => $userid)),
189 } else { // verify ok, but no userid
190 return $this->doForm($request, $userid);
193 return $this->doForm($request, $userid);
202 // c-hanging-comment-ender-p: nil
203 // indent-tabs-mode: nil