<?php
//=================================================================
// KOOBI R7 - CMS - PORTALSOFTWARE(c) DREAM4(r) 
// HTTP://WWW.DREAM4.DE
//=================================================================
// ACHTUNG!
// DIESER PROGRAMMCODE IST URHERRECHTLICH GESCHUETZT
// DIE URHEBERRECHTE LIEGEN BEI DEN JEWEILIGEN AUTOREN.
// AENDERUNGEN IN DIESER DATEI ERFOLGEN AUF EIGENE GEFAHR!
// ES IST NICHT GESTATTET, DEN COPYRIGHTHINWEIS ZU AENDERN ODER ZU
// ENTFERNEN. NUR WENN SIE EINE COPYRIGHTHINWEIS-ENTFERNUNG BESTELLT
// HABEN, DUERFEN SIE DIESEN ENTFERNEN.
//================================================================
// BEI FRAGEN WENDEN SIE SICH BITTE AN: KONTAKT@DREAM4.DE
//=================================================================
class Newsletter
{
	var $_muster_email = '/^[ -._A-Za-zÀ-ÖØ-öø-ÿ0-9-]+(\.[_A-Za-zÀ-ÖØ-öø-ÿ0-9-]+)*@([a-zA-Z0-9-]+\.)+([a-zA-Z]{2,4})$/';
	var $_muster = '/[^A-Za-zÀ-ÖØ-öø-ÿ0-9]/';
	
	//=================================================================
	// Form fuer Seitenausgabe (klein)
	//=================================================================
	function displayForm($where='page')
	{
		$GLOBALS['tmpl']->assign('nl_items', $this->getNl());
		$out = $GLOBALS['tmpl']->fetch($GLOBALS['config']['source'] . '/newsletter/newsletter_small.tpl');
		return $out;
	}
	
	//=================================================================
	// Abo abbstellen
	//=================================================================
	function DelAbo()
	{
		$error = false;
		//
		if(@empty($_GET['email']) || @empty($_GET['code']) || @!preg_match($this->_muster_email, $_GET['email']) || @preg_match($this->_muster, $_GET['code']))
		{
			$error = true;
			$error_msg[] = $GLOBALS['config_vars']['Newsletter_e_unsubscribe'];
		}
		
		if(!$error)
		{
			$check = $GLOBALS['db']->RowQuery("SELECT Id FROM " . PREFIX . "_newsletter_abos WHERE Aktiv='1' AND Email='" . $_GET['email'] . "' AND Code='" . $_GET['code'] . "'");
			if(!is_object($check))
			{
				$error = true;
				$error_msg[] = $GLOBALS['config_vars']['Newsletter_e_unsubscribe'];
			} else {
				$GLOBALS['db']->RowQuery("DELETE FROM " . PREFIX . "_newsletter_abos WHERE Email='" . $_GET['email'] . "' AND Code='" . $_GET['code'] . "'");
			}
		}
		
		if($error)
		{	
			$GLOBALS['tmpl']->assign('error', $error_msg);
		}
		
		$headernav = $GLOBALS['config_vars']['Newsletter_unsubscribe'];
		$GLOBALS['tmpl']->assign('headernav', $headernav);
		$GLOBALS['tmpl']->assign('pagetitle_html', clean_page_title($headernav, ''));
		$GLOBALS['tmpl']->assign('pagetitle', '');
		$GLOBALS['tmpl']->assign('content', $GLOBALS['tmpl']->fetch($GLOBALS['config']['source'] . '/newsletter/newsletter_unsubscribe.tpl'));
	}
	
	//=================================================================
	// VErfuegbare Newsletter holen...
	//=================================================================
	function getNl()
	{
		$nl = array();
		$sql = $GLOBALS['db']->Query("SELECT Name,Id,Info FROM " . PREFIX . "_newsletter WHERE Sektion='" . AREA . "' ORDER BY Name ASC");
		$c=0;
		while($row = $sql->fetchrow())
		{	
			$c++;
			$row->Count=$c;
			array_push($nl, $row);
		}
		$GLOBALS['tmpl']->assign('Nl_Count',$c); 
		return $nl;
	}
	
