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