コード例 #1
0
 /**
  * Method to subscribe a user to the newsletter. Implements the parent
  * abstract method.
  *
  * Hint: $subscriber_info array description:
  *
  * $subscriber_info[user_id] - User id of the user to subscribe.
  * $subscriber_info[username] - Username of the user to subscribe.
  * $subscriber_info[name] - Name of the user to subscribe.
  * $subscriber_info[email] - Email of the user to subscribe.
  * $subscriber_info[gid] - Group identifier in which the user will be subscribed.
  *
  * @access	public
  * @param   array $subscriber_info Subscriber info based on newsletter type.
  * @param   array $attributes Array of addictional attributes for subscription
  * @return  true if successfully subscribed. false if something wrong.
  * @since	0.6
  */
 function subscribe($subscriber_info, $attributes = null)
 {
     if (is_null($attributes) || !is_array($attributes)) {
         $attributes = array();
     }
     jincimport('utility.jincjoomlahelper');
     jincimport('utility.servicelocator');
     $servicelocator = ServiceLocator::getInstance();
     $logger = $servicelocator->getLogger();
     if (!isset($subscriber_info['user_id'])) {
         $this->setError('COM_JINC_ERR008');
         return false;
     }
     $news_id = $this->get('id');
     $user_id = $subscriber_info['user_id'];
     $user_info = JINCJoomlaHelper::getUserInfo($user_id);
     if (empty($user_info)) {
         $this->setError('COM_JINC_ERR008');
         return false;
     }
     $user =& JFactory::getUser($user_id);
     if (!$user->authorize('jinc.subscribe', 'com_jinc.newsletter.' . $news_id)) {
         $this->setError('COM_JINC_ERR006');
         return false;
     }
     if (!$this->checkMandatoryAttributes($attributes)) {
         $this->setError('COM_JINC_ERR048');
         return false;
     }
     if ($this->isSubscribed($subscriber_info) > 0) {
         $this->setError('COM_JINC_ERR015');
         return false;
     }
     jincimport('utility.jinchelper');
     $ip = JINCHelper::getRealIpAddr();
     $query = 'INSERT IGNORE INTO #__jinc_subscriber ' . '(news_id , user_id, datasub, ipaddr) ' . 'VALUES (' . (int) $news_id . ', ' . (int) $user_id . ', ' . 'now(), INET_ATON(\'' . $ip . '\'))';
     $logger->debug('PrivateNewsletter: Executing query: ' . $query);
     $dbo =& JFactory::getDBO();
     $dbo->setQuery($query);
     if (!$dbo->query()) {
         $this->setError('COM_JINC_ERR009');
         return false;
     }
     $sub_id = $dbo->insertid();
     $this->insertAttributeOnSubscription($sub_id, $attributes);
     $this->sendWelcome($user_info, $attributes);
     $dispatcher =& JDispatcher::getInstance();
     $params = array('news_id' => $this->get('id'), 'news_name' => $this->get('name'), 'subs_name' => $user_info['name'], 'news_notify' => $this->get('notify'));
     $result = $dispatcher->trigger('jinc_subscribe', $params);
     return true;
 }
