/** * The method to bind subscriber to J! user. * To test <b>?option=com_newsletter&task=subscribe.subscribe&newsletter-name=index.php * @return void * @since 1.0 */ public function subscribe() { // Initialise variables. $app = JFactory::getApplication(); $user = JFactory::getUser(); $db = JFactory::getDbo(); // Get variables from request $name = JRequest::getString('newsletter-name', null); $email = JRequest::getString('newsletter-email', null); $html = (int) JRequest::getInt('newsletter-html', null); $listsIds = DataHelper::toArrayOfInts(JRequest::getVar('newsletter-lists', array())); $fbenabled = JRequest::getInt('fbenabled', array()); //$sendto = JRequest::getVar('sendto'); // Check token, d_i_e on error. JRequest::checkToken() or jexit('Invalid Token'); if (empty($name) || empty($email) || !in_array($html, array(0, 1)) || empty($listsIds)) { jexit('One or more parameters is missing'); } $comParams = JComponentHelper::getComponent('com_newsletter')->params; $isNew = false; // Let's check if we can create user as confirmed $confirmed = $comParams->get('users_autoconfirm') == '1'; // try to get user data from FB $fbAppId = $comParams->get('fbappid'); $fbSecret = $comParams->get('fbsecret'); if (!empty($fbAppId) && !empty($fbSecret) && !empty($fbenabled)) { $me = SubscriberHelper::getFbMe($fbAppId, $fbSecret); if (!empty($me->email) && $me->email == $email) { $confirmed = true; } } // If it is a user's email $emailsAreEqual = false; if (!empty($user->id) && $user->email == $email) { $confirmed = true; $emailsAreEqual = true; } // Get from db $subscriber = JModel::getInstance('Subscriber', 'NewsletterModelEntity'); $subscriber->load(array('email' => $email)); // If subscriber does not exist then create it if (!$subscriber->getId()) { $subscriber->create(array('name' => $name, 'email' => $email, 'state' => '1', 'html' => $html, 'user_id' => $emailsAreEqual ? $user->id : 0, 'confirmed' => $confirmed)); } else { // Update subscriber if ($confirmed == true) { // Confirm subscriber and // ALL ITS ASSIGNINGS TO LISTS $subscriber->confirm(); } if ($emailsAreEqual) { // If user is authorized $subscriber->user_id = $user->id; $subscriber->save(); } } // Add subscribers to lists, ignore if already in db $assignedListsIds = array(); foreach ($listsIds as $list) { if (!$subscriber->isInList($list)) { $subscriber->assignToList($list); $assignedListsIds[] = $list; } } // Get lists we assigned $listManager = JModel::getInstance('Lists', 'NewsletterModel'); $lists = $listManager->getItemsByIds($assignedListsIds); // Add to history all subscriptions foreach ($lists as $list) { $history = JTable::getInstance('history', 'NewsletterTable'); $history->save(array('subscriber_id' => $subscriber->getId(), 'list_id' => $list->list_id, 'newsletter_id' => NULL, 'action' => NewsletterTableHistory::ACTION_SIGNEDUP, 'date' => date('Y-m-d H:i:s'), 'text' => addslashes($list->name))); unset($history); } // Triggering the automailing process. $amManager = new NewsletterAutomailingManager(); $amManager->processSubscription(array('subscriberId' => $subscriber->subscriber_id)); // If subscriber is confirmed then no need to send emails. $message = JText::sprintf('Thank you %s for subscribing to our Newsletter!', $name); if (!$subscriber->isConfirmed()) { // Let's send newsletters $mailer = new MigurMailer(); foreach ($lists as $list) { try { $newsletter = JModel::getInstance('Newsletter', 'NewsletterModelEntity'); $newsletter->loadAsWelcoming($list->send_at_reg); PlaceholderHelper::setPlaceholder('listname', $list->name); $res = $mailer->send(array('type' => $newsletter->isFallback() ? 'plain' : $subscriber->getType(), 'subscriber' => $subscriber->toObject(), 'newsletter_id' => $newsletter->newsletter_id, 'tracking' => true)); if ($res->state) { $message = JText::sprintf('Thank you %s for subscribing to our Newsletter! You will need to confirm your subscription. There should be an email in your inbox in a few minutes!', $name); LogHelper::addMessage('COM_NEWSLETTER_WELLCOMING_NEWSLETTER_SENT_SUCCESSFULLY', LogHelper::CAT_SUBSCRIPTION, array('Email' => $subscriber->email, 'Newsletter' => $newsletter->name)); } else { throw new Exception(json_encode($res->errors)); } } catch (Exception $e) { LogHelper::addError('COM_NEWSLETTER_WELCOMING_SEND_FAILED', LogHelper::CAT_SUBSCRIPTION, array('Error' => $e->getMessage(), 'Email' => $subscriber->email, 'Newsletter' => $newsletter->name)); } } } jexit($message); }