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

$db =& JFactory::getDBO();

function emailNotInDB($email) {
  $db =& JFactory::getDBO();
  $query = 'SELECT * FROM #__nspro_subs WHERE Email = "'.mysql_escape_string($email).'"';
  $db->setQuery($query);
  $myResult = $db->loadObject();

  return ($myResult->email == "");
}

      jimport('joomla.application.component.helper');
      $cparams = JComponentHelper::getParams( 'com_nspro' );
      $component = JComponentHelper::getComponent( 'com_nspro' );
      $cparams = new JParameter( $component->params );
      
  $uredirectToPage = $cparams->get('unsub_confirm_redirect', false);
  $uredirectPage = $cparams->get('unsub_confirm_redir_page', false);

$myEmailLabel = $this->myEmailLabel;
$recipient = $this->recipient;

$buttonText = $this->buttonText;
$notifyUnsub = $this->notifyUnsub;
$subject = $this->subject;
$fromName = $this->fromName;
$fromEmail = $this->fromEmail;
$fromNameConfirmun = $this->fromNameConfirmun;
$fromEmailConfirmun = $this->fromEmailConfirmun;
$subjectConfirmun = $this->subjectConfirmun;
$sendingWithSetEmail = $this->sendingWithSetEmail;
$emailWidth = $this->emailWidth;
$buttonWidth = $this->buttonWidth;

$url = JRoute::_('index.php?option=com_nspro&view=nspro&layout=unsubscribe');
$confirm_url = 'index.php?option=com_nspro&view=nspro&layout=confirmunsub';


print '<div class="nspro componentheading">'.JText::_('Unsubscribe from Newsletter').'</div><br/><div class="nspro unsubscribe">';
if (isset($_POST["un_email"])) {
  if ($_POST["un_email"] === "") {
    $myReplacement = '<span style="color: #f00;">' . JText::_('No email specified') . '</span>' .
                     '<div class="nspro unsubscribe"><form action="' . $url . '" method="post">' . "\n" .
                     '<table><tr><td>' . $myEmailLabel . '</td><td><input class="nspro unsubscribe inputbox" type="text" name="un_email" width="250"/></td></tr>' . "\n" .
                     '<tr><td colspan="2"><input class="nspro unsubscribe button" type="submit" value="' . $buttonText . '" style="width: 100%;"/></td></tr></table></form></div>' . "\n";
    print $myReplacement;
    return true;
  }
  if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $_POST["un_email"])) {
    $myReplacement = '<span style="color: #f00;">' . JText::_('Invalid email specified') . '</span>' .
                     '<div class="nspro unsubscribe"><form action="' . $url . '" method="post">' . "\n" .
                     '<table><tr><td>' . $myEmailLabel . '</td><td><input class="nspro unsubscribe inputbox" type="text" name="un_email" width="250"/></td></tr>' . "\n" .
                     '<tr><td colspan="2"><input class="nspro unsubscribe button" type="submit" value="' . $buttonText . '" style="width: 100%;"/></td></tr></table></form></div>' . "\n";
    print $myReplacement;
    return true;
  }
  if (emailNotInDB($_POST["un_email"])) {
    $myReplacement = '<span style="color: #f00;">' . JText::_('Email not found in subscriber list') . '</span>' .
                     '<div class="nspro unsubscribe"><form action="' . $url . '" method="post">' . "\n" .
                     '<table><tr><td>' . $myEmailLabel . '</td><td><input class="nspro unsubscribe inputbox" type="text" name="un_email" width="250"/></td></tr>' . "\n" .
                     '<tr><td colspan="2"><input class="nspro unsubscribe button" type="submit" value="' . $buttonText . '" style="width: 100%;"/></td></tr></table></form></div>' . "\n";
    print $myReplacement;
    return true;
  }

  if ($this->confirmUnsub === '0') {

    if ($notifyUnsub != '0') {
      $mailSender = &JFactory::getMailer();
      $mailSender->addRecipient($recipient);

      $query = 'SELECT * FROM #__nspro_subs WHERE Email = "'.mysql_escape_string($email).'"';
      $db->setQuery($query);
      $myResult = $db->loadObject();

      if ($sendingWithSetEmail) {
        $mailSender->setSender(array($fromEmail,$fromName));
      }
      else {
        $mailSender->setSender(array($_POST["un_email"],$myResult->name));
      }

      $myMessage = $myEmailLabel . ' ' . $_POST["un_email"] . ', ' . date("r");

      $mailSender->setSubject($subject);
      $mailSender->setBody($myMessage);

      if (!$mailSender->Send()) {
        $myReplacement = '<span style="color: #f00;">' . JText::_('Your unsubscription was not completed') . '</span>';
        print $myReplacement;
      }
      else {
        if (!$uredirectToPage) {
          $myReplacement = '<span style="color: #f00;">' . JText::_('Your unsubscription was completed') . '</span>';
          print $myReplacement;
        }
        else {
          header("Location: ".$uredirectPage);
        }    
      }
      
    }
    else {
      if (!$uredirectToPage) {
        $myReplacement = '<span style="color: #f00;">' . JText::_('Your unsubscription was completed') . '</span>';
        print $myReplacement;
      }
      else {
        header("Location: ".$uredirectPage);
      }    

    }

    $query = 'DELETE FROM #__nspro_subs WHERE Email = "'.mysql_escape_string($_POST["un_email"]).'"';
    $db->setQuery($query);
    if (!$db->query()) {
      print JText::_('Database Deletion Error. Please try again');
      return;
    }
  }
  else {

    $uri = & JURI::getInstance();
    $myBase = JURI::base();
    $myMessage = '<a href="'.$myBase.$confirm_url.'&u='.md5($_POST["un_email"]).'">'.JText::_('Click to complete your Unsubscription').
                 "</a>\n<br/>\n<br/>".JText::_('or copy this to your address bar')."\n<br/>\n<br/>".$myBase.$confirm_url.'&u='.md5($_POST["un_email"]);
    if (JUtility::sendMail($fromEmailConfirmun, $fromNameConfirmun, $_POST["un_email"], $subjectConfirmun, $myMessage, true) !== true) {
      $myReplacement = '<span style="color: #f00;">' . JText::_('Your unsubscription could not be completed') . '</span>';
    }
    else {
      $myReplacement = '<span style="color: #f00;">' . JText::_('An email has been sent to you to confirm unsubscription') . '</span>';
    }
    print $myReplacement;

  }

  return true;
}

if ($recipient === "") {
  $myReplacement = '<span style="color: #f00;">No recipient specified</span>';
  print $myReplacement;
  return true;
}

$myReplacement = '<div class="nspro unsubscribe"><form action="' . $url . '" method="post">' . "\n" .
                 '<table><tr><td>' . $myEmailLabel . '</td><td><input class="nspro unsubscribe inputbox" type="text" name="un_email" width="250"/></td></tr>' . "\n" .
                 '<tr><td colspan="2"><input class="nspro unsubscribe button" type="submit" value="' . $buttonText . '" style="width: 100%;"/></td></tr></table></form></div>' . "\n";
print $myReplacement;


print '</div>';

?>