]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/PasswordReset.php
Remove history
[SourceForge/phpwiki.git] / lib / plugin / PasswordReset.php
1 <?php // -*-php-*-
2 rcs_id('$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 getVersion() {
46         return preg_replace("/[Revision: $]/", '',
47                             "\$Revision$");
48     }
49
50     function getDefaultArguments() {
51         return array('user' => '');
52     }
53
54     /* reset password, verified */
55     function doReset($userid) {
56  
57         $user = WikiUser($userid);
58         $prefs = $user->getPreferences();
59         $prefs->set('passwd','');
60         if ($user->setPreferences($prefs)) {
61             $alert = new Alert(_("Message"),
62                                fmt("The password for user %s has been deleted.", $userid));
63         } else {
64             $alert = new Alert(_("Error"),
65                                fmt("The password for user %s could not be deleted.", $userid));
66         }
67         $alert->show();
68     }
69
70     function doEmail(&$request, $userid) {
71  
72         $thisuser = WikiUser($userid);
73         $prefs = $thisuser->getPreferences();
74         $email = $prefs->get('email');
75         $passwd = $prefs->get('passwd'); // plain?
76         $from = $request->_user->getId() . '@' .  $request->get('REMOTE_HOST');
77         if (mail($email,
78                  "[".WIKI_NAME."] PasswortReset", 
79                  "PasswortReset requested by $from\r\n".
80                  "Password for ".WIKI_NAME.": $passwd",
81                  "From: $from"))
82             $alert = new Alert(_("Message"),
83                                fmt("Email sent to the stored email address for user %s", $userid));
84         else
85             $alert = new Alert(_("Error"),
86                                fmt("Error sending email with password for user %s.", $userid));
87         $alert->show();
88     }
89
90     function doForm(&$request, $userid='', $header = '', $footer = '') { 
91         $post_args = $request->getArg('admin_reset');
92         if (!$header) {
93             $header = HTML::p(_("Reset password of user: "),
94                               HTML::Raw('&nbsp;'),
95                               HTML::input(array('type' => 'text',
96                                                 'name' => "user",
97                                                 'value' => $userid))
98                               );
99         }
100         if (!$footer) {
101             $isadmin = $request->_user->isAdmin();
102             $footer = HTML::p(Button('submit:admin_reset[reset]', 
103                                       $isadmin ? _("Yes") : _("Send email"), 
104                                       $isadmin ? 'wikiadmin' : 'button'),
105                                HTML::Raw('&nbsp;'),
106                                Button('submit:admin_reset[cancel]', _("Cancel"), 'button'));
107         }
108         return HTML::form(array('action' => $request->getPostURL(),
109                                 'method' => 'post'),
110                           $header,
111                           HiddenInputs($request->getArgs(), false, array('admin_reset', 'user')),
112                           ENABLE_PAGEPERM ? '' : HiddenInputs(array('require_authority_for_post' => WIKIAUTH_ADMIN)),
113                           $footer );
114     }
115
116     function run($dbi, $argstr, &$request, $basepage) {
117         $args = $this->getArgs($argstr, $request);
118         if (isa($request,'MockRequest'))
119             return '';
120
121         $user =& $request->_user;
122         $post_args = $request->getArg('admin_reset');
123         $userid = $args['user'];
124         if (!$userid) $userid = $request->getArg('user');
125         $isadmin = $user->isAdmin();
126         if ($request->isPost()) {
127             @$reset = $post_args['reset'];
128             if (empty($reset))
129                 return $this->doForm($request, $userid);
130             if (!$userid) {
131                 $alert = new Alert(_("Warning:"),
132                                    _("You need to specify the userid!"));
133                 $alert->show();
134                 return $this->doForm($request);
135             }
136             if ($userid and !empty($post_args['verify'])) {
137                 if ($user->isAdmin()) {
138                     return $this->doReset($userid);
139                 } else {
140                     return $this->doEmail($request, $userid);
141                 }
142             } elseif (empty($post_args['verify'])) {
143                 //TODO: verify should check if the user exists, his prefs can be read/safed 
144                 //      and the email is verified, even if admin.
145                 $buttons = HTML::p(Button('submit:admin_reset[reset]', 
146                                           $isadmin ? _("Yes") : _("Send email"), 
147                                           $isadmin ? 'wikiadmin' : 'button'),
148                                    HTML::Raw('&nbsp;'),
149                                    Button('submit:admin_reset[cancel]', _("Cancel"), 'button'));
150                 $header = HTML::strong("Verify");
151                 if (!$user->isAdmin()) {
152                     // check for email
153                     if ($userid == $user->UserName() and $user->isAuthenticated()) {
154                         $alert = new Alert(_("Already logged in"),
155                                            HTML(fmt("Changing passwords is done at "), WikiLink(_("UserPreferences"))));
156                         $alert->show();
157                         return;
158                     }
159                     $thisuser = WikiUser($userid);
160                     $prefs = $thisuser->getPreferences();
161                     $email = $prefs->get('email');
162                     if (!$email) {
163                         $alert = new Alert(_("Error"),
164                                            HTML(fmt("No email stored for user %s.", $userid),
165                                                 HTML::br(),
166                                                 fmt("You need to ask an Administrator to reset this password. See below: "),
167                                                 HTML::br(), WikiLink(ADMIN_USER)));
168                         $alert->show();
169                         return;
170                     }
171                     $verified = $thisuser->_prefs->_prefs['email']->getraw('emailVerified');
172                     if (!$verified)
173                         $header->pushContent(HTML::br(), "Warning: This users email address is unverified!");
174                 }
175                 return $this->doForm($request, $userid,
176                                      $header,
177                                      HTML(HTML::hr(),
178                                           fmt("Do you really want to reset the password of user %s?", $userid),
179                                           $isadmin ? '' : _("An email will be sent."),
180                                           HiddenInputs(array('admin_reset[verify]' => 1, 'user' => $userid)),
181                                           $buttons));
182             } else { // verify ok, but no userid
183                 return $this->doForm($request, $userid);
184             }
185         } else {
186             return $this->doForm($request, $userid);
187         }
188     }
189 };
190
191 // For emacs users
192 // Local Variables:
193 // mode: php
194 // tab-width: 8
195 // c-basic-offset: 4
196 // c-hanging-comment-ender-p: nil
197 // indent-tabs-mode: nil
198 // End:
199 ?>