<?php
/**
 * Joomla! 1.5 component NS Pro
 *
 * @version $Id: controller.php 2009-04-28 01:47:12 svn $
 * @author Christopher Mavros
 * @package Joomla
 * @subpackage NS Pro
 * @license GNU/GPL
 *
 * Pro Version of Newsletter Subscriber.
Provides:
Keep subscriptions in database
Unsubscription Page
Optional Confirmation on subscribe/unsubscribe
Subscription Manager
Send Newsletters
Newsletter Manager
Subscription Statistics
Unique Emails (does not allow duplicates)
 *
 * This component file was created using the Joomla Component Creator by Not Web Design
 * http://www.notwebdesign.com/joomla_component_creator/
 *
 */

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

jimport('joomla.application.component.controller');

/**
 * NS Pro Component Controller
 */
class NsproController extends JController {
    function display() {

        if( strtolower(JRequest::getVar( 'task' )) == 'forwardtofriend') {

          jimport('joomla.application.component.helper');
          $params = JComponentHelper::getParams( 'com_nspro' );
          $component = JComponentHelper::getComponent( 'com_nspro' );
          $params = new JParameter( $component->params );

          $allow = $params->get('enable_ftf', '1');

          if ($allow == '1') {

            $db =& JFactory::getDBO();
            if (!JRequest::getVar( 'nid' )) {
              print JText::_('Invalid Newsletter ID');
              return;
            }
            else {

              $query = 'SELECT * FROM `#__nspro_newsletters` WHERE `id` = ' . mysql_escape_string(JRequest::getVar( 'nid' ));

              $db->setQuery($query);

              $mNL = $db->loadObject();

              if (!$mNL) {
                print JText::_('Invalid Newsletter ID');
                return;
              }
            }


            if (JRequest::getVar( 'fremail' )) {
              if (eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$", JRequest::getVar( 'fremail' ))) {                
                $target_dir = JPATH_ADMINISTRATOR.DS."components/com_nspro/uploads/";
                $mybase = JString::str_ireplace('/administrator/', '/', JURI::base());
                $myLink = $mybase.'index.php?option=com_nspro&layout=confirmunsub'.'&u='.md5(JRequest::getVar( 'fremail' ));
                $myFTFLink = $mybase.'index.php?option=com_nspro&task=forwardtofriend&nid=' . $mNL->id;
                $myEDLLink = $mybase.'index.php?option=com_nspro&layout=editlists'.'&u='.md5(JRequest::getVar( 'fremail' ));
                $mMessage = JString::str_ireplace('{unsubscribe_link}', $myLink, $mNL->message);
                $mMessage = JString::str_ireplace('{subscriber_name}', 'Friend', $mMessage);
                $mMessage = JString::str_ireplace('{subscriber_email}', JRequest::getVar( 'fremail' ), $mMessage);
                $mMessage = JString::str_ireplace('{forward_to_friend_link}', '<a href="'. $myFTFLink .'">', $mMessage);
                $mMessage = JString::str_ireplace('{/forward_to_friend_link}', '</a>', $mMessage);
                $mMessage = JString::str_ireplace('{unsub_quick}', '<a href="'. $myLink .'">', $mMessage);
                $mMessage = JString::str_ireplace('{/unsub_quick}', '</a>', $mMessage);
                $mMessage = JString::str_ireplace('{edit_list_link}', '<a href="'. $myEDLLink .'">', $mMessage);
                $mMessage = JString::str_ireplace('{/edit_list_link}', '</a>', $mMessage);
                
                $query = 'SELECT * FROM `#__nspro_newsletter_attachments` WHERE `newsletter_id` = ' . $mNL->id . ' ORDER BY `id`';
                $db->setQuery($query);
                $mattachments = $db->loadObjectList();
              
                $attachment = array();
                foreach( $mattachments as $mat) {
                  $attachment[] = $target_dir.$mat->filename;
                }
                
                if (JUtility::sendMail($mNL->fEmail, $mNL->fName, JRequest::getVar( 'fremail' ), $mNL->subject, $mMessage, true, null, null, $attachment) !== true) {
                  print JText::_('Newsletter could not be sent.');
                }
                else {
                  print JText::_('Newsletter sent successfully.');
                  print '<br/><a href="index.php?option=com_nspro&amp;task=forwardtofriend&amp;nid='. $mNL->id .'">'.JText::_('Back to Forward To Friend Form').'</a>';
                  return;
                }
              }
              else {
                print JText::_('Invalid Friend Email. Please write a valid mail.');
              }
            }

            print '<form action="index.php" method="get">'."\n".
                  '<table><tr><td>'."\n".
                  JText::_('Friend e-mail:') .
                  '</td><td><input type="text" class="ns modns inputbox" name="fremail"/></td></tr>'."\n".
                  '<tr><td colspan="2"><input type="submit" class="ns modns button" value="' . JText::_('Send to Friend!') . '"/></td></tr>'."\n".
                  '</table>'."\n".
                  '<input type="hidden" name="option" value="com_nspro"/>'."\n".
                  '<input type="hidden" name="task" value="forwardtofriend"/>'."\n".
                  '<input type="hidden" name="nid" value="' . JRequest::getVar( 'nid' ) . '"'."\n".
                  '</form>';
            return;
          }
          else {
            print JText::_('Forward to friend is disabled.');
          }
        }

        // Make sure we have a default view
        if( !JRequest::getVar( 'view' )) {
            JRequest::setVar('view', 'nspro' );
        }
        parent::display();
    }

}
?>