]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/PasswordReset.php
unify gettext msg
[SourceForge/phpwiki.git] / lib / plugin / PasswordReset.php
1 <?php // -*-php-*-
2 rcs_id('$Id: PasswordReset.php,v 1.2 2006-06-18 11:04:50 rurban Exp $');
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: 1.2 $");
48     }
49
50     function getDefaultArguments() {
51         return array('user' => 0);
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, $header = '', $footer = '') { 
91         $post_args = $request->getArg('admin_reset');
92         $userid = $request->getArg('user');
93         if (!$header) {
94             $header = HTML::p(_("Reset password of user: "),
95                               HTML::Raw('&nbsp;'),
96                               HTML::input(array('type' => 'text',
97                                                 'name' => "user",
98                                                 'value' => $userid))
99                               );
100         }
101         if (!$footer) {
102             $isadmin = $request->_user->isAdmin();
103             $footer = HTML::p(Button('submit:admin_reset[reset]', 
104                                       $isadmin ? _("Yes") : _("Send email"), 
105                                       $isadmin ? 'wikiadmin' : 'button'),
106                                HTML::Raw('&nbsp;'),
107                                Button('submit:admin_reset[cancel]', _("Cancel"), 'button'));
108         }
109         return HTML::form(array('action' => $request->getPostURL(),
110                                 'method' => 'post'),
111                           $header,
112                           HiddenInputs($request->getArgs(), false, array('admin_reset', 'user')),
113                           ENABLE_PAGEPERM ? '' : HiddenInputs(array('require_authority_for_post' => WIKIAUTH_ADMIN)),
114                           $footer );
115     }
116
117     function run($dbi, $argstr, &$request, $basepage) {
118         $args = $this->getArgs($argstr, $request);
119         if (isa($request,'MockRequest'))
120             return '';
121
122         $user =& $request->_user;
123         $post_args = $request->getArg('admin_reset');
124         $userid = $request->getArg('user');
125         $isadmin = $user->isAdmin();
126         if ($request->isPost()) {
127             if (!$userid) {
128                 $alert = new Alert(_("Warning:"),
129                                    _("You need to specify the userid!"));
130                 $alert->show();
131                 return $this->doForm($request);
132             }
133             @$reset = $post_args['reset'];
134             if ($reset and $userid and !empty($post_args['verify'])) {
135                 if ($user->isAdmin()) {
136                     return $this->doReset($userid);
137                 } else {
138                     return $this->doEmail($request, $userid);
139                 }
140             } elseif ($reset and empty($post_args['verify'])) {
141                 $buttons = HTML::p(Button('submit:admin_reset[reset]', 
142                                           $isadmin ? _("Yes") : _("Send email"), 
143                                           $isadmin ? 'wikiadmin' : 'button'),
144                                    HTML::Raw('&nbsp;'),
145                                    Button('submit:admin_reset[cancel]', _("Cancel"), 'button'));
146                 $header = HTML::strong("Verify");
147                 if (!$user->isAdmin()) {
148                     // check for email
149                     if ($userid == $user->UserName() and $user->isAuthenticated()) {
150                         $alert = new Alert(_("Already logged in"),
151                                            HTML(fmt("Changing passwords is done at "), WikiLink(_("UserPreferences"))));
152                         $alert->show();
153                         return;
154                     }
155                     $thisuser = WikiUser($userid);
156                     $prefs = $thisuser->getPreferences();
157                     $email = $prefs->get('email');
158                     if (!$email) {
159                         $alert = new Alert(_("Error"),
160                                            HTML(fmt("No email stored for user %s.", $userid),
161                                                 HTML::br(),
162                                                 fmt("You need to ask an Administrator to reset this password. See below: "),
163                                                 HTML::br(), WikiLink(ADMIN_USER)));
164                         $alert->show();
165                         return;
166                     }
167                     $verified = $thisuser->_prefs->_prefs['email']->getraw('emailVerified');
168                     if (!$verified)
169                         $header->pushContent(HTML::br(), "Warning: This users email address is unverified!");
170                 }
171                 return $this->doForm($request,
172                                      $header,
173                                      HTML(HTML::hr(),
174                                           fmt("Do you really want to reset the password of user %s?", $userid),
175                                           $isadmin ? '' : _("An email will be sent."),
176                                           HiddenInputs(array('admin_reset[verify]' => 1, 'user' => $userid)),
177                                           $buttons));
178             } else {
179                 return $this->doForm($request);
180             }
181         } else {
182             return $this->doForm($request);
183         }
184     }
185 };
186
187 // $Log: not supported by cvs2svn $
188 // Revision 1.1  2006/03/19 16:31:57  rurban
189 // I would have needed that very often
190 //
191
192 // For emacs users
193 // Local Variables:
194 // mode: php
195 // tab-width: 8
196 // c-basic-offset: 4
197 // c-hanging-comment-ender-p: nil
198 // indent-tabs-mode: nil
199 // End:
200 ?>