109 lines
3.1 KiB
PHP
109 lines
3.1 KiB
PHP
<?php
|
|
defined('BASEPATH') OR exit('No direct script access allowed');
|
|
|
|
/**
|
|
* Send OTP Email Library
|
|
* Generates and sends a one-time password via email for two-factor authentication
|
|
*/
|
|
class SendOtpEmail
|
|
{
|
|
private $ci;
|
|
private $user;
|
|
private $otpCode;
|
|
private $sendResult;
|
|
|
|
public function __construct($user)
|
|
{
|
|
$this->ci =& get_instance();
|
|
$this->user = $user;
|
|
|
|
$this->ci->load->library('email');
|
|
$this->ci->load->model('beheer/UserOtp_model');
|
|
|
|
$this->handle();
|
|
}
|
|
|
|
/**
|
|
* Get the result of the email send operation
|
|
* @return bool True if email was sent successfully
|
|
*/
|
|
public function getSendResult()
|
|
{
|
|
return $this->sendResult;
|
|
}
|
|
|
|
/**
|
|
* Get the generated OTP code (for testing purposes)
|
|
* @return string The OTP code
|
|
*/
|
|
public function getOtpCode()
|
|
{
|
|
return $this->otpCode;
|
|
}
|
|
|
|
/**
|
|
* Main handler for OTP email process
|
|
*/
|
|
private function handle()
|
|
{
|
|
$this->generateOTP();
|
|
$this->sendOTPEmail();
|
|
}
|
|
|
|
/**
|
|
* Generate a new OTP code
|
|
*/
|
|
private function generateOTP()
|
|
{
|
|
// Generate OTP valid for 10 minutes
|
|
$this->otpCode = $this->ci->UserOtp_model->generateOTP($this->user->id, 10);
|
|
}
|
|
|
|
/**
|
|
* Send the OTP code via email
|
|
*/
|
|
private function sendOTPEmail()
|
|
{
|
|
// Build email configuration directly from environment variables
|
|
$config = [
|
|
'protocol' => $_ENV['PROTOCOL'] ?? getenv('PROTOCOL') ?: 'mail',
|
|
'smtp_host' => $_ENV['SMTP_HOST'] ?? getenv('SMTP_HOST') ?: '',
|
|
'smtp_user' => $_ENV['SMTP_USER'] ?? getenv('SMTP_USER') ?: '',
|
|
'smtp_pass' => $_ENV['SMTP_PASS'] ?? getenv('SMTP_PASS') ?: '',
|
|
'smtp_port' => $_ENV['SMTP_PORT'] ?? getenv('SMTP_PORT') ?: 25,
|
|
'smtp_crypto' => 'tls',
|
|
'smtp_timeout' => 30,
|
|
'charset' => 'iso-8859-1',
|
|
'mailtype' => 'html',
|
|
'newline' => "\r\n",
|
|
'crlf' => "\r\n",
|
|
'wordwrap' => TRUE,
|
|
'validate' => TRUE
|
|
];
|
|
|
|
// Re-initialize email library with config
|
|
$this->ci->email->initialize($config);
|
|
|
|
$data = [
|
|
'username' => $this->user->username,
|
|
'firstname' => $this->user->firstname,
|
|
'middlename' => $this->user->middlename,
|
|
'lastname' => $this->user->lastname,
|
|
'otp_code' => $this->otpCode
|
|
];
|
|
|
|
$html = $this->ci->load->view('emails/twofactor_otp_email', ['user' => $data], true);
|
|
|
|
$this->ci->email->from('noreply@dbois.nl', 'DBOIS');
|
|
$this->ci->email->to($this->user->email);
|
|
$this->ci->email->subject('DBOIS Tweefactorauthenticatie - Verificatiecode');
|
|
$this->ci->email->message($html);
|
|
|
|
$this->sendResult = $this->ci->email->send();
|
|
|
|
if (!$this->sendResult) {
|
|
log_message('error', 'Failed to send 2FA OTP email to ' . $this->user->email . ': ' . $this->ci->email->print_debugger());
|
|
}
|
|
}
|
|
}
|