<?php
// ======================================================================== //
//                                                                          //
//   KooBI- Webportalsystem                                                 //
//   SCRiPTMAFiA 2005 - THE DiRTY HANDS ON YOUR SCRiPTS                     //
//                                                                          //
//   ====================================================================   //
//                                                                          //   
//   dream4 | webservices - Am Michelsgrund 7 - 69469 Weinheim - Germany    //    
//   tel. +49(0) 6201 - 980442 / fax. +49(0) 6201 - 64809                   //
//                                                                          //
// ======================================================================== //
if(!checkactive('newsletter')) include(dirname(__FILE__)."/lib/notactive.php" );
switch($_REQUEST['action']){
	case "" :
	
	if($nlcode != 89769){
		$message = array();
		
		$insert = 1;
		if(!ereg("^[_A-Za-zÀ-ÖØ-öø-ÿ0-9-]+(\.[_A-Za-zÀ-ÖØ-öø-ÿ0-9-]+)*@([a-zA-Z0-9-]+\.)+([a-zA-Z]{2,4})$", $_POST['email_newsletter'])){
			array_push($message, '<br />' . $lang['isno_email']);
			$insert = -1;
		}
		
		if(count($_POST['newsletter'])<1){
			array_push($message, '<br />' . "BITTE NEWSLETTER WÄHLEN");
			$insert = -1;
		}
		
		if($insert == 1){
			foreach ($_POST['newsletter'] as $id => $sel) {
				if( $sel != "")  {
					$sql = new dbc ("SELECT email FROM ".PREFIX."_newsletter_abos WHERE email='$_POST[email_newsletter]' and newsletter='$id'");
					$numexi = $sql->numrows();
					
					$sql2 = new dbc ("SELECT name FROM ".PREFIX."_newsletter WHERE id='$id'");
					$row2 = $sql2->fetchrow();
					
					if($numexi == 1) {
						array_push($message, $lang['newsletter_errornewabo'] . " <strong>" . $row2->name ."</strong>");
					}
					
					if($numexi < 1) {
						array_push($message, $lang['newsletter_okabonew'] . " <strong>" . $row2->name . "</strong>" );
						$securecode = securecode();
						$sql = new dbc ("
						INSERT INTO ".PREFIX."_newsletter_abos  (
						id,
						newsletter,
						name,
						email,
						ctime,
						format,
						securecode,
						active,
						area
						) VALUES (
						'',
						'$id',
						'$_POST[name_newsletter]',
						'$_POST[email_newsletter]',
						'".time()."',
						'$_POST[newsletterformat]',
						'$securecode',
						'0',
						'$_REQUEST[area]'
						)
						");
						$subscribeurl = "http://" . $_SERVER['HTTP_HOST'] . str_replace("/index.php","",$_SERVER['PHP_SELF']) . "/index.php?p=newsletter&action=subscribe&area=$_REQUEST[area]&email=$_POST[email_newsletter]&code=$securecode";
						$subscribe_link_pre = str_replace("__NEWSLETTERNAME__", "'" . $row2->name . "'" , $lang['newsletter_subscribe_link']);
						$subscribe_link_pre = str_replace("__SUBSCRIBEURL__", $subscribeurl, $subscribe_link_pre);
						$subscribe_link .= $subscribe_link_pre;
					}
				}
			}
		}
		foreach($message  as $result){
			$epre .= $result . "<br />";
		}
		if($insert == 1) { 
			// =====================================================================
			// <<-- AKTIVIERUNGS- EMAIL -->>
			// =====================================================================
			$subject = str_replace("__HOMEPAGENAME__", $pref['hpname'], $lang['newsletteractivate_subject']);
			
			$e_message = $lang['newsletteractivate_body'];
			$e_message = str_replace("__NAME__", $_POST['name_newsletter'], $e_message);
			$e_message = str_replace("__LINK__", $subscribe_link, $e_message);
			
			$body = str_replace("\n","\n\n", $e_message);
			
			@mail($_POST['email_newsletter'], $subject, stripslashes($body),
			"From: $pref[company_email_reply]\r\n"
			."Reply-To: $pref[company_email_reply]\r\n"
			."X-Mailer: PHP/" . phpversion());
			
			$epre .= '<br />' . $lang['newsletter_confirm_message']; 
		}
		
		$premessage = ($insert == 1) ? $lang['thankyou_result'] : $lang['error_result'];
		$EOUT = msg('newsletter', $premessage . '<br />' . $epre , str_replace("__URL__", "index.php?area=$_REQUEST[area]", $lang['redirect']), "index.php?area=$_REQUEST[area]", 1, 10);
		$NOOUT = 1;
	}
	break;
	
	case "subscribe" :
	$email = escs($_REQUEST['email']);
	$code = escs($_REQUEST['code']);
	
	$sql = new dbc("SELECT id FROM ".PREFIX."_newsletter_abos WHERE securecode='$code' AND email='$email' AND active='0'");
	$row = $sql->fetchrow();
	$erg = $sql->numrows();
	
	if($erg > 0) {
		$sql = new dbc("UPDATE ".PREFIX."_newsletter_abos SET active='1' WHERE securecode='$code' AND email='$email'");
		$EOUT = msg('newsletter', $lang['newsletter_subscription_ok'], str_replace("__URL__", "index.php?area=$_REQUEST[area]", $lang['redirect']), "index.php?area=$_REQUEST[area]", 1, 7);
		$NOOUT = 1;
	} else {
		$EOUT = msg('newsletter', $lang['newsletter_subscription_notok'], str_replace("__URL__", "index.php?area=$_REQUEST[area]", $lang['redirect']), "index.php?area=$_REQUEST[area]", 1, 7);
		$NOOUT = 1;
	}
	
	break;
	
	// =====================================================================
	// <<-- NEWSLETTER ABMELDEN -->>
	// =====================================================================
	case "unsubscribe" :
	$email = escs($_REQUEST['email']);
	$code = escs($_REQUEST['code']);
	
	$sql = new dbc("SELECT id FROM ".PREFIX."_newsletter_abos WHERE securecode='$code' AND email='$email' AND active='1'");
	$erg = $sql->numrows();
	
	if($erg > 0) {
		$sql = new dbc("DELETE FROM ".PREFIX."_newsletter_abos WHERE securecode='$code' AND email='$email'");
		$EOUT = msg('newsletter', $lang['newsletter_unsubscription_ok'], str_replace("__URL__", "index.php?area=$_REQUEST[area]", $lang['redirect']), "index.php?area=$_REQUEST[area]", 1, 7);
		$NOOUT = 1;
	} else {
		$EOUT = msg('newsletter', $lang['newsletter_unsubscription_notok'], str_replace("__URL__", "index.php?area=$_REQUEST[area]", $lang['redirect']), "index.php?area=$_REQUEST[area]", 1, 7);
		$NOOUT = 1;
	}
	break;
}?>