/** * Validate the subscription form * * @param Widget $objWidget * * @return array|bool */ protected function validateForm(Widget $objWidget = null) { // Validate the e-mail address $varInput = \Idna::encodeEmail(\Input::post('email', true)); if (!\Validator::isEmail($varInput)) { $this->Template->mclass = 'error'; $this->Template->message = $GLOBALS['TL_LANG']['ERR']['email']; return false; } $this->Template->email = $varInput; // Validate the channel selection $arrChannels = \Input::post('channels'); if (!is_array($arrChannels)) { $this->Template->mclass = 'error'; $this->Template->message = $GLOBALS['TL_LANG']['ERR']['noChannels']; return false; } $arrChannels = array_intersect($arrChannels, $this->nl_channels); // see #3240 if (!is_array($arrChannels) || empty($arrChannels)) { $this->Template->mclass = 'error'; $this->Template->message = $GLOBALS['TL_LANG']['ERR']['noChannels']; return false; } $this->Template->selectedChannels = $arrChannels; // Check if there are any new subscriptions $arrSubscriptions = array(); if (($objSubscription = \NewsletterRecipientsModel::findBy(array("email=? AND active=1"), $varInput)) !== null) { $arrSubscriptions = $objSubscription->fetchEach('pid'); } $arrRemove = array_intersect($arrChannels, $arrSubscriptions); if (!is_array($arrRemove) || empty($arrRemove)) { $this->Template->mclass = 'error'; $this->Template->message = $GLOBALS['TL_LANG']['ERR']['unsubscribed']; return false; } // Validate the captcha if ($objWidget !== null) { $objWidget->validate(); if ($objWidget->hasErrors()) { return false; } } return array($varInput, $arrRemove); }
/** * Add a new recipient * * @param string $strEmail * @param array $arrNew */ protected function addRecipient($strEmail, $arrNew) { // Remove old subscriptions that have not been activated yet if (($objOld = \NewsletterRecipientsModel::findBy(array("email=? AND active=''"), $strEmail)) !== null) { while ($objOld->next()) { $objOld->delete(); } } $time = time(); $strToken = md5(uniqid(mt_rand(), true)); // Add the new subscriptions foreach ($arrNew as $id) { $objRecipient = new \NewsletterRecipientsModel(); $objRecipient->pid = $id; $objRecipient->tstamp = $time; $objRecipient->email = $strEmail; $objRecipient->active = ''; $objRecipient->addedOn = $time; $objRecipient->ip = $this->anonymizeIp(\Environment::get('ip')); $objRecipient->token = $strToken; $objRecipient->confirmed = ''; $objRecipient->save(); // Remove the blacklist entry (see #4999) if (($objBlacklist = \NewsletterBlacklistModel::findByHashAndPid(md5($strEmail), $id)) !== null) { $objBlacklist->delete(); } } // Get the channels $objChannel = \NewsletterChannelModel::findByIds($arrNew); // Prepare the simple token data $arrData = array(); $arrData['token'] = $strToken; $arrData['domain'] = \Idna::decode(\Environment::get('host')); $arrData['link'] = \Idna::decode(\Environment::get('base')) . \Environment::get('request') . (strpos(\Environment::get('request'), '?') !== false ? '&' : '?') . 'token=' . $strToken; $arrData['channel'] = $arrData['channels'] = implode("\n", $objChannel->fetchEach('title')); // Activation e-mail $objEmail = new \Email(); $objEmail->from = $GLOBALS['TL_ADMIN_EMAIL']; $objEmail->fromName = $GLOBALS['TL_ADMIN_NAME']; $objEmail->subject = sprintf($GLOBALS['TL_LANG']['MSC']['nl_subject'], \Idna::decode(\Environment::get('host'))); $objEmail->text = \StringUtil::parseSimpleTokens($this->nl_subscribe, $arrData); $objEmail->sendTo($strEmail); // Redirect to the jumpTo page if ($this->jumpTo && ($objTarget = $this->objModel->getRelated('jumpTo')) instanceof PageModel) { /** @var PageModel $objTarget */ $this->redirect($objTarget->getFrontendUrl()); } \System::getContainer()->get('session')->getFlashBag()->set('nl_confirm', $GLOBALS['TL_LANG']['MSC']['nl_confirm']); $this->reload(); }