xref: /dokuwiki/inc/Action/Resendpwd.php (revision f21dad3906d4ec6b3d86685599409894630abdc1)
1<?php
2/**
3 * Created by IntelliJ IDEA.
4 * User: andi
5 * Date: 2/11/17
6 * Time: 9:33 AM
7 */
8
9namespace dokuwiki\Action;
10
11use dokuwiki\Action\Exception\ActionAbort;
12
13class Resendpwd extends AbstractAclAction {
14
15    /** @inheritdoc */
16    function minimumPermission() {
17        return AUTH_NONE;
18    }
19
20    /** @inheritdoc */
21    public function preProcess() {
22        if($this->resendpwd()) {
23            throw new ActionAbort('login');
24        }
25    }
26
27    /**
28     * Send a  new password
29     *
30     * This function handles both phases of the password reset:
31     *
32     *   - handling the first request of password reset
33     *   - validating the password reset auth token
34     *
35     * @author Benoit Chesneau <benoit@bchesneau.info>
36     * @author Chris Smith <chris@jalakai.co.uk>
37     * @author Andreas Gohr <andi@splitbrain.org>
38     * @fixme this should be split up into multiple methods
39     * @return bool true on success, false on any error
40     */
41    function resendpwd() {
42        global $lang;
43        global $conf;
44        /* @var \DokuWiki_Auth_Plugin $auth */
45        global $auth;
46        global $INPUT;
47
48        if(!actionOK('resendpwd')) {
49            msg($lang['resendna'], -1);
50            return false;
51        }
52
53        $token = preg_replace('/[^a-f0-9]+/', '', $INPUT->str('pwauth'));
54
55        if($token) {
56            // we're in token phase - get user info from token
57
58            $tfile = $conf['cachedir'] . '/' . $token{0} . '/' . $token . '.pwauth';
59            if(!file_exists($tfile)) {
60                msg($lang['resendpwdbadauth'], -1);
61                $INPUT->remove('pwauth');
62                return false;
63            }
64            // token is only valid for 3 days
65            if((time() - filemtime($tfile)) > (3 * 60 * 60 * 24)) {
66                msg($lang['resendpwdbadauth'], -1);
67                $INPUT->remove('pwauth');
68                @unlink($tfile);
69                return false;
70            }
71
72            $user = io_readfile($tfile);
73            $userinfo = $auth->getUserData($user, $requireGroups = false);
74            if(!$userinfo['mail']) {
75                msg($lang['resendpwdnouser'], -1);
76                return false;
77            }
78
79            if(!$conf['autopasswd']) { // we let the user choose a password
80                $pass = $INPUT->str('pass');
81
82                // password given correctly?
83                if(!$pass) return false;
84                if($pass != $INPUT->str('passchk')) {
85                    msg($lang['regbadpass'], -1);
86                    return false;
87                }
88
89                // change it
90                if(!$auth->triggerUserMod('modify', array($user, array('pass' => $pass)))) {
91                    msg($lang['proffail'], -1);
92                    return false;
93                }
94
95            } else { // autogenerate the password and send by mail
96
97                $pass = auth_pwgen($user);
98                if(!$auth->triggerUserMod('modify', array($user, array('pass' => $pass)))) {
99                    msg($lang['proffail'], -1);
100                    return false;
101                }
102
103                if(auth_sendPassword($user, $pass)) {
104                    msg($lang['resendpwdsuccess'], 1);
105                } else {
106                    msg($lang['regmailfail'], -1);
107                }
108            }
109
110            @unlink($tfile);
111            return true;
112
113        } else {
114            // we're in request phase
115
116            if(!$INPUT->post->bool('save')) return false;
117
118            if(!$INPUT->post->str('login')) {
119                msg($lang['resendpwdmissing'], -1);
120                return false;
121            } else {
122                $user = trim($auth->cleanUser($INPUT->post->str('login')));
123            }
124
125            $userinfo = $auth->getUserData($user, $requireGroups = false);
126            if(!$userinfo['mail']) {
127                msg($lang['resendpwdnouser'], -1);
128                return false;
129            }
130
131            // generate auth token
132            $token = md5(auth_randombytes(16)); // random secret
133            $tfile = $conf['cachedir'] . '/' . $token{0} . '/' . $token . '.pwauth';
134            $url = wl('', array('do' => 'resendpwd', 'pwauth' => $token), true, '&');
135
136            io_saveFile($tfile, $user);
137
138            $text = rawLocale('pwconfirm');
139            $trep = array(
140                'FULLNAME' => $userinfo['name'],
141                'LOGIN' => $user,
142                'CONFIRM' => $url
143            );
144
145            $mail = new \Mailer();
146            $mail->to($userinfo['name'] . ' <' . $userinfo['mail'] . '>');
147            $mail->subject($lang['regpwmail']);
148            $mail->setBody($text, $trep);
149            if($mail->send()) {
150                msg($lang['resendpwdconfirm'], 1);
151            } else {
152                msg($lang['regmailfail'], -1);
153            }
154            return true;
155        }
156        // never reached
157    }
158
159}
160