]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/PasswordReset.php
Remove useless semicolon
[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 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     {
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("Email sent to the stored email address for user %s", $userid));
89         else
90             $alert = new Alert(_("Error"),
91                 fmt("Error sending email 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 email"),
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                     return $this->doReset($userid);
146                 } else {
147                     return $this->doEmail($request, $userid);
148                 }
149             } elseif (empty($post_args['verify'])) {
150                 //TODO: verify should check if the user exists, his prefs can be read/safed
151                 //      and the email is verified, even if admin.
152                 $buttons = HTML::p(Button('submit:admin_reset[reset]',
153                         $isadmin ? _("Yes") : _("Send email"),
154                         $isadmin ? 'wikiadmin' : 'button'),
155                     HTML::Raw('&nbsp;'),
156                     Button('submit:admin_reset[cancel]', _("Cancel"), 'button'));
157                 $header = HTML::strong("Verify");
158                 if (!$user->isAdmin()) {
159                     // check for email
160                     if ($userid == $user->UserName() and $user->isAuthenticated()) {
161                         $alert = new Alert(_("Already logged in"),
162                             HTML(fmt("Changing passwords is done at "), WikiLink(_("UserPreferences"))));
163                         $alert->show();
164                         return;
165                     }
166                     $thisuser = WikiUser($userid);
167                     $prefs = $thisuser->getPreferences();
168                     $email = $prefs->get('email');
169                     if (!$email) {
170                         $alert = new Alert(_("Error"),
171                             HTML(fmt("No email stored for user %s.", $userid),
172                                 HTML::br(),
173                                 fmt("You need to ask an Administrator to reset this password. See below: "),
174                                 HTML::br(), WikiLink(ADMIN_USER)));
175                         $alert->show();
176                         return;
177                     }
178                     $verified = $thisuser->_prefs->_prefs['email']->getraw('emailVerified');
179                     if (!$verified)
180                         $header->pushContent(HTML::br(), "Warning: This users email address is unverified!");
181                 }
182                 return $this->doForm($request, $userid,
183                     $header,
184                     HTML(HTML::hr(),
185                         fmt("Do you really want to reset the password of user %s?", $userid),
186                         $isadmin ? '' : _("An email will be sent."),
187                         HiddenInputs(array('admin_reset[verify]' => 1, 'user' => $userid)),
188                         $buttons));
189             } else { // verify ok, but no userid
190                 return $this->doForm($request, $userid);
191             }
192         } else {
193             return $this->doForm($request, $userid);
194         }
195     }
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: