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