	//=================================================================
	// Abonnement aktivieren
	//=================================================================
	function ActivateAbo()
	{
		$error = false;
		//
		if(@empty($_GET['email']) || @empty($_GET['code']) || @!preg_match($this->_muster_email, $_GET['email']) || @preg_match($this->_muster, $_GET['code']))
		{
			$error = true;
			$error_msg[] = $GLOBALS['config_vars']['Newsletter_e_activatee'];
		}
		
		if(!$error)
		{
			$check = $GLOBALS['db']->RowQuery("SELECT Id FROM " . PREFIX . "_newsletter_abos WHERE Aktiv!='1' AND Email='" . $_GET['email'] . "' AND Code='" . $_GET['code'] . "'");
			if(!is_object($check))
			{
				$error = true;
				$error_msg[] = $GLOBALS['config_vars']['Newsletter_e_activatee'];
			} else {
				$GLOBALS['db']->RowQuery("UPDATE " . PREFIX . "_newsletter_abos SET Aktiv='1' WHERE Email='" . $_GET['email'] . "' AND Code='" . $_GET['code'] . "'");
			}
		}
		
		if($error)
		{	
			$GLOBALS['tmpl']->assign('email_error', $error_msg);
		}
		
		$headernav = $GLOBALS['config_vars']['Newsletter'];
		$GLOBALS['tmpl']->assign('headernav', $headernav);
		$GLOBALS['tmpl']->assign('pagetitle_html', clean_page_title($headernav, ''));
		$GLOBALS['tmpl']->assign('pagetitle', '');
		$GLOBALS['tmpl']->assign('content', $GLOBALS['tmpl']->fetch($GLOBALS['config']['source'] . '/newsletter/newsletter_abo_ok.tpl'));
		
	}
	
	//=================================================================
	// Neues Abonnement
	//=================================================================
	function AboNew()
	{
		if(isset($_POST['action']) && $_POST['action']=='abonew')
		{
			$error = false;
			
			// Keine E-Mail
			if(@empty($_POST['nl_email']))
			{
				$error = true;
				$error_msg[] = $GLOBALS['config_vars']['Newsletter_e_nomail'];
			}
			
			// E-Mail-Pruefung
			if(!$error)
			{
				if(@!preg_match($this->_muster_email, $_POST['nl_email']))  
				{
					$error = true;
					$error_msg[] = $GLOBALS['config_vars']['Newsletter_e_email'];
				}
			}
			
			if(@empty($_POST['nl_welche']))
			{
				$error = true;
				$error_msg[] = $GLOBALS['config_vars']['Newsletter_e_noabo'];
			}
			
			if(!$error)
			{
				foreach($_POST['nl_welche'] as $nlid => $w)
				{
					$sql_nl = $GLOBALS['db']->Query("SELECT Name,Id FROM " . PREFIX . "_newsletter WHERE Id='" . abs((int)$nlid) . "'");
					while($row = $sql_nl->fetchrow())
					{
						$check = $GLOBALS['db']->RowQuery("SELECT Email FROM " . PREFIX . "_newsletter_abos WHERE Aktiv='1' AND Newsletter_Id='" . abs((int)$nlid) . "'");
						if(is_object($check) && (strtolower($check->Email) == strtolower($_POST['nl_email'])))
						{
							$error = true;
							$error_msg[] = str_replace('__NAME__', $row->Name, $GLOBALS['config_vars']['Newsletter_e_aboexists']);
						}
					}
				}
			}
			
			
			// Keine
			
			if($error)
			{
				$GLOBALS['tmpl']->assign('email_error', $error_msg);
			} else {
				
				$secure = rand_string(8);
				
				switch(@$_POST['nl_format'])
				{
					case '': case 'text': $f = 'text'; break;
					case 'html': $f = 'html'; break;
				}
				
				foreach($_POST['nl_welche'] as $nlid => $w)
				{
					$GLOBALS['db']->Query("INSERT INTO " . PREFIX . "_newsletter_abos 
					(
						Id,
						Newsletter_Id,
						Datum,
						Email,
						Format,
						Sektion,
						Code,
						Aktiv
					) VALUES (
						'',
						'" . abs((int)$nlid) . "',
						'" . stime() . "',
						'" . $_POST['nl_email'] . "',
						'" . $f . "',
						'" . AREA . "',
						'" . $secure . "',
						'0'
					)");
				}
				
				$GLOBALS['tmpl']->assign('Entry_Ok', 1);
				
				$Koobi = new Koobi;
				$Text = $GLOBALS['config_vars']['Newsletter_text'];
				$Text = str_replace('__WEBSEITE__', thisHome(), $Text);
				$Text = str_replace('__LINK__', thisHome() . 'index.php?p=newsletter&action=activate&email=' . $_POST['nl_email'] . '&code=' . $secure, $Text);
				
				$Koobi->koobi_mail($_POST['nl_email'], $Text, $GLOBALS['config_vars']['Newsletter_subject'], $GLOBALS['settings']['Mail_Absender'], $GLOBALS['settings']['Mail_Name'], 'text', '', '');
			}
			
		}
		 
		$headernav = $GLOBALS['config_vars']['Newsletter'];
		$GLOBALS['tmpl']->assign('headernav', $headernav);
		$GLOBALS['tmpl']->assign('pagetitle_html', clean_page_title($headernav, ''));
		$GLOBALS['tmpl']->assign('pagetitle', '');
		$GLOBALS['tmpl']->assign('content', $GLOBALS['tmpl']->fetch($GLOBALS['config']['source'] . '/newsletter/newsletter_abo.tpl'));
	}
}
?>