getInput(); $verificationMail = (is_null($mailAddr)) ? $input->post->get('verificationAddr', '', 'STRING') : $mailAddr; $verification = new EmailValidation(array('value' => $verificationMail)); $valid = $verification->validate(); $this->verificationMailAddr = (empty($valid)) ? '' : $verificationMail; // Ajax Validation => fid, Form Submit => postid $this->formId = $input->post->get('fid', $input->post->get('postid', 0, 'int'), 'int'); $config = $app->getConfig(); $this->mailFrom = $config->get('mailfrom', ''); $this->mailFromName = $config->get('fromname', ''); $this->code = $this->createVerificationCode(); $this->db = Factory::getContainer()->get(DatabaseInterface::class); $this->initializeLogger('visformssubmit'); } public function sendVerificationMail() { if (empty($this->verificationMailAddr)) { return Text::_('COM_VISFORMS_EMAIL_VERIFICATION_EMAIL_REQUIRED'); } if (empty($this->code)) { return Text::_('COM_VISFORMS_EMAIL_VERIFICATION_MAIL_NO_CODE'); } if (empty($this->mailFrom)) { return Text::_('COM_VISFORMS_EMAIL_VERIFICATION_MAIL_NO_SENDER'); } $storedCodes = $this->getStoredCodes(); // ToDo: Implement a rate limit $codeStored = $this->storeVerificationCode(); if (empty($codeStored)) { return Text::_('COM_VISFORMS_EMAIL_VERIFICATION_CANNOT_STORE_CODE'); } try { $mail = Factory::getContainer()->get(MailerFactoryInterface::class)->createMailer(); $mail->CharSet = "utf-8"; $mail->addRecipient(explode(",", $this->verificationMailAddr)); $mail->setSender($this->mailFrom); $mail->setSubject(Text::_('COM_VISFORMS_VERIFICATION_CODE') . ' ' . $this->mailFromName); $mail->IsHTML(true); $mail->Encoding = 'base64'; $mail->setBody(Text::sprintf('COM_VISFORMS_VERIFICATION_MAIL_BODY', $this->code)); $sent = $mail->Send(); if (!empty($sent)) { $this->removeExpiredCodes(); return Text::_('COM_VISFORMS_EMAIL_VERIFICATION_MAIL_SEND'); } else { $this->removeExpiredCodes(); $this->removeVerificationCode($codeStored); return Text::_('COM_VISFORMS_EMAIL_VERIFICATION_MAIL_SEND_PROBLEMS'); } } catch (\Exception $e) { $this->removeExpiredCodes(); $this->removeVerificationCode($codeStored); $this->addLogEntry(Text::_('COM_VISFORMS_EMAIL_VERIFICATION_MAIL_SEND_PROBLEMS') . ': ' . $e->getMessage(), Log::ERROR); return Text::_('COM_VISFORMS_EMAIL_VERIFICATION_MAIL_SEND_PROBLEMS'); } } protected function createVerificationCode() { if (empty($this->verificationMailAddr) || strlen($this->verificationMailAddr) < 4) { return ''; } try { // use cryptographic save function return random_int(100000, 999999); } catch (RandomException $e) { // in error case fall back on shuffle $code = str_replace('@', '', $this->verificationMailAddr); $code = str_replace('.', '', $code); $code = str_shuffle($code); $code = strtoupper(substr($code, 0, min(strlen($code), 6))); return $code; } } public function getStoredCodes() { if (!empty($this->storedCodes)) { return $this->storedCodes; } $db = $this->db; $query = $db->createQuery(); $query->select('*') ->from($db->qn('#__visverificationcodes')) ->where($db->qn('email') . ' = :email') ->where($db->qn('fid') . ' = :fid') ->bind(':email', $this->verificationMailAddr, ParameterType::STRING) ->bind(':fid', $this->formId, ParameterType::INTEGER); try { $db->setQuery($query); return $db->loadObjectList(); } catch (\RuntimeException $e) { return false; } } protected function storeVerificationCode() { $return = true; // check if there are already codes stored for this mail address $storedCodes = $this->getStoredCodes(); if (!empty($storedCodes) && is_array($storedCodes)) { // get first element and update it with new code $storedCode = array_shift($storedCodes); $storedCode->code = $this->code; $storedCode->created = Factory::getDate()->toSql(); // reset attempts $storedCode->attempts = 0; try { $this->db->updateObject('#__visverificationcodes', $storedCode, 'id'); $return = $storedCode->id; } catch (\RuntimeException $e) { $return = false; } } else { $storedCode = new \stdClass(); $storedCode->fid = $this->formId; $storedCode->email = $this->verificationMailAddr; $storedCode->code = $this->code; $storedCode->created = Factory::getDate()->toSql(); try { $this->db->insertObject('#__visverificationcodes', $storedCode); $storedCode->id = $this->db->insertid(); $return = $storedCode->id; } catch (\RuntimeException $e) { $return = false; } } unset($storedCode); if (!empty($storedCodes) && is_array($storedCodes)) { // database is not clean: there was more than one code stored for the mail address: remove them foreach ($storedCodes as $storedCode) { $this->removeVerificationCode($storedCode->id); } } return $return; } public function removeVerificationCode($id) { $db = $this->db; $query = $db->createQuery(); $query->delete($db->qn('#__visverificationcodes')) ->where($db->quoteName('id') . ' = :id') ->bind(':id', $id, ParameterType::INTEGER); try { $db->setQuery($query); $db->execute(); return true; } catch (\RuntimeException $e) { return false; } } protected function removeExpiredCodes() { $db = $this->db; $query = $db->createQuery(); $query->delete($db->qn('#__visverificationcodes')) ->where($db->qn('created') . ' < UTC_TIMESTAMP() - INTERVAL 30 MINUTE ') ->where($db->qn('created') . ' != ' . $db->getNullDate()); try { $db->setQuery($query); return $db->execute(); } catch (\RuntimeException $e) { return false; } } public function updateAttempts($id) { $db = $this->db; $query = $db->createQuery(); $query->update($db->qn('#__visverificationcodes')) ->set($db->qn('attempts') . ' = ' . $db->qn('attempts') . ' + 1') ->where($db->quoteName('id') . ' = :id') ->bind(':id', $id, ParameterType::INTEGER); try { $db->setQuery($query); $db->execute(); } catch (\RuntimeException $e) { $test = $e->getMessage(); } } }