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
19 along with PhpWiki; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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 email 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
42 return _("PasswordReset");
45 function getDescription () {
46 return _("Allow admin to reset any users password, allow user to request his password by e-mail.");
49 function getVersion() {
50 return preg_replace("/[Revision: $]/", '',
54 function getDefaultArguments() {
55 return array('user' => '');
58 /* reset password, verified */
59 function doReset($userid) {
61 $user = WikiUser($userid);
62 $prefs = $user->getPreferences();
63 $prefs->set('passwd','');
64 if ($user->setPreferences($prefs)) {
65 $alert = new Alert(_("Message"),
66 fmt("The password for user %s has been deleted.", $userid));
68 $alert = new Alert(_("Error"),
69 fmt("The password for user %s could not be deleted.", $userid));
74 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",
86 $alert = new Alert(_("Message"),
87 fmt("Email sent to the stored email address for user %s", $userid));
89 $alert = new Alert(_("Error"),
90 fmt("Error sending email with password for user %s.", $userid));
94 function doForm(&$request, $userid='', $header = '', $footer = '') {
95 $post_args = $request->getArg('admin_reset');
97 $header = HTML::p(_("Reset password of user: "),
99 HTML::input(array('type' => 'text',
105 $isadmin = $request->_user->isAdmin();
106 $footer = HTML::p(Button('submit:admin_reset[reset]',
107 $isadmin ? _("Yes") : _("Send email"),
108 $isadmin ? 'wikiadmin' : 'button'),
110 Button('submit:admin_reset[cancel]', _("Cancel"), 'button'));
112 return HTML::form(array('action' => $request->getPostURL(),
115 HiddenInputs($request->getArgs(), false, array('admin_reset', 'user')),
116 ENABLE_PAGEPERM ? '' : HiddenInputs(array('require_authority_for_post' => WIKIAUTH_ADMIN)),
120 function run($dbi, $argstr, &$request, $basepage) {
121 $args = $this->getArgs($argstr, $request);
122 if (isa($request,'MockRequest'))
125 $user =& $request->_user;
126 $post_args = $request->getArg('admin_reset');
127 $userid = $args['user'];
128 if (!$userid) $userid = $request->getArg('user');
129 $isadmin = $user->isAdmin();
130 if ($request->isPost()) {
131 @$reset = $post_args['reset'];
133 return $this->doForm($request, $userid);
135 $alert = new Alert(_("Warning:"),
136 _("You need to specify the userid!"));
138 return $this->doForm($request);
140 if ($userid and !empty($post_args['verify'])) {
141 if ($user->isAdmin()) {
142 return $this->doReset($userid);
144 return $this->doEmail($request, $userid);
146 } elseif (empty($post_args['verify'])) {
147 //TODO: verify should check if the user exists, his prefs can be read/safed
148 // and the email is verified, even if admin.
149 $buttons = HTML::p(Button('submit:admin_reset[reset]',
150 $isadmin ? _("Yes") : _("Send email"),
151 $isadmin ? 'wikiadmin' : 'button'),
153 Button('submit:admin_reset[cancel]', _("Cancel"), 'button'));
154 $header = HTML::strong("Verify");
155 if (!$user->isAdmin()) {
157 if ($userid == $user->UserName() and $user->isAuthenticated()) {
158 $alert = new Alert(_("Already logged in"),
159 HTML(fmt("Changing passwords is done at "), WikiLink(_("UserPreferences"))));
163 $thisuser = WikiUser($userid);
164 $prefs = $thisuser->getPreferences();
165 $email = $prefs->get('email');
167 $alert = new Alert(_("Error"),
168 HTML(fmt("No email stored for user %s.", $userid),
170 fmt("You need to ask an Administrator to reset this password. See below: "),
171 HTML::br(), WikiLink(ADMIN_USER)));
175 $verified = $thisuser->_prefs->_prefs['email']->getraw('emailVerified');
177 $header->pushContent(HTML::br(), "Warning: This users email address is unverified!");
179 return $this->doForm($request, $userid,
182 fmt("Do you really want to reset the password of user %s?", $userid),
183 $isadmin ? '' : _("An email will be sent."),
184 HiddenInputs(array('admin_reset[verify]' => 1, 'user' => $userid)),
186 } else { // verify ok, but no userid
187 return $this->doForm($request, $userid);
190 return $this->doForm($request, $userid);
200 // c-hanging-comment-ender-p: nil
201 // indent-tabs-mode: nil