<?php
require_once DIR_ENGINE . 'newsletter.php';
if( !empty( $config['recaptcha_private'] ) && !empty( $config['recaptcha_public'] ) ) $oReCaptcha = new recaptchalib($config['recaptcha_private']);
if( $a == 'add' ){
	if (!$oReCaptcha->verify($_POST['g-recaptcha-response'],$_SERVER['REMOTE_ADDR'])){
		$oTpl->assign( 'aMessage', Array( 'message' => $lang['cf_reCaptcha'].join('<br>',$oReCaptcha->getErrors()), 'bError' => true ) );
		echo $oTpl->parse( 'message.tpl' );

		exit;
	}
	if( !empty( $_POST['sEmail'] ) && checkEmail( $_POST['sEmail'] ) == true ){
		$oDB->executeSQL( 'UPDATE `' . $oDB->throwTableName( DB_USER_ACTIONS ) . '` SET `sEmail` = \'' . $oDB->escape( $_POST['sEmail'] ) . '\' WHERE `sEmail` IS NULL AND `iCustomer` = ' . $_SESSION['iCustomer' . LANGUAGE] );
		saveNewsletter( $_POST['sEmail'] );
		$oTpl->assign( 'aMessage', Array( 'message' => $lang['Email_added'] ) );
		setcookie( 'newsletter', 1, time()+3600*24*365 );
		echo $oTpl->parse( 'message.tpl' );
		exit;
	} else{
		$oTpl->assign( 'aMessage', Array( 'message' => $lang['cf_no_word'], 'bError' => true ) );
		echo $oTpl->parse( 'message.tpl' );
		exit;
	}
	$bDisplayedPage = true;
}
if( $a == 'signoutform' ){
	if( !empty( $_POST['sEmail'] ) && checkEmail( $_POST['sEmail'] ) ){
		if( checkNewsletterEmail( $_POST['sEmail'] ) ){
			if( !isset( $aUrl ) ){
				$aUrl = parse_url( $_SERVER['REQUEST_URI'] );
				if( !isset( $aUrl['host'] ) ) $aUrl['host'] = $_SERVER['SERVER_NAME'];
				if( !isset( $aUrl['path'] ) ) $aUrl['path'] = null;
				if( isset( $aUrl['query'] ) ) $aUrl['query'] = '?' . $aUrl['query'];
				else $aUrl['query'] = null;
			}
			$aUrl['path'] = str_replace( 'signoutform,newsletter.html', '', $aUrl['path'] );
			$m_h = "From: " . $config['newsletter_email'] . "\n";
			$m_h.= "MIME-Version: 1.0\n";
			$m_h.= "Content-Transfer-Encoding: 8bit\n";
			$m_h.= "Content-Type: text/html; charset=\"utf-8\"\n";
			$m_b = "<html>\n<head>\n<title>" . $lang['newsletter_signout_title'] . "</title>\n<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />\n</head>\n<body>\n";
			$m_b.= '<img src="http' . ( $_SERVER['HTTPS'] ? 's' : '' ) . '://' . $aUrl['host'] . $aUrl['path'] . DIR_TEMPLATES . 'images/mailing/logo.png" style="float: left; margin-right: 1em;"/><p>' . $lang['newsletter_signout_confirm_link_text_prefix'] . '</p><p><a href="http' . ( $_SERVER['HTTPS'] ? 's' : '' ) . '://' . $aUrl['host'] . $aUrl['path'] . 'signoutform,newsletter.html?sEmail=' . $_POST['sEmail'] . '&amp;sHash=' . md5( $_POST['sEmail'] . SESSION_NAME_SELLSMART . PK ) . '">' . $lang['newsletter_signout_confirm_link_text'] . '</a></p><p>' . $lang['newsletter_signout_confirm_link_text_suffix'] . "</p></body>\n</html>\n";
			$m_b.= "\n";
			$m_h = str_replace( "\n", PHP_EOL, str_replace( "\r", '', $m_h ) );
			$m_b = str_replace( "\n", PHP_EOL, str_replace( "\r", '', $m_b ) );
			if( DEBUG_SELLSMART ) file_put_contents( 'email_log.txt', var_export( Array( '', $_POST['sTitle'], $m_b, $m_h ), true ) . "\n\n", FILE_APPEND );
			if( @email( $_POST['sEmail'], $lang['newsletter_signout_title'], $m_b, $m_h ) ){
				$oTpl->assign( 'aMessage', Array( 'message' => $lang['Mail_send_correct'] ) );
				$content.= $oTpl->parse( 'message.tpl' );
			} else{
				$oTpl->assign( 'aMessage', Array( 'message' => $lang['Mail_send_error'], 'bError' => true ) );
				$content.= $oTpl->parse( 'message.tpl' );
			}
		} else{
			$oTpl->assign( 'aMessage', Array( 'message' => $lang['Mail_send_correct'] ) );
			$content.= $oTpl->parse( 'message.tpl' );
		}
	} elseif( !empty( $_GET['sEmail'] ) && !empty( $_GET['sHash'] ) ){
		if( md5( $_GET['sEmail'] . SESSION_NAME_SELLSMART . PK ) == $_GET['sHash'] ){
			deleteNewsletter( $_GET['sEmail'] );
			$oTpl->assign( 'aMessage', Array( 'message' => $lang['Done_removing_newsletter'] ) );
			$content.= $oTpl->parse( 'message.tpl' );
		} else{
			$oTpl->assign( 'aMessage', Array( 'message' => $lang['Error_removing_newsletter'], 'bError' => true ) );
			$content.= $oTpl->parse( 'message.tpl' );
		}
	} else{
		$content.= $oTpl->parse( 'newsletter_signout.tpl' );
	}
}
?>