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