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 getDefaultArguments() {
50 return array('user' => '');
53 /* reset password, verified */
54 function doReset($userid) {
56 $user = WikiUser($userid);
57 $prefs = $user->getPreferences();
58 $prefs->set('passwd','');
59 if ($user->setPreferences($prefs)) {
60 $alert = new Alert(_("Message"),
61 fmt("The password for user %s has been deleted.", $userid));
63 $alert = new Alert(_("Error"),
64 fmt("The password for user %s could not be deleted.", $userid));
69 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",
81 $alert = new Alert(_("Message"),
82 fmt("Email sent to the stored email address for user %s", $userid));
84 $alert = new Alert(_("Error"),
85 fmt("Error sending email with password for user %s.", $userid));
89 function doForm(&$request, $userid='', $header = '', $footer = '') {
90 $post_args = $request->getArg('admin_reset');
92 $header = HTML::p(_("Reset password of user: "),
94 HTML::input(array('type' => 'text',
100 $isadmin = $request->_user->isAdmin();
101 $footer = HTML::p(Button('submit:admin_reset[reset]',
102 $isadmin ? _("Yes") : _("Send email"),
103 $isadmin ? 'wikiadmin' : 'button'),
105 Button('submit:admin_reset[cancel]', _("Cancel"), 'button'));
107 return HTML::form(array('action' => $request->getPostURL(),
110 HiddenInputs($request->getArgs(), false, array('admin_reset', 'user')),
111 ENABLE_PAGEPERM ? '' : HiddenInputs(array('require_authority_for_post' => WIKIAUTH_ADMIN)),
115 function run($dbi, $argstr, &$request, $basepage) {
116 $args = $this->getArgs($argstr, $request);
117 if (isa($request,'MockRequest'))
120 $user =& $request->_user;
121 $post_args = $request->getArg('admin_reset');
122 $userid = $args['user'];
123 if (!$userid) $userid = $request->getArg('user');
124 $isadmin = $user->isAdmin();
125 if ($request->isPost()) {
126 @$reset = $post_args['reset'];
128 return $this->doForm($request, $userid);
130 $alert = new Alert(_("Warning:"),
131 _("You need to specify the userid!"));
133 return $this->doForm($request);
135 if ($userid and !empty($post_args['verify'])) {
136 if ($user->isAdmin()) {
137 return $this->doReset($userid);
139 return $this->doEmail($request, $userid);
141 } elseif (empty($post_args['verify'])) {
142 //TODO: verify should check if the user exists, his prefs can be read/safed
143 // and the email is verified, even if admin.
144 $buttons = HTML::p(Button('submit:admin_reset[reset]',
145 $isadmin ? _("Yes") : _("Send email"),
146 $isadmin ? 'wikiadmin' : 'button'),
148 Button('submit:admin_reset[cancel]', _("Cancel"), 'button'));
149 $header = HTML::strong("Verify");
150 if (!$user->isAdmin()) {
152 if ($userid == $user->UserName() and $user->isAuthenticated()) {
153 $alert = new Alert(_("Already logged in"),
154 HTML(fmt("Changing passwords is done at "), WikiLink(_("UserPreferences"))));
158 $thisuser = WikiUser($userid);
159 $prefs = $thisuser->getPreferences();
160 $email = $prefs->get('email');
162 $alert = new Alert(_("Error"),
163 HTML(fmt("No email stored for user %s.", $userid),
165 fmt("You need to ask an Administrator to reset this password. See below: "),
166 HTML::br(), WikiLink(ADMIN_USER)));
170 $verified = $thisuser->_prefs->_prefs['email']->getraw('emailVerified');
172 $header->pushContent(HTML::br(), "Warning: This users email address is unverified!");
174 return $this->doForm($request, $userid,
177 fmt("Do you really want to reset the password of user %s?", $userid),
178 $isadmin ? '' : _("An email will be sent."),
179 HiddenInputs(array('admin_reset[verify]' => 1, 'user' => $userid)),
181 } else { // verify ok, but no userid
182 return $this->doForm($request, $userid);
185 return $this->doForm($request, $userid);
194 // c-hanging-comment-ender-p: nil
195 // indent-tabs-mode: nil