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