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

  $myEmail = $_GET['u'];

  $myprecious = -1;

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

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

  $myRealEmail = '';

  for ($i=0, $n=count( $this->subs ); $i < $n; $i++) {
    $row = &$this->subs[$i];

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

  if ($myprecious != -1) {
    $db =& JFactory::getDBO();
    $query = 'DELETE FROM #__nspro_subs WHERE Id = "'.$myprecious.'"';
    $db->setQuery($query);
    if (!$db->query()) {
      print JText::_('Database Deletion Error. Please try again');
      return;
    }
    else {

      if ($notifyUnsub) {
        $myMessage = $myEmailLabel . ' ' . $myRealEmail . ', ' . date("r");
        $mailSender = &JFactory::getMailer();
        $mailSender->addRecipient($recipient);

        if ($sendingWithSetEmail) {
          $mailSender->setSender(array($fromEmail,$fromName));
        }
        else {

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

          $mailSender->setSender(array($_POST["un_email"],$myResult->name));
        }

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

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

      $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 {
    print JText::_('This link has expired');
  }


?>
