<?php // no direct access
defined('_JEXEC') or die('Restricted access');

  $myEmail = $_GET['u'];
  $nssource = $_GET['nss'];
  
  jimport('joomla.application.component.helper');
  $cparams = JComponentHelper::getParams( 'com_nspro' );
  $component = JComponentHelper::getComponent( 'com_nspro' );
  $cparams = new JParameter( $component->params );

  $redirectToPage = $cparams->get('confirm_redirect', false);
  $redirectPage = $cparams->get('confirm_redir_page', false);

  $db =& JFactory::getDBO();

  $query = 'SELECT * FROM #__nspro_subs ORDER BY `datetime` DESC';

  $db->setQuery( $query );
  $msubs = $db->loadRowList();

  $myprecious = -1;

  for ($i=0, $n=count( $msubs ); $i < $n; $i++) {
    $row = $msubs[$i];

    if ($row[5] == '0') {
      if (md5($row[2]) == $myEmail) {
        $myprecious = $row[0];
         break;
      }
    }
  }

  if ($myprecious != -1) {
    if ($nssource == 'nsplgp') {
      jimport('joomla.application.plugin.helper');

      $plugin = & JPluginHelper::getPlugin('content', 'newsletter_subscriber_pro');

      $myParams = new JParameter($plugin->params);

      $recipient = $myParams->def('email_recipient', '');
      $subject   = $myParams->def('subject', 'New subscription to your site!');
      $fromName  = $myParams->def('from_name', 'Newsletter Subscriber');
      $fromEmail = $myParams->def('from_email', 'newsletter_subscriber@yoursite.com');
      $saveList  = $myParams->def('save_list', true);
      $savePath  = $myParams->def('save_path', 'mailing_list.txt');
      $saveFormat = $myParams->def('save_format', '%n (%e);');
      $pageText  = $myParams->def('page_text', 'Thank you for subscribing to our site.');
      $errorText = $myParams->def('errot_text', 'Your subscription could not be submitted. Please try again.');
      $myNameLabel = $myParams->def('name_label', 'Name:');
      $myEmailLabel = $myParams->def('email_label', 'Email:');
      $thanksTextColor = $myParams->def('thank_text_color', '#FF0000');
      $errorTextColor = $myParams->def('error_text_color', '#FF0000');            

      $sendingWithSetEmail = $myParams->def('sending_from_set', true);
    }
    else {
      jimport('joomla.application.module.helper');

      $module = JModuleHelper::getModule( 'mod_newsletter_subscriber_pro' );

      $myParams = $module->params;

      $recipient = $myParams->get('email_recipient', '');
      $subject   = $myParams->get('subject', 'New subscription to your site!');
      $fromName  = $myParams->get('from_name', 'Newsletter Subscriber');
      $fromEmail = $myParams->get('from_email', 'newsletter_subscriber@yoursite.com');
      $saveList  = $myParams->get('save_list', true);
      $savePath  = $myParams->get('save_path', 'mailing_list.txt');
      $saveFormat = $myParams->get('save_format', '%n (%e);');
      $pageText  = $myParams->get('page_text', 'Thank you for subscribing to our site.');
      $errorText = $myParams->get('errot_text', 'Your subscription could not be submitted. Please try again.');
      $myNameLabel = $myParams->get('name_label', 'Name:');
      $myEmailLabel = $myParams->get('email_label', 'Email:');   
      $thanksTextColor = $myParams->get('thank_text_color', '#FF0000');
      $errorTextColor = $myParams->get('error_text_color', '#FF0000');      

      $sendingWithSetEmail = $myParams->get('sending_from_set', true);

    }

    $db =& JFactory::getDBO();
    $query = 'UPDATE `#__nspro_subs` SET `confirmed` = "1" WHERE `id` = "'.$myprecious.'"';
    $db->setQuery($query);
    if (!$db->query()) {
      print JText::_('Database Update Error. Please try again');
      return;
    }
    else {

      $query = 'SELECT * FROM `#__nspro_subs` WHERE `id` = "'.$myprecious.'"';
      $db->setQuery($query);
      $mSub = $db->loadObject();

      $myMessage = $myNameLabel . ' ' . $mSub->name . ', ' .
                   $myEmailLabel . ' ' . $mSub->email . ', ' .
                   date("r");

      $mailSender = &JFactory::getMailer();
      $mailSender->addRecipient($recipient);
      if ($sendingWithSetEmail) {
        $mailSender->setSender(array($fromEmail,$fromName));
      }
      else {
        $mailSender->setSender(array($mSub->email,$mSub->name));
      }
      $mailSender->setSubject($subject);
      $mailSender->setBody($myMessage);
      if (!$mailSender->Send()) {
        $myReplacement = '<span style="color: '.$errorTextColor.';">' . $errorText . '</span>';
        print $myReplacement;
      }
      else {
        if (!$redirectToPage) {
          $myReplacement = '<span style="color: '.$thanksTextColor.';">' . $pageText . '</span>';
          print $myReplacement;
        }
        else {
          header("Location: ".$redirectPage);
        }
      }
      if ($saveList) {
          $file = fopen($savePath, "a");
          if ($file !== false) {          
            $mstring = JString::str_ireplace('%n', $mSub->name, $saveFormat);
            $mstring = JString::str_ireplace('%e', $mSub->email, $mstring); 
            fwrite($file, $mstring);
            fclose($file);
          }
      }
    }
  }
  else {
    print JText::_('This link has expired');
  }


?>
