]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/PasswordReset.php
Remove rcs_id
[SourceForge/phpwiki.git] / lib / plugin / PasswordReset.php
1 <?php // -*-php-*-
2 // $Id$
3 /**
4  * Copyright (C) 2006 $ThePhpWikiProgrammingTeam
5  *
6  * This file is part of PhpWiki.
7  *
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.
12  *
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.
17  *
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
21  */
22
23 /**
24  * 1. User forgot password but has email in the prefs.
25  *    => action=email&user=username will send the password per email in plaintext.
26  *
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
31  *
32  *    If no email exists but is not verified,
33  *    => "Warning: This users email address is unverified!"
34  *
35  * 2. Admin may reset any users password, with verification.
36  *    => action=reset&user=username
37  */
38 class WikiPlugin_PasswordReset
39 extends WikiPlugin
40 {
41     function getName () {
42         return _("PasswordReset");
43     }
44
45     function getDescription () {
46         return _("Allow admin to reset any users password, allow user to request his password by e-mail.");
47     }
48
49     function getDefaultArguments() {
50         return array('user' => '');
51     }
52
53     /* reset password, verified */
54     function doReset($userid) {
55
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));
62         } else {
63             $alert = new Alert(_("Error"),
64                                fmt("The password for user %s could not be deleted.", $userid));
65         }
66         $alert->show();
67     }
68
69     function doEmail(&$request, $userid) {
70
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');
76         if (mail($email,
77                  "[".WIKI_NAME."] PasswortReset",
78                  "PasswortReset requested by $from\r\n".
79                  "Password for ".WIKI_NAME.": $passwd",
80                  "From: $from"))
81             $alert = new Alert(_("Message"),
82                                fmt("Email sent to the stored email address for user %s", $userid));
83         else
84             $alert = new Alert(_("Error"),
85                                fmt("Error sending email with password for user %s.", $userid));
86         $alert->show();
87     }
88
89     function doForm(&$request, $userid='', $header = '', $footer = '') {
90         $post_args = $request->getArg('admin_reset');
91         if (!$header) {
92             $header = HTML::p(_("Reset password of user: "),
93                               HTML::Raw('&nbsp;'),
94                               HTML::input(array('type' => 'text',
95                                                 'name' => "user",
96                                                 'value' => $userid))
97                               );
98         }
99         if (!$footer) {
100             $isadmin = $request->_user->isAdmin();
101             $footer = HTML::p(Button('submit:admin_reset[reset]',
102                                       $isadmin ? _("Yes") : _("Send email"),
103                                       $isadmin ? 'wikiadmin' : 'button'),
104                                HTML::Raw('&nbsp;'),
105                                Button('submit:admin_reset[cancel]', _("Cancel"), 'button'));
106         }
107         return HTML::form(array('action' => $request->getPostURL(),
108                                 'method' => 'post'),
109                           $header,
110                           HiddenInputs($request->getArgs(), false, array('admin_reset', 'user')),
111                           ENABLE_PAGEPERM ? '' : HiddenInputs(array('require_authority_for_post' => WIKIAUTH_ADMIN)),
112                           $footer );
113     }
114
115     function run($dbi, $argstr, &$request, $basepage) {
116         $args = $this->getArgs($argstr, $request);
117         if (isa($request,'MockRequest'))
118             return '';
119
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'];
127             if (empty($reset))
128                 return $this->doForm($request, $userid);
129             if (!$userid) {
130                 $alert = new Alert(_("Warning:"),
131                                    _("You need to specify the userid!"));
132                 $alert->show();
133                 return $this->doForm($request);
134             }
135             if ($userid and !empty($post_args['verify'])) {
136                 if ($user->isAdmin()) {
137                     return $this->doReset($userid);
138                 } else {
139                     return $this->doEmail($request, $userid);
140                 }
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'),
147                                    HTML::Raw('&nbsp;'),
148                                    Button('submit:admin_reset[cancel]', _("Cancel"), 'button'));
149                 $header = HTML::strong("Verify");
150                 if (!$user->isAdmin()) {
151                     // check for email
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"))));
155                         $alert->show();
156                         return;
157                     }
158                     $thisuser = WikiUser($userid);
159                     $prefs = $thisuser->getPreferences();
160                     $email = $prefs->get('email');
161                     if (!$email) {
162                         $alert = new Alert(_("Error"),
163                                            HTML(fmt("No email stored for user %s.", $userid),
164                                                 HTML::br(),
165                                                 fmt("You need to ask an Administrator to reset this password. See below: "),
166                                                 HTML::br(), WikiLink(ADMIN_USER)));
167                         $alert->show();
168                         return;
169                     }
170                     $verified = $thisuser->_prefs->_prefs['email']->getraw('emailVerified');
171                     if (!$verified)
172                         $header->pushContent(HTML::br(), "Warning: This users email address is unverified!");
173                 }
174                 return $this->doForm($request, $userid,
175                                      $header,
176                                      HTML(HTML::hr(),
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)),
180                                           $buttons));
181             } else { // verify ok, but no userid
182                 return $this->doForm($request, $userid);
183             }
184         } else {
185             return $this->doForm($request, $userid);
186         }
187     }
188 };
189
190 // Local Variables:
191 // mode: php
192 // tab-width: 8
193 // c-basic-offset: 4
194 // c-hanging-comment-ender-p: nil
195 // indent-tabs-mode: nil
196 // End:
197 ?>