コード例 #2
0
 /**
  * Method to subscribe a user to the newsletter. Implements the parent
  * abstract method.
  *
  * Hint: $subscriber_info array description:
  *
  * $subscriber_info[email] - Email of the user to subscribe.
  * $subscriber_info[noptin] - If true the optin won't be sent and the user
  *                            will be directly subscribed to the newsletter.
  *
  * @access	public
  * @param   array $subscriber_info Subscriber info based on newsletter type.
  * @param   array $attributes Array of addictional attributes for subscription
  * @return  true if successfully subscribed. false if something wrong.
  * @since	0.6
  */
 function subscribe($subscriber_info, $attributes = null)
 {
     if (is_null($attributes) || !is_array($attributes)) {
         $attributes = array();
     }
     jincimport('utility.randomizer');
     jincimport('utility.inputchecker');
     jincimport('utility.servicelocator');
     $servicelocator = ServiceLocator::getInstance();
     $logger = $servicelocator->getLogger();
     if (!isset($subscriber_info['email'])) {
         $this->setError('COM_JINC_ERR008');
         return false;
     }
     $email = $subscriber_info['email'];
     if (!InputChecker::checkMail($email)) {
         $this->setError('COM_JINC_ERR012');
         return false;
     }
     if (!$this->checkMandatoryAttributes($attributes)) {
         $this->setError('COM_JINC_ERR048');
         return false;
     }
     if (!$this->isSubscribed($subscriber_info)) {
         $email = $subscriber_info['email'];
         $news_id = $this->get('id');
         if (isset($subscriber_info['noptin']) && $subscriber_info['noptin']) {
             $datasub = 'now()';
             $random = '';
         } else {
             jincimport('utility.randomizer');
             $datasub = 'NULL';
             $random = Randomizer::getRandomString();
         }
         jincimport('utility.jinchelper');
         $ip = JINCHelper::getRealIpAddr();
         $dbo =& JFactory::getDBO();
         $query = 'INSERT IGNORE INTO #__jinc_subscriber ' . '(news_id , email, datasub, random, ipaddr) ' . 'VALUES (' . (int) $news_id . ', ' . $dbo->quote($email) . ', ' . $datasub . ', ' . $dbo->quote($random) . ', ' . 'INET_ATON(\'' . $ip . '\'))';
         $logger->debug('PublicNewsletter: Executing query: ' . $query);
         $dbo->setQuery($query);
         if (!$dbo->query()) {
             $this->setError('COM_JINC_ERR009');
             return false;
         }
         $sub_id = $dbo->insertid();
         $logger->finer('PublicNewsletter: Inserting attribute addictional info: ' . $sub_id);
         $this->insertAttributeOnSubscription($sub_id, $attributes);
         if (strlen($random) > 0) {
             $query = 'DELETE FROM #__jinc_subscriber ' . 'WHERE news_id = ' . (int) $news_id . ' ' . 'AND email = ' . $dbo->quote($email) . ' ' . 'AND random != ' . $dbo->quote($random) . ' ' . 'AND random != \'\'';
             $logger->debug('PublicNewsletter: executing query: ' . $query);
             $logger->finer('PublicNewsletter: other subscription requests.');
             $dbo->setQuery($query);
             if (!$dbo->query()) {
                 $logger->info('PublicNewsletter: error removing other subscription requests.');
                 $this->setError('COM_JINC_ERR009');
                 return false;
             }
             // Setting opt-in message
             $root_uri = JURI::root();
             $conf_url = $root_uri . 'index.php?option=com_jinc&task=newsletter.confirm&';
             $conf_url .= 'id=' . $news_id . '&';
             $conf_url .= 'random=' . urldecode($random) . '&';
             $conf_url .= 'user_mail=' . urldecode($email);
             $msg = $this->get('optin');
             $msg = preg_replace('/\\[SENDER\\]/s', $this->get('sendername'), $msg);
             $msg = preg_replace('/\\[SENDERMAIL\\]/s', $this->get('senderaddr'), $msg);
             $msg = preg_replace('/\\[NEWSLETTER\\]/s', $this->get('name'), $msg);
             $msg = preg_replace('/\\[EMAIL\\]/s', $email, $msg);
             $msg = preg_replace('/\\[OPTIN_URL\\]/s', $conf_url, $msg);
             foreach ($attributes as $attr_name => $attr_value) {
                 $msg = preg_replace('/\\[ATTR_' . strtoupper($attr_name) . '\\]/s', $attr_value, $msg);
             }
             $msg = preg_replace('#src[ ]*=[ ]*\\"(?!https?://)(?:\\.\\./|\\./|/)?#', 'src="' . $root_uri, $msg);
             $msg = preg_replace('#href[ ]*=[ ]*\\"(?!https?://)(?!mailto?:)(?!tel?:)(?:\\.\\./|\\./|/)?#', 'href="' . $root_uri, $msg);
             $msg = preg_replace('#url[ ]*\\(\'(?!https?://)(?:\\.\\./|\\./|/)?#', 'url(\'' . $root_uri, $msg);
             // Message composition
             $message =& JFactory::getMailer();
             $message->ContentType = "text/html";
             $message->setSubject($this->get('optin_subject'));
             $message->setBody($msg);
             if (strlen($this->get('senderaddr')) > 0) {
                 $message->setSender(array($this->get('senderaddr'), $this->get('sendername')));
             }
             if (strlen($this->get('replyto_addr')) > 0) {
                 $message->addReplyTo(array($this->get('replyto_addr'), $this->get('replyto_name')));
             }
             $message->addRecipient($email);
             $logger->finer('PublicNewsletter: Sending message to ' . $email . ' with body: ' . $msg);
             $result = $message->send();
             if (!$result) {
                 $this->setError(JText::_('COM_JINC_ERR001'));
                 return false;
             }
         }
     } else {
         $this->setError('COM_JINC_ERR015');
         return false;
     }
     if (isset($subscriber_info['noptin']) && $subscriber_info['noptin']) {
         $dispatcher =& JDispatcher::getInstance();
         $params = array('news_id' => $this->get('id'), 'news_name' => $this->get('name'), 'subs_name' => $subscriber_info['email'], 'news_notify' => $this->get('notify'));
         $result = $dispatcher->trigger('jinc_subscribe', $params);
     }
     return true;
 }