xref: /plugin/twofactoremail/action.php (revision 1a1b1d069b52ce2d2876a3042b7efc87326b2ed1) !
1a5dfe92fSAndreas Gohr<?php
2a5dfe92fSAndreas Gohr
3a5dfe92fSAndreas Gohruse dokuwiki\Form\Form;
4a5dfe92fSAndreas Gohruse dokuwiki\plugin\twofactor\Provider;
5a5dfe92fSAndreas Gohr
6a5dfe92fSAndreas Gohr/**
7a5dfe92fSAndreas Gohr * Twofactor Provider that sends codes by email
8a5dfe92fSAndreas Gohr */
9a5dfe92fSAndreas Gohrclass action_plugin_twofactoremail extends Provider
10a5dfe92fSAndreas Gohr{
11a5dfe92fSAndreas Gohr
12a5dfe92fSAndreas Gohr    /** @inheritdoc */
13a5dfe92fSAndreas Gohr    public function getLabel()
14a5dfe92fSAndreas Gohr    {
15*1a1b1d06SAndreas Gohr        global $USERINFO;
16*1a1b1d06SAndreas Gohr        return $this->getLang('name') . ' ' . $USERINFO['mail'];
17a5dfe92fSAndreas Gohr    }
18a5dfe92fSAndreas Gohr
19a5dfe92fSAndreas Gohr    /** @inheritdoc */
20a5dfe92fSAndreas Gohr    public function isConfigured()
21a5dfe92fSAndreas Gohr    {
22a5dfe92fSAndreas Gohr        return $this->settings->get('verified');
23a5dfe92fSAndreas Gohr    }
24a5dfe92fSAndreas Gohr
25a5dfe92fSAndreas Gohr    /** @inheritdoc */
26a5dfe92fSAndreas Gohr    public function renderProfileForm(Form $form)
27a5dfe92fSAndreas Gohr    {
28a5dfe92fSAndreas Gohr        $form->addHTML('<p>' . $this->getLang('verifynotice') . '</p>');
29a5dfe92fSAndreas Gohr        $form->addTextInput('verify', $this->getLang('verifymodule'));
30a5dfe92fSAndreas Gohr        return $form;
31a5dfe92fSAndreas Gohr    }
32a5dfe92fSAndreas Gohr
33a5dfe92fSAndreas Gohr    /** @inheritdoc */
34a5dfe92fSAndreas Gohr    public function handleProfileForm()
35a5dfe92fSAndreas Gohr    {
36a5dfe92fSAndreas Gohr        global $INPUT;
37a5dfe92fSAndreas Gohr
38*1a1b1d06SAndreas Gohr        if($INPUT->bool('init')) {
39a5dfe92fSAndreas Gohr            $this->initSecret();
40a5dfe92fSAndreas Gohr            $code = $this->generateCode();
41*1a1b1d06SAndreas Gohr            $this->transmitMessage($code);
42*1a1b1d06SAndreas Gohr        }
43*1a1b1d06SAndreas Gohr
44*1a1b1d06SAndreas Gohr        if ($INPUT->has('verify')) {
45a5dfe92fSAndreas Gohr            if ($this->checkCode($INPUT->str('verify'))) {
46a5dfe92fSAndreas Gohr                $this->settings->set('verified', true);
47a5dfe92fSAndreas Gohr            } else {
48*1a1b1d06SAndreas Gohr                // send a new code
49*1a1b1d06SAndreas Gohr                $code = $this->generateCode();
50*1a1b1d06SAndreas Gohr                $this->transmitMessage($code);
51a5dfe92fSAndreas Gohr            }
52a5dfe92fSAndreas Gohr        }
53a5dfe92fSAndreas Gohr    }
54a5dfe92fSAndreas Gohr
55a5dfe92fSAndreas Gohr    /** @inheritdoc */
56a5dfe92fSAndreas Gohr    public function transmitMessage($code)
57a5dfe92fSAndreas Gohr    {
58a5dfe92fSAndreas Gohr        global $USERINFO;
59a5dfe92fSAndreas Gohr        $to = $USERINFO['mail'];
60a5dfe92fSAndreas Gohr        if (!$to) throw new \Exception($this->getLang('codesentfail'));
61a5dfe92fSAndreas Gohr
62a5dfe92fSAndreas Gohr        // Create the email object.
63a5dfe92fSAndreas Gohr        $body = io_readFile($this->localFN('mail'));
64a5dfe92fSAndreas Gohr        $mail = new Mailer();
65a5dfe92fSAndreas Gohr        $mail->to($to);
66a5dfe92fSAndreas Gohr        $mail->subject($this->getLang('subject'));
67a5dfe92fSAndreas Gohr        $mail->setBody($body, ['CODE' => $code]);
68a5dfe92fSAndreas Gohr        $result = $mail->send();
69a5dfe92fSAndreas Gohr        if (!$result) throw new \Exception($this->getLang('codesentfail'));
70a5dfe92fSAndreas Gohr
71a5dfe92fSAndreas Gohr        return $this->getLang('codesent');
72a5dfe92fSAndreas Gohr    }
73a5dfe92fSAndreas Gohr
74a5dfe92fSAndreas Gohr}
75