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