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 = '')
98 $header = HTML::p(_("Reset password of user: "),
100 HTML::input(array('type' => 'text',
106 $isadmin = $request->_user->isAdmin();
107 $footer = HTML::p(Button('submit:admin_reset[reset]',
108 $isadmin ? _("Yes") : _("Send e-mail"),
109 $isadmin ? 'wikiadmin' : 'button'),
111 Button('submit:admin_reset[cancel]', _("Cancel"), 'button'));
113 return HTML::form(array('action' => $request->getPostURL(),
116 HiddenInputs($request->getArgs(), false, array('admin_reset', 'user')),
117 ENABLE_PAGEPERM ? '' : HiddenInputs(array('require_authority_for_post' => WIKIAUTH_ADMIN)),
121 function run($dbi, $argstr, &$request, $basepage)
123 $args = $this->getArgs($argstr, $request);
124 if (isa($request, 'MockRequest'))
127 $user =& $request->_user;
128 $post_args = $request->getArg('admin_reset');
129 $userid = $args['user'];
130 if (!$userid) $userid = $request->getArg('user');
131 $isadmin = $user->isAdmin();
132 if ($request->isPost()) {
133 @$reset = $post_args['reset'];
135 return $this->doForm($request, $userid);
137 $alert = new Alert(_("Warning:"),
138 _("You need to specify the userid!"));
140 return $this->doForm($request);
142 if ($userid and !empty($post_args['verify'])) {
143 if ($user->isAdmin()) {
144 $this->doReset($userid);
147 $this->doEmail($request, $userid);
150 } elseif (empty($post_args['verify'])) {
151 //TODO: verify should check if the user exists, his prefs can be read/safed
152 // and the email is verified, even if admin.
153 $buttons = HTML::p(Button('submit:admin_reset[reset]',
154 $isadmin ? _("Yes") : _("Send e-mail"),
155 $isadmin ? 'wikiadmin' : 'button'),
157 Button('submit:admin_reset[cancel]', _("Cancel"), 'button'));
158 $header = HTML::strong("Verify");
159 if (!$user->isAdmin()) {
161 if ($userid == $user->UserName() and $user->isAuthenticated()) {
162 $alert = new Alert(_("Already logged in"),
163 HTML(fmt("Changing passwords is done at "), WikiLink(_("UserPreferences"))));
167 $thisuser = WikiUser($userid);
168 $prefs = $thisuser->getPreferences();
169 $email = $prefs->get('email');
171 $alert = new Alert(_("Error"),
172 HTML(fmt("No e-mail stored for user %s.", $userid),
174 fmt("You need to ask an Administrator to reset this password. See below: "),
175 HTML::br(), WikiLink(ADMIN_USER)));
179 $verified = $thisuser->_prefs->_prefs['email']->getraw('emailVerified');
181 $header->pushContent(HTML::br(), "Warning: This users email address is unverified!");
183 return $this->doForm($request, $userid,
186 fmt("Do you really want to reset the password of user %s?", $userid),
187 $isadmin ? '' : _("An e-mail will be sent."),
188 HiddenInputs(array('admin_reset[verify]' => 1, 'user' => $userid)),
190 } else { // verify ok, but no userid
191 return $this->doForm($request, $userid);
194 return $this->doForm($request, $userid);
203 // c-hanging-comment-ender-p: nil
204 // indent-tabs-mode: